Skip to content

Commit

Permalink
Added fallback in getIP in case the offline db is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfintel committed Jul 31, 2024
1 parent 1932906 commit 1d28610
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions backend/getIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

define('API_KEY_FILE', 'getIP_ipInfo_apikey.php');
define('SERVER_LOCATION_CACHE_FILE', 'getIP_serverLocation.php');
define('OFFLINE_IPINFO_DB_FILE', 'country_asn.mmdb');

require_once 'getIP_util.php';

Expand Down Expand Up @@ -136,7 +137,10 @@ function getIspInfo_ipinfoApi($ip){
require_once("geoip2.phar");
use MaxMind\Db\Reader;
function getIspInfo_ipinfoOfflineDb($ip){
$reader = new Reader('country_asn.mmdb');
if (!file_exists(OFFLINE_IPINFO_DB_FILE) || !is_readable(OFFLINE_IPINFO_DB_FILE)){
return null;
}
$reader = new Reader(OFFLINE_IPINFO_DB_FILE);
$data = $reader->get($ip);
if(!is_array($data)){
return null;
Expand Down Expand Up @@ -184,7 +188,7 @@ function formatResponse_simple($ip,$ispName=null){
if(!is_null($r)){
echo $r;
}else{
echo getIspInfo_fallback($ip);
echo formatResponse_simple($ip);
}
}
}
Expand Down

0 comments on commit 1d28610

Please sign in to comment.