Create a gist now

Instantly share code, notes, and snippets.

@fffonion /Readme.md
Last active Jul 26, 2017

What would you like to do?
Simple GeoIP and ASNnum query server http://ip.yooooo.us
  • nginx with ngx_http_geoip_module, echo-nginx-module, lua-nginx-module is required; libgeoip needs to be installed and geoip database should be placed under /usr/share/GeoIP.
  • http://example.com for current GeoIP and http://example.com/ip for current IP only
  • http://example.com/x.x.x.x to query any IP.
  • http://example.com/domain-name for GeoIP of domain-name and http://example.com/domain-name/ip to return IP of domain-name only. If multiple IPs are set to single domain-name, all of them will be returned. Only A record will be used.
  • Sample requests are:
$ curl http://example.com/
x.x.x.x
Country, City
ASN number

$ curl http://example.com/ip
x.x.x.x

$ curl http://example.com/74.125.203.199
74.125.203.199
United States, Mountain View
AS15169 Google Inc.

$ curl http://example.com/www.google.com.hk
74.125.203.199
United States, Mountain View
AS15169 Google Inc.

$ curl http://example.com/www.google.com.hk/ip
74.125.203.199

$ curl http://example.com/www.google.com.hk/dns
www-wide.l.google.com 74.125.203.199

  • Run update_geoip.sh with root privilege to update GeoIP databases.
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
geoip_org /usr/share/GeoIP/GeoIPASNum.dat;
geoip_proxy 127.0.0.1;
log_format app_log '$remote_addr [$time_local] "$request" $status $body_bytes_sent '
'$request_time $http_host "$http_referer" $http_user_agent';
map $remote_addr $app_loggable {
default 1;
"127.0.0.1" 0;
"::1" 0;
}
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/ip-access.log app_log if=$app_loggable;
error_log /var/log/nginx/ip-error.log warn;
location = /{
add_header "Content-Type" "text/plain";
content_by_lua_block {
ngx.say(ngx.var.http_x_forwarded_for or ngx.var.remote_addr)
if ngx.var.geoip_country_name ~= nil then
ngx.say(ngx.var.geoip_country_name, ", ", ngx.var.geoip_city or "", "\n", ngx.var.geoip_org)
end
}
}
location ~ ^/([\d\.\:]+)$ {
set_by_lua $query_ip '
local m, err = ngx.re.match(ngx.var.uri, "^/(.+)")
if m then
ngx.log(ngx.INFO, "ip", m[1])
return m[1]
end
';
set $up "http://127.0.0.1/";
proxy_set_header X-Forwarded-For $query_ip;
proxy_set_header Host $http_host;
proxy_pass $up;
}
location = /ip {
add_header "Content-Type" "text/plain";
echo $remote_addr;
}
location ~ /([^/]+)/*([^/]*)$ {
set $query_host $1;
set $qopt $2;
content_by_lua_block {
local resolver = require "resty.dns.resolver"
local r, err = resolver:new {
nameservers = {"8.8.8.8", {"8.8.4.4", 53} },
retrans = 5,
timeout = 2000
}
if not r then
ngx.log(ngx.ERR, "can't new resolver", err)
ngx.exit(500)
end
local answers, err = r:query(ngx.var.query_host)
if not answers then
ngx.log(ngx.ERR, "query error", ngx.var.query_host, ":", err, " answer: ", answers.errcode, answers.errstr)
ngx.exit(200)
elseif #answers == 0 then
ngx.exit(200)
end
if string.len(ngx.var.qopt) > 0 then
table.foreach(answers, function(i, a)
if ngx.var.qopt == "dns" and a.cname then
ngx.print(a.cname.. " ")
end
if a.address then
ngx.say(a.address)
end
end)
else
local _ = {}
table.foreach(answers, function(i, a)
if a.address then
table.insert(_, {"/"..a.address})
end
end)
if #_ == 0 then
ngx.exit(200)
end
local resps = { ngx.location.capture_multi(_) }
for i, resp in ipairs(resps) do
ngx.say(resp.body)
end
end
}
}
}
wget -q http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz -O - |gzip -d > /usr/share/GeoIP/GeoIPASNum.dat.new && mv /usr/share/GeoIP/GeoIPASNum.dat{.new,}
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -O - |gzip -d > /usr/share/GeoIP/GeoLiteCity.dat.new && mv /usr/share/GeoIP/GeoLiteCity.dat{.new,}
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz -O - |gzip -d > /usr/share/GeoIP/GeoIP.dat.new && mv /usr/share/GeoIP/GeoIP.dat{.new,}
wget -q http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz -O - |gzip -d > /usr/share/GeoIP/GeoIPv6.dat.new && mv /usr/share/GeoIP/GeoIPv6.dat{.new,}
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz -O - |gzip -d > /usr/share/GeoIP/GeoLiteCityv6.dat.new && mv /usr/share/GeoIP/GeoLiteCityv6.dat{.new,}
wget -q http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz -O - |gzip -d > /usr/share/GeoIP/GeoIPASNumv6.dat.new && mv /usr/share/GeoIP/GeoIPASNumv6.dat{.new,}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment