Skip to content

Commit ec057f5

Browse files
Add lightweight ipaddr lookup API
1 parent 6b7b4fc commit ec057f5

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.67) unstable; urgency=medium
2+
3+
* Add lightweight ipaddr lookup API
4+
5+
-- Federico Ceratto <[email protected]> Thu, 31 Aug 2023 15:45:06 +0200
6+
17
ooni-api (1.0.66) unstable; urgency=medium
28

39
* Add short description to incidents

api/ooniapi/probe_services.py

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

118118

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

0 commit comments

Comments
 (0)