diff --git a/libservice/src/Aggregator.cpp b/libservice/src/Aggregator.cpp index 502c2f21..fcb767c0 100644 --- a/libservice/src/Aggregator.cpp +++ b/libservice/src/Aggregator.cpp @@ -31,7 +31,7 @@ static std::string getEndpoint(const std::string& host, const std::string& url) static bool isIpv4ClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) { in_addr_t clientAddrBinary; if (inet_pton(AF_INET, addr.c_str(), &clientAddrBinary) != 1) { - throw std::runtime_error("Cannot parse IPv4 client address: {}" + addr); + throw std::runtime_error("Couldn't parse IPv4 client address"); } return ipChecker.isV4AddressExternal(clientAddrBinary); } @@ -39,35 +39,45 @@ static bool isIpv4ClientExternal(const IpAddressChecker& ipChecker, const std::s static bool isIpv6ClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) { in6_addr clientAddrBinary{}; if (inet_pton(AF_INET6, addr.c_str(), &clientAddrBinary) != 1) { - throw std::runtime_error("Cannot parse IPv6 client address: {}" + addr); + throw std::runtime_error("Couldn't parse IPv6 client address"); } return ipChecker.isV6AddressExternal(clientAddrBinary); } -static bool isClientExternal(const IpAddressChecker& ipChecker, const std::string& addr, bool isIpV6) { - return isIpV6 ? isIpv6ClientExternal(ipChecker, addr) : isIpv4ClientExternal(ipChecker, addr); +static bool isClientExternal(const IpAddressChecker& ipChecker, const std::string& addr, bool isIpv6) { + return isIpv6 ? isIpv6ClientExternal(ipChecker, addr) : isIpv4ClientExternal(ipChecker, addr); +} + +static bool isClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) { + bool isPossiblyIpv6{std::count(addr.begin(), addr.end(), ':') >= 2}; + return isClientExternal(ipChecker, addr, isPossiblyIpv6); } static void incrementServiceClientsNumber( const IpAddressChecker& ipChecker, Service& service, const httpparser::HttpRequest& request, const DiscoverySessionMeta& meta) { + bool isExternal{false}; std::string clientAddr; - if (!request.xForwardedFor.empty()) { - clientAddr = request.xForwardedFor.front(); - } else if (discoverySessionFlagsIsIPv4(meta.flags)) { - clientAddr = ipv4ToString(meta.sourceIPData); - } else if (discoverySessionFlagsIsIPv6(meta.flags)) { - clientAddr = ipv6ToString(meta.sourceIPData); - } - try { - if (isClientExternal(ipChecker, clientAddr, discoverySessionFlagsIsIPv6(meta.flags))) { - ++service.externalClientsNumber; + if (!request.xForwardedFor.empty()) { + clientAddr = request.xForwardedFor.front(); + isExternal = isClientExternal(ipChecker, clientAddr); + } else if (discoverySessionFlagsIsIPv4(meta.flags)) { + clientAddr = ipv4ToString(meta.sourceIPData); + isExternal = isClientExternal(ipChecker, clientAddr, false); + } else if (discoverySessionFlagsIsIPv6(meta.flags)) { + clientAddr = ipv6ToString(meta.sourceIPData); + isExternal = isClientExternal(ipChecker, clientAddr, true); } else { - ++service.internalClientsNumber; + return; } } catch (const std::runtime_error& e) { - LOG_TRACE(e.what()); - return; + LOG_TRACE("Couldn't determine if the client is external: {} (client address: {})", e.what(), clientAddr); + } + + if (isExternal) { + ++service.externalClientsNumber; + } else { + ++service.internalClientsNumber; } }