Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ooniapi/services/ooniprobe/src/ooniprobe/routers/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion ooniapi/services/ooniprobe/src/ooniprobe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

Expand All @@ -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]
7 changes: 7 additions & 0 deletions ooniapi/services/ooniprobe/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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("") == ""