Compare commits

..

27 Commits

Author SHA1 Message Date
Lion Kortlepel aaefea1d01 update commandline 2021-11-09 22:05:04 +01:00
Lion Kortlepel 863e8eb8cf Bump version 2021-10-09 23:53:59 +02:00
Lion Kortlepel d0431c0b9d Use v2 api 2021-09-27 15:46:37 +02:00
Lion Kortlepel 0961f86662 Add heartbeat-api-v API version header 2021-09-27 15:46:37 +02:00
Lion Kortlepel 7f2ca025f8 Start using new heartbeat response format 2021-09-27 15:46:37 +02:00
Lion Kortlepel 2355327c21 CMake: Fix typo in SANITIZE codepath 2021-09-15 17:55:34 +02:00
Lion Kortlepel 3837e101e2 CMake: Use gzipped debug info on linux 2021-09-15 17:55:34 +02:00
Lion Kortlepel fa19ba08e3 Sentry: Properly store DSN 2021-09-15 17:55:34 +02:00
Lion Kortlepel 57d0eb735e Add cryptography header for the future 2021-09-15 17:55:34 +02:00
Lion Kortlepel 15704abf6c Http, Heartbeat: Process status < 0 differently, report as "Invalid
Response Code"
2021-09-15 17:55:34 +02:00
Lion Kortlepel 6883c96d33 Http: Add Sentry error breadcrumbs on internal https POST errors 2021-09-15 17:55:34 +02:00
Lion Kortlepel f632606d76 Heartbeat: Dont report 200 + INVALID_KEY to Sentry 2021-09-15 17:55:34 +02:00
Lion Kortlepel c70ada2926 Config: private by default 2021-09-15 17:55:34 +02:00
Lion Kortlepel 80aebcb9a7 Actions: prerelease by default 2021-09-13 11:58:01 +03:00
Lion Kortlepel 3fc397814c Move update check to after initialization (since its blocking) 2021-09-11 11:38:06 +03:00
Lion Kortlepel 6542be09ee Clarify what sentry sends, add a way to turn off the warning 2021-09-11 11:38:06 +03:00
Lion Kortlepel 38b934bc0f Move signal handling into its own translation unit to limit overlap 2021-09-11 11:38:06 +03:00
Lion Kortlepel a2f92b5791 Update changelog, use std::exit instead of exit 2021-09-11 11:38:06 +03:00
Lion Kortlepel 30624c77a2 Update commandline; reset terminal before exit 2021-09-11 11:38:06 +03:00
Lion Kortlepel b1664bb184 Application: Perform hard-shutdown after 3 Ctrl+C's 2021-09-11 11:38:06 +03:00
Lion Kortlepel ffac000cd2 Config: Add basic opt-out for Sentry 2021-09-11 11:38:06 +03:00
Lion Kortlepel 3cd94380e2 Changelog: Add recent additions 2021-09-11 11:38:06 +03:00
Lion Kortlepel b055fd8bda GracefullyShutdown: Add "subsystem x/y shutting down" message
Remove old "X shutting down", "X shut down" messages, they were bad and
confusing
2021-09-11 11:38:06 +03:00
Lion Kortlepel d43ee4b7b6 Bump version to 2.3.2 2021-09-11 11:38:06 +03:00
Lion Kortlepel a514591650 Main: Add Ctrl+C handler for windows 2021-09-11 11:38:06 +03:00
Lion Kortlepel 0f9f81e9fa Http: Add cloudflare 5XX status code strings 2021-09-11 11:38:06 +03:00
Lion Kortlepel 11e94e91a7 Update Changelog.md to reflect latest changes in recent merge from #44 2021-09-10 13:41:45 +03:00
36 changed files with 922 additions and 676 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ jobs:
tag_name: ${{ github.ref }} tag_name: ${{ github.ref }}
release_name: ${{ github.ref }} release_name: ${{ github.ref }}
draft: false draft: false
prerelease: false prerelease: true
body: | body: |
Files included in this release: Files included in this release:
- `BeamMP-Server.exe` is the windows build - `BeamMP-Server.exe` is the windows build
-1
View File
@@ -1,5 +1,4 @@
.idea/ .idea/
.sentry-native/
*.orig *.orig
*.toml *.toml
boost_* boost_*
+6 -3
View File
@@ -1,15 +1,18 @@
[submodule "include/commandline"] [submodule "include/commandline"]
path = include/commandline path = include/commandline
url = https://github.com/lionkor/commandline url = https://github.com/lionkor/commandline
[submodule "socket.io-client-cpp"]
path = socket.io-client-cpp
url = https://github.com/socketio/socket.io-client-cpp
[submodule "asio"] [submodule "asio"]
path = asio path = asio
url = https://github.com/chriskohlhoff/asio url = https://github.com/chriskohlhoff/asio
[submodule "rapidjson"] [submodule "rapidjson"]
path = rapidjson path = rapidjson
url = https://github.com/Tencent/rapidjson url = https://github.com/Tencent/rapidjson
[submodule "include/toml11"] [submodule "include/tomlplusplus"]
path = include/toml11 path = include/tomlplusplus
url = https://github.com/ToruNiina/toml11 url = https://github.com/marzer/tomlplusplus
[submodule "include/sentry-native"] [submodule "include/sentry-native"]
path = include/sentry-native path = include/sentry-native
url = https://github.com/getsentry/sentry-native url = https://github.com/getsentry/sentry-native
+8 -14
View File
@@ -1,10 +1,5 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
message(STATUS "You can find build instructions and a list of dependencies in the README at \ message(STATUS "You can find build instructions and a list of dependencies in the README at \
https://github.com/BeamMP/BeamMP-Server") https://github.com/BeamMP/BeamMP-Server")
@@ -30,7 +25,6 @@ add_subdirectory("include/sentry-native")
message(STATUS "Setting compiler flags") message(STATUS "Setting compiler flags")
if (WIN32) if (WIN32)
#-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static #-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static
set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}) set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET})
include_directories(${VcpkgRoot}/include) include_directories(${VcpkgRoot}/include)
@@ -38,10 +32,10 @@ if (WIN32)
elseif (UNIX) elseif (UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -static-libstdc++") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -static-libstdc++")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -s -fno-builtin") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -gz -fno-builtin")
if (SANITIZE) if (SANITIZE)
message(STATUS "sanitize is ON") message(STATUS "sanitize is ON")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLanAGS} -fsanitize=undefined,thread") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,thread")
endif (SANITIZE) endif (SANITIZE)
endif () endif ()
@@ -62,12 +56,10 @@ message(STATUS "Adding local source dependencies")
include_directories("asio/asio/include") include_directories("asio/asio/include")
include_directories("rapidjson/include") include_directories("rapidjson/include")
include_directories("websocketpp") include_directories("websocketpp")
add_subdirectory("socket.io-client-cpp")
add_subdirectory("include/commandline") add_subdirectory("include/commandline")
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
message(STATUS "Looking for Boost") message(STATUS "Looking for Boost")
@@ -89,7 +81,8 @@ add_executable(BeamMP-Server
include/Http.h src/Http.cpp include/Http.h src/Http.cpp
include/TSentry.h src/TSentry.cpp include/TSentry.h src/TSentry.cpp
include/TPPSMonitor.h src/TPPSMonitor.cpp include/TPPSMonitor.h src/TPPSMonitor.cpp
include/TNetwork.h src/TNetwork.cpp) include/TNetwork.h src/TNetwork.cpp
include/SignalHandling.h src/SignalHandling.cpp)
target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}") target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}")
@@ -103,13 +96,12 @@ target_include_directories(BeamMP-Server PUBLIC
${Boost_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}
${LUA_INCLUDE_DIR} ${LUA_INCLUDE_DIR}
${CURL_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}
"socket.io-client-cpp/src"
"include/tomlplusplus" "include/tomlplusplus"
"include/sentry-native/include" "include/sentry-native/include"
"include/curl/include") "include/curl/include")
message(STATUS "Looking for SSL") message(STATUS "Looking for SSL")
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
message(STATUS "CURL IS ${CURL_LIBRARIES}") message(STATUS "CURL IS ${CURL_LIBRARIES}")
@@ -123,6 +115,7 @@ if (UNIX)
crypto crypto
${OPENSSL_LIBRARIES} ${OPENSSL_LIBRARIES}
commandline commandline
sioclient_tls
sentry) sentry)
elseif (WIN32) elseif (WIN32)
include(FindLua) include(FindLua)
@@ -137,5 +130,6 @@ elseif (WIN32)
${LUA_LIBRARIES} ${LUA_LIBRARIES}
${OPENSSL_LIBRARIES} ${OPENSSL_LIBRARIES}
commandline commandline
sioclient_tls
sentry) sentry)
endif () endif ()
+17
View File
@@ -1,3 +1,20 @@
# v2.3.3
- CHANGED servers to be private by default
# v2.3.2
- ADDED Ctrl+C causes a graceful shutdown on windows (did already on linux)
- ADDED more meaningful shutdown messages
- ADDED even better backend connection error reporting
- ADDED `SendErrors` config in `ServerConfig.toml` to opt-out of error reporting
- ADDED hard-shutdown if Ctrl+C pressed 3 times
- FIXED issue with shells like bash being unusable after server exit
# v2.3.1
- CHANGED join/sync timeout to 20 minutes, players wont drop if loading takes >5 mins
# v2.3.0 # v2.3.0
- ADDED version check - the server will now let you know when a new release is out - ADDED version check - the server will now let you know when a new release is out
+1 -7
View File
@@ -75,18 +75,12 @@ These package names are in the debian / ubuntu style. Feel free to PR your own g
**If** you're building it from source, you'll need `libboost1.70-all-dev` or `libboost1.71-all-dev` or higher as well. **If** you're building it from source, you'll need `libboost1.70-all-dev` or `libboost1.71-all-dev` or higher as well.
If you can't find this version of boost (only 1.6x, for example), you can either update to a newer version of your distro, build boost yourself, or use an unstable rolling release (like Debian `sid` aka `unstable`). If you can't find this version of boost (only 1.6x, for example), you can either update to a newer version of your distro, build boost yourself, or use an unstable rolling release (like Debian `sid` aka `unstable`).
In the end you should end up with a command something like this:
```sh
sudo apt install git make cmake g++-10 liblua5.3 libz-dev rapidjson-dev libopenssl-dev libboost1.71-all-dev
```
### How to build ### How to build
On windows, use git-bash for these commands. On Linux, these should work in your shell. On windows, use git-bash for these commands. On Linux, these should work in your shell.
1. Make sure you have all [prerequisites](#prerequisites) installed 1. Make sure you have all [prerequisites](#prerequisites) installed
2. Clone the repository in a location of your choice with `git clone --recurse-submodules https://github.com/BeamMP/BeamMP-Server`. 2. Clone the repository in a location of your choice with **`git clone --recurse-submodules https://github.com/BeamMP/BeamMP-Server`**. Now change into the cloned directory by running `cd BeamMP-Server`.
3. Ensure that all submodules are initialized by running `git submodule update --init --recursive`. Then change into the cloned directory by running `cd BeamMP-Server`. 3. Ensure that all submodules are initialized by running `git submodule update --init --recursive`. Then change into the cloned directory by running `cd BeamMP-Server`.
4. Checkout the branch of the release you want to compile (`master` is often unstable), for example `git checkout tags/v1.20` for version 1.20. You can find the latest version [here](https://github.com/BeamMP/BeamMP-Server/tags). 4. Checkout the branch of the release you want to compile (`master` is often unstable), for example `git checkout tags/v1.20` for version 1.20. You can find the latest version [here](https://github.com/BeamMP/BeamMP-Server/tags).
5. Run `cmake .` (with `.`) 5. Run `cmake .` (with `.`)
+3 -14
View File
@@ -12,17 +12,6 @@
class TServer; class TServer;
#ifdef WIN32
// for socklen_t
#include <WS2tcpip.h>
#endif // WIN32
struct TConnection final {
SOCKET Socket;
struct sockaddr SockAddr;
socklen_t SockAddrLen;
};
class TClient final { class TClient final {
public: public:
using TSetOfVehicleData = std::vector<TVehicleData>; using TSetOfVehicleData = std::vector<TVehicleData>;
@@ -41,7 +30,7 @@ public:
TVehicleDataLockPair GetAllCars(); TVehicleDataLockPair GetAllCars();
void SetName(const std::string& Name) { mName = Name; } void SetName(const std::string& Name) { mName = Name; }
void SetRoles(const std::string& Role) { mRole = Role; } void SetRoles(const std::string& Role) { mRole = Role; }
void SetIdentifier(const std::string& key, const std::string& value) { mIdentifiers[key] = value; } void AddIdentifier(const std::string& ID) { mIdentifiers.insert(ID); };
std::string GetCarData(int Ident); std::string GetCarData(int Ident);
void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; } void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; }
void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; } void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; }
@@ -49,7 +38,7 @@ public:
void SetStatus(int Status) { mStatus = Status; } void SetStatus(int Status) { mStatus = Status; }
// locks // locks
void DeleteCar(int Ident); void DeleteCar(int Ident);
[[nodiscard]] const std::unordered_map<std::string, std::string>& GetIdentifiers() const { return mIdentifiers; } [[nodiscard]] std::set<std::string> GetIdentifiers() const { return mIdentifiers; }
[[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; } [[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; }
[[nodiscard]] SOCKET GetDownSock() const { return mSocket[1]; } [[nodiscard]] SOCKET GetDownSock() const { return mSocket[1]; }
[[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; } [[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; }
@@ -89,7 +78,7 @@ private:
bool mIsSyncing = false; bool mIsSyncing = false;
mutable std::mutex mMissedPacketsMutex; mutable std::mutex mMissedPacketsMutex;
std::queue<std::string> mPacketsSync; std::queue<std::string> mPacketsSync;
std::unordered_map<std::string, std::string> mIdentifiers; std::set<std::string> mIdentifiers;
bool mIsGuest = false; bool mIsGuest = false;
std::mutex mVehicleDataMutex; std::mutex mVehicleDataMutex;
TSetOfVehicleData mVehicleData; TSetOfVehicleData mVehicleData;
+14 -24
View File
@@ -5,26 +5,13 @@ extern TSentry Sentry;
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <cstring>
#include <deque> #include <deque>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <sstream>
#include "Compat.h"
#include "TConsole.h" #include "TConsole.h"
struct Version {
uint8_t major;
uint8_t minor;
uint8_t patch;
Version(uint8_t major, uint8_t minor, uint8_t patch);
Version(const std::array<uint8_t, 3>& v);
std::string AsString();
};
// static class handling application start, shutdown, etc. // static class handling application start, shutdown, etc.
// yes, static classes, singletons, globals are all pretty // yes, static classes, singletons, globals are all pretty
// bad idioms. In this case we need a central way to access // bad idioms. In this case we need a central way to access
@@ -40,10 +27,12 @@ public:
, Resource("Resources") , Resource("Resources")
, MapName("/levels/gridmap_v2/info.json") , MapName("/levels/gridmap_v2/info.json")
, MaxPlayers(10) , MaxPlayers(10)
, Private(false) , Private(true)
, MaxCars(1) , MaxCars(1)
, DebugModeEnabled(false) , DebugModeEnabled(false)
, Port(30814) { } , Port(30814)
, SendErrors(true)
, SendErrorsMessageEnabled(true) { }
std::string ServerName; std::string ServerName;
std::string ServerDesc; std::string ServerDesc;
std::string Resource; std::string Resource;
@@ -55,9 +44,10 @@ public:
bool DebugModeEnabled; bool DebugModeEnabled;
int Port; int Port;
std::string CustomIP; std::string CustomIP;
bool SendErrors;
bool SendErrorsMessageEnabled;
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
}; };
using TShutdownHandler = std::function<void()>; using TShutdownHandler = std::function<void()>;
// methods // methods
@@ -68,9 +58,8 @@ public:
// Causes all threads to finish up and exit gracefull gracefully // Causes all threads to finish up and exit gracefull gracefully
static void GracefullyShutdown(); static void GracefullyShutdown();
static TConsole& Console() { return *mConsole; } static TConsole& Console() { return *mConsole; }
static std::string ServerVersionString(); static std::string ServerVersion() { return "2.3.3"; }
static const Version& ServerVersion() { return mVersion; } static std::string ClientVersion() { return "2.0"; }
static std::string ClientVersionString() { return "2.0"; }
static std::string PPS() { return mPPS; } static std::string PPS() { return mPPS; }
static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; } static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; }
@@ -82,16 +71,14 @@ public:
static std::string GetBackup2Hostname() { return "backup2.beammp.com"; } static std::string GetBackup2Hostname() { return "backup2.beammp.com"; }
static std::string GetBackendUrlForSocketIO() { return "https://backend.beammp.com"; } static std::string GetBackendUrlForSocketIO() { return "https://backend.beammp.com"; }
static void CheckForUpdates(); static void CheckForUpdates();
static std::array<uint8_t, 3> VersionStrToInts(const std::string& str); static std::array<int, 3> VersionStrToInts(const std::string& str);
static bool IsOutdated(const Version& Current, const Version& Newest); static bool IsOutdated(const std::array<int, 3>& Current, const std::array<int, 3>& Newest);
private: private:
static inline std::string mPPS; static inline std::string mPPS;
static std::unique_ptr<TConsole> mConsole; static std::unique_ptr<TConsole> mConsole;
static inline std::mutex mShutdownHandlersMutex {}; static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {}; static inline std::deque<TShutdownHandler> mShutdownHandlers {};
static inline Version mVersion { 2, 4, 0 };
}; };
std::string ThreadName(bool DebugModeOverride = false); std::string ThreadName(bool DebugModeOverride = false);
@@ -100,6 +87,7 @@ void RegisterThread(const std::string& str);
#define KB 1024 #define KB 1024
#define MB (KB * 1024) #define MB (KB * 1024)
#define SSU_UNRAW SECRET_SENTRY_URL
#define _file_basename std::filesystem::path(__FILE__).filename().string() #define _file_basename std::filesystem::path(__FILE__).filename().string()
#define _line std::to_string(__LINE__) #define _line std::to_string(__LINE__)
@@ -125,9 +113,11 @@ void RegisterThread(const std::string& str);
#else #else
#define _this_location (ThreadName() + _file_basename + ":" + _line + " ") #define _this_location (ThreadName() + _file_basename + ":" + _line + " ")
#endif #endif
#define SU_RAW SSU_UNRAW
#else // !defined(DEBUG) #else // !defined(DEBUG)
#define SU_RAW RAWIFY(SSU_UNRAW)
#define _this_location (ThreadName()) #define _this_location (ThreadName())
#endif // defined(DEBUG) #endif // defined(DEBUG)
@@ -164,4 +154,4 @@ void LogChatMessage(const std::string& name, int id, const std::string& msg);
std::string Comp(std::string Data); std::string Comp(std::string Data);
std::string DeComp(std::string Compressed); std::string DeComp(std::string Compressed);
std::string GetPlatformAgnosticErrorString(); #define S_DSN SU_RAW
-2
View File
@@ -7,7 +7,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
using SOCKET = int; using SOCKET = int;
using DWORD = unsigned long; using DWORD = unsigned long;
using PDWORD = unsigned long*; using PDWORD = unsigned long*;
@@ -27,7 +26,6 @@ inline void CloseSocketProper(int TheSocket) {
inline void CloseSocketProper(SOCKET TheSocket) { inline void CloseSocketProper(SOCKET TheSocket) {
shutdown(TheSocket, 2); // 2 == SD_BOTH shutdown(TheSocket, 2); // 2 == SD_BOTH
closesocket(TheSocket); closesocket(TheSocket);
} }
#endif // WIN32 #endif // WIN32
+114
View File
@@ -0,0 +1,114 @@
// Copyright Anonymous275 8/11/2020
#pragma once
#include <array>
#include <cstdarg>
#include <string>
namespace Crypto {
constexpr auto time = __TIME__;
constexpr auto seed = static_cast<int>(time[7]) + static_cast<int>(time[6]) * 10 + static_cast<int>(time[4]) * 60 + static_cast<int>(time[3]) * 600 + static_cast<int>(time[1]) * 3600 + static_cast<int>(time[0]) * 36000;
// 1988, Stephen Park and Keith Miller
// "Random Number Generators: Good Ones Are Hard To Find", considered as "minimal standard"
// Park-Miller 31 bit pseudo-random number generator, implemented with G. Carta's optimisation:
// with 32-bit math and without division
template <int N>
struct RandomGenerator {
private:
static constexpr unsigned a = 16807; // 7^5
static constexpr unsigned m = 2147483647; // 2^31 - 1
static constexpr unsigned s = RandomGenerator<N - 1>::value;
static constexpr unsigned lo = a * (s & 0xFFFFu); // Multiply lower 16 bits by 16807
static constexpr unsigned hi = a * (s >> 16u); // Multiply higher 16 bits by 16807
static constexpr unsigned lo2 = lo + ((hi & 0x7FFFu) << 16u); // Combine lower 15 bits of hi with lo's upper bits
static constexpr unsigned hi2 = hi >> 15u; // Discard lower 15 bits of hi
static constexpr unsigned lo3 = lo2 + hi;
public:
static constexpr unsigned max = m;
static constexpr unsigned value = lo3 > m ? lo3 - m : lo3;
};
template <>
struct RandomGenerator<0> {
static constexpr unsigned value = seed;
};
template <int N, int M>
struct RandomInt {
static constexpr auto value = RandomGenerator<N + 1>::value % M;
};
template <int N>
struct RandomChar {
static const char value = static_cast<char>(1 + RandomInt<N, 0x7F - 1>::value);
};
template <size_t N, int K, typename Char>
struct MangleString {
private:
const char _key;
std::array<Char, N + 1> _encrypted;
constexpr Char enc(Char c) const {
return c ^ _key;
}
Char dec(Char c) const {
return c ^ _key;
}
public:
template <size_t... Is>
constexpr MangleString(const Char* str, std::index_sequence<Is...>)
: _key(RandomChar<K>::value)
, _encrypted { enc(str[Is])... } { }
decltype(auto) decrypt() {
for (size_t i = 0; i < N; ++i) {
_encrypted[i] = dec(_encrypted[i]);
}
_encrypted[N] = '\0';
return _encrypted.data();
}
};
static auto w_printf = [](const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
};
static auto w_printf_s = [](const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
};
static auto w_sprintf_s = [](char* buf, size_t buf_size, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
};
static auto w_sprintf_s_ret = [](char* buf, size_t buf_size, const char* fmt, ...) {
int ret;
va_list args;
va_start(args, fmt);
ret = vsprintf(buf, fmt, args);
va_end(args);
return ret;
};
#define XOR_C(s) [] { constexpr Crypto::MangleString< sizeof(s)/sizeof(char) - 1, __COUNTER__, char > expr( s, std::make_index_sequence< sizeof(s)/sizeof(char) - 1>() ); return expr; }().decrypt()
#define XOR_W(s) [] { constexpr Crypto::MangleString< sizeof(s)/sizeof(wchar_t) - 1, __COUNTER__, wchar_t > expr( s, std::make_index_sequence< sizeof(s)/sizeof(wchar_t) - 1>() ); return expr; }().decrypt()
#define RAWIFY(s) XOR_C(s)
}
+1 -2
View File
@@ -5,9 +5,8 @@
namespace Http { namespace Http {
std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr); std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr);
std::string POST(const std::string& host, int port, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr); std::string POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json, int* status = nullptr);
namespace Status { namespace Status {
std::string ToString(int code); std::string ToString(int code);
} }
const std::string ErrorString = "-1";
} }
+3
View File
@@ -0,0 +1,3 @@
#pragma once
void SetupSignalHandlers();
+69
View File
@@ -0,0 +1,69 @@
#pragma once
#include <atomic>
#include <deque>
#include <mutex>
#include <sio_client.h>
#include <thread>
#include <memory>
/*
* We send relevant server events over socket.io to the backend.
*
* We send all events to `backend.beammp.com`, to the room `/key`
* where `key` is the currently active auth-key.
*/
enum class SocketIOEvent {
ConsoleOut,
CPUUsage,
MemoryUsage,
NetworkUsage,
PlayerList,
};
enum class SocketIORoom {
None,
Stats,
Player,
Info,
Console,
};
class SocketIO final {
private:
struct Event;
public:
enum class EventType {
};
// Singleton pattern
static SocketIO& Get();
void Emit(SocketIOEvent Event, const std::string& Data);
~SocketIO();
void SetAuthenticated(bool auth) { mAuthenticated = auth; }
private:
SocketIO() noexcept;
void ThreadMain();
struct Event {
std::string Name;
std::string Data;
};
bool mAuthenticated { false };
sio::client mClient;
std::thread mThread;
std::atomic_bool mCloseThread { false };
std::mutex mQueueMutex;
std::deque<Event> mQueue;
friend std::unique_ptr<SocketIO> std::make_unique<SocketIO>();
};
-2
View File
@@ -10,8 +10,6 @@ public:
[[nodiscard]] bool Failed() const { return mFailed; } [[nodiscard]] bool Failed() const { return mFailed; }
void FlushToFile();
private: private:
void CreateConfigFile(std::string_view name); void CreateConfigFile(std::string_view name);
void ParseFromFile(std::string_view name); void ParseFromFile(std::string_view name);
+1
View File
@@ -2,6 +2,7 @@
#include "commandline/commandline.h" #include "commandline/commandline.h"
#include "TLuaFile.h" #include "TLuaFile.h"
#include "Cryptography.h"
#include <atomic> #include <atomic>
#include <fstream> #include <fstream>
+1 -3
View File
@@ -4,11 +4,10 @@
#include "IThreaded.h" #include "IThreaded.h"
#include "TLuaFile.h" #include "TLuaFile.h"
#include "TServer.h" #include "TServer.h"
#include <optional>
#include <lua.hpp> #include <lua.hpp>
#include <memory> #include <memory>
#include <optional>
#include <set> #include <set>
#include <unordered_map>
class TLuaEngine : public IThreaded { class TLuaEngine : public IThreaded {
public: public:
@@ -26,7 +25,6 @@ public:
std::optional<std::reference_wrapper<TLuaFile>> GetScript(lua_State* L); std::optional<std::reference_wrapper<TLuaFile>> GetScript(lua_State* L);
static std::unordered_map<std::string, lua_State*> mGlobals;
private: private:
void FolderList(const std::string& Path, bool HotSwap); void FolderList(const std::string& Path, bool HotSwap);
void RegisterFiles(const fs::path& Path, bool HotSwap); void RegisterFiles(const fs::path& Path, bool HotSwap);
+6 -7
View File
@@ -37,13 +37,12 @@ public:
~TLuaFile(); ~TLuaFile();
void SetStopThread(bool StopThread) { mStopThread = StopThread; } void SetStopThread(bool StopThread) { mStopThread = StopThread; }
TLuaEngine& Engine() { return mEngine; } TLuaEngine& Engine() { return mEngine; }
std::string GetPluginPath() const; [[nodiscard]] std::string GetPluginName() const;
std::string GetPluginName() const; [[nodiscard]] std::string GetFileName() const;
std::string GetFileName() const; [[nodiscard]] const lua_State* GetState() const;
const lua_State* GetState() const; [[nodiscard]] bool GetStopThread() const { return mStopThread; }
bool GetStopThread() const { return mStopThread; } [[nodiscard]] const TLuaEngine& Engine() const { return mEngine; }
const TLuaEngine& Engine() const { return mEngine; } [[nodiscard]] std::string GetRegistered(const std::string& Event) const;
std::string GetRegistered(const std::string& Event) const;
private: private:
TLuaEngine& mEngine; TLuaEngine& mEngine;
+2 -4
View File
@@ -4,8 +4,6 @@
#include "TResourceManager.h" #include "TResourceManager.h"
#include "TServer.h" #include "TServer.h"
struct TConnection;
class TNetwork { class TNetwork {
public: public:
TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager); TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
@@ -17,8 +15,8 @@ public:
std::string TCPRcv(TClient& c); std::string TCPRcv(TClient& c);
void ClientKick(TClient& c, const std::string& R); void ClientKick(TClient& c, const std::string& R);
[[nodiscard]] bool SyncClient(const std::weak_ptr<TClient>& c); [[nodiscard]] bool SyncClient(const std::weak_ptr<TClient>& c);
void Identify(const TConnection& client); void Identify(SOCKET TCPSock);
void Authentication(const TConnection& ClientConnection); void Authentication(SOCKET TCPSock);
[[nodiscard]] bool CheckBytes(TClient& c, int32_t BytesRcv); [[nodiscard]] bool CheckBytes(TClient& c, int32_t BytesRcv);
void SyncResources(TClient& c); void SyncResources(TClient& c);
[[nodiscard]] bool UDPSend(TClient& Client, std::string Data) const; [[nodiscard]] bool UDPSend(TClient& Client, std::string Data) const;
Submodule include/toml11 deleted from 647381020e
+33 -61
View File
@@ -23,21 +23,33 @@ void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
} }
void Application::GracefullyShutdown() { void Application::GracefullyShutdown() {
info("please wait while all subsystems are shutting down..."); static bool AlreadyShuttingDown = false;
static uint8_t ShutdownAttempts = 0;
if (AlreadyShuttingDown) {
++ShutdownAttempts;
// hard shutdown at 2 additional tries
if (ShutdownAttempts == 2) {
info("hard shutdown forced by multiple shutdown requests");
std::exit(0);
}
info("already shutting down!");
return;
} else {
AlreadyShuttingDown = true;
}
trace("waiting for lock release");
std::unique_lock Lock(mShutdownHandlersMutex); std::unique_lock Lock(mShutdownHandlersMutex);
for (auto& Handler : mShutdownHandlers) { info("please wait while all subsystems are shutting down...");
Handler(); for (size_t i = 0; i < mShutdownHandlers.size(); ++i) {
info("Subsystem " + std::to_string(i + 1) + "/" + std::to_string(mShutdownHandlers.size()) + " shutting down");
mShutdownHandlers[i]();
} }
} }
std::string Application::ServerVersionString() { std::array<int, 3> Application::VersionStrToInts(const std::string& str) {
return mVersion.AsString(); std::array<int, 3> Version;
}
std::array<uint8_t, 3> Application::VersionStrToInts(const std::string& str) {
std::array<uint8_t, 3> Version;
std::stringstream ss(str); std::stringstream ss(str);
for (uint8_t& i : Version) { for (int& i : Version) {
std::string Part; std::string Part;
std::getline(ss, Part, '.'); std::getline(ss, Part, '.');
std::from_chars(&*Part.begin(), &*Part.begin() + Part.size(), i); std::from_chars(&*Part.begin(), &*Part.begin() + Part.size(), i);
@@ -45,13 +57,12 @@ std::array<uint8_t, 3> Application::VersionStrToInts(const std::string& str) {
return Version; return Version;
} }
// FIXME: This should be used by operator< on Version bool Application::IsOutdated(const std::array<int, 3>& Current, const std::array<int, 3>& Newest) {
bool Application::IsOutdated(const Version& Current, const Version& Newest) { if (Newest[0] > Current[0]) {
if (Newest.major > Current.major) {
return true; return true;
} else if (Newest.major == Current.major && Newest.minor > Current.minor) { } else if (Newest[0] == Current[0] && Newest[1] > Current[1]) {
return true; return true;
} else if (Newest.major == Current.major && Newest.minor == Current.minor && Newest.patch > Current.patch) { } else if (Newest[0] == Current[0] && Newest[1] == Current[1] && Newest[2] > Current[2]) {
return true; return true;
} else { } else {
return false; return false;
@@ -64,11 +75,13 @@ void Application::CheckForUpdates() {
auto Response = Http::GET(GetBackendHostname(), 443, "/v/s"); auto Response = Http::GET(GetBackendHostname(), 443, "/v/s");
bool Matches = std::regex_match(Response, VersionRegex); bool Matches = std::regex_match(Response, VersionRegex);
if (Matches) { if (Matches) {
auto MyVersion = ServerVersion(); auto MyVersion = VersionStrToInts(ServerVersion());
auto RemoteVersion = Version(VersionStrToInts(Response)); auto RemoteVersion = VersionStrToInts(Response);
if (IsOutdated(MyVersion, RemoteVersion)) { if (IsOutdated(MyVersion, RemoteVersion)) {
std::string RealVersionString = RemoteVersion.AsString(); std::string RealVersionString = std::to_string(RemoteVersion[0]) + ".";
warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION OUT! There's a new version (v" + RealVersionString + ") of the BeamMP-Server available! For info on how to update your server, visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server." + std::string(ANSI_RESET)); RealVersionString += std::to_string(RemoteVersion[1]) + ".";
RealVersionString += std::to_string(RemoteVersion[2]);
warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION OUT! There's a new version (v" + RealVersionString + ") of the BeamMP-Server available! For more info visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server." + std::string(ANSI_RESET));
} else { } else {
info("Server up-to-date!"); info("Server up-to-date!");
} }
@@ -147,21 +160,6 @@ void RegisterThread(const std::string& str) {
threadNameMap[std::this_thread::get_id()] = str; threadNameMap[std::this_thread::get_id()] = str;
} }
Version::Version(uint8_t major, uint8_t minor, uint8_t patch)
: major(major)
, minor(minor)
, patch(patch) { }
Version::Version(const std::array<uint8_t, 3>& v)
: Version(v[0], v[1], v[2]) {
}
std::string Version::AsString() {
std::stringstream ss {};
ss << int(major) << "." << int(minor) << "." << int(patch);
return ss.str();
}
void LogChatMessage(const std::string& name, int id, const std::string& msg) { void LogChatMessage(const std::string& name, int id, const std::string& msg) {
std::stringstream ss; std::stringstream ss;
ss << "[CHAT] "; ss << "[CHAT] ";
@@ -171,31 +169,5 @@ void LogChatMessage(const std::string& name, int id, const std::string& msg) {
ss << name << ""; ss << name << "";
} }
ss << msg; ss << msg;
} Application::Console().Write(ss.str());
std::string GetPlatformAgnosticErrorString() {
#ifdef WIN32
// This will provide us with the error code and an error message, all in one.
int err;
char msgbuf[256];
msgbuf[0] = '\0';
err = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf,
sizeof(msgbuf),
nullptr);
if (*msgbuf) {
return std::to_string(GetLastError()) + " - " + std::string(msgbuf);
} else {
return std::to_string(GetLastError());
}
#else // posix
return std::strerror(errno);
#endif
} }
+102 -22
View File
@@ -15,7 +15,85 @@ namespace net = boost::asio; // from <boost/asio.hpp>
namespace ssl = net::ssl; // from <boost/asio/ssl.hpp> namespace ssl = net::ssl; // from <boost/asio/ssl.hpp>
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp> using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
std::string GenericRequest(http::verb verb, const std::string& host, int port, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, const std::string& ContentType, unsigned int* status) { std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
try {
// Check command line arguments.
int version = 11;
// The io_context is required for all I/O
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx(ssl::context::tlsv12_client);
// This holds the root certificate used for verification
// we don't do / have this
// load_root_certificates(ctx);
// Verify the remote server's certificate
ctx.set_verify_mode(ssl::verify_none);
// These objects perform our I/O
tcp::resolver resolver(ioc);
beast::ssl_stream<beast::tcp_stream> stream(ioc, ctx);
// Set SNI Hostname (many hosts need this to handshake successfully)
if (!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str())) {
beast::error_code ec { static_cast<int>(::ERR_get_error()), net::error::get_ssl_category() };
throw beast::system_error { ec };
}
// Look up the domain name
auto const results = resolver.resolve(host.c_str(), std::to_string(port));
// Make the connection on the IP address we get from a lookup
beast::get_lowest_layer(stream).connect(results);
// Perform the SSL handshake
stream.handshake(ssl::stream_base::client);
// Set up an HTTP GET request message
http::request<http::string_body> req { http::verb::get, target, version };
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::write(stream, req);
// This buffer is used for reading and must be persisted
beast::flat_buffer buffer;
// Declare a container to hold the response
http::response<http::string_body> res;
// Receive the HTTP response
http::read(stream, buffer, res);
// Gracefully close the stream
beast::error_code ec;
stream.shutdown(ec);
if (ec == net::error::eof) {
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec = {};
}
if (status) {
*status = res.base().result_int();
}
// ignore ec
// If we get here then the connection is closed gracefully
return std::string(res.body());
} catch (std::exception const& e) {
Application::Console().Write(__func__ + std::string(": ") + e.what());
return "-1";
}
}
std::string Http::POST(const std::string& host, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, bool json, int* status) {
try { try {
net::io_context io; net::io_context io;
@@ -29,7 +107,7 @@ std::string GenericRequest(http::verb verb, const std::string& host, int port, c
decltype(resolver)::results_type results; decltype(resolver)::results_type results;
auto try_connect_with_protocol = [&](tcp protocol) { auto try_connect_with_protocol = [&](tcp protocol) {
try { try {
results = resolver.resolve(protocol, host, std::to_string(port)); results = resolver.resolve(protocol, host, std::to_string(443));
if (!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str())) { if (!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str())) {
boost::system::error_code ec { static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category() }; boost::system::error_code ec { static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category() };
// FIXME: we could throw and crash, if we like // FIXME: we could throw and crash, if we like
@@ -49,17 +127,20 @@ std::string GenericRequest(http::verb verb, const std::string& host, int port, c
bool ok = try_connect_with_protocol(tcp::v4()); bool ok = try_connect_with_protocol(tcp::v4());
if (!ok) { if (!ok) {
Application::Console().Write("[ERROR] failed to resolve or connect in POST " + host + target); Application::Console().Write("[ERROR] failed to resolve or connect in POST " + host + target);
Sentry.AddErrorBreadcrumb("failed to resolve or connect to " + host + target, __FILE__, std::to_string(__LINE__)); // FIXME: this is ugly.
return "-1"; return "-1";
} }
//} //}
stream.handshake(ssl::stream_base::client); stream.handshake(ssl::stream_base::client);
http::request<http::string_body> req { verb, target, 11 /* http 1.1 */ }; http::request<http::string_body> req { http::verb::post, target, 11 /* http 1.1 */ };
req.set(http::field::host, host); req.set(http::field::host, host);
if (!body.empty()) { if (!body.empty()) {
req.set(http::field::content_type, ContentType); // "application/json" if (json) {
// "application/x-www-form-urlencoded" req.set(http::field::content_type, "application/json");
} else {
req.set(http::field::content_type, "application/x-www-form-urlencoded");
}
req.set(http::field::content_length, std::to_string(body.size())); req.set(http::field::content_length, std::to_string(body.size()));
req.body() = body; req.body() = body;
// info("body is " + body + " (" + req.body() + ")"); // info("body is " + body + " (" + req.body() + ")");
@@ -107,10 +188,6 @@ std::string GenericRequest(http::verb verb, const std::string& host, int port, c
} }
Sentry.SetContext("https-post-response-data", response_data); Sentry.SetContext("https-post-response-data", response_data);
if (status) {
*status = response.base().result_int();
}
std::stringstream result; std::stringstream result;
result << response; result << response;
@@ -121,26 +198,19 @@ std::string GenericRequest(http::verb verb, const std::string& host, int port, c
// info(result.str()); // info(result.str());
std::string debug_response_str; std::string debug_response_str;
std::getline(result, debug_response_str); std::getline(result, debug_response_str);
//debug("POST " + host + target + ": " + debug_response_str); //debug("POST " + host + target + ": " + debug_response_str);
return std::string(response.body()); return std::string(response.body());
} catch (const std::exception& e) { } catch (const std::exception& e) {
Application::Console().Write(__func__ + std::string(": ") + e.what()); Application::Console().Write(e.what());
return Http::ErrorString; Sentry.AddErrorBreadcrumb(e.what(), __FILE__, std::to_string(__LINE__)); // FIXME: this is ugly.
return "-1";
} }
} }
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
return GenericRequest(http::verb::get, host, port, target, {}, {}, {}, status);
}
std::string Http::POST(const std::string& host, int port, const std::string& target, const std::unordered_map<std::string, std::string>& fields, const std::string& body, const std::string& ContentType, unsigned int* status) {
return GenericRequest(http::verb::post, host, port, target, fields, body, ContentType, status);
}
// RFC 2616, RFC 7231 // RFC 2616, RFC 7231
static std::map<size_t, const char*> Map = { static std::map<size_t, const char*> Map = {
{ -1, "Invalid Response Code"},
{ 100, "Continue" }, { 100, "Continue" },
{ 101, "Switching Protocols" }, { 101, "Switching Protocols" },
{ 102, "Processing" }, { 102, "Processing" },
@@ -203,12 +273,22 @@ static std::map<size_t, const char*> Map = {
{ 508, "Loop Detected" }, { 508, "Loop Detected" },
{ 510, "Not Extended" }, { 510, "Not Extended" },
{ 511, "Network Authentication Required" }, { 511, "Network Authentication Required" },
// cloudflare status codes
{ 520, "(CDN) Web Server Returns An Unknown Error" },
{ 521, "(CDN) Web Server Is Down" },
{ 522, "(CDN) Connection Timed Out" },
{ 523, "(CDN) Origin Is Unreachable" },
{ 524, "(CDN) A Timeout Occurred" },
{ 525, "(CDN) SSL Handshake Failed" },
{ 526, "(CDN) Invalid SSL Certificate" },
{ 527, "(CDN) Railgun Listener To Origin Error" },
{ 530, "(CDN) 1XXX Internal Error" },
}; };
std::string Http::Status::ToString(int code) { std::string Http::Status::ToString(int code) {
if (Map.find(code) != Map.end()) { if (Map.find(code) != Map.end()) {
return Map.at(code); return Map.at(code);
} else { } else {
return "Unassigned"; return std::to_string(code);
} }
} }
+65
View File
@@ -0,0 +1,65 @@
#include "SignalHandling.h"
#include "Common.h"
#ifdef __unix
#include <csignal>
static void UnixSignalHandler(int sig) {
switch (sig) {
case SIGPIPE:
warn("ignoring SIGPIPE");
break;
case SIGTERM:
info("gracefully shutting down via SIGTERM");
Application::GracefullyShutdown();
break;
case SIGINT:
info("gracefully shutting down via SIGINT");
Application::GracefullyShutdown();
break;
default:
debug("unhandled signal: " + std::to_string(sig));
break;
}
}
#endif // __unix
#ifdef WIN32
#include <windows.h>
// return TRUE if handled, FALSE if not
BOOL WINAPI Win32CtrlC_Handler(DWORD CtrlType) {
switch (CtrlType) {
case CTRL_C_EVENT:
info("gracefully shutting down via CTRL+C");
Application::GracefullyShutdown();
return TRUE;
case CTRL_BREAK_EVENT:
info("gracefully shutting down via CTRL+BREAK");
Application::GracefullyShutdown();
return TRUE;
case CTRL_CLOSE_EVENT:
info("gracefully shutting down via close");
Application::GracefullyShutdown();
return TRUE;
}
// we dont care for any others like CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT
return FALSE;
}
#endif // WIN32
void SetupSignalHandlers() {
// signal handlers for unix#include <windows.h>
#ifdef __unix
trace("registering handlers for SIGINT, SIGTERM, SIGPIPE");
signal(SIGPIPE, UnixSignalHandler);
signal(SIGTERM, UnixSignalHandler);
#ifndef DEBUG
signal(SIGINT, UnixSignalHandler);
#endif // DEBUG
#endif // __unix
// signal handlers for win32
#ifdef WIN32
trace("registering handlers for CTRL_*_EVENTs");
SetConsoleCtrlHandler(Win32CtrlC_Handler, TRUE);
#endif // WIN32
}
+98
View File
@@ -0,0 +1,98 @@
#include "SocketIO.h"
#include "Common.h"
#include <iostream>
//TODO Default disabled with config option
static std::unique_ptr<SocketIO> SocketIOInstance = std::make_unique<SocketIO>();
SocketIO& SocketIO::Get() {
return *SocketIOInstance;
}
SocketIO::SocketIO() noexcept
: mThread([this] { ThreadMain(); }) {
mClient.socket()->on("network", [&](sio::event&e) {
if(e.get_message()->get_string() == "Welcome"){
info("SocketIO Authenticated!");
mAuthenticated = true;
}
});
mClient.socket()->on("welcome", [&](sio::event&) {
info("Got welcome from backend! Authenticating SocketIO...");
mClient.socket()->emit("onInitConnection", Application::Settings.Key);
});
mClient.set_logs_quiet();
mClient.set_reconnect_delay(10000);
mClient.connect(Application::GetBackendUrlForSocketIO());
}
SocketIO::~SocketIO() {
mCloseThread.store(true);
mThread.join();
}
static constexpr auto EventNameFromEnum(SocketIOEvent Event) {
switch (Event) {
case SocketIOEvent::CPUUsage:
return "cpu usage";
case SocketIOEvent::MemoryUsage:
return "memory usage";
case SocketIOEvent::ConsoleOut:
return "console out";
case SocketIOEvent::NetworkUsage:
return "network usage";
case SocketIOEvent::PlayerList:
return "player list";
default:
error("unreachable code reached (developer error)");
abort();
}
}
void SocketIO::Emit(SocketIOEvent Event, const std::string& Data) {
if (!mAuthenticated) {
debug("trying to emit a socket.io event when not yet authenticated");
return;
}
std::string EventName = EventNameFromEnum(Event);
debug("emitting event \"" + EventName + "\" with data: \"" + Data);
std::unique_lock Lock(mQueueMutex);
mQueue.push_back({EventName, Data });
debug("queue now has " + std::to_string(mQueue.size()) + " events");
}
void SocketIO::ThreadMain() {
while (!mCloseThread.load()) {
bool empty;
{ // queue lock scope
std::unique_lock Lock(mQueueMutex);
empty = mQueue.empty();
} // end queue lock scope
if (empty || !mClient.opened()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
} else {
Event TheEvent;
{ // queue lock scope
std::unique_lock Lock(mQueueMutex);
TheEvent = mQueue.front();
mQueue.pop_front();
} // end queue lock scope
debug("sending \"" + TheEvent.Name + "\" event");
mClient.socket()->emit(TheEvent.Name, TheEvent.Data);
debug("sent \"" + TheEvent.Name + "\" event");
}
}
// using std::cout as this happens during static destruction and the logger might be dead already
std::cout << "closing " + std::string(__func__) << std::endl;
mClient.sync_close();
mClient.clear_con_listeners();
std::cout << "closed" << std::endl;
}
+95 -45
View File
@@ -1,6 +1,4 @@
#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT #include <toml.hpp> // header-only version of TOML++
#include <toml11/toml.hpp> // header-only version of TOML++
#include "TConfig.h" #include "TConfig.h"
#include <fstream> #include <fstream>
@@ -20,6 +18,8 @@ static constexpr std::string_view StrName = "Name";
static constexpr std::string_view StrDescription = "Description"; static constexpr std::string_view StrDescription = "Description";
static constexpr std::string_view StrResourceFolder = "ResourceFolder"; static constexpr std::string_view StrResourceFolder = "ResourceFolder";
static constexpr std::string_view StrAuthKey = "AuthKey"; static constexpr std::string_view StrAuthKey = "AuthKey";
static constexpr std::string_view StrSendErrors = "SendErrors";
static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
TConfig::TConfig() { TConfig::TConfig() {
if (!fs::exists(ConfigFileName) || !fs::is_regular_file(ConfigFileName)) { if (!fs::exists(ConfigFileName) || !fs::is_regular_file(ConfigFileName)) {
@@ -34,21 +34,13 @@ TConfig::TConfig() {
} }
} }
void TConfig::FlushToFile() { void WriteSendErrors(const std::string& name) {
auto data = toml::parse(ConfigFileName); std::ofstream CfgFile { name, std::ios::out | std::ios::app };
data["General"] = toml::table(); CfgFile << "# You can turn on/off the SendErrors message you get on startup here" << std::endl
data["General"][StrAuthKey.data()] = Application::Settings.Key; << StrSendErrorsMessageEnabled << " = true" << std::endl
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled; << "# If SendErrors is `true`, the server will send helpful info about crashes and other issues back to the BeamMP developers. This info may include your config, who is on your server at the time of the error, and similar general information. This kind of data is vital in helping us diagnose and fix issues faster. This has no impact on server performance. You can opt-out of this system by setting this to `false`."
data["General"][StrPrivate.data()] = Application::Settings.Private; << std::endl
data["General"][StrPort.data()] = Application::Settings.Port; << StrSendErrors << " = true" << std::endl;
data["General"][StrName.data()] = Application::Settings.ServerName;
data["General"][StrMaxCars.data()] = Application::Settings.MaxCars;
data["General"][StrMaxPlayers.data()] = Application::Settings.MaxPlayers;
data["General"][StrMap.data()] = Application::Settings.MapName;
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
std::ofstream Stream(ConfigFileName);
Stream << data << std::flush;
} }
void TConfig::CreateConfigFile(std::string_view name) { void TConfig::CreateConfigFile(std::string_view name) {
@@ -63,33 +55,37 @@ void TConfig::CreateConfigFile(std::string_view name) {
error("an error occurred and was ignored during config transfer: " + std::string(e.what())); error("an error occurred and was ignored during config transfer: " + std::string(e.what()));
} }
{ // create file context toml::table tbl { {
std::ofstream ofs(name.data());
}
auto data = toml::parse<toml::preserve_comments>(name.data()); { "General",
toml::table { {
data["General"] = toml::table(); { StrDebug, Application::Settings.DebugModeEnabled },
data["General"][StrAuthKey.data()] = Application::Settings.Key; { StrPrivate, Application::Settings.Private },
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled; { StrPort, Application::Settings.Port },
data["General"][StrPrivate.data()] = Application::Settings.Private; { StrMaxCars, Application::Settings.MaxCars },
data["General"][StrPort.data()] = Application::Settings.Port; { StrMaxPlayers, Application::Settings.MaxPlayers },
data["General"][StrName.data()] = Application::Settings.ServerName; { StrMap, Application::Settings.MapName },
data["General"][StrMaxCars.data()] = Application::Settings.MaxCars; { StrName, Application::Settings.ServerName },
data["General"][StrMaxPlayers.data()] = Application::Settings.MaxPlayers; { StrDescription, Application::Settings.ServerDesc },
data["General"][StrMap.data()] = Application::Settings.MapName; { StrResourceFolder, Application::Settings.Resource },
data["General"][StrDescription.data()] = Application::Settings.ServerDesc; { StrAuthKey, Application::Settings.Key },
data["General"][StrResourceFolder.data()] = Application::Settings.Resource; //{ StrSendErrors, Application::Settings.SendErrors },
} } },
} };
std::ofstream ofs { std::string(name) }; std::ofstream ofs { std::string(name) };
if (ofs.good()) { if (ofs.good()) {
ofs << "# This is the BeamMP-Server config file.\n" ofs << "# This is the BeamMP-Server config file.\n"
"# Help & Documentation: `https://wiki.beammp.com/en/home/server-maintenance`\n" "# Help & Documentation: `https://wiki.beammp.com/en/home/server-maintenance`\n"
"# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n" "# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n"
<< '\n'; << '\n';
ofs << data << '\n'; ofs << tbl << '\n';
error("There was no \"" + std::string(ConfigFileName) + "\" file (this is normal for the first time running the server), so one was generated for you. It was automatically filled with the settings from your Server.cfg, if you have one. Please open ServerConfig.toml and ensure your AuthKey and other settings are filled in and correct, then restart the server. The old Server.cfg file will no longer be used and causes a warning if it exists from now on."); error("There was no \"" + std::string(ConfigFileName) + "\" file (this is normal for the first time running the server), so one was generated for you. It was automatically filled with the settings from your Server.cfg, if you have one. Please open ServerConfig.toml and ensure your AuthKey and other settings are filled in and correct, then restart the server. The old Server.cfg file will no longer be used and causes a warning if it exists from now on.");
mFailed = true; mFailed = true;
ofs.close();
WriteSendErrors(std::string(name));
} else { } else {
error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work."); error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work.");
mFailed = true; mFailed = true;
@@ -98,17 +94,71 @@ void TConfig::CreateConfigFile(std::string_view name) {
void TConfig::ParseFromFile(std::string_view name) { void TConfig::ParseFromFile(std::string_view name) {
try { try {
toml::value data = toml::parse<toml::preserve_comments>(name.data()); toml::table FullTable = toml::parse_file(name);
Application::Settings.DebugModeEnabled = data["General"][StrDebug.data()].as_boolean(); toml::table GeneralTable = *FullTable["General"].as_table();
Application::Settings.Private = data["General"][StrPrivate.data()].as_boolean(); if (auto val = GeneralTable[StrDebug].value<bool>(); val.has_value()) {
Application::Settings.Port = data["General"][StrPort.data()].as_integer(); Application::Settings.DebugModeEnabled = val.value();
Application::Settings.MaxCars = data["General"][StrMaxCars.data()].as_integer(); } else {
Application::Settings.MaxPlayers = data["General"][StrMaxPlayers.data()].as_integer(); throw std::runtime_error(std::string(StrDebug));
Application::Settings.MapName = data["General"][StrMap.data()].as_string(); }
Application::Settings.ServerName = data["General"][StrName.data()].as_string(); if (auto val = GeneralTable[StrPrivate].value<bool>(); val.has_value()) {
Application::Settings.ServerDesc = data["General"][StrDescription.data()].as_string(); Application::Settings.Private = val.value();
Application::Settings.Resource = data["General"][StrResourceFolder.data()].as_string(); } else {
Application::Settings.Key = data["General"][StrAuthKey.data()].as_string(); throw std::runtime_error(std::string(StrPrivate));
}
if (auto val = GeneralTable[StrPort].value<int>(); val.has_value()) {
Application::Settings.Port = val.value();
} else {
throw std::runtime_error(std::string(StrPort));
}
if (auto val = GeneralTable[StrMaxCars].value<int>(); val.has_value()) {
Application::Settings.MaxCars = val.value();
} else {
throw std::runtime_error(std::string(StrMaxCars));
}
if (auto val = GeneralTable[StrMaxPlayers].value<int>(); val.has_value()) {
Application::Settings.MaxPlayers = val.value();
} else {
throw std::runtime_error(std::string(StrMaxPlayers));
}
if (auto val = GeneralTable[StrMap].value<std::string>(); val.has_value()) {
Application::Settings.MapName = val.value();
} else {
throw std::runtime_error(std::string(StrMap));
}
if (auto val = GeneralTable[StrName].value<std::string>(); val.has_value()) {
Application::Settings.ServerName = val.value();
} else {
throw std::runtime_error(std::string(StrName));
}
if (auto val = GeneralTable[StrDescription].value<std::string>(); val.has_value()) {
Application::Settings.ServerDesc = val.value();
} else {
throw std::runtime_error(std::string(StrDescription));
}
if (auto val = GeneralTable[StrResourceFolder].value<std::string>(); val.has_value()) {
Application::Settings.Resource = val.value();
} else {
throw std::runtime_error(std::string(StrResourceFolder));
}
if (auto val = GeneralTable[StrAuthKey].value<std::string>(); val.has_value()) {
Application::Settings.Key = val.value();
} else {
throw std::runtime_error(std::string(StrAuthKey));
}
// added later, so behaves differently
if (auto val = GeneralTable[StrSendErrors].value<bool>(); val.has_value()) {
Application::Settings.SendErrors = val.value();
} else {
// dont throw, instead write it into the file and use default
WriteSendErrors(std::string(name));
}
if (auto val = GeneralTable[StrSendErrorsMessageEnabled].value<bool>(); val.has_value()) {
Application::Settings.SendErrorsMessageEnabled = val.value();
} else {
// no idea what to do here, ignore...?
// this entire toml parser sucks and is replaced in the upcoming lua.
}
} catch (const std::exception& err) { } catch (const std::exception& err) {
error("Error parsing config file value: " + std::string(err.what())); error("Error parsing config file value: " + std::string(err.what()));
mFailed = true; mFailed = true;
+67 -29
View File
@@ -3,8 +3,12 @@
#include "Client.h" #include "Client.h"
#include "Http.h" #include "Http.h"
//#include "SocketIO.h" //#include "SocketIO.h"
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <sstream> #include <sstream>
namespace json = rapidjson;
void THeartbeatThread::operator()() { void THeartbeatThread::operator()() {
RegisterThread("Heartbeat"); RegisterThread("Heartbeat");
std::string Body; std::string Body;
@@ -36,52 +40,88 @@ void THeartbeatThread::operator()() {
Body += "&pps=" + Application::PPS(); Body += "&pps=" + Application::PPS();
auto SentryReportError = [&](const std::string& transaction, int status) { auto SentryReportError = [&](const std::string& transaction, int status) {
if (status < 0) {
status = 0;
}
auto Lock = Sentry.CreateExclusiveContext(); auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("heartbeat", Sentry.SetContext("heartbeat",
{ { "response-body", T }, { { "response-body", T },
{ "request-body", Body } }); { "request-body", Body } });
Sentry.SetTransaction(transaction); Sentry.SetTransaction(transaction);
trace("sending log to sentry: " + std::to_string(status) + " for " + transaction);
Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")"); Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")");
}; };
auto Target = "/heartbeat"; auto Target = "/heartbeat";
unsigned int ResponseCode = 0; int ResponseCode = -1;
T = Http::POST(Application::GetBackendHostname(), 443, Target, {}, Body, "application/x-www-form-urlencoded", &ResponseCode); const std::vector<std::string> Urls = {
Application::GetBackendHostname(),
Application::GetBackup1Hostname(),
Application::GetBackup2Hostname(),
};
if (T.substr(0, 2) != "20" || ResponseCode != 200) { json::Document Doc;
trace("got " + T + " from backend"); bool Ok = false;
SentryReportError(Application::GetBackendHostname() + Target, ResponseCode); for (const auto& Url : Urls) {
T = Http::POST(Url, Target, { { "api-v", "2" } }, Body, false, &ResponseCode);
trace(T);
Doc.Parse(T.data(), T.size());
if (Doc.HasParseError() || !Doc.IsObject()) {
error("Backend response failed to parse as valid json");
debug("Response was: `" + T + "`");
Sentry.SetContext("JSON Response", { { "reponse", T } });
SentryReportError(Url + Target, ResponseCode);
} else if (ResponseCode != 200) {
SentryReportError(Url + Target, ResponseCode);
} else {
// all ok
Ok = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
T = Http::POST(Application::GetBackup1Hostname(), 443, Target, {}, Body, "application/x-www-form-urlencoded", &ResponseCode); }
if (T.substr(0, 2) != "20" || ResponseCode != 200) { std::string Status {};
SentryReportError(Application::GetBackup1Hostname() + Target, ResponseCode); std::string Code {};
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::string Message {};
T = Http::POST(Application::GetBackup2Hostname(), 443, Target, {}, Body, "application/x-www-form-urlencoded", &ResponseCode); const auto StatusKey = "status";
if (T.substr(0, 2) != "20" || ResponseCode != 200) { const auto CodeKey = "code";
warn("Backend system refused server! Server will not show in the public server list."); const auto MessageKey = "msg";
isAuth = false; if (Ok) {
SentryReportError(Application::GetBackup2Hostname() + Target, ResponseCode); if (Doc.HasMember(StatusKey) && Doc[StatusKey].IsString()) {
} Status = Doc[StatusKey].GetString();
} else {
Sentry.SetContext("JSON Response", { { StatusKey, "invalid string / missing" } });
Ok = false;
}
if (Doc.HasMember(CodeKey) && Doc[CodeKey].IsString()) {
Code = Doc[CodeKey].GetString();
} else {
Sentry.SetContext("JSON Response", { { CodeKey, "invalid string / missing" } });
Ok = false;
}
if (Doc.HasMember(MessageKey) && Doc[MessageKey].IsString()) {
Message = Doc[MessageKey].GetString();
} else {
Sentry.SetContext("JSON Response", { { MessageKey, "invalid string / missing" } });
Ok = false;
}
if (!Ok) {
error("Missing/invalid json members in backend response");
Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__));
} }
} }
if (!isAuth) { if (Ok && !isAuth) {
if (T == "2000") { if (Status == "2000") {
info(("Authenticated!")); info(("Authenticated!"));
isAuth = true; isAuth = true;
} else if (T == "200") { } else if (Status == "200") {
info(("Resumed authenticated session!")); info(("Resumed authenticated session!"));
isAuth = true; isAuth = true;
} else {
if (Message.empty()) {
Message = "Backend didn't provide a reason";
}
error("Backend REFUSED the auth key. " + Message);
} }
} }
//SocketIO::Get().SetAuthenticated(isAuth);
} }
} }
@@ -94,8 +134,8 @@ std::string THeartbeatThread::GenerateCall() {
<< "&port=" << Application::Settings.Port << "&port=" << Application::Settings.Port
<< "&map=" << Application::Settings.MapName << "&map=" << Application::Settings.MapName
<< "&private=" << (Application::Settings.Private ? "true" : "false") << "&private=" << (Application::Settings.Private ? "true" : "false")
<< "&version=" << Application::ServerVersionString() << "&version=" << Application::ServerVersion()
<< "&clientversion=" << Application::ClientVersionString() << "&clientversion=" << Application::ClientVersion()
<< "&name=" << Application::Settings.ServerName << "&name=" << Application::Settings.ServerName
<< "&modlist=" << mResourceManager.TrimmedList() << "&modlist=" << mResourceManager.TrimmedList()
<< "&modstotalsize=" << mResourceManager.MaxModSize() << "&modstotalsize=" << mResourceManager.MaxModSize()
@@ -109,10 +149,8 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
, mServer(Server) { , mServer(Server) {
Application::RegisterShutdownHandler([&] { Application::RegisterShutdownHandler([&] {
if (mThread.joinable()) { if (mThread.joinable()) {
debug("shutting down Heartbeat");
mShutdown = true; mShutdown = true;
mThread.join(); mThread.join();
debug("shut down Heartbeat");
} }
}); });
Start(); Start();
-4
View File
@@ -6,8 +6,6 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::unordered_map<std::string, lua_State*> TLuaEngine::mGlobals;
// necessary as lua relies on global state // necessary as lua relies on global state
TLuaEngine* TheEngine; TLuaEngine* TheEngine;
@@ -25,10 +23,8 @@ TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
FolderList(Path, false); FolderList(Path, false);
mPath = Path; mPath = Path;
Application::RegisterShutdownHandler([&] {if (mThread.joinable()) { Application::RegisterShutdownHandler([&] {if (mThread.joinable()) {
debug("shutting down LuaEngine");
mShutdown = true; mShutdown = true;
mThread.join(); mThread.join();
debug("shut down LuaEngine");
} }); } });
Start(); Start();
} }
+58 -318
View File
@@ -3,44 +3,14 @@
#include "Common.h" #include "Common.h"
#include "CustomAssert.h" #include "CustomAssert.h"
#include "Defer.h" #include "Defer.h"
#include "Http.h"
#include "TLuaEngine.h" #include "TLuaEngine.h"
#include "TNetwork.h" #include "TNetwork.h"
#include "TServer.h" #include "TServer.h"
#include <functional>
#include <future> #include <future>
#include <thread> #include <thread>
namespace LuaTable { // TODO: REWRITE
void Begin(lua_State* L) {
lua_newtable(L);
}
void End(lua_State* L, const std::string& name) {
lua_setglobal(L, name.c_str());
}
void BeginEntry(lua_State* L, const std::string& name) {
lua_pushstring(L, name.c_str());
}
void EndEntry(lua_State* L) {
lua_settable(L, -3);
}
void InsertFunction(lua_State* L, const std::string& name, lua_CFunction func) {
BeginEntry(L, name);
lua_pushcfunction(L, func);
EndEntry(L);
}
void InsertInteger(lua_State* L, const std::string& name, lua_Integer i) {
BeginEntry(L, name);
lua_pushinteger(L, i);
EndEntry(L);
}
}
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg); void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg);
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg); std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg);
@@ -70,13 +40,11 @@ std::shared_ptr<TLuaArg> CreateArg(lua_State* L, int T, int S) {
} }
return temp; return temp;
} }
void ClearStack(lua_State* L) { void ClearStack(lua_State* L) {
lua_settop(L, 0); lua_settop(L, 0);
} }
std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg> arg) { std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg> arg) {
std::lock_guard<std::mutex> lockGuard(lua->Lock); std::lock_guard<std::mutex> lockGuard(lua->Lock);
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArg> arg) { return CallFunction(lua, R, arg); }); std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArg> arg) { return CallFunction(lua, R, arg); });
std::future<std::any> f1 = task.get_future(); std::future<std::any> f1 = task.get_future();
@@ -230,7 +198,6 @@ void ExecuteAsync(TLuaFile* lua, const std::string& FuncName) {
} }
void CallAsync(TLuaFile* lua, const std::string& Func, int U) { void CallAsync(TLuaFile* lua, const std::string& Func, int U) {
lua->SetStopThread(false); lua->SetStopThread(false);
int D = 1000 / U; int D = 1000 / U;
while (!lua->GetStopThread()) { while (!lua->GetStopThread()) {
@@ -381,11 +348,11 @@ int lua_GetIdentifiers(lua_State* L) {
auto IDs = MaybeClient.value().lock()->GetIdentifiers(); auto IDs = MaybeClient.value().lock()->GetIdentifiers();
if (IDs.empty()) if (IDs.empty())
return 0; return 0;
LuaTable::Begin(L); lua_newtable(L);
for (const auto& Pair : IDs) { for (const std::string& ID : IDs) {
LuaTable::BeginEntry(L, Pair.first); lua_pushstring(L, ID.substr(0, ID.find(':')).c_str());
lua_pushstring(L, Pair.second.c_str()); lua_pushstring(L, ID.c_str());
LuaTable::EndEntry(L); lua_settable(L, -3);
} }
} else } else
return 0; return 0;
@@ -565,49 +532,49 @@ int lua_Set(lua_State* L) {
case 0: //debug case 0: //debug
if (lua_isboolean(L, 2)) { if (lua_isboolean(L, 2)) {
Application::Settings.DebugModeEnabled = lua_toboolean(L, 2); Application::Settings.DebugModeEnabled = lua_toboolean(L, 2);
info(Name + (" | Set `Debug` to ") + (Application::Settings.DebugModeEnabled ? "true" : "false")); info(Name + (" | Debug -> ") + (Application::Settings.DebugModeEnabled ? "true" : "false"));
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected boolean for ID : 0")); SendError(Engine(), L, ("set invalid argument [2] expected boolean for ID : 0"));
break; break;
case 1: //private case 1: //private
if (lua_isboolean(L, 2)) { if (lua_isboolean(L, 2)) {
Application::Settings.Private = lua_toboolean(L, 2); Application::Settings.Private = lua_toboolean(L, 2);
info(Name + (" | Set `Private` to ") + (Application::Settings.Private ? "true" : "false")); info(Name + (" | Private -> ") + (Application::Settings.Private ? "true" : "false"));
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected boolean for ID : 1")); SendError(Engine(), L, ("set invalid argument [2] expected boolean for ID : 1"));
break; break;
case 2: //max cars case 2: //max cars
if (lua_isnumber(L, 2)) { if (lua_isnumber(L, 2)) {
Application::Settings.MaxCars = int(lua_tointeger(L, 2)); Application::Settings.MaxCars = int(lua_tointeger(L, 2));
info(Name + (" | Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars)); info(Name + (" | MaxCars -> ") + std::to_string(Application::Settings.MaxCars));
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected number for ID : 2")); SendError(Engine(), L, ("set invalid argument [2] expected number for ID : 2"));
break; break;
case 3: //max players case 3: //max players
if (lua_isnumber(L, 2)) { if (lua_isnumber(L, 2)) {
Application::Settings.MaxPlayers = int(lua_tointeger(L, 2)); Application::Settings.MaxPlayers = int(lua_tointeger(L, 2));
info(Name + (" | Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers)); info(Name + (" | MaxPlayers -> ") + std::to_string(Application::Settings.MaxPlayers));
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected number for ID : 3")); SendError(Engine(), L, ("set invalid argument [2] expected number for ID : 3"));
break; break;
case 4: //Map case 4: //Map
if (lua_isstring(L, 2)) { if (lua_isstring(L, 2)) {
Application::Settings.MapName = lua_tostring(L, 2); Application::Settings.MapName = lua_tostring(L, 2);
info(Name + (" | Set `Map` to ") + Application::Settings.MapName); info(Name + (" | MapName -> ") + Application::Settings.MapName);
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 4")); SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 4"));
break; break;
case 5: //Name case 5: //Name
if (lua_isstring(L, 2)) { if (lua_isstring(L, 2)) {
Application::Settings.ServerName = lua_tostring(L, 2); Application::Settings.ServerName = lua_tostring(L, 2);
info(Name + (" | Set `Name` to ") + Application::Settings.ServerName); info(Name + (" | ServerName -> ") + Application::Settings.ServerName);
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 5")); SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 5"));
break; break;
case 6: //Desc case 6: //Desc
if (lua_isstring(L, 2)) { if (lua_isstring(L, 2)) {
Application::Settings.ServerDesc = lua_tostring(L, 2); Application::Settings.ServerDesc = lua_tostring(L, 2);
info(Name + (" | Set `Description` to ") + Application::Settings.ServerDesc); info(Name + (" | ServerDesc -> ") + Application::Settings.ServerDesc);
} else } else
SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 6")); SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 6"));
break; break;
@@ -615,11 +582,12 @@ int lua_Set(lua_State* L) {
warn(("Invalid config ID : ") + std::to_string(C)); warn(("Invalid config ID : ") + std::to_string(C));
break; break;
} }
return 0; return 0;
} }
extern "C" { extern "C" {
int InternalLuaPrint(lua_State* L, bool pretty) { int lua_Print(lua_State* L) {
int Arg = lua_gettop(L); int Arg = lua_gettop(L);
std::string to_print; std::string to_print;
for (int i = 1; i <= Arg; i++) { for (int i = 1; i <= Arg; i++) {
@@ -652,78 +620,12 @@ int InternalLuaPrint(lua_State* L, bool pretty) {
to_print += "\t"; to_print += "\t";
} }
} }
if (pretty) { luaprint(to_print);
luaprint(to_print);
} else {
Application::Console().WriteRaw(to_print);
}
return 0; return 0;
} }
int lua_Print(lua_State* L) {
return InternalLuaPrint(L, true);
}
int lua_PrintRaw(lua_State* L) {
return InternalLuaPrint(L, false);
}
} }
int lua_TempFix(lua_State* L) { int lua_TempFix(lua_State* L);
if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1));
auto MaybeClient = GetClient(Engine().Server(), ID);
if (!MaybeClient || MaybeClient.value().expired())
return 0;
std::string Ret;
auto c = MaybeClient.value().lock();
if (c->IsGuest()) {
Ret = "Guest-" + c->GetName();
} else
Ret = c->GetName();
lua_pushstring(L, Ret.c_str());
} else
SendError(Engine(), L, "GetDID not enough arguments");
return 1;
}
int lua_Registered(lua_State* L) {
lua_Debug info;
lua_getstack(L, 0, &info);
lua_getinfo(L, "n", &info);
if (auto it = TLuaEngine::mGlobals.find(info.name); it != TLuaEngine::mGlobals.end()) {
lua_getglobal(it->second, info.name);
if (lua_isfunction(it->second, -1)) {
lua_pcall(it->second, 0, 0, 0); //TODO revisit to allow arguments and return also we need to mutex this
}
return 0;
}
SendError(Engine(), L, "Cannot find global '" + std::string(info.name) + "\'");
return 0;
}
int lua_Register(lua_State* L) {
if (lua_isstring(L, 1)) {
std::string Name(lua_tolstring(L, 1, nullptr));
lua_getglobal(L, Name.c_str());
if (lua_isfunction(L, -1)) {
TLuaEngine::mGlobals.emplace(Name, L);
for (auto& Script : Engine().LuaFiles()) {
if (Script->GetState() != L) {
lua_register(Script->GetState(), Name.c_str(), lua_Registered);
}
}
} else {
SendError(Engine(), L, Name + " is not a global function!");
ClearStack(L);
}
} else {
SendError(Engine(), L, "Wrong arguments to `Register`, expected string");
}
return 0;
}
void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote) { void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote) {
auto Lock = std::unique_lock(mInitMutex); auto Lock = std::unique_lock(mInitMutex);
@@ -768,7 +670,6 @@ std::string TLuaFile::GetOrigin() {
} }
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg) { std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg) {
lua_State* luaState = lua->GetState(); lua_State* luaState = lua->GetState();
lua_getglobal(luaState, FuncName.c_str()); lua_getglobal(luaState, FuncName.c_str());
if (lua_isfunction(luaState, -1)) { if (lua_isfunction(luaState, -1)) {
@@ -802,203 +703,31 @@ void TLuaFile::SetFileName(const std::string& Name) {
mFileName = Name; mFileName = Name;
} }
// GetOSName() -> Linux || Windows || Other
int lua_GetOSName(lua_State* L) {
#if defined(__linux) || defined(__linux__)
lua_pushstring(L, "Linux");
#elif defined(WIN32)
lua_pushstring(L, "Windows");
#else
lua_pushstring(L, "Unknown");
#endif
return 1;
}
int lua_GetServerVersion(lua_State* L) {
const auto& ver = Application::ServerVersion();
lua_pushinteger(L, ver.major);
lua_pushinteger(L, ver.minor);
lua_pushinteger(L, ver.patch);
return 3;
}
// status, body = HttpsGET(host, port, target)
// example usage:
// send a GET https://example.com:443/index.html:
// status, body = MP.HttpGET("example.com", 443, "/index.html")
int lua_HttpsGET(lua_State* L) {
if (!lua_isstring(L, 1)) {
SendError(Engine(), L, "`HttpsGET` expects host (type string) as first argument.");
ClearStack(L);
return 0;
}
if (!lua_isnumber(L, 2)) {
SendError(Engine(), L, "`HttpsGET` expects port (type number) as second argument.");
ClearStack(L);
return 0;
}
if (!lua_isstring(L, 3)) {
SendError(Engine(), L, "`HttpsGET` expects target (type string) as third argument.");
ClearStack(L);
return 0;
}
auto Host = lua_tostring(L, 1);
auto Port = int(lua_tointeger(L, 2));
auto Target = lua_tostring(L, 3);
ClearStack(L);
unsigned int Status;
auto Body = Http::GET(Host, Port, Target, &Status);
lua_pushinteger(L, Status);
auto PrettyRemote = "https://" + std::string(Host) + ":" + std::to_string(Port) + std::string(Target);
if (Body == Http::ErrorString) {
SendError(Engine(), L, "HTTPS GET " + PrettyRemote + " failed status " + std::to_string(Status) + ". Check the console or log for more info.");
return 1;
} else {
debug("GET " + PrettyRemote + " completed status " + std::to_string(Status));
}
lua_pushstring(L, Body.c_str());
return 2;
}
// status, body = HttpsPOST(host, port, target, body, content_type)
int lua_HttpsPOST(lua_State* L) {
if (!lua_isstring(L, 1)) {
SendError(Engine(), L, "`HttpsPOST` expects host (type string) as 1. argument.");
ClearStack(L);
return 0;
}
if (!lua_isnumber(L, 2)) {
SendError(Engine(), L, "`HttpsPOST` expects port (type number) as 2. argument.");
ClearStack(L);
return 0;
}
if (!lua_isstring(L, 3)) {
SendError(Engine(), L, "`HttpsPOST` expects target (type string) as 3. argument.");
ClearStack(L);
return 0;
}
if (!lua_isstring(L, 4)) {
SendError(Engine(), L, "`HttpsPOST` expects body (type string) as 4. argument.");
ClearStack(L);
return 0;
}
if (!lua_isstring(L, 5)) {
SendError(Engine(), L, "`HttpsPOST` expects content_type (type string) as 5. argument.");
ClearStack(L);
return 0;
}
auto Host = lua_tostring(L, 1);
auto Port = int(lua_tointeger(L, 2));
auto Target = lua_tostring(L, 3);
auto RequestBody = lua_tostring(L, 4);
auto ContentType = lua_tostring(L, 5);
ClearStack(L);
// build fields
std::unordered_map<std::string, std::string> Fields;
unsigned int Status;
auto ResponseBody = Http::POST(Host, Port, Target, {}, RequestBody, ContentType, &Status);
lua_pushinteger(L, Status);
auto PrettyRemote = "https://" + std::string(Host) + ":" + std::to_string(Port) + std::string(Target);
if (ResponseBody == Http::ErrorString) {
SendError(Engine(), L, "HTTPS POST " + PrettyRemote + " failed status " + std::to_string(Status) + ". Check the console or log for more info.");
return 1;
} else {
debug("POST " + PrettyRemote + " completed status " + std::to_string(Status));
}
lua_pushstring(L, ResponseBody.c_str());
return 2;
}
int lua_GetPluginName(lua_State* L) {
auto MaybeFile = Engine().GetScript(L);
if (MaybeFile) {
lua_pushstring(L, MaybeFile.value().get().GetPluginName().c_str());
return 1;
} else {
warn("no plugin associated with this state");
return 0;
}
}
int lua_GetPluginPath(lua_State* L) {
auto MaybeFile = Engine().GetScript(L);
if (MaybeFile) {
lua_pushstring(L, MaybeFile.value().get().GetPluginPath().c_str());
return 1;
} else {
warn("no plugin associated with this state");
return 0;
}
}
int lua_Dump(lua_State* L) {
}
void TLuaFile::Load() { void TLuaFile::Load() {
Assert(mLuaState); Assert(mLuaState);
luaL_openlibs(mLuaState); luaL_openlibs(mLuaState);
lua_register(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers);
LuaTable::Begin(mLuaState); lua_register(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);
lua_register(mLuaState, "TriggerLocalEvent", lua_TriggerEventL);
LuaTable::BeginEntry(mLuaState, "Settings"); lua_register(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
LuaTable::Begin(mLuaState); lua_register(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
// put Settings enums here lua_register(mLuaState, "isPlayerConnected", lua_isConnected);
LuaTable::InsertInteger(mLuaState, "Debug", 0); lua_register(mLuaState, "RegisterEvent", lua_RegisterEvent);
LuaTable::InsertInteger(mLuaState, "Private", 1); lua_register(mLuaState, "GetPlayerName", lua_GetPlayerName);
LuaTable::InsertInteger(mLuaState, "MaxCars", 2); lua_register(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
LuaTable::InsertInteger(mLuaState, "MaxPlayers", 3); lua_register(mLuaState, "GetPlayerDiscordID", lua_TempFix);
LuaTable::InsertInteger(mLuaState, "Map", 4); lua_register(mLuaState, "CreateThread", lua_CreateThread);
LuaTable::InsertInteger(mLuaState, "Name", 5); lua_register(mLuaState, "GetPlayerVehicles", lua_GetCars);
LuaTable::InsertInteger(mLuaState, "Description", 6); lua_register(mLuaState, "SendChatMessage", lua_sendChat);
LuaTable::EndEntry(mLuaState); lua_register(mLuaState, "GetPlayers", lua_GetAllPlayers);
lua_register(mLuaState, "GetPlayerGuest", lua_GetGuest);
LuaTable::InsertFunction(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers); lua_register(mLuaState, "StopThread", lua_StopThread);
LuaTable::InsertFunction(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG); lua_register(mLuaState, "DropPlayer", lua_dropPlayer);
LuaTable::InsertFunction(mLuaState, "TriggerLocalEvent", lua_TriggerEventL); lua_register(mLuaState, "GetPlayerHWID", lua_HWID);
LuaTable::InsertFunction(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
LuaTable::InsertFunction(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
LuaTable::InsertFunction(mLuaState, "IsPlayerConnected", lua_isConnected);
LuaTable::InsertFunction(mLuaState, "RegisterEvent", lua_RegisterEvent);
LuaTable::InsertFunction(mLuaState, "GetPlayerName", lua_GetPlayerName);
LuaTable::InsertFunction(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
LuaTable::InsertFunction(mLuaState, "GetPlayerDiscordID", lua_TempFix);
LuaTable::InsertFunction(mLuaState, "CreateThread", lua_CreateThread);
LuaTable::InsertFunction(mLuaState, "GetPlayerVehicles", lua_GetCars);
LuaTable::InsertFunction(mLuaState, "SendChatMessage", lua_sendChat);
LuaTable::InsertFunction(mLuaState, "GetPlayers", lua_GetAllPlayers);
LuaTable::InsertFunction(mLuaState, "GetPlayerGuest", lua_GetGuest);
LuaTable::InsertFunction(mLuaState, "StopThread", lua_StopThread);
LuaTable::InsertFunction(mLuaState, "DropPlayer", lua_dropPlayer);
LuaTable::InsertFunction(mLuaState, "Register", lua_Register);
LuaTable::InsertFunction(mLuaState, "GetPlayerHWID", lua_HWID);
LuaTable::InsertFunction(mLuaState, "Sleep", lua_Sleep);
LuaTable::InsertFunction(mLuaState, "Set", lua_Set);
LuaTable::InsertFunction(mLuaState, "GetOSName", lua_GetOSName);
LuaTable::InsertFunction(mLuaState, "HttpsGET", lua_HttpsGET);
LuaTable::InsertFunction(mLuaState, "HttpsPOST", lua_HttpsPOST);
LuaTable::InsertFunction(mLuaState, "GetServerVersion", lua_GetServerVersion);
LuaTable::InsertFunction(mLuaState, "GetPluginName", lua_GetPluginName);
LuaTable::InsertFunction(mLuaState, "GetPluginPath", lua_GetPluginPath);
LuaTable::End(mLuaState, "MP");
lua_register(mLuaState, "print", lua_Print);
lua_register(mLuaState, "printRaw", lua_PrintRaw);
lua_register(mLuaState, "dump", lua_Dump);
lua_register(mLuaState, "exit", lua_ServerExit); lua_register(mLuaState, "exit", lua_ServerExit);
lua_register(mLuaState, "Sleep", lua_Sleep);
lua_register(mLuaState, "print", lua_Print);
lua_register(mLuaState, "Set", lua_Set);
if (!mConsole) if (!mConsole)
Reload(); Reload();
} }
@@ -1060,13 +789,6 @@ TLuaFile::~TLuaFile() {
lua_close(mLuaState); lua_close(mLuaState);
} }
std::string TLuaFile::GetPluginPath() const {
auto path = fs::path(Application::Settings.Resource);
path /= "Server";
path /= mPluginName;
return path.string();
}
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) { void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
Assert(L); Assert(L);
auto MaybeS = Engine.GetScript(L); auto MaybeS = Engine.GetScript(L);
@@ -1077,7 +799,25 @@ void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
TLuaFile& S = MaybeS.value(); TLuaFile& S = MaybeS.value();
a = fs::path(S.GetFileName()).filename().string(); a = fs::path(S.GetFileName()).filename().string();
} }
warn(a + (" | Error in MP Lua call: ") + msg); warn(a + (" | Incorrect Call of ") + msg);
}
int lua_TempFix(lua_State* L) {
if (lua_isnumber(L, 1)) {
int ID = int(lua_tonumber(L, 1));
auto MaybeClient = GetClient(Engine().Server(), ID);
if (!MaybeClient || MaybeClient.value().expired())
return 0;
std::string Ret;
auto c = MaybeClient.value().lock();
if (c->IsGuest()) {
Ret = "Guest-" + c->GetName();
} else
Ret = c->GetName();
lua_pushstring(L, Ret.c_str());
} else
SendError(Engine(), L, "GetDID not enough arguments");
return 1;
} }
void TLuaArg::PushArgs(lua_State* State) { void TLuaArg::PushArgs(lua_State* State) {
+115 -57
View File
@@ -20,18 +20,14 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
}); });
Application::RegisterShutdownHandler([&] { Application::RegisterShutdownHandler([&] {
if (mUDPThread.joinable()) { if (mUDPThread.joinable()) {
debug("shutting down TCPServer");
mShutdown = true; mShutdown = true;
mUDPThread.detach(); mUDPThread.detach();
debug("shut down TCPServer");
} }
}); });
Application::RegisterShutdownHandler([&] { Application::RegisterShutdownHandler([&] {
if (mTCPThread.joinable()) { if (mTCPThread.joinable()) {
debug("shutting down TCPServer");
mShutdown = true; mShutdown = true;
mTCPThread.detach(); mTCPThread.detach();
debug("shut down TCPServer");
} }
}); });
mTCPThread = std::thread(&TNetwork::TCPServerMain, this); mTCPThread = std::thread(&TNetwork::TCPServerMain, this);
@@ -46,7 +42,22 @@ void TNetwork::UDPServerMain() {
error(("Can't start Winsock!")); error(("Can't start Winsock!"));
//return; //return;
} }
#endif // WIN32
mUDPSock = socket(AF_INET, SOCK_DGRAM, 0);
// Create a server hint structure for the server
sockaddr_in serverAddr {};
serverAddr.sin_addr.S_un.S_addr = ADDR_ANY; //Any Local
serverAddr.sin_family = AF_INET; // Address format is IPv4
serverAddr.sin_port = htons(Application::Settings.Port); // Convert from little to big endian
// Try and bind the socket to the IP and port
if (bind(mUDPSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
error(("Can't bind socket!") + std::to_string(WSAGetLastError()));
std::this_thread::sleep_for(std::chrono::seconds(5));
exit(-1);
//return;
}
#else // unix
mUDPSock = socket(AF_INET, SOCK_DGRAM, 0); mUDPSock = socket(AF_INET, SOCK_DGRAM, 0);
// Create a server hint structure for the server // Create a server hint structure for the server
sockaddr_in serverAddr {}; sockaddr_in serverAddr {};
@@ -56,11 +67,12 @@ void TNetwork::UDPServerMain() {
// Try and bind the socket to the IP and port // Try and bind the socket to the IP and port
if (bind(mUDPSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) { if (bind(mUDPSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) {
error("bind() failed: " + GetPlatformAgnosticErrorString()); error(("Can't bind socket!") + std::string(strerror(errno)));
std::this_thread::sleep_for(std::chrono::seconds(5)); std::this_thread::sleep_for(std::chrono::seconds(5));
exit(-1); exit(-1);
//return; //return;
} }
#endif
info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ") info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
+ std::to_string(Application::Settings.MaxPlayers) + (" Clients")); + std::to_string(Application::Settings.MaxPlayers) + (" Clients"));
@@ -107,23 +119,13 @@ void TNetwork::TCPServerMain() {
error("Can't start Winsock!"); error("Can't start Winsock!");
return; return;
} }
#endif // WIN32 SOCKET client, Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
TConnection client {};
SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int optval = 1;
#ifdef WIN32
const char* optval_ptr = reinterpret_cast<const char*>(&optval);
#else
void* optval_ptr = reinterpret_cast<void*>(&optval);
#endif
setsockopt(Listener, SOL_SOCKET, SO_REUSEADDR, optval_ptr, sizeof(optval));
// TODO: check optval or return value idk
sockaddr_in addr {}; sockaddr_in addr {};
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.S_un.S_addr = ADDR_ANY;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(uint16_t(Application::Settings.Port)); addr.sin_port = htons(Application::Settings.Port);
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) != 0) { if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
error("bind() failed: " + GetPlatformAgnosticErrorString()); error("Can't bind socket! " + std::to_string(WSAGetLastError()));
std::this_thread::sleep_for(std::chrono::seconds(5)); std::this_thread::sleep_for(std::chrono::seconds(5));
exit(-1); exit(-1);
} }
@@ -131,9 +133,53 @@ void TNetwork::TCPServerMain() {
error("Invalid listening socket"); error("Invalid listening socket");
return; return;
} }
if (listen(Listener, SOMAXCONN)) { if (listen(Listener, SOMAXCONN)) {
error("listen() failed: " + GetPlatformAgnosticErrorString()); error("listener failed " + std::to_string(GetLastError()));
// FIXME leak Listener //TODO Fix me leak for Listener socket
return;
}
info("Vehicle event network online");
do {
try {
client = accept(Listener, nullptr, nullptr);
if (client == -1) {
warn("Got an invalid client socket on connect! Skipping...");
continue;
}
std::thread ID(&TNetwork::Identify, this, client);
ID.detach();
} catch (const std::exception& e) {
error("fatal: " + std::string(e.what()));
}
} while (client);
CloseSocketProper(client);
WSACleanup();
#else // unix
// wondering why we need slightly different implementations of this?
// ask ms.
SOCKET client = -1;
SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int optval = 1;
setsockopt(Listener, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
// TODO: check optval or return value idk
sockaddr_in addr {};
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(uint16_t(Application::Settings.Port));
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) != 0) {
error(("Can't bind socket! ") + std::string(strerror(errno)));
std::this_thread::sleep_for(std::chrono::seconds(5));
exit(-1);
}
if (Listener == -1) {
error(("Invalid listening socket"));
return;
}
if (listen(Listener, SOMAXCONN)) {
error(("listener failed ") + std::string(strerror(errno)));
//TODO fix me leak Listener
return; return;
} }
info(("Vehicle event network online")); info(("Vehicle event network online"));
@@ -143,9 +189,8 @@ void TNetwork::TCPServerMain() {
debug("shutdown during TCP wait for accept loop"); debug("shutdown during TCP wait for accept loop");
break; break;
} }
client.SockAddrLen = sizeof(client.SockAddr); client = accept(Listener, nullptr, nullptr);
client.Socket = accept(Listener, &client.SockAddr, &client.SockAddrLen); if (client == -1) {
if (client.Socket == -1) {
warn(("Got an invalid client socket on connect! Skipping...")); warn(("Got an invalid client socket on connect! Skipping..."));
continue; continue;
} }
@@ -154,15 +199,12 @@ void TNetwork::TCPServerMain() {
} catch (const std::exception& e) { } catch (const std::exception& e) {
error(("fatal: ") + std::string(e.what())); error(("fatal: ") + std::string(e.what()));
} }
} while (client.Socket); } while (client);
debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
CloseSocketProper(client.Socket);
#ifdef WIN32
CloseSocketProper(client); CloseSocketProper(client);
WSACleanup(); #endif
#endif // WIN32
} }
#undef GetObject //Fixes Windows #undef GetObject //Fixes Windows
@@ -170,19 +212,19 @@ void TNetwork::TCPServerMain() {
#include "Json.h" #include "Json.h"
namespace json = rapidjson; namespace json = rapidjson;
void TNetwork::Identify(const TConnection& client) { void TNetwork::Identify(SOCKET TCPSock) {
RegisterThreadAuto(); RegisterThreadAuto();
char Code; char Code;
if (recv(client.Socket, &Code, 1, 0) != 1) { if (recv(TCPSock, &Code, 1, 0) != 1) {
CloseSocketProper(client.Socket); CloseSocketProper(TCPSock);
return; return;
} }
if (Code == 'C') { if (Code == 'C') {
Authentication(client); Authentication(TCPSock);
} else if (Code == 'D') { } else if (Code == 'D') {
HandleDownload(client.Socket); HandleDownload(TCPSock);
} else { } else {
CloseSocketProper(client.Socket); CloseSocketProper(TCPSock);
} }
} }
@@ -205,21 +247,17 @@ void TNetwork::HandleDownload(SOCKET TCPSock) {
}); });
} }
void TNetwork::Authentication(const TConnection& ClientConnection) { void TNetwork::Authentication(SOCKET TCPSock) {
auto Client = CreateClient(ClientConnection.Socket); auto Client = CreateClient(TCPSock);
char AddrBuf[30];
inet_ntoa(reinterpret_cast<const struct sockaddr_in*>(&ClientConnection.SockAddr)->sin_addr);
auto str = inet_ntop(AF_INET, static_cast<const void*>(reinterpret_cast<const struct sockaddr_in*>(&ClientConnection.SockAddr)), AddrBuf, sizeof(struct sockaddr_in));
Client->SetIdentifier("ip", str);
std::string Rc; std::string Rc;
info("Identifying new ClientConnection..."); info("Identifying new client...");
Rc = TCPRcv(*Client); Rc = TCPRcv(*Client);
if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") { if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") {
Rc = Rc.substr(2); Rc = Rc.substr(2);
if (Rc.length() > 4 || Rc != Application::ClientVersionString()) { if (Rc.length() > 4 || Rc != Application::ClientVersion()) {
ClientKick(*Client, "Outdated Version!"); ClientKick(*Client, "Outdated Version!");
return; return;
} }
@@ -241,14 +279,14 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
auto RequestString = R"({"key":")" + Rc + "\"}"; auto RequestString = R"({"key":")" + Rc + "\"}";
auto Target = "/pkToUser"; auto Target = "/pkToUser";
unsigned int ResponseCode = 0; int ResponseCode = -1;
if (!Rc.empty()) { if (!Rc.empty()) {
Rc = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, {}, RequestString, "application/json", &ResponseCode); Rc = Http::POST(Application::GetBackendUrlForAuth(), Target, {}, RequestString, true, &ResponseCode);
} }
json::Document AuthResponse; json::Document AuthResponse;
AuthResponse.Parse(Rc.c_str()); AuthResponse.Parse(Rc.c_str());
if (Rc == Http::ErrorString || AuthResponse.HasParseError()) { if (Rc == "-1" || AuthResponse.HasParseError()) {
ClientKick(*Client, "Invalid key! Please restart your game."); ClientKick(*Client, "Invalid key! Please restart your game.");
return; return;
} }
@@ -281,9 +319,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
Client->SetRoles(AuthResponse["roles"].GetString()); Client->SetRoles(AuthResponse["roles"].GetString());
Client->SetIsGuest(AuthResponse["guest"].GetBool()); Client->SetIsGuest(AuthResponse["guest"].GetBool());
for (const auto& ID : AuthResponse["identifiers"].GetArray()) { for (const auto& ID : AuthResponse["identifiers"].GetArray()) {
auto Raw = std::string(ID.GetString()); Client->AddIdentifier(ID.GetString());
auto SepIndex = Raw.find(':');
Client->SetIdentifier(Raw.substr(0, SepIndex), Raw.substr(SepIndex + 1));
} }
} else { } else {
ClientKick(*Client, "Invalid authentication data!"); ClientKick(*Client, "Invalid authentication data!");
@@ -359,12 +395,12 @@ bool TNetwork::TCPSend(TClient& c, const std::string& Data, bool IsSync) {
int32_t Temp = send(c.GetTCPSock(), &Send[Sent], Size - Sent, MSG_NOSIGNAL); int32_t Temp = send(c.GetTCPSock(), &Send[Sent], Size - Sent, MSG_NOSIGNAL);
#endif //WIN32 #endif //WIN32
if (Temp == 0) { if (Temp == 0) {
debug("send() == 0: " + GetPlatformAgnosticErrorString()); debug("send() == 0: " + std::string(std::strerror(errno)));
if (c.GetStatus() > -1) if (c.GetStatus() > -1)
c.SetStatus(-1); c.SetStatus(-1);
return false; return false;
} else if (Temp < 0) { } else if (Temp < 0) {
debug("send() < 0: " + GetPlatformAgnosticErrorString()); //TODO fix it was spamming yet everyone stayed on the server debug("send() < 0: " + std::string(std::strerror(errno))); //TODO fix it was spamming yet everyone stayed on the server
if (c.GetStatus() > -1) if (c.GetStatus() > -1)
c.SetStatus(-1); c.SetStatus(-1);
CloseSocketProper(c.GetTCPSock()); CloseSocketProper(c.GetTCPSock());
@@ -383,7 +419,11 @@ bool TNetwork::CheckBytes(TClient& c, int32_t BytesRcv) {
c.SetStatus(-1); c.SetStatus(-1);
return false; return false;
} else if (BytesRcv < 0) { } else if (BytesRcv < 0) {
debug("(TCP) recv() failed: " + GetPlatformAgnosticErrorString()); #ifdef WIN32
debug(("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError()));
#else // unix
debug(("(TCP) recv failed with error: ") + std::string(strerror(errno)));
#endif // WIN32
if (c.GetStatus() > -1) if (c.GetStatus() > -1)
c.SetStatus(-1); c.SetStatus(-1);
CloseSocketProper(c.GetTCPSock()); CloseSocketProper(c.GetTCPSock());
@@ -902,17 +942,31 @@ bool TNetwork::UDPSend(TClient& Client, std::string Data) const {
#endif // WIN32 #endif // WIN32
sendOk = sendto(mUDPSock, Data.c_str(), len, 0, (sockaddr*)&Addr, int(AddrSize)); sendOk = sendto(mUDPSock, Data.c_str(), len, 0, (sockaddr*)&Addr, int(AddrSize));
#ifdef WIN32
if (sendOk == -1) { if (sendOk == -1) {
debug("(UDP) sendto() failed: " + GetPlatformAgnosticErrorString()); debug(("(UDP) Send Failed Code : ") + std::to_string(WSAGetLastError()));
if (Client.GetStatus() > -1) if (Client.GetStatus() > -1)
Client.SetStatus(-1); Client.SetStatus(-1);
return false; return false;
} else if (sendOk == 0) { } else if (sendOk == 0) {
debug(("(UDP) sendto() returned 0")); debug(("(UDP) sendto returned 0"));
if (Client.GetStatus() > -1) if (Client.GetStatus() > -1)
Client.SetStatus(-1); Client.SetStatus(-1);
return false; return false;
} }
#else // unix
if (sendOk == -1) {
debug(("(UDP) Send Failed Code : ") + std::string(strerror(errno)));
if (Client.GetStatus() > -1)
Client.SetStatus(-1);
return false;
} else if (sendOk == 0) {
debug(("(UDP) sendto returned 0"));
if (Client.GetStatus() > -1)
Client.SetStatus(-1);
return false;
}
#endif // WIN32
return true; return true;
} }
@@ -926,7 +980,11 @@ std::string TNetwork::UDPRcvFromClient(sockaddr_in& client) const {
#endif // WIN32 #endif // WIN32
if (Rcv == -1) { if (Rcv == -1) {
error("(UDP) Error receiving from client! recvfrom() failed: " + GetPlatformAgnosticErrorString()); #ifdef WIN32
error(("(UDP) Error receiving from Client! Code : ") + std::to_string(WSAGetLastError()));
#else // unix
error(("(UDP) Error receiving from Client! Code : ") + std::string(strerror(errno)));
#endif // WIN32
return ""; return "";
} }
return std::string(Ret.begin(), Ret.begin() + Rcv); return std::string(Ret.begin(), Ret.begin() + Rcv);
-2
View File
@@ -7,10 +7,8 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
Application::SetPPS("-"); Application::SetPPS("-");
Application::RegisterShutdownHandler([&] { Application::RegisterShutdownHandler([&] {
if (mThread.joinable()) { if (mThread.joinable()) {
debug("shutting down PPSMonitor");
mShutdown = true; mShutdown = true;
mThread.join(); mThread.join();
debug("shut down PPSMonitor");
} }
}); });
Start(); Start();
+24 -11
View File
@@ -1,24 +1,20 @@
#include "TSentry.h" #include "TSentry.h"
#include "Common.h" #include "Common.h"
#include <cstring>
#include <sentry.h> #include <sentry.h>
#include <sstream> #include <sstream>
// compile-time length of a string/array
template <size_t N>
constexpr size_t ConstexprLength(char const (&)[N]) {
return N - 1;
}
TSentry::TSentry() { TSentry::TSentry() {
if constexpr (ConstexprLength(SECRET_SENTRY_URL) == 0) { if (std::strlen(S_DSN) == 0) {
mValid = false; mValid = false;
} else { } else {
mValid = true; mValid = true;
sentry_options_t* options = sentry_options_new(); sentry_options_t* options = sentry_options_new();
sentry_options_set_dsn(options, SECRET_SENTRY_URL); sentry_options_set_dsn(options, S_DSN);
auto ReleaseString = "BeamMP-Server@" + Application::ServerVersionString(); sentry_options_set_debug(options, false); // needs to always be false
sentry_options_set_symbolize_stacktraces(options, true); sentry_options_set_symbolize_stacktraces(options, true);
auto ReleaseString = "BeamMP-Server@" + Application::ServerVersion();
sentry_options_set_release(options, ReleaseString.c_str()); sentry_options_set_release(options, ReleaseString.c_str());
sentry_options_set_max_breadcrumbs(options, 10); sentry_options_set_max_breadcrumbs(options, 10);
sentry_init(options); sentry_init(options);
@@ -33,9 +29,26 @@ TSentry::~TSentry() {
void TSentry::PrintWelcome() { void TSentry::PrintWelcome() {
if (mValid) { if (mValid) {
info("Sentry started"); if (!Application::Settings.SendErrors) {
mValid = false;
if (Application::Settings.SendErrorsMessageEnabled) {
info("Opted out of error reporting (SendErrors), Sentry disabled.");
} else {
info("Sentry disabled");
}
} else {
if (Application::Settings.SendErrorsMessageEnabled) {
info("Sentry started! Reporting errors automatically. This sends data to the developers in case of errors and crashes. You can learn more, turn this message off or opt-out of this in the ServerConfig.toml.");
} else {
info("Sentry started");
}
}
} else { } else {
info("Sentry disabled in unofficial build"); if (Application::Settings.SendErrorsMessageEnabled) {
info("Sentry disabled in unofficial build. Automatic error reporting disabled.");
} else {
info("Sentry disabled in unofficial build");
}
} }
} }
+1 -1
View File
@@ -14,7 +14,7 @@
namespace json = rapidjson; namespace json = rapidjson;
TServer::TServer(int argc, char** argv) { TServer::TServer(int argc, char** argv) {
info("BeamMP Server v" + Application::ServerVersionString()); info("BeamMP Server v" + Application::ServerVersion());
if (argc > 1) { if (argc > 1) {
Application::Settings.CustomIP = argv[1]; Application::Settings.CustomIP = argv[1];
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.'); size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');
+11 -37
View File
@@ -3,6 +3,7 @@
#include "Common.h" #include "Common.h"
#include "CustomAssert.h" #include "CustomAssert.h"
#include "Http.h" #include "Http.h"
#include "SignalHandling.h"
#include "TConfig.h" #include "TConfig.h"
#include "THeartbeatThread.h" #include "THeartbeatThread.h"
#include "TLuaEngine.h" #include "TLuaEngine.h"
@@ -11,58 +12,30 @@
#include "TResourceManager.h" #include "TResourceManager.h"
#include "TServer.h" #include "TServer.h"
#include <iostream>
#include <thread> #include <thread>
#ifdef __unix
#include <csignal>
void UnixSignalHandler(int sig) {
switch (sig) {
case SIGPIPE:
warn("ignoring SIGPIPE");
break;
case SIGTERM:
info("gracefully shutting down via SIGTERM");
Application::GracefullyShutdown();
break;
case SIGINT:
info("gracefully shutting down via SIGINT");
Application::GracefullyShutdown();
break;
default:
debug("unhandled signal: " + std::to_string(sig));
break;
}
}
#endif // __unix
// this is provided by the build system, leave empty for source builds // this is provided by the build system, leave empty for source builds
// global, yes, this is ugly, no, it cant be done another way // global, yes, this is ugly, no, it cant be done another way
TSentry Sentry {}; TSentry Sentry {};
#include <iostream>
int main(int argc, char** argv) try { int main(int argc, char** argv) try {
#ifdef __unix
trace("registering handlers for SIGINT, SIGTERM, SIGPIPE");
signal(SIGPIPE, UnixSignalHandler);
signal(SIGTERM, UnixSignalHandler);
#ifndef DEBUG
signal(SIGINT, UnixSignalHandler);
#endif // DEBUG
#endif // __unix
setlocale(LC_ALL, "C"); setlocale(LC_ALL, "C");
SetupSignalHandlers();
bool Shutdown = false; bool Shutdown = false;
Application::RegisterShutdownHandler([&Shutdown] { Shutdown = true; }); Application::RegisterShutdownHandler([&Shutdown] { Shutdown = true; });
Application::RegisterShutdownHandler([] { TriggerLuaEvent("onShutdown", false, nullptr, {}, true); });
TServer Server(argc, argv); TServer Server(argc, argv);
TConfig Config; TConfig Config;
if (Config.Failed()) { if (Config.Failed()) {
info("Closing in 10 seconds"); info("Closing in 10 seconds");
std::this_thread::sleep_for(std::chrono::seconds(10)); // loop to make it possible to ctrl+c instead
for (size_t i = 0; i < 20; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
return 1; return 1;
} }
@@ -72,7 +45,6 @@ int main(int argc, char** argv) try {
Sentry.SetupUser(); Sentry.SetupUser();
Sentry.PrintWelcome(); Sentry.PrintWelcome();
Application::CheckForUpdates();
TResourceManager ResourceManager; TResourceManager ResourceManager;
TPPSMonitor PPSMonitor(Server); TPPSMonitor PPSMonitor(Server);
THeartbeatThread Heartbeat(ResourceManager, Server); THeartbeatThread Heartbeat(ResourceManager, Server);
@@ -80,11 +52,13 @@ int main(int argc, char** argv) try {
TLuaEngine LuaEngine(Server, Network); TLuaEngine LuaEngine(Server, Network);
PPSMonitor.SetNetwork(Network); PPSMonitor.SetNetwork(Network);
Application::Console().InitializeLuaConsole(LuaEngine); Application::Console().InitializeLuaConsole(LuaEngine);
Application::CheckForUpdates();
// TODO: replace // TODO: replace
while (!Shutdown) { while (!Shutdown) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(50));
} }
info("Shutdown.");
} catch (const std::exception& e) { } catch (const std::exception& e) {
error(e.what()); error(e.what());
Sentry.LogException(e, _file_basename, _line); Sentry.LogException(e, _file_basename, _line);