mirror of https://github.com/dnomd343/echoIP
3 changed files with 154 additions and 0 deletions
@ -0,0 +1,17 @@ |
|||||
|
<?php |
||||
|
|
||||
|
function getClientIp() { |
||||
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
||||
|
$ip = $_SERVER['HTTP_CLIENT_IP']; |
||||
|
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) { |
||||
|
$ip = $_SERVER['HTTP_X_REAL_IP']; |
||||
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||||
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||||
|
$ip = preg_replace('/,.*/', '', $ip); |
||||
|
} else { |
||||
|
$ip = $_SERVER['REMOTE_ADDR']; |
||||
|
} |
||||
|
return preg_replace('/^::ffff:/', '', $ip); |
||||
|
} |
||||
|
|
||||
|
?> |
@ -0,0 +1,107 @@ |
|||||
|
<?php |
||||
|
|
||||
|
function getIPInfo($ip, $is_cli) { |
||||
|
$specialIpInfo = getSpecialIpInfo($ip); |
||||
|
if (is_string($specialIpInfo)) { |
||||
|
$info['ip'] = $ip; |
||||
|
$info['as'] = null; |
||||
|
$info['city'] = null; |
||||
|
$info['region'] = null; |
||||
|
$info['country'] = null; |
||||
|
$info['timezone'] = null; |
||||
|
$info['loc'] = null; |
||||
|
$info['isp'] = $specialIpInfo; |
||||
|
} else { |
||||
|
$rawIspInfo = getIspInfo($ip); |
||||
|
|
||||
|
$info['ip'] = $ip; |
||||
|
$info['as'] = getAS($rawIspInfo); |
||||
|
$info['city'] = $rawIspInfo['city']; |
||||
|
$info['region'] = $rawIspInfo['region']; |
||||
|
$info['country'] = get_country($rawIspInfo['country'])['en']; |
||||
|
$info['timezone'] = $rawIspInfo['timezone']; |
||||
|
$info['loc'] = $rawIspInfo['loc']; |
||||
|
$info['isp'] = getIsp($rawIspInfo); |
||||
|
} |
||||
|
|
||||
|
if ($is_cli == true) { |
||||
|
$cli = "IP: ".$info['ip'].PHP_EOL; |
||||
|
$cli = $cli."AS: ".$info['as'].PHP_EOL; |
||||
|
$cli = $cli."City: ".$info['city'].PHP_EOL; |
||||
|
$cli = $cli."Region: ".$info['region'].PHP_EOL; |
||||
|
$cli = $cli."Country: ".$info['country'].PHP_EOL; |
||||
|
$cli = $cli."Timezone: ".$info['timezone'].PHP_EOL; |
||||
|
$cli = $cli."Location: ".$info['loc'].PHP_EOL; |
||||
|
$cli = $cli."ISP: ".$info['isp'].PHP_EOL; |
||||
|
return $cli; |
||||
|
} |
||||
|
|
||||
|
return json_encode($info); |
||||
|
} |
||||
|
|
||||
|
function getSpecialIpInfo($ip) { |
||||
|
if ('::1' === $ip) { |
||||
|
return 'localhost IPv6 access'; |
||||
|
} |
||||
|
if (stripos($ip, 'fe80:') === 0) { |
||||
|
return 'link-local IPv6 access'; |
||||
|
} |
||||
|
if (strpos($ip, '127.') === 0) { |
||||
|
return 'localhost IPv4 access'; |
||||
|
} |
||||
|
if (strpos($ip, '10.') === 0) { |
||||
|
return 'private IPv4 access'; |
||||
|
} |
||||
|
if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) { |
||||
|
return 'private IPv4 access'; |
||||
|
} |
||||
|
if (strpos($ip, '192.168.') === 0) { |
||||
|
return 'private IPv4 access'; |
||||
|
} |
||||
|
if (strpos($ip, '169.254.') === 0) { |
||||
|
return 'link-local IPv4 access'; |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
function getIspInfo($ip) { |
||||
|
$json = file_get_contents('https://ipinfo.io/'.$ip.'/json'); |
||||
|
if (!is_string($json)) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
$data = json_decode($json, true); |
||||
|
if (!is_array($data)) { |
||||
|
return null; |
||||
|
} |
||||
|
return $data; |
||||
|
} |
||||
|
|
||||
|
function getIsp($rawIspInfo) { |
||||
|
if ( |
||||
|
!is_array($rawIspInfo) |
||||
|
|| !array_key_exists('org', $rawIspInfo) |
||||
|
|| !is_string($rawIspInfo['org']) |
||||
|
|| empty($rawIspInfo['org']) |
||||
|
) { |
||||
|
return 'Unknown ISP'; |
||||
|
} |
||||
|
return preg_replace('/AS\\d+\\s/', '', $rawIspInfo['org']); |
||||
|
} |
||||
|
|
||||
|
function getAS($rawIspInfo) { |
||||
|
if ( |
||||
|
!is_array($rawIspInfo) |
||||
|
|| !array_key_exists('org', $rawIspInfo) |
||||
|
|| !is_string($rawIspInfo['org']) |
||||
|
|| empty($rawIspInfo['org']) |
||||
|
) { |
||||
|
return 'Unknown AS'; |
||||
|
} |
||||
|
if (preg_match('/AS\\d+\\s/', $rawIspInfo['org'], $as) !== 1) { |
||||
|
return 'Unknown AS'; |
||||
|
} |
||||
|
return trim($as['0']); |
||||
|
} |
||||
|
|
||||
|
?> |
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
|
||||
|
include("getInfo.php"); |
||||
|
include("getIP.php"); |
||||
|
|
||||
|
function sendHeaders() { |
||||
|
header('Content-Type: application/json; charset=utf-8'); |
||||
|
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'); |
||||
|
} |
||||
|
|
||||
|
$ip = $_GET['ip']; |
||||
|
if ($ip) { |
||||
|
if (!filter_var($ip, \FILTER_VALIDATE_IP)) { |
||||
|
echo "Illegal IP format".PHP_EOL; |
||||
|
exit; |
||||
|
} |
||||
|
} else { |
||||
|
$ip = getClientIp(); |
||||
|
} |
||||
|
|
||||
|
if ($_GET['cli'] == "true") { |
||||
|
echo getIPInfo($ip, true); |
||||
|
} else { |
||||
|
sendHeaders(); |
||||
|
echo getIPInfo($ip, false); |
||||
|
} |
||||
|
|
||||
|
?> |
Loading…
Reference in new issue