Browse Source

feat: support querying IP information

pull/1/head
Dnomd343 4 years ago
parent
commit
58b6d9113d
  1. 17
      backend/getIP.php
  2. 107
      backend/getInfo.php
  3. 30
      backend/queryInfo.php

17
backend/getIP.php

@ -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);
}
?>

107
backend/getInfo.php

@ -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']);
}
?>

30
backend/queryInfo.php

@ -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…
Cancel
Save