Browse Source

update: support error page, new logic of nginx, more parameters of backend

master
Dnomd343 4 years ago
parent
commit
3cea0c8ab2
  1. 62
      backend/queryInfo.php
  2. 50
      conf/nginx/docker.conf
  3. 57
      conf/nginx/ip.conf
  4. 1
      error.html

62
backend/queryInfo.php

@ -37,7 +37,7 @@ function getIPInfo($ip) {
$info['timezone'] = $data['timezone'];
$info['loc'] = $data['loc'];
$info['isp'] = $data['isp'];
if ($addr[0] == '中国' || $detail['country'] == '中国') {
if ($detail['country'] == '中国') {
$info['country'] = 'CN - China(中国)';
$info['timezone'] = 'Asia/Shanghai';
if ($detail['region'] == '台湾') { // 修正台湾数据带 "市" 或 "县" 的情况
@ -58,9 +58,11 @@ function getIPInfo($ip) {
}
if ($detail['region'] != '' || $detail['city'] != '') { // 修正后数据不同时为空
$cityLoc = getLoc($detail['region'], $detail['city']); // 获取城市经纬度
$info['region'] = $cityLoc['region'];
$info['city'] = $cityLoc['city'];
$info['loc'] = $cityLoc['lat'] . ',' . $cityLoc['lon'];
if ($cityLoc['region'] != '香港' && $cityLoc['region'] != '澳门' && $cityLoc['region'] != '台湾') { // 跳过港澳台数据
$info['region'] = $cityLoc['region'];
$info['city'] = $cityLoc['city'];
$info['loc'] = $cityLoc['lat'] . ',' . $cityLoc['lon'];
}
}
if ($detail['isp'] == '教育网') { // 载入纯真库分析出的ISP数据
$info['isp'] = 'China Education and Research Network';
@ -155,7 +157,50 @@ function tryCIDR($beginIP, $endIP) { // 给定IP范围,尝试计算CIDR
}
}
function main() {
function getVersion() { // 获取自身及数据库版本号
global $myVersion;
$version['echoip'] = $myVersion;
$qqwry = new QQWry('qqwry.dat');
$IPIP = new IPDB('ipipfree.ipdb');
$version['qqwry.dat'] = $qqwry->getVersion();
$version['ipip.net'] = $IPIP->getVersion();
return $version;
}
function formatDate($str) { // 将YYYYMMDD处理为YYYY-MM-DD
return substr($str, 0, 4) . '-' . substr($str, 4, 2) . '-' . substr($str, 6, 2);
}
function routeParam() {
// error -> 请求出错
// version -> 获取版本数据
// cli -> 来自命令行下的请求
// justip -> 仅查询IP地址
// ip -> 请求指定IP的数据
if ($_GET['error'] == "true") { // 请求出错
if ($_GET['cli'] == "true") { // 命令行模式
echo 'Illegal Request' . PHP_EOL;
} else {
header('HTTP/1.1 302 Moved Temporarily');
header('Location: /error.html');
}
exit; // 退出程序
}
if ($_GET['version'] == "true") { // 请求版本信息
$version = getVersion();
if ($_GET['cli'] == "true") { // 命令行模式
echo "echoip -> " . $version['echoip'] . PHP_EOL;
echo "qqwry.dat -> " . formatDate($version['qqwry.dat']) . PHP_EOL;
echo "ipip.net -> " . formatDate($version['ipip.net']) . PHP_EOL;
} else {
header('Content-Type: application/json; charset=utf-8');
echo json_encode($version); // 返回JSON数据
}
exit; // 退出程序
}
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; // 获取客户端IP
if ($_GET['justip'] == "true") { // 仅查询IP地址
if ($_GET['cli'] == "true") { // 命令行模式
@ -166,8 +211,8 @@ function main() {
}
exit;
}
$ip = isset($_GET['ip']) ? $_GET['ip'] : $ip; // 若存在请求信息则查询该IP
$ip = isset($_GET['ip']) ? $_GET['ip'] : $ip; // 若存在请求信息则查询该IP
if (!filter_var($ip, \FILTER_VALIDATE_IP)) { // 输入IP不合法
if ($_GET['cli'] == "true") { // 命令行模式
echo "Illegal IP format" . PHP_EOL;
@ -183,4 +228,9 @@ function main() {
echo getIPInfo($ip);
}
function main() {
routeParam(); // 处理传入参数
}
$myVersion = 'v1.1';
main();

50
conf/nginx/docker.conf

@ -2,34 +2,41 @@ server {
listen 8080;
root /var/www/echoIP;
error_page 403 404 500 501 502 503 504 = /error.html;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Real-IP;
location = /ua {
if ($http_user_agent ~* (curl|wget)) {
return 200 $http_user_agent\n;
}
default_type application/json;
return 200 "{\"ua\":\"$http_user_agent\"}";
}
location = / {
set $empty "";
set $query_param ?justip=true&cli=true;
if ($http_user_agent ~* (curl|wget)) {
proxy_pass http://127.0.0.1:8080/ip$empty;
proxy_pass http://127.0.0.1:8080/query$query_param;
}
index index.html;
}
location = /ua {
location = /ip {
set $query_param ?justip=true;
if ($http_user_agent ~* (curl|wget)) {
return 200 $http_user_agent\n;
set $query_param $query_param&cli=true;
}
default_type application/json;
return 200 $http_user_agent;
proxy_pass http://127.0.0.1:8080/query$query_param;
}
location = /ip {
set $query justip=true;
location = /version {
set $query_param ?version=true;
if ($http_user_agent ~* (curl|wget)) {
set $query $query&cli=true;
set $query_param $query_param&cli=true;
}
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param QUERY_STRING $query;
fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php;
proxy_pass http://127.0.0.1:8080/query$query_param;
}
location ~* ^/([^/]+?)$ {
@ -40,24 +47,27 @@ server {
}
location ^~ /info {
set $is_cli 0;
set $is_legal 0;
set $query_param ?cli=false;
if ($http_user_agent ~* (curl|wget)) {
set $query_param ?cli=true;
}
if ($uri ~* ^/info/?$) {
set $is_legal 1;
}
if ($uri ~* ^/info/([^/]+?)$) {
set $is_legal 1;
set $query ip=$1;
set $query_param $query_param&ip=$1;
}
if ($is_legal = 0) {
return 404;
}
if ($http_user_agent ~* (curl|wget)) {
set $query $query&cli=true;
set $query_param $query_param&error=true;
}
proxy_pass http://127.0.0.1:8080/query$query_param;
}
location = /query {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param QUERY_STRING $query;
fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php;
}
}

57
conf/nginx/ip.conf

@ -22,39 +22,45 @@ server {
proxy_pass http://127.0.0.1:1601;
}
}
server {
listen 1601;
listen 127.0.0.1:1601;
root /var/www/echoIP;
error_page 403 404 500 501 502 503 504 = /error.html;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Real-IP;
location = /ua {
if ($http_user_agent ~* (curl|wget)) {
return 200 $http_user_agent\n;
}
default_type application/json;
return 200 "{\"ua\":\"$http_user_agent\"}";
}
location = / {
set $empty "";
set $query_param ?justip=true&cli=true;
if ($http_user_agent ~* (curl|wget)) {
proxy_pass http://127.0.0.1:1601/ip$empty;
proxy_pass http://127.0.0.1:1601/query$query_param;
}
index index.html;
}
location = /ua {
location = /ip {
set $query_param ?justip=true;
if ($http_user_agent ~* (curl|wget)) {
return 200 $http_user_agent\n;
set $query_param $query_param&cli=true;
}
default_type application/json;
return 200 $http_user_agent;
proxy_pass http://127.0.0.1:1601/query$query_param;
}
location = /ip {
set $query justip=true;
location = /version {
set $query_param ?version=true;
if ($http_user_agent ~* (curl|wget)) {
set $query $query&cli=true;
set $query_param $query_param&cli=true;
}
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; # php-fpm接口
fastcgi_param QUERY_STRING $query;
fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php;
proxy_pass http://127.0.0.1:1601/query$query_param;
}
location ~* ^/([^/]+?)$ {
@ -65,24 +71,27 @@ server {
}
location ^~ /info {
set $is_cli 0;
set $is_legal 0;
set $query_param ?cli=false;
if ($http_user_agent ~* (curl|wget)) {
set $query_param ?cli=true;
}
if ($uri ~* ^/info/?$) {
set $is_legal 1;
}
if ($uri ~* ^/info/([^/]+?)$) {
set $is_legal 1;
set $query ip=$1;
set $query_param $query_param&ip=$1;
}
if ($is_legal = 0) {
return 404;
}
if ($http_user_agent ~* (curl|wget)) {
set $query $query&cli=true;
set $query_param $query_param&error=true;
}
proxy_pass http://127.0.0.1:1601/query$query_param;
}
location = /query {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; # php-fpm接口
fastcgi_param QUERY_STRING $query;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/echoIP/backend/queryInfo.php;
}
}
}

1
error.html

@ -0,0 +1 @@
Page Not Found
Loading…
Cancel
Save