From 974dda9f8b6550cc961066f2392715075d873cfc Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 28 Apr 2022 14:12:26 +0200 Subject: [PATCH] HTTPServer: Add config value to specify listen IP Change default IP to localhost, Set default SSL to false due to this. --- include/Common.h | 3 ++- src/Http.cpp | 2 +- src/TConfig.cpp | 7 ++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/Common.h b/include/Common.h index 8861a77..39714d0 100644 --- a/include/Common.h +++ b/include/Common.h @@ -53,7 +53,8 @@ public: bool SendErrors { true }; bool SendErrorsMessageEnabled { true }; int HTTPServerPort { 8080 }; - bool HTTPServerUseSSL { true }; + std::string HTTPServerIP { "127.0.0.1" }; + bool HTTPServerUseSSL { false }; bool HideUpdateMessages { false }; [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } }; diff --git a/src/Http.cpp b/src/Http.cpp index 9a3b9b6..aeb66d5 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -374,7 +374,7 @@ void Http::Server::THttpServerInstance::operator()() try { beammp_debug("Http Server: " + Req.method + " " + Req.target + " -> " + std::to_string(Res.status)); }); Application::SetSubsystemStatus("HTTPServer", Application::Status::Good); - auto ret = HttpLibServerInstance->listen("0.0.0.0", Application::Settings.HTTPServerPort); + auto ret = HttpLibServerInstance->listen(Application::Settings.HTTPServerIP.c_str(), Application::Settings.HTTPServerPort); if (!ret) { beammp_error("Failed to start http server (failed to listen). Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it."); } diff --git a/src/TConfig.cpp b/src/TConfig.cpp index 1260cb7..f21954e 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -29,6 +29,7 @@ static constexpr std::string_view StrHTTPServerUseSSL = "UseSSL"; static constexpr std::string_view StrSSLKeyPath = "SSLKeyPath"; static constexpr std::string_view StrSSLCertPath = "SSLCertPath"; static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort"; +static constexpr std::string_view StrHTTPServerIP = "HTTPServerIP"; TConfig::TConfig(const std::string& ConfigFileName) : mConfigFileName(ConfigFileName) { @@ -84,8 +85,10 @@ void TConfig::FlushToFile() { data["HTTP"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath; data["HTTP"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath; data["HTTP"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort; + SetComment(data["HTTP"][StrHTTPServerIP.data()].comments(), " Which IP to listen on. Pick 0.0.0.0 for a public-facing server with no specific IP, and 127.0.0.1 or 'localhost' for a local server."); + data["HTTP"][StrHTTPServerIP.data()] = Application::Settings.HTTPServerIP; data["HTTP"][StrHTTPServerUseSSL.data()] = Application::Settings.HTTPServerUseSSL; - SetComment(data["HTTP"][StrHTTPServerUseSSL.data()].comments(), " Recommended to keep enabled. With SSL the server will serve https and requires valid key and cert files"); + SetComment(data["HTTP"][StrHTTPServerUseSSL.data()].comments(), " Recommended to have enabled for servers which face the internet. With SSL the server will serve https and requires valid key and cert files"); data["HTTP"][StrHTTPServerEnabled.data()] = Application::Settings.HTTPServerEnabled; SetComment(data["HTTP"][StrHTTPServerEnabled.data()].comments(), " Enables the internal HTTP server"); std::ofstream Stream(mConfigFileName, std::ios::trunc | std::ios::out); @@ -172,6 +175,7 @@ void TConfig::ParseFromFile(std::string_view name) { TryReadValue(data, "HTTP", StrSSLKeyPath, Application::Settings.SSLKeyPath); TryReadValue(data, "HTTP", StrSSLCertPath, Application::Settings.SSLCertPath); TryReadValue(data, "HTTP", StrHTTPServerPort, Application::Settings.HTTPServerPort); + TryReadValue(data, "HTTP", StrHTTPServerIP, Application::Settings.HTTPServerIP); TryReadValue(data, "HTTP", StrHTTPServerEnabled, Application::Settings.HTTPServerEnabled); TryReadValue(data, "HTTP", StrHTTPServerUseSSL, Application::Settings.HTTPServerUseSSL); } catch (const std::exception& err) { @@ -210,6 +214,7 @@ void TConfig::PrintDebug() { beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\""); beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\""); beammp_debug(std::string(StrHTTPServerPort) + ": \"" + std::to_string(Application::Settings.HTTPServerPort) + "\""); + beammp_debug(std::string(StrHTTPServerIP) + ": \"" + Application::Settings.HTTPServerIP + "\""); // special! beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + ""); }