From 29c07db966019a216537983c1fb6d61a998e6e30 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Thu, 31 Aug 2023 15:44:36 +0200 Subject: [PATCH] Add lightweight ipaddr lookup API --- api/debian/changelog | 6 +++++ api/ooniapi/probe_services.py | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/api/debian/changelog b/api/debian/changelog index 98302ec4..6f6a8136 100644 --- a/api/debian/changelog +++ b/api/debian/changelog @@ -1,3 +1,9 @@ +ooni-api (1.0.70) unstable; urgency=medium + + * Lightweight ipaddr detection API + + -- Federico Ceratto Fri, 22 Sep 2023 12:49:04 +0200 + ooni-api (1.0.67) unstable; urgency=medium * Support zstd compression for measurement uploads diff --git a/api/ooniapi/probe_services.py b/api/ooniapi/probe_services.py index c8288017..d8cbb0d5 100644 --- a/api/ooniapi/probe_services.py +++ b/api/ooniapi/probe_services.py @@ -117,6 +117,47 @@ def probe_geoip(probe_cc: str, asn: str) -> Tuple[Dict, str, int]: return resp, probe_cc, asn_int +@probe_services_blueprint.route("/api/_/discover_probe_ipaddr", methods=["GET"]) +def discover_probe_ipaddr() -> Response: + """Probe Services: discover probe ipaddr + --- + responses: + '200': + description: Give a URL test list to a probe running web_connectivity + tests; additional data for other tests; + schema: + type: object + properties: + v: + type: integer + description: response format version + cc: + type: string + description: probe CC inferred from GeoIP or ZZ + asn: + type: string + description: probe ASN inferred from GeoIP or AS0 + network_name: + type: string + description: probe network name inferred from GeoIP or None + """ + ipaddr = extract_probe_ipaddr() + cc = "ZZ" + asn = "AS0" + network_name = "" + try: + cc = lookup_probe_cc(ipaddr) + asn, network_name = lookup_probe_network(ipaddr) + metrics.incr("geoip_ipaddr_found") + except geoip2.errors.AddressNotFoundError: + metrics.incr("geoip_ipaddr_not_found") + except Exception as e: + log = current_app.logger + log.error(str(e), exc_info=True) + + return nocachejson(v=1, ipaddr=ipaddr, cc=cc, asn=asn, network_name=network_name) + + @probe_services_blueprint.route("/api/v1/check-in", methods=["POST"]) def check_in() -> Response: """Probe Services: check-in. Probes ask for tests to be run