Http: Add config value to turn it off, move all http settings into a category in the config

This commit is contained in:
Lion Kortlepel 2021-12-06 09:53:13 +01:00
parent bd41382233
commit 62cc1e9ce4
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
5 changed files with 46 additions and 41 deletions

View File

@ -42,6 +42,7 @@ public:
std::string Key {}; std::string Key {};
std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" }; std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" };
std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" }; std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" };
bool HTTPServerEnabled { true };
int MaxPlayers { 10 }; int MaxPlayers { 10 };
bool Private { true }; bool Private { true };
int MaxCars { 1 }; int MaxCars { 1 };
@ -70,7 +71,7 @@ public:
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; }
static inline TSettings Settings {}; static TSettings Settings;
static std::string GetBackendUrlForAuth() { return "auth.beammp.com"; } static std::string GetBackendUrlForAuth() { return "auth.beammp.com"; }
static std::string GetBackendHostname() { return "backend.beammp.com"; } static std::string GetBackendHostname() { return "backend.beammp.com"; }

View File

@ -12,6 +12,7 @@
#include "CustomAssert.h" #include "CustomAssert.h"
#include "Http.h" #include "Http.h"
Application::TSettings Application::Settings = {};
std::unique_ptr<TConsole> Application::mConsole = std::make_unique<TConsole>(); std::unique_ptr<TConsole> Application::mConsole = std::make_unique<TConsole>();
void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) { void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {

View File

@ -9,6 +9,7 @@
#include <istream> #include <istream>
#include <sstream> #include <sstream>
// General
static constexpr std::string_view StrDebug = "Debug"; static constexpr std::string_view StrDebug = "Debug";
static constexpr std::string_view StrPrivate = "Private"; static constexpr std::string_view StrPrivate = "Private";
static constexpr std::string_view StrPort = "Port"; static constexpr std::string_view StrPort = "Port";
@ -21,19 +22,13 @@ 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 StrSendErrors = "SendErrors";
static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage"; static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
static constexpr std::string_view StrHTTPServerEnabled = "HTTPServerEnabled";
// HTTP
static constexpr std::string_view StrSSLKeyPath = "SSLKeyPath"; static constexpr std::string_view StrSSLKeyPath = "SSLKeyPath";
static constexpr std::string_view StrSSLCertPath = "SSLCertPath"; static constexpr std::string_view StrSSLCertPath = "SSLCertPath";
static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort"; static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
void WriteSendErrors(const std::string& name) {
std::ofstream CfgFile { name, std::ios::out | std::ios::app };
CfgFile << "# You can turn on/off the SendErrors message you get on startup here" << std::endl
<< StrSendErrorsMessageEnabled << " = true" << std::endl
<< "# 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`."
<< std::endl
<< StrSendErrors << " = true" << std::endl;
}
TConfig::TConfig(const std::string& ConfigFileName) TConfig::TConfig(const std::string& ConfigFileName)
: mConfigFileName(ConfigFileName) { : mConfigFileName(ConfigFileName) {
if (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName)) { if (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName)) {
@ -57,9 +52,11 @@ TConfig::TConfig(const std::string& ConfigFileName)
* whether it is in TConfig.cpp or the configuration file. * whether it is in TConfig.cpp or the configuration file.
*/ */
void TConfig::FlushToFile() { void TConfig::FlushToFile() {
auto data = toml::parse(mConfigFileName); auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
data["General"] = toml::table(); data["General"] = toml::table();
data["General"].comments().push_back(" General BeamMP Server settings");
data["General"][StrAuthKey.data()] = Application::Settings.Key; data["General"][StrAuthKey.data()] = Application::Settings.Key;
data["General"][StrAuthKey.data()].comments().push_back(" AuthKey has to be filled out in order to run the server");
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled; data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
data["General"][StrPrivate.data()] = Application::Settings.Private; data["General"][StrPrivate.data()] = Application::Settings.Private;
data["General"][StrPort.data()] = Application::Settings.Port; data["General"][StrPort.data()] = Application::Settings.Port;
@ -70,10 +67,14 @@ void TConfig::FlushToFile() {
data["General"][StrDescription.data()] = Application::Settings.ServerDesc; data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
data["General"][StrResourceFolder.data()] = Application::Settings.Resource; data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
data["General"][StrSendErrors.data()] = Application::Settings.SendErrors; data["General"][StrSendErrors.data()] = Application::Settings.SendErrors;
data["General"][StrSendErrors.data()].comments().push_back(" You can turn on/off the SendErrors message you get on startup here");
data["General"][StrSendErrorsMessageEnabled.data()] = Application::Settings.SendErrorsMessageEnabled; data["General"][StrSendErrorsMessageEnabled.data()] = Application::Settings.SendErrorsMessageEnabled;
data["General"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath; data["General"][StrSendErrorsMessageEnabled.data()].comments().push_back(" 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"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath; data["HTTP"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath;
data["General"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort; data["HTTP"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath;
data["HTTP"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort;
data["HTTP"][StrHTTPServerEnabled.data()] = Application::Settings.HTTPServerEnabled;
data["HTTP"][StrHTTPServerEnabled.data()].comments().push_back(" Enables the internal HTTP server");
std::ofstream Stream(mConfigFileName); std::ofstream Stream(mConfigFileName);
Stream << data << std::flush; Stream << data << std::flush;
} }
@ -94,35 +95,23 @@ void TConfig::CreateConfigFile(std::string_view name) {
std::ofstream ofs(name.data()); std::ofstream ofs(name.data());
} }
auto data = toml::parse<toml::preserve_comments>(name.data()); FlushToFile();
//{ StrSendErrors, Application::Settings.SendErrors },
data["General"] = toml::table(); size_t FileSize = fs::file_size(name);
data["General"][StrAuthKey.data()] = Application::Settings.Key; std::fstream ofs { std::string(name), std::ios::in | std::ios::out };
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
data["General"][StrPrivate.data()] = Application::Settings.Private;
data["General"][StrPort.data()] = Application::Settings.Port;
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;
data["General"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath;
data["General"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath;
data["General"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort;
std::ofstream ofs { std::string(name) };
if (ofs.good()) { if (ofs.good()) {
std::string Contents {};
Contents.resize(FileSize);
ofs.readsome(Contents.data(), FileSize);
ofs.seekp(0);
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'; << Contents;
beammp_error("There was no \"" + std::string(mConfigFileName) + "\" 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."); beammp_error("There was no \"" + std::string(mConfigFileName) + "\" 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(); ofs.close();
WriteSendErrors(std::string(name));
} else { } else {
beammp_error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work."); beammp_error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work.");
mFailed = true; mFailed = true;
@ -130,6 +119,7 @@ void TConfig::CreateConfigFile(std::string_view name) {
} }
void TConfig::ParseFromFile(std::string_view name) { void TConfig::ParseFromFile(std::string_view name) {
bool UpdateConfigFile = false;
try { try {
toml::value data = toml::parse<toml::preserve_comments>(name.data()); toml::value data = toml::parse<toml::preserve_comments>(name.data());
Application::Settings.DebugModeEnabled = data["General"][StrDebug.data()].as_boolean(); Application::Settings.DebugModeEnabled = data["General"][StrDebug.data()].as_boolean();
@ -142,12 +132,17 @@ void TConfig::ParseFromFile(std::string_view name) {
Application::Settings.ServerDesc = data["General"][StrDescription.data()].as_string(); Application::Settings.ServerDesc = data["General"][StrDescription.data()].as_string();
Application::Settings.Resource = data["General"][StrResourceFolder.data()].as_string(); Application::Settings.Resource = data["General"][StrResourceFolder.data()].as_string();
Application::Settings.Key = data["General"][StrAuthKey.data()].as_string(); Application::Settings.Key = data["General"][StrAuthKey.data()].as_string();
Application::Settings.SSLKeyPath = data["General"][StrSSLKeyPath.data()].as_string(); if (!data["HTTP"][StrSSLKeyPath.data()].is_string()) {
Application::Settings.SSLCertPath = data["General"][StrSSLCertPath.data()].as_string(); UpdateConfigFile = true;
Application::Settings.HTTPServerPort = data["General"][StrHTTPServerPort.data()].as_integer(); } else {
Application::Settings.SSLKeyPath = data["HTTP"][StrSSLKeyPath.data()].as_string();
Application::Settings.SSLCertPath = data["HTTP"][StrSSLCertPath.data()].as_string();
Application::Settings.HTTPServerPort = data["HTTP"][StrHTTPServerPort.data()].as_integer();
Application::Settings.HTTPServerEnabled = data["HTTP"][StrHTTPServerEnabled.data()].as_boolean();
}
if (!data["General"][StrSendErrors.data()].is_boolean() if (!data["General"][StrSendErrors.data()].is_boolean()
|| !data["General"][StrSendErrorsMessageEnabled.data()].is_boolean()) { || !data["General"][StrSendErrorsMessageEnabled.data()].is_boolean()) {
WriteSendErrors(std::string(name)); UpdateConfigFile = true;
} else { } else {
Application::Settings.SendErrors = data["General"][StrSendErrors.data()].as_boolean(); Application::Settings.SendErrors = data["General"][StrSendErrors.data()].as_boolean();
Application::Settings.SendErrorsMessageEnabled = data["General"][StrSendErrorsMessageEnabled.data()].as_boolean(); Application::Settings.SendErrorsMessageEnabled = data["General"][StrSendErrorsMessageEnabled.data()].as_boolean();
@ -158,6 +153,10 @@ void TConfig::ParseFromFile(std::string_view name) {
return; return;
} }
PrintDebug(); PrintDebug();
if (UpdateConfigFile) {
FlushToFile();
}
// all good so far, let's check if there's a key // all good so far, let's check if there's a key
if (Application::Settings.Key.empty()) { if (Application::Settings.Key.empty()) {
beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server."); beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server.");
@ -177,6 +176,7 @@ void TConfig::PrintDebug() {
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\""); beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\"");
beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\""); beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\"");
beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\""); beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\"");
beammp_debug(std::string(StrHTTPServerPort) + ": \"" + std::to_string(Application::Settings.HTTPServerPort) + "\"");
// special! // special!
beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + ""); beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + "");
} }

View File

@ -159,6 +159,7 @@ void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, c
} }
if (Cancelled) { if (Cancelled) {
beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' failed to execute in time and was not waited for. It may still finish executing at a later time."); beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' failed to execute in time and was not waited for. It may still finish executing at a later time.");
LuaAPI::MP::Engine->ReportErrors({ Result });
} else if (Result->Error) { } else if (Result->Error) {
if (Result->ErrorMessage != BeamMPFnNotFoundError) { if (Result->ErrorMessage != BeamMPFnNotFoundError) {
beammp_lua_error(Result->Function + ": " + Result->ErrorMessage); beammp_lua_error(Result->Function + ": " + Result->ErrorMessage);

View File

@ -155,8 +155,10 @@ int BeamMPServerMain(MainArguments Arguments) {
Application::Console().InitializeLuaConsole(LuaEngine); Application::Console().InitializeLuaConsole(LuaEngine);
Application::CheckForUpdates(); Application::CheckForUpdates();
if (Application::Settings.HTTPServerEnabled) {
Http::Server::SetupEnvironment(); Http::Server::SetupEnvironment();
Http::Server::THttpServerInstance HttpServerInstance {}; Http::Server::THttpServerInstance HttpServerInstance {};
}
beammp_debug("cert.pem is " + std::to_string(fs::file_size("cert.pem")) + " bytes"); beammp_debug("cert.pem is " + std::to_string(fs::file_size("cert.pem")) + " bytes");
beammp_debug("key.pem is " + std::to_string(fs::file_size("key.pem")) + " bytes"); beammp_debug("key.pem is " + std::to_string(fs::file_size("key.pem")) + " bytes");