From a4f70ebab326b4f2a499bde35947eff4f7f67a3e Mon Sep 17 00:00:00 2001 From: IP2Location Date: Fri, 6 Sep 2024 16:05:39 +0800 Subject: [PATCH] Added IP2Location.io API as failover for geolocation --- scripts/zmtelemetry.pl.in | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scripts/zmtelemetry.pl.in b/scripts/zmtelemetry.pl.in index 666996e9f8..2f589ff96e 100644 --- a/scripts/zmtelemetry.pl.in +++ b/scripts/zmtelemetry.pl.in @@ -245,9 +245,21 @@ sub getGeo { (my $latitude, my $longitude) = split /,/, $content->{loc}; return ($content->{city}, $content->{region}, $content->{country}, $latitude, $longitude); } else { - Warning("Geoip data retrieval returned HTTP POST error code: $resp_code"); - Debug("Geoip data retrieval failure response message: $resp_msg"); - return ($unknown, $unknown, $unknown, $unknown, $unknown); + my $endpoint2 = 'https://api.ip2location.io'; + my $ua2 = LWP::UserAgent->new; + my $req2 = HTTP::Request->new(GET => $endpoint2); + my $resp2 = $ua2->request($req2); + my $resp_msg2 = $resp2->decoded_content; + my $resp_code2 = $resp2->code; + + if ($resp2->is_success) { + my $content = decode_json($resp_msg2); + return ($content->{city_name}, $content->{region_name}, $content->{country_code}, $content->{latitude}, $content->{longitude}); + } else { + Warning("Geoip data retrieval returned HTTP POST error code: $resp_code2"); + Debug("Geoip data retrieval failure response message: $resp_msg2"); + return ($unknown, $unknown, $unknown, $unknown, $unknown); + } } }