From ffac000cd23fe26d920e88700e359c0c02e1649c Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 9 Sep 2021 17:22:03 +0300 Subject: [PATCH] Config: Add basic opt-out for Sentry --- Changelog.md | 2 +- include/Common.h | 4 +++- src/TConfig.cpp | 18 ++++++++++++++++++ src/TSentry.cpp | 9 +++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index 221f19e..8fbb87e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,7 +3,7 @@ - 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 # v2.3.1 diff --git a/include/Common.h b/include/Common.h index 64e0585..3897bbb 100644 --- a/include/Common.h +++ b/include/Common.h @@ -30,7 +30,8 @@ public: , Private(false) , MaxCars(1) , DebugModeEnabled(false) - , Port(30814) { } + , Port(30814) + , SendErrors(true) { } std::string ServerName; std::string ServerDesc; std::string Resource; @@ -42,6 +43,7 @@ public: bool DebugModeEnabled; int Port; std::string CustomIP; + bool SendErrors; [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } }; using TShutdownHandler = std::function; diff --git a/src/TConfig.cpp b/src/TConfig.cpp index d2a0696..5132a66 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -18,6 +18,7 @@ static constexpr std::string_view StrName = "Name"; static constexpr std::string_view StrDescription = "Description"; static constexpr std::string_view StrResourceFolder = "ResourceFolder"; static constexpr std::string_view StrAuthKey = "AuthKey"; +static constexpr std::string_view StrSendErrors = "SendErrors"; TConfig::TConfig() { if (!fs::exists(ConfigFileName) || !fs::is_regular_file(ConfigFileName)) { @@ -32,6 +33,13 @@ TConfig::TConfig() { } } +void WriteSendErrors(const std::string& name) { + std::ofstream CfgFile { name, std::ios::out | std::ios::app }; + CfgFile << "# 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 data. You can opt-out of this system by setting this to `false`." + << std::endl + << StrSendErrors << " = true" << std::endl; +} + void TConfig::CreateConfigFile(std::string_view name) { // build from old config Server.cfg @@ -59,6 +67,7 @@ void TConfig::CreateConfigFile(std::string_view name) { { StrDescription, Application::Settings.ServerDesc }, { StrResourceFolder, Application::Settings.Resource }, { StrAuthKey, Application::Settings.Key }, + //{ StrSendErrors, Application::Settings.SendErrors }, } } }, @@ -72,6 +81,8 @@ void TConfig::CreateConfigFile(std::string_view name) { 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."); mFailed = true; + ofs.close(); + WriteSendErrors(std::string(name)); } else { error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work."); mFailed = true; @@ -132,6 +143,13 @@ void TConfig::ParseFromFile(std::string_view name) { } else { throw std::runtime_error(std::string(StrAuthKey)); } + // added later, so behaves differently + if (auto val = GeneralTable[StrSendErrors].value(); val.has_value()) { + Application::Settings.SendErrors = val.value(); + } else { + // dont throw, instead write it into the file and use default + WriteSendErrors(std::string(name)); + } } catch (const std::exception& err) { error("Error parsing config file value: " + std::string(err.what())); mFailed = true; diff --git a/src/TSentry.cpp b/src/TSentry.cpp index fd8069f..d09df92 100644 --- a/src/TSentry.cpp +++ b/src/TSentry.cpp @@ -34,9 +34,14 @@ TSentry::~TSentry() { void TSentry::PrintWelcome() { if (mValid) { - info("Sentry started"); + if (!Application::Settings.SendErrors) { + mValid = false; + info("Opted out of error reporting (SendErrors), Sentry disabled."); + } else { + info("Sentry started! Reporting errors automatically. You can opt-out of this in the ServerConfig."); + } } else { - info("Sentry disabled in unofficial build"); + info("Sentry disabled in unofficial build. Automatic error reporting disabled."); } }