diff --git a/ooniapi/services/ooniprobe/src/ooniprobe/routers/reports.py b/ooniapi/services/ooniprobe/src/ooniprobe/routers/reports.py index 213669ee..439b0d85 100644 --- a/ooniapi/services/ooniprobe/src/ooniprobe/routers/reports.py +++ b/ooniapi/services/ooniprobe/src/ooniprobe/routers/reports.py @@ -273,10 +273,8 @@ def compare_probe_msmt_cc_asn( if db_probe_cc == cc and db_asn == asn: Metrics.PROBE_CC_ASN_MATCH.inc() elif db_probe_cc != cc: - log.error(f"db_cc != cc: {db_probe_cc} != {cc}") Metrics.PROBE_CC_ASN_NO_MATCH.labels(mismatch="cc", reported=cc, detected=db_probe_cc).inc() elif db_asn != asn: - log.error(f"db_asn != asn: {db_asn} != {asn}") Metrics.PROBE_CC_ASN_NO_MATCH.labels(mismatch="asn", reported=asn, detected=db_asn).inc() except Exception: pass diff --git a/ooniapi/services/ooniprobe/src/ooniprobe/utils.py b/ooniapi/services/ooniprobe/src/ooniprobe/utils.py index bd473ebd..4cd1ecbc 100644 --- a/ooniapi/services/ooniprobe/src/ooniprobe/utils.py +++ b/ooniapi/services/ooniprobe/src/ooniprobe/utils.py @@ -135,7 +135,7 @@ def extract_probe_ipaddr(request: Request) -> str: for h in real_ip_headers: if h in request.headers: - return request.headers.getlist(h)[0].rpartition(" ")[-1] + return get_first_ip(request.headers.getlist(h)[0]) return request.client.host if request.client else "" @@ -152,3 +152,14 @@ def lookup_probe_network(ipaddr: str, asn_reader: ASNReaderDep) -> Tuple[str, st "AS{}".format(resp.autonomous_system_number), resp.autonomous_system_organization or "0", ) + +def get_first_ip(headers: str) -> str: + """ + parse the first ip from a comma-separated list of ips encoded as a string + + example: + in: '123.123.123, 1.1.1.1' + out: '123.123.123' + """ + + return headers.partition(',')[0] \ No newline at end of file diff --git a/ooniapi/services/ooniprobe/tests/test_utils.py b/ooniapi/services/ooniprobe/tests/test_utils.py new file mode 100644 index 00000000..f34ee403 --- /dev/null +++ b/ooniapi/services/ooniprobe/tests/test_utils.py @@ -0,0 +1,7 @@ +from ooniprobe.utils import get_first_ip + +def test_get_first_ip(): + + assert get_first_ip("1.1.1.1") == "1.1.1.1" + assert get_first_ip("1.1.1.1, 2.2.2.2") == "1.1.1.1" + assert get_first_ip("") == "" \ No newline at end of file