diff --git a/README.md b/README.md index c3aaf03..fb01bc3 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ shell> curl ip.343.re/8.8.8.8 ··· ``` -![echoIP-cli](https://pic.dnomd343.top/images/aDE.png) +![echoIP-cli](https://pic.dnomd343.top/images/pkr.png) ### 网页访问模式 @@ -42,6 +42,8 @@ shell> curl ip.343.re/8.8.8.8 ### Docker方式 +echoIP支持Docker容器部署,在[Docker Hub](https://hub.docker.com/repository/docker/dnomd343/echoip)或[Github Package](https://github.com/dnomd343?tab=packages&repo_name=echoIP)有打包完成的镜像下载。 + 确定你的服务器上有Docker环境 ``` @@ -218,6 +220,52 @@ shell> docker run -dit --name echoip -p 1601:8080 echoip shell> docker exec -it echoip bash ``` +### 部分接口 + +1. echoIP支持在URL中指定查询目标IP,格式形如 `https://ip.343.re?ip=1.2.3.4`,访问时自动显示该IP地址的信息。 + +2. echoIP后端支持返回当前版本信息,接口位于 `/version` 下,若请求来自命令行,则返回可视化格式,否则返回JSON数据。 + +``` +shell> curl ip.343.re/version +echoip -> v1.1 +qqwry.dat -> 2021-04-21 +ipip.net -> 2019-07-03 + +shell> curl https://ip.343.re/version --user-agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42' +{"echoip":"v1.1","qqwry.dat":"20210421","ipip.net":"20190703"} +``` + +3. echoIP后端统一接口为 `/query`,可请求以下参数 + +``` +error -> 请求出错 +version -> 获取版本数据 +cli -> 来自命令行下的请求 +justip -> 仅查询IP地址 +ip -> 请求指定IP的数据 +``` + +示例 + +``` +shell> curl "ip.343.re/query?justip=true" +{"ip":"116.57.98.121"} +shell> curl "ip.343.re/query?justip=true&cli=true" +116.57.98.124 +shell> curl "ip.343.re/query?cli=true&ip=7.7.7.7" +IP: 7.7.7.7 +AS: AS8003 +City: Atlantic City +Region: New Jersey +Country: US - United States(美国) +Timezone: America/New_York +Location: 39.3642,-74.4231 +ISP: Global Resource Systems, LLC +Scope: 7.0.0.0/8 +Detail: 美国俄亥俄州哥伦布市DoD网络信息中心 +``` + ### ipinfo.io 在线请求,格式为 `https://ipinfo.io/$IP/json`,返回指定IP对应的信息,形如: diff --git a/assets/js/main.js b/assets/js/main.js index 0a37646..4c1ed2a 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -14,7 +14,7 @@ $(document).ready(function() { $("button").text("Searching..."); $("table").hide(1000); $("input").val(trim($("input").val())); - if (checkIP($("input").val()) == "ok") { + if ($("input").val() == '' || checkIP($("input").val()) == "ok") { getInfo(); } else { errorIP(); diff --git a/assets/js/main.min.js b/assets/js/main.min.js index 4ec3768..ce2e05e 100644 --- a/assets/js/main.min.js +++ b/assets/js/main.min.js @@ -1 +1 @@ -function getInfo(){$.get("/info/"+$("input").val(),(function(data){if(console.log(data),"F"!=data.status)if($("input").val()||$("input").val(data.ip),$("button").text("Search"),$("table").show(1e3),$("#ip").text(data.ip),data.as=null==data.as?"Unknow":data.as,data.city=null==data.city?"Unknow":data.city,data.region=null==data.region?"Unknow":data.region,data.country=null==data.country?"Unknow":data.country,data.timezone=null==data.timezone?"Unknow":data.timezone,data.isp=null==data.isp?"Unknow":data.isp,data.scope=null==data.scope?"Unknow":data.scope,data.detail=null==data.detail||" "==data.detail?"Unknow":data.detail,$("#as").text(data.as),$("#city").text(data.city),$("#region").text(data.region),$("#country").text(data.country),$("#timezone").text(data.timezone),$("#isp").text(data.isp),$("#scope").text(data.scope),$("#detail").text(data.detail),null==data.loc)$("#loc").text("Unknow"),clear();else{var earthUri="https://earth.google.com/web/@"+data.loc+",0a,398836d,1y,0h,0t,0r";$("#loc").html(''+data.loc+""),draw(parseFloat(data.loc.split(",")[0]),parseFloat(data.loc.split(",")[1]))}else errorIP()}))}function trim(str){return str.replace(/(^\s*)|(\s*$)/g,"")}function errorIP(){$("button").text("Illegal IP"),$("button").css({"border-color":"#ff406f","background-color":"#ff406f"}),$("input").focus()}function checkIP(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\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){0,4}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$/,V4=ipStr.match(regIPv4),V6=ipStr.match(regIPv6);return null===V4&&null===V6?"error":"ok"}function getQuery(name){var reg=new RegExp("(^|&)"+name+"=([^&]*)(&|$)"),result=window.location.search.substr(1).match(reg);return null!=result?unescape(result[2]):null}function clear(){var map;new mapboxgl.Map({container:"map",style:"mapbox://styles/mapbox/streets-v11",center:[0,0],zoom:1}).on("load",(function(){console.log("reset map")}))}function draw(x,y){var map=new mapboxgl.Map({container:"map",style:"mapbox://styles/mapbox/streets-v11",center:[y,x],zoom:3}),size=100,pulsingDot={width:100,height:100,data:new Uint8Array(4e4),onAdd:function(){var canvas=document.createElement("canvas");canvas.width=this.width,canvas.height=this.height,this.context=canvas.getContext("2d")},render:function(){var duration=1e3,t=performance.now()%1e3/1e3,radius=15,outerRadius=35*t+15,context=this.context;return context.clearRect(0,0,this.width,this.height),context.beginPath(),context.arc(this.width/2,this.height/2,outerRadius,0,2*Math.PI),context.fillStyle="rgba(255, 200, 200,"+(1-t)+")",context.fill(),context.beginPath(),context.arc(this.width/2,this.height/2,15,0,2*Math.PI),context.fillStyle="rgba(255, 100, 100, 1)",context.strokeStyle="white",context.lineWidth=2+4*(1-t),context.fill(),context.stroke(),this.data=context.getImageData(0,0,this.width,this.height).data,map.triggerRepaint(),!0}};map.on("load",(function(){map.addImage("pulsing-dot",pulsingDot,{pixelRatio:2}),map.addLayer({id:"points",type:"symbol",source:{type:"geojson",data:{type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"Point",coordinates:[y,x]}}]}},layout:{"icon-image":"pulsing-dot"}})}))}$(document).ready((function(){null===getQuery("ip")?getInfo():($("input").val(getQuery("ip")),getInfo()),$("table").hide(),$("button").click((function(){$("button").css({"border-color":"","background-color":""}),$("button").text("Searching..."),$("table").hide(1e3),$("input").val(trim($("input").val())),"ok"==checkIP($("input").val())?getInfo():errorIP()}))})),$(document).keydown((function(event){13==event.keyCode&&$("button").focus()})),mapboxgl.accessToken="pk.eyJ1Ijoic2hldm9ua3VhbiIsImEiOiJja20yMjlnNDYybGg2Mm5zNW40eTNnNnUwIn0.6xj6sgjWvdQgT_7OQUy_Jg"; \ No newline at end of file +function getInfo(){$.get("/info/"+$("input").val(),(function(data){if(console.log(data),"F"!=data.status)if($("input").val()||$("input").val(data.ip),$("button").text("Search"),$("table").show(1e3),$("#ip").text(data.ip),data.as=null==data.as?"Unknow":data.as,data.city=null==data.city?"Unknow":data.city,data.region=null==data.region?"Unknow":data.region,data.country=null==data.country?"Unknow":data.country,data.timezone=null==data.timezone?"Unknow":data.timezone,data.isp=null==data.isp?"Unknow":data.isp,data.scope=null==data.scope?"Unknow":data.scope,data.detail=null==data.detail||" "==data.detail?"Unknow":data.detail,$("#as").text(data.as),$("#city").text(data.city),$("#region").text(data.region),$("#country").text(data.country),$("#timezone").text(data.timezone),$("#isp").text(data.isp),$("#scope").text(data.scope),$("#detail").text(data.detail),null==data.loc)$("#loc").text("Unknow"),clear();else{var earthUri="https://earth.google.com/web/@"+data.loc+",0a,398836d,1y,0h,0t,0r";$("#loc").html(''+data.loc+""),draw(parseFloat(data.loc.split(",")[0]),parseFloat(data.loc.split(",")[1]))}else errorIP()}))}function trim(str){return str.replace(/(^\s*)|(\s*$)/g,"")}function errorIP(){$("button").text("Illegal IP"),$("button").css({"border-color":"#ff406f","background-color":"#ff406f"}),$("input").focus()}function checkIP(ipStr){if(null===ipStr)return"error";var regIPv4=/^((25[0-5]|2[0-4]\d|[01]?\d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-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){0,4}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$/,V4=ipStr.match(regIPv4),V6=ipStr.match(regIPv6);return null===V4&&null===V6?"error":"ok"}function getQuery(name){var reg=new RegExp("(^|&)"+name+"=([^&]*)(&|$)"),result=window.location.search.substr(1).match(reg);return null!=result?unescape(result[2]):null}function clear(){var map;new mapboxgl.Map({container:"map",style:"mapbox://styles/mapbox/streets-v11",center:[0,0],zoom:1}).on("load",(function(){console.log("reset map")}))}function draw(x,y){var map=new mapboxgl.Map({container:"map",style:"mapbox://styles/mapbox/streets-v11",center:[y,x],zoom:3}),size=100,pulsingDot={width:100,height:100,data:new Uint8Array(4e4),onAdd:function(){var canvas=document.createElement("canvas");canvas.width=this.width,canvas.height=this.height,this.context=canvas.getContext("2d")},render:function(){var duration=1e3,t=performance.now()%1e3/1e3,radius=15,outerRadius=35*t+15,context=this.context;return context.clearRect(0,0,this.width,this.height),context.beginPath(),context.arc(this.width/2,this.height/2,outerRadius,0,2*Math.PI),context.fillStyle="rgba(255, 200, 200,"+(1-t)+")",context.fill(),context.beginPath(),context.arc(this.width/2,this.height/2,15,0,2*Math.PI),context.fillStyle="rgba(255, 100, 100, 1)",context.strokeStyle="white",context.lineWidth=2+4*(1-t),context.fill(),context.stroke(),this.data=context.getImageData(0,0,this.width,this.height).data,map.triggerRepaint(),!0}};map.on("load",(function(){map.addImage("pulsing-dot",pulsingDot,{pixelRatio:2}),map.addLayer({id:"points",type:"symbol",source:{type:"geojson",data:{type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"Point",coordinates:[y,x]}}]}},layout:{"icon-image":"pulsing-dot"}})}))}$(document).ready((function(){null===getQuery("ip")?getInfo():($("input").val(getQuery("ip")),getInfo()),$("table").hide(),$("button").click((function(){$("button").css({"border-color":"","background-color":""}),$("button").text("Searching..."),$("table").hide(1e3),$("input").val(trim($("input").val())),""==$("input").val()||"ok"==checkIP($("input").val())?getInfo():errorIP()}))})),$(document).keydown((function(event){13==event.keyCode&&$("button").focus()})),mapboxgl.accessToken="pk.eyJ1Ijoic2hldm9ua3VhbiIsImEiOiJja20yMjlnNDYybGg2Mm5zNW40eTNnNnUwIn0.6xj6sgjWvdQgT_7OQUy_Jg"; \ No newline at end of file diff --git a/backend/country.php b/backend/country.php index 1b87572..4b74656 100644 --- a/backend/country.php +++ b/backend/country.php @@ -15,18 +15,18 @@ class countryDB extends SQLite3 { } } -function getCountry($code) { // 根据两位国家代码获取英文与中文全称 +function getCountry($code) { // 根据两位国家代码获取英文与中文全称 if ($code == null) { return null; } $db = new countryDB; - $raw = $db->query('SELECT * FROM main WHERE alpha_2="'.$code.'";')->fetchArray(SQLITE3_ASSOC); + $raw = $db->query('SELECT * FROM main WHERE alpha_2=\'' . $code . '\';')->fetchArray(SQLITE3_ASSOC); $data['code'] = $code; if ($raw) { $data['en'] = $raw['name_en']; $data['cn'] = $raw['name_cn']; } else { - $data['en'] = "Unknow"; + $data['en'] = null; $data['cn'] = null; } return $data; diff --git a/backend/queryInfo.php b/backend/queryInfo.php index 7790a5f..f9c5a21 100644 --- a/backend/queryInfo.php +++ b/backend/queryInfo.php @@ -31,8 +31,11 @@ function getIPInfo($ip) { $info['city'] = $data['city']; $info['region'] = $data['region']; if (isset($data['country'])) { - $info['country'] = $data['country'] . ' - ' . $country['en']; - $info['country'] .= "(" . $country['cn'] . ")"; + $info['country'] = $data['country']; + if (isset($country['en'])) { + $info['country'] .= ' - ' . $country['en']; + $info['country'] .= "(" . $country['cn'] . ")"; + } } $info['timezone'] = $data['timezone']; $info['loc'] = $data['loc']; diff --git a/conf/nginx/docker.conf b/conf/nginx/docker.conf index 58bb4b2..7e53dd9 100644 --- a/conf/nginx/docker.conf +++ b/conf/nginx/docker.conf @@ -2,7 +2,7 @@ server { listen 8080; root /var/www/echoIP; - error_page 403 404 500 501 502 503 504 = /error.html; + error_page 403 404 = /error.html; set_real_ip_from 0.0.0.0/0; real_ip_header X-Real-IP; diff --git a/conf/nginx/ip.conf b/conf/nginx/ip.conf index 098743b..e36fbb6 100644 --- a/conf/nginx/ip.conf +++ b/conf/nginx/ip.conf @@ -27,7 +27,7 @@ server { listen 127.0.0.1:1601; root /var/www/echoIP; - error_page 403 404 500 501 502 503 504 = /error.html; + error_page 403 404 = /error.html; set_real_ip_from 0.0.0.0/0; real_ip_header X-Real-IP; diff --git a/error.html b/error.html index f8baf66..8d308d9 100644 --- a/error.html +++ b/error.html @@ -8,8 +8,8 @@ Dnomd343 - IP - - + +