Skip to content

Commit

Permalink
fix: Remove using src address metadata in case of X-Forwarded-For
Browse files Browse the repository at this point in the history
  • Loading branch information
hparzych committed Jan 29, 2024
1 parent 3ef5dc2 commit f88b73d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions libservice/src/Aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,28 @@ static bool isIpv6ClientExternal(const IpAddressChecker& ipChecker, const std::s
return ipChecker.isV6AddressExternal(clientAddrBinary);
}

static bool isClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) {
bool isIpv6{addr.find(':') != std::string::npos};
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 void incrementServiceClientsNumber(
const IpAddressChecker& ipChecker, Service& service, const httpparser::HttpRequest& request, const DiscoverySessionMeta& meta) {
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))) {
bool isExternal{false};
if (!request.xForwardedFor.empty()) {
isExternal = isClientExternal(ipChecker, request.xForwardedFor.front());
} else if (discoverySessionFlagsIsIPv4(meta.flags)) {
isExternal = isClientExternal(ipChecker, ipv4ToString(meta.sourceIPData), false);
} else if (discoverySessionFlagsIsIPv6(meta.flags)) {
isExternal = isClientExternal(ipChecker, ipv6ToString(meta.sourceIPData), true);
}

if (isExternal) {
++service.externalClientsNumber;
} else {
++service.internalClientsNumber;
Expand Down

0 comments on commit f88b73d

Please sign in to comment.