Browse Source

feat: help message, format of detail on cli

master
dnomd343 3 years ago
parent
commit
9191b09e2d
  1. 20
      backend/getInfo.php
  2. 49
      backend/queryInfo.php
  3. 2
      conf/nginx/ip.conf

20
backend/getInfo.php

@ -102,16 +102,16 @@ function getIPInfo($ip) {
global $request;
if ($request['cli']) { // 使用命令行模式
$cli = "IP: ".$info['ip'] . PHP_EOL;
$cli .= "AS: ".$info['as'] . PHP_EOL;
$cli .= "City: ".$info['city'] . PHP_EOL;
$cli .= "Region: ".$info['region'] . PHP_EOL;
$cli .= "Country: ".$info['country'] . PHP_EOL;
$cli .= "Timezone: ".$info['timezone'] . PHP_EOL;
$cli .= "Location: ".$info['loc'] . PHP_EOL;
$cli .= "ISP: ".$info['isp'] . PHP_EOL;
$cli .= "Scope: ".$info['scope'] . PHP_EOL;
$cli .= "Detail: ".$info['detail'] . PHP_EOL;
$cli = "IP: " . $info['ip'] . PHP_EOL;
if ($info['as'] != NULL) { $cli .= "AS: " . $info['as'] . PHP_EOL; }
if ($info['city'] != NULL) { $cli .= "City: " . $info['city'] . PHP_EOL; }
if ($info['region'] != NULL) { $cli .= "Region: " . $info['region'] . PHP_EOL; }
if ($info['country'] != NULL) { $cli .= "Country: " . $info['country'] . PHP_EOL; }
if ($info['timezone'] != NULL) { $cli .= "Timezone: " . $info['timezone'] . PHP_EOL; }
if ($info['loc'] != NULL) { $cli .= "Location: " . $info['loc'] . PHP_EOL; }
if ($info['isp'] != NULL) { $cli .= "ISP: " . $info['isp'] . PHP_EOL; }
if ($info['scope'] != NULL) { $cli .= "Scope: " . $info['scope'] . PHP_EOL; }
if ($info['detail'] != NULL) { $cli .= "Detail: " . $info['detail'] . PHP_EOL; }
return $cli;
}

49
backend/queryInfo.php

@ -65,6 +65,9 @@ function preRount() { // 解析请求路径
if ($requestUri == '/' || $requestUri == '/ip') { // URI -> / or /ip
$request['justip'] = true;
return;
} else if ($requestUri == '/help') { // URI -> /help
$request['help'] = true;
return;
} else if ($requestUri == '/version') { // URI -> /version
$request['version'] = true;
return;
@ -85,7 +88,9 @@ function preRount() { // 解析请求路径
} else if ($requestUri == '/query') { // URI -> /query?xxx=xxx
if ($_GET['error'] == 'true') { $request['error'] = true; }
if ($_GET['version'] == 'true') { $request['version'] = true; }
if ($_GET['help'] == 'true') { $request['help'] = true; }
if ($_GET['gbk'] == 'true') { $request['gbk'] = true; }
if ($_GET['qr'] == 'true') { $request['qr'] = true; }
if ($_GET['justip'] == 'true') { $request['justip'] = true; }
if (isset($_GET['ip'])) { $request['ip'] = $_GET['ip']; }
return;
@ -128,6 +133,7 @@ function preRount() { // 解析请求路径
function routeParam() {
// error -> 请求出错
// version -> 获取版本数据
// help -> 显示帮助信息
// cli -> 来自命令行下的请求
// gbk -> 返回数据使用GBK编码
// qr -> 生成二维码
@ -137,6 +143,7 @@ function routeParam() {
global $request;
global $webUri;
global $helpContent;
if ($request['error']) { // 请求出错
if ($request['cli']) { // 命令行模式
echo 'Illegal Request' . PHP_EOL;
@ -147,6 +154,16 @@ function routeParam() {
exit; // 退出
}
if ($request['help']) { // 显示帮助信息
if (!$request['cli']) { // 网页模式不输出
header('HTTP/1.1 302 Moved Temporarily');
header('Location: /error');
} else {
echo $helpContent;
}
exit;
}
if ($request['version']) { // 请求版本信息
$version = getVersion();
if ($request['cli']) { // 命令行模式
@ -216,6 +233,7 @@ $myVersion = 'v1.2';
$request = array(
'error' => false,
'version' => false,
'help' => false,
'cli' => false,
'gbk' => false,
'qr' => false,
@ -229,6 +247,37 @@ if (isset($_SERVER['HTTP_HOST'])) {
$webUri = $_SERVER['HTTP_HOST'];
}
}
$helpContent = PHP_EOL . 'echoIP - ' . $myVersion . ' (https://github.com/dnomd343/echoIP)' . PHP_EOL . '
Format: http(s)://' . $webUri . '{Request_URI}
/ or /ip -> Show client IP.
/info or /info/ -> Show detail of client IP.
/{ip} or /info/{ip} -> Show detail of {ip}.
/info/gbk -> Show detail of client IP (use GBK encoding).
/{ip}/gbk or /info/{ip}/gbk -> Show detail of {ip} (use GBK encoding).
/qr -> Show QR code of client IP (use special unicode characters).
/qr/ -> Show QR code of client IP (use full characters).
/qr/{xx} -> Show QR code of client IP (Use two custom characters).
/help -> Show help message.
/ua -> Show http user-agent of client.
/version -> Show version of echoIP and IP database.
/query?xxx=xxx&xxx=xxx
|-> error=true: Show error request.
|-> version=true: Show help message.
|-> help=true: Show version of echoIP and IP database.
|-> gbk=true: Use GBK encoding.
|-> qr=true: Show QR code of client IP.
|-> justip=true: Only query the client IP.
|-> ip={ip}: Query of specified IP.
';
$webUri = 'http://' . $webUri . '/';
main();

2
conf/nginx/ip.conf

@ -6,6 +6,7 @@ server {
if ($http_user_agent !~* (curl|wget)) {
return 301 https://$server_name$request_uri;
}
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:1601;
}
@ -27,6 +28,7 @@ server {
gzip_vary on;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:1601;
}

Loading…
Cancel
Save