Browse Source

refactor: rebuild the query section

pull/1/head
Dnomd343 4 years ago
parent
commit
fa9a236a13
  1. 17
      backend/getIP.php
  2. 6
      backend/getInfo.php
  3. 26
      backend/queryInfo.php

17
backend/getIP.php

@ -1,17 +0,0 @@
<?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);
}
?>

6
backend/getInfo.php

@ -2,7 +2,7 @@
include("getCountry.php");
function getIPInfo($ip, $is_cli) {
function getIPInfo($ip) {
$specialIpInfo = getSpecialIpInfo($ip);
if (is_string($specialIpInfo)) {
$info['ip'] = $ip;
@ -15,7 +15,6 @@ function getIPInfo($ip, $is_cli) {
$info['isp'] = $specialIpInfo;
} else {
$rawIspInfo = getIspInfo($ip);
$info['ip'] = $ip;
$info['as'] = getAS($rawIspInfo);
$info['city'] = $rawIspInfo['city'];
@ -27,7 +26,7 @@ function getIPInfo($ip, $is_cli) {
$info['country'] .= "(".get_country($rawIspInfo['country'])['cn'].")";
}
if ($is_cli == true) {
if ($_GET['cli'] == "true") {
$cli = "IP: ".$info['ip'].PHP_EOL;
$cli = $cli."AS: ".$info['as'].PHP_EOL;
$cli = $cli."City: ".$info['city'].PHP_EOL;
@ -39,6 +38,7 @@ function getIPInfo($ip, $is_cli) {
return $cli;
}
header('Content-Type: application/json; charset=utf-8');
return json_encode($info);
}

26
backend/queryInfo.php

@ -1,30 +1,14 @@
<?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);
if (!filter_var($ip, \FILTER_VALIDATE_IP)) {
echo "Illegal IP format".PHP_EOL;
exit;
}
echo getIPInfo($ip);
?>

Loading…
Cancel
Save