diff --git a/backend/getIP.php b/backend/getIP.php index 530cd5797..b7673e664 100755 --- a/backend/getIP.php +++ b/backend/getIP.php @@ -2,7 +2,7 @@ /* * This script detects the client's IP address and fetches ISP info from ipinfo.io/ - * Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Country and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (will be empty if isp detection is disabled). + * Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Country and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (or an empty string if isp detection is disabled or if it failed). * Client side, the output of this script can be treated as JSON or as regular text. If the output is regular text, it will be shown to the user as is. */ @@ -13,440 +13,181 @@ require_once 'getIP_util.php'; -/** - * @param string $ip - * - * @return string|null - */ -function getLocalOrPrivateIpInfo($ip) -{ +function getLocalOrPrivateIpInfo($ip){ // ::1/128 is the only localhost ipv6 address. there are no others, no need to strpos this if ('::1' === $ip) { return 'localhost IPv6 access'; } - // simplified IPv6 link-local address (should match fe80::/10) if (stripos($ip, 'fe80:') === 0) { return 'link-local IPv6 access'; } - // fc00::/7 Unique Local IPv6 Unicast Addresses if (preg_match('/^(fc|fd)([0-9a-f]{0,4}:){1,7}[0-9a-f]{1,4}$/i', $ip) === 1) { return 'ULA IPv6 access'; } - // anything within the 127/8 range is localhost ipv4, the ip must start with 127.0 if (strpos($ip, '127.') === 0) { return 'localhost IPv4 access'; } - // 10/8 private IPv4 if (strpos($ip, '10.') === 0) { return 'private IPv4 access'; } - // 172.16/12 private IPv4 if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) { return 'private IPv4 access'; } - // 192.168/16 private IPv4 if (strpos($ip, '192.168.') === 0) { return 'private IPv4 access'; } - // IPv4 link-local if (strpos($ip, '169.254.') === 0) { return 'link-local IPv4 access'; } - return null; } -/** - * @return string - */ -function getIpInfoTokenString() -{ - if ( - !file_exists(API_KEY_FILE) - || !is_readable(API_KEY_FILE) - ) { - return ''; +function getIspInfo_ipinfoApi($ip){ + if (!file_exists(API_KEY_FILE) || !is_readable(API_KEY_FILE)){ + return null; } - require API_KEY_FILE; - - if (empty($IPINFO_APIKEY)) { - return ''; - } - - return '?token=' . $IPINFO_APIKEY; -} - -/** - * @param string $ip - * - * @return array|null - */ -function getIspInfo($ip) -{ - $token=getIpInfoTokenString(); - if (empty($token)){ + if(empty($IPINFO_APIKEY)){ return null; } - $json = file_get_contents('https://ipinfo.io/' . $ip . '/json' . $token); + $json = file_get_contents('https://ipinfo.io/' . $ip . '/json?token=' . $IPINFO_APIKEY); if (!is_string($json)) { return null; } - $data = json_decode($json, true); if (!is_array($data)) { return null; } - - return $data; -} - -/** - * @param array|null $rawIspInfo - * - * @return string - */ -function getIsp($rawIspInfo) -{ - if (is_array($rawIspInfo)) { - /* variant with no token - has json like: - { - "ip": "xxx.xxx.xxx.xxx", - "hostname": "example.com", - "city": "Vienna", - "region": "Vienna", - "country": "AT", - "loc": "48.2085,16.3721", - "org": "ASxxxx T-Mobile Austria GmbH", - "postal": "nnnn", - "timezone": "Europe/Vienna", - "readme": "https://ipinfo.io/missingauth" + $isp=null; + //ISP name, if present, is either in org or asn.name + if (array_key_exists('org', $data) && is_string($data['org']) && !empty($data['org'])) { + // Remove AS##### from ISP name, if present + $isp = preg_replace('/AS\\d+\\s/', '', $data['org']); + } elseif (array_key_exists('asn', $data) && is_array($data['asn']) && !empty($data['asn']) && array_key_exists('name', $data['asn']) && is_string($data['asn']['name'])) { + $isp = $data['asn']['name']; + } else{ + return null; + } + $country=null; + if(array_key_exists('country',$data) && is_string($data['country'])){ + $country = $data['country']; + } + //If requested by the client (and we have the required information), calculate the distance + $distance=null; + if(isset($_GET['distance']) && ($_GET['distance']==='mi' || $_GET['distance']==='km') && array_key_exists('loc', $data) && is_string($data['loc'])){ + $unit = $_GET['distance']; + $clientLoc = $data['loc']; + $serverLoc = null; + if (file_exists(SERVER_LOCATION_CACHE_FILE) && is_readable(SERVER_LOCATION_CACHE_FILE)) { + require SERVER_LOCATION_CACHE_FILE; } - */ - if ( - array_key_exists('org', $rawIspInfo) - && is_string($rawIspInfo['org']) - && !empty($rawIspInfo['org']) - ) { - // Remove AS##### from ISP name, if present - return preg_replace('/AS\\d+\\s/', '', $rawIspInfo['org']); + if (!is_string($serverLoc) || empty($serverLoc)) { + $json = file_get_contents('https://ipinfo.io/json?token=' . $IPINFO_APIKEY); + if (!is_string($json)) { + return null; + } + $sdata = json_decode($json, true); + if (!is_array($sdata) || !array_key_exists('loc', $sdata) || !is_string($sdata['loc']) || empty($sdata['loc'])) { + return null; + } + $serverLoc = $sdata['loc']; + file_put_contents(SERVER_LOCATION_CACHE_FILE, " $processedString, + 'rawIspInfo' => $data ?: '', + ]); } require_once("geoip2.phar"); use MaxMind\Db\Reader; -/** - * @param string $ip - * - * @return string - */ -function getIspInfo_offlineDb($ip){ +function getIspInfo_ipinfoOfflineDb($ip){ $reader = new Reader('country_asn.mmdb'); - $record = $reader->get($ip); - return $record; -} - -/** - * @return void - */ -function sendHeaders() -{ - header('Content-Type: application/json; charset=utf-8'); - - if (isset($_GET['cors'])) { - header('Access-Control-Allow-Origin: *'); - header('Access-Control-Allow-Methods: GET, POST'); - } - - header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, s-maxage=0'); - header('Cache-Control: post-check=0, pre-check=0', false); - header('Pragma: no-cache'); -} - -/** - * @param string $ip - * @param string|null $ipInfo - * @param string|null $distance - * @param array|null $rawIspInfo - * - * @return void - */ -function sendResponse( - $ip, - $ipInfo = null, - $distance = null, - $rawIspInfo = null -) { - $processedString = $ip; - if (is_string($ipInfo)) { - $processedString .= ' - ' . $ipInfo; - } - - if ( - is_array($rawIspInfo) - && array_key_exists('country', $rawIspInfo) - ) { - $processedString .= ', ' . $rawIspInfo['country']; - } - if (is_string($distance)) { - $processedString .= ' (' . $distance . ')'; + $data = $reader->get($ip); + if(!is_array($data)){ + return null; } - - sendHeaders(); - echo json_encode([ + $processedString = $ip.' - ' . $data['as_name'] . ', ' . $data['country_name']; + return json_encode([ 'processedString' => $processedString, - 'rawIspInfo' => $rawIspInfo ?: '', + 'rawIspInfo' => $data ?: '', ]); } -/** - * @param string $ip - * @param array|null $rawIspInfo - * - * @return void - */ -function sendResponse_offlineDb( - $ip, - $rawIspInfo = null -) { - $processedString = $ip; - if(is_array($rawIspInfo)){ - $processedString .= ' - ' . $rawIspInfo['as_name'] . ', ' . $rawIspInfo['country_name']; - }else{ - $processedString .= ' - Unknown ISP'; +function formatResponse_simple($ip,$ispName=null){ + $processedString=$ip; + if(is_string($ispName)){ + $processedString.=' - '.$ispName; } - sendHeaders(); - echo json_encode([ + return json_encode([ 'processedString' => $processedString, - 'rawIspInfo' => $rawIspInfo ?: '', + 'rawIspInfo' => '', ]); } -$ip = getClientIp(); - -$localIpInfo = getLocalOrPrivateIpInfo($ip); -// local ip, no need to fetch further information -if (is_string($localIpInfo)) { - sendResponse($ip, $localIpInfo); - exit; -} - -if (!isset($_GET['isp'])) { - sendResponse($ip); - exit; +header('Content-Type: application/json; charset=utf-8'); +if (isset($_GET['cors'])) { + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Methods: GET, POST'); } +header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, s-maxage=0'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); -$rawIspInfo = getIspInfo($ip); -if (is_null($rawIspInfo)){ - $rawIspInfo = getIspInfo_offlineDb($ip); - sendResponse_offlineDb($ip,$rawIspInfo); +$ip = getClientIp(); +//if the user requested the ISP info, we first try to fetch it using ipinfo.io (if there is no api key set it fails without sending data, it can also fail because of rate limiting or invalid responses), then we try with the offline db, if that also fails (or if ISP info was not requested) we just respond with the IP address +if(isset($_GET['isp'])){ + $localIpInfo = getLocalOrPrivateIpInfo($ip); + //local ip, no need to fetch further information + if (is_string($localIpInfo)) { + echo formatResponse_simple($ip,$localIpInfo); + }else{ + $r=getIspInfo_ipinfoApi($ip); + if(!is_null($r)){ + echo $r; + }else{ + $r=getIspInfo_ipinfoOfflineDb($ip); + if(!is_null($r)){ + echo $r; + }else{ + echo getIspInfo_fallback($ip); + } + } + } }else{ - $isp = getIsp($rawIspInfo); - $distance = getDistance($rawIspInfo); - sendResponse($ip, $isp, $distance, $rawIspInfo); + echo formatResponse_simple($ip); } diff --git a/backend/getIP_util.php b/backend/getIP_util.php index 5cc6b64b9..7aeaae1fe 100755 --- a/backend/getIP_util.php +++ b/backend/getIP_util.php @@ -17,4 +17,3 @@ function getClientIp() { return preg_replace('/^::ffff:/', '', $ip); } -