Skip to content

Commit 29c07db

Browse files
Add lightweight ipaddr lookup API
1 parent 17bb38b commit 29c07db

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

api/debian/changelog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
ooni-api (1.0.70) unstable; urgency=medium
2+
3+
* Lightweight ipaddr detection API
4+
5+
-- Federico Ceratto <[email protected]> Fri, 22 Sep 2023 12:49:04 +0200
6+
17
ooni-api (1.0.67) unstable; urgency=medium
28

39
* Support zstd compression for measurement uploads

api/ooniapi/probe_services.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,47 @@ def probe_geoip(probe_cc: str, asn: str) -> Tuple[Dict, str, int]:
117117
return resp, probe_cc, asn_int
118118

119119

120+
@probe_services_blueprint.route("/api/_/discover_probe_ipaddr", methods=["GET"])
121+
def discover_probe_ipaddr() -> Response:
122+
"""Probe Services: discover probe ipaddr
123+
---
124+
responses:
125+
'200':
126+
description: Give a URL test list to a probe running web_connectivity
127+
tests; additional data for other tests;
128+
schema:
129+
type: object
130+
properties:
131+
v:
132+
type: integer
133+
description: response format version
134+
cc:
135+
type: string
136+
description: probe CC inferred from GeoIP or ZZ
137+
asn:
138+
type: string
139+
description: probe ASN inferred from GeoIP or AS0
140+
network_name:
141+
type: string
142+
description: probe network name inferred from GeoIP or None
143+
"""
144+
ipaddr = extract_probe_ipaddr()
145+
cc = "ZZ"
146+
asn = "AS0"
147+
network_name = ""
148+
try:
149+
cc = lookup_probe_cc(ipaddr)
150+
asn, network_name = lookup_probe_network(ipaddr)
151+
metrics.incr("geoip_ipaddr_found")
152+
except geoip2.errors.AddressNotFoundError:
153+
metrics.incr("geoip_ipaddr_not_found")
154+
except Exception as e:
155+
log = current_app.logger
156+
log.error(str(e), exc_info=True)
157+
158+
return nocachejson(v=1, ipaddr=ipaddr, cc=cc, asn=asn, network_name=network_name)
159+
160+
120161
@probe_services_blueprint.route("/api/v1/check-in", methods=["POST"])
121162
def check_in() -> Response:
122163
"""Probe Services: check-in. Probes ask for tests to be run

0 commit comments

Comments
 (0)