From 67f5759e4702a8e2fb0f7707269d99379ba33327 Mon Sep 17 00:00:00 2001 From: Chris Miles Date: Wed, 6 Jul 2022 22:01:58 -0500 Subject: [PATCH] [Server] Configuration Issues Checker (LAN Detection) (#2283) * LAN detect * Add more checks, consolidate logic * Tweaks * Tweaks * Update world_config.cpp * Tweak logic * Add DNS resolution * Delete task runner after being used * JSON path notation adjust --- common/eqemu_config.cpp | 4 + common/eqemu_config.h | 5 +- common/ip_util.cpp | 161 +++++++++++++++++++++++++++++ common/ip_util.h | 9 +- world/main.cpp | 3 + world/world_config.cpp | 220 ++++++++++++++++++++++++++++++++++++++-- world/world_config.h | 2 + 7 files changed, 395 insertions(+), 9 deletions(-) diff --git a/common/eqemu_config.cpp b/common/eqemu_config.cpp index 1790e7788..85161ba8a 100644 --- a/common/eqemu_config.cpp +++ b/common/eqemu_config.cpp @@ -100,6 +100,10 @@ void EQEmuConfig::parse_config() WorldHTTPEnabled = true; } + if (_root["server"].get("disable_config_checks", "false").asString() == "true") { + DisableConfigChecks = true; + } + /** * UCS */ diff --git a/common/eqemu_config.h b/common/eqemu_config.h index 2f36a6317..215586fd0 100644 --- a/common/eqemu_config.h +++ b/common/eqemu_config.h @@ -58,6 +58,7 @@ class EQEmuConfig uint16 WorldHTTPPort; std::string WorldHTTPMimeFile; std::string SharedKey; + bool DisableConfigChecks; // From std::string ChatHost; @@ -130,7 +131,7 @@ class EQEmuConfig void parse_config(); EQEmuConfig() - { + { } virtual ~EQEmuConfig() {} @@ -174,7 +175,7 @@ class EQEmuConfig } catch (std::exception &) { return false; - } + } return true; } diff --git a/common/ip_util.cpp b/common/ip_util.cpp index e1f5d0794..59e9f7d60 100644 --- a/common/ip_util.cpp +++ b/common/ip_util.cpp @@ -18,7 +18,23 @@ * */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "ip_util.h" +#include "http/httplib.h" +#include "http/uri.h" +#include "eqemu_logsys.h" +#include "event/event_loop.h" +#include "net/dns.h" +#include "event/task_scheduler.h" /** * @param ip @@ -70,3 +86,148 @@ bool IpUtil::IsIpInPrivateRfc1918(const std::string &ip) IpUtil::IsIpInRange(ip, "192.168.0.0", "255.255.0.0") ); } + +/** + * Gets local address - pings google to inspect what interface was used locally + * @return + */ +std::string IpUtil::GetLocalIPAddress() +{ + char my_ip_address[16]; + unsigned int my_port; + struct sockaddr_in server_address{}; + struct sockaddr_in my_address{}; + int sockfd; + + // Connect to server + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + return ""; + } + + // Set server_addr + bzero(&server_address, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = inet_addr("172.217.160.99"); + server_address.sin_port = htons(80); + + // Connect to server + if (connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) { + close(sockfd); + return ""; + } + + // Get my ip address and port + bzero(&my_address, sizeof(my_address)); + socklen_t len = sizeof(my_address); + getsockname(sockfd, (struct sockaddr *) &my_address, &len); + inet_ntop(AF_INET, &my_address.sin_addr, my_ip_address, sizeof(my_ip_address)); + my_port = ntohs(my_address.sin_port); + + return fmt::format("{}", my_ip_address); +} + +/** + * Gets public address + * Uses various websites as options to return raw public IP back to the client + * @return + */ +std::string IpUtil::GetPublicIPAddress() +{ + std::vector endpoints = { + "http://ifconfig.me", + "http://api.ipify.org", + "http://ipinfo.io/ip", + "http://ipecho.net/plain", + }; + + for (auto &s: endpoints) { + // http get request + uri u(s); + + httplib::Client r( + fmt::format( + "{}://{}", + u.get_scheme(), + u.get_host() + ).c_str() + ); + + httplib::Headers headers = { + {"Content-type", "text/plain; charset=utf-8"}, + {"User-Agent", "curl/7.81.0"} + }; + + r.set_connection_timeout(1, 0); + r.set_read_timeout(1, 0); + r.set_write_timeout(1, 0); + + if (auto res = r.Get(fmt::format("/{}", u.get_path()).c_str(), headers)) { + if (res->status == 200) { + if (res->body.find('.') != std::string::npos) { + return res->body; + } + } + } + } + + return {}; +} + +std::string IpUtil::DNSLookupSync(const std::string &addr, int port) +{ + auto task_runner = new EQ::Event::TaskScheduler(); + auto res = task_runner->Enqueue( + [&]() -> std::string { + bool running = true; + std::string ret; + + EQ::Net::DNSLookup( + addr, port, false, [&](const std::string &addr) { + ret = addr; + if (addr.empty()) { + ret = ""; + running = false; + } + + return ret; + } + ); + + std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); + + auto &loop = EQ::EventLoop::Get(); + while (running) { + if (!ret.empty()) { + running = false; + } + + std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); + if (std::chrono::duration_cast(end - begin).count() > 1500) { + LogInfo( + "[DNSLookupSync] Deadline exceeded [{}]", + 1500 + ); + running = false; + } + + loop.Process(); + } + + return ret; + } + ); + + std::string result = res.get(); + safe_delete(task_runner); + + return result; +} + +bool IpUtil::IsIPAddress(const std::string &ip_address) +{ + struct sockaddr_in sa{}; + int result = inet_pton(AF_INET, ip_address.c_str(), &(sa.sin_addr)); + return result != 0; +} + + diff --git a/common/ip_util.h b/common/ip_util.h index 76ea33fdb..3f67492a4 100644 --- a/common/ip_util.h +++ b/common/ip_util.h @@ -30,7 +30,14 @@ public: static uint32_t IPToUInt(const std::string &ip); static bool IsIpInRange(const std::string &ip, const std::string &network, const std::string &mask); static bool IsIpInPrivateRfc1918(const std::string &ip); + static std::string GetLocalIPAddress(); + static std::string GetPublicIPAddress(); + static std::string DNSLookupSync( + const std::string &addr, + int port + ); + static bool IsIPAddress(const std::string &ip_address); }; -#endif //EQEMU_IP_UTIL_H \ No newline at end of file +#endif //EQEMU_IP_UTIL_H diff --git a/world/main.cpp b/world/main.cpp index 17552952f..4a8cf2639 100644 --- a/world/main.cpp +++ b/world/main.cpp @@ -104,6 +104,7 @@ union semun { #include "world_store.h" #include "world_event_scheduler.h" #include "shared_task_manager.h" +#include "../common/ip_util.h" WorldStore world_store; ClientList client_list; @@ -634,6 +635,8 @@ int main(int argc, char **argv) } ); + WorldConfig::CheckForPossibleConfigurationIssues(); + EQStreamManagerInterfaceOptions opts(9000, false, false); opts.daybreak_options.resend_delay_ms = RuleI(Network, ResendDelayBaseMS); opts.daybreak_options.resend_delay_factor = RuleR(Network, ResendDelayFactor); diff --git a/world/world_config.cpp b/world/world_config.cpp index f57368a10..b66d4b9d7 100644 --- a/world/world_config.cpp +++ b/world/world_config.cpp @@ -16,17 +16,225 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "../common/global_define.h" +#include "../common/eqemu_logsys.h" #include "world_config.h" +#include "../common/ip_util.h" WorldConfig *WorldConfig::_world_config = nullptr; +std::string WorldConfig::GetByName(const std::string &var_name) const +{ + if (var_name == "UpdateStats") { + return (UpdateStats ? "true" : "false"); + } + if (var_name == "LoginDisabled") { + return (LoginDisabled ? "true" : "false"); + } + return (EQEmuConfig::GetByName(var_name)); +} -std::string WorldConfig::GetByName(const std::string &var_name) const { - if(var_name == "UpdateStats") - return(UpdateStats?"true":"false"); - if(var_name == "LoginDisabled") - return(LoginDisabled?"true":"false"); - return(EQEmuConfig::GetByName(var_name)); +void WorldConfig::CheckForPossibleConfigurationIssues() +{ + if (_config->DisableConfigChecks) { + LogInfo("Configuration checking [disabled]"); + return; + } + + const std::string local_address = IpUtil::GetLocalIPAddress(); + const std::string public_address = IpUtil::GetPublicIPAddress(); + const std::string config_file = "eqemu_config.json"; + const std::ifstream is_in_docker("/.dockerenv"); + + if (local_address.empty() && public_address.empty()) { + LogInfo("Configuration check, probes failed for local and public address, returning"); + return; + } + + LogInfo("Checking for possible configuration issues"); + LogInfo("To disable configuration checks, set [server.disable_config_checks] to [true] in [{}]", config_file); + + std::string config_address = _config->WorldAddress; + if (!IpUtil::IsIPAddress(config_address)) { + config_address = IpUtil::DNSLookupSync(_config->WorldAddress, 9000); + LogInfo( + "World config address using DNS [{}] resolves to [{}]", + _config->WorldAddress, + config_address + ); + } + + std::cout << std::endl; + + // lan detection + if (local_address != public_address + && IpUtil::IsIpInPrivateRfc1918(local_address) + && local_address != _config->LocalAddress + && !is_in_docker + ) { + LogWarning("# LAN detection (Configuration)"); + LogWarning(""); + LogWarning("You appear to be on a LAN and your localaddress may not be properly set!"); + LogWarning("This can prevent local clients from properly connecting to your server"); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#world]"); + LogWarning(""); + LogWarning("Config file [{}] path [server.world] variable [localaddress]", config_file); + LogWarning(""); + LogWarning("Local address (eqemu_config) value [{}] detected value [{}]", _config->LocalAddress, local_address); + std::cout << std::endl; + } + + // docker configuration + if ( + ( + (_config->LocalAddress.empty() && config_address.empty()) || + (config_address != public_address) + ) + && is_in_docker + ) { + LogWarning("# Docker Configuration (Configuration)"); + LogWarning(""); + LogWarning("You appear to running EQEmu in a docker container"); + LogWarning("In order for networking to work properly you will need to properly configure your server"); + LogWarning(""); + LogWarning( + "If your Docker host is on a [LAN] or behind a NAT / Firewall, your [localaddress] variable under [server.world] will need to"); + LogWarning( + "be set to your LAN address on the host, not the container address. [address] will need to be your public address"); + LogWarning(""); + LogWarning( + "If your Docker host is directly on the [public internet], your [localaddress] variable under [server.world] can be set to [127.0.0.1]." + ); + LogWarning(""); + LogWarning("[address] will need to be your public address"); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#world]"); + LogWarning(""); + LogWarning("Config file [{}] path [server.world] variable(s) [localaddress] [address]", config_file); + LogWarning(""); + LogWarning("Local address (eqemu_config) value [{}] detected value [{}]", _config->LocalAddress, local_address); + LogWarning( + "Public address (eqemu_config) value [{}] detected value [{}]", + config_address, + public_address + ); + std::cout << std::endl; + } + + // docker LAN not set + if (_config->LocalAddress.empty() && is_in_docker) { + LogWarning("# Docker LAN (Configuration)"); + LogWarning(""); + LogWarning("You appear to running EQEmu in a docker container"); + LogWarning( + "Your local address does not appear to be set, this may not be an issue if your deployment is not on a LAN" + ); + LogWarning(""); + LogWarning( + "If your Docker host is on a [LAN] or behind a NAT / Firewall, your [localaddress] variable under [server.world] will need to"); + LogWarning( + "be set to your LAN address on the host, not the container address. [address] will need to be your public address"); + LogWarning(""); + LogWarning( + "If your Docker host is directly on the [public internet], your [localaddress] variable under [server.world] can be set to [127.0.0.1]." + ); + LogWarning(""); + LogWarning("[address] will need to be your public address"); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#world]"); + LogWarning(""); + LogWarning("Config file [{}] path [server.world] variable(s) [localaddress] [address]", config_file); + LogWarning(""); + LogWarning("Local address (eqemu_config) value [{}] detected value [{}]", _config->LocalAddress, local_address); + LogWarning( + "Public address (eqemu_config) value [{}] detected value [{}]", + config_address, + public_address + ); + std::cout << std::endl; + } + + // public address different from configuration + if (!config_address.empty() && public_address != config_address) { + LogWarning("# Public address (Configuration)"); + LogWarning(""); + LogWarning("Your configured public address appears to be different from what's detected!"); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#world]"); + LogWarning(""); + LogWarning("Config file [{}] path [server.world] variable [address]", config_file); + LogWarning(""); + LogWarning( + "Public address (eqemu_config) value [{}] detected value [{}]", + config_address, + public_address + ); + std::cout << std::endl; + } + + // public address set to meta-address + if (config_address == "0.0.0.0") { + LogWarning("# Public meta-address (Configuration)"); + LogWarning(""); + LogWarning("Your configured public address is set to a meta-address (0.0.0.0) (all-interfaces)"); + LogWarning( + "The meta-address may not work properly and it is recommended you configure your public address explicitly"); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#world]"); + LogWarning(""); + LogWarning("Config file [{}] path [server.world] variable [address]", config_file); + LogWarning(""); + LogWarning( + "Public address (eqemu_config) value [{}] detected value [{}]", + config_address, + public_address + ); + std::cout << std::endl; + } + + // local address set to meta-address + if (_config->LocalAddress == "0.0.0.0") { + LogWarning("# Local meta-address (Configuration)"); + LogWarning(""); + LogWarning("Your configured local address is set to a meta-address (0.0.0.0) (all-interfaces)"); + LogWarning( + "The meta-address may not work properly and it is recommended you configure your local address explicitly" + ); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#world]"); + LogWarning(""); + LogWarning("Config file [{}] path [server.world] variable [localaddress]", config_file); + LogWarning(""); + LogWarning("Local address (eqemu_config) value [{}] detected value [{}]", _config->LocalAddress, local_address); + std::cout << std::endl; + } + + // ucs (public) + if ( + (!config_address.empty() && _config->MailHost != config_address) || + (!config_address.empty() && _config->ChatHost != config_address) + ) { + LogWarning("# UCS Address Mailhost (Configuration)"); + LogWarning(""); + LogWarning( + "UCS (Universal Chat Service) mail or chat appears to use a different address from your main world address" + ); + LogWarning("This can result in a chat service that doesn't network properly"); + LogWarning(""); + LogWarning("Docs [https://docs.eqemu.io/server/installation/configure-your-eqemu_config/#mailserver]"); + LogWarning(""); + LogWarning( + "[server.world.address] value [{}] [server.chatserver.host] [{}]", + config_address, + _config->ChatHost + ); + LogWarning( + "[server.world.address] value [{}] [server.mailserver.host] [{}]", + config_address, + _config->MailHost + ); + std::cout << std::endl; + } } diff --git a/world/world_config.h b/world/world_config.h index 2a274ed29..5af2ee976 100644 --- a/world/world_config.h +++ b/world/world_config.h @@ -68,6 +68,8 @@ public: static void SetWorldAddress(std::string addr) { if (_world_config) _world_config->WorldAddress=addr; } static void SetLocalAddress(std::string addr) { if (_world_config) _world_config->LocalAddress=addr; } + static void CheckForPossibleConfigurationIssues(); + void Dump() const; };