2016-01-05 21:53:16 +01:00
|
|
|
###############################################################################
|
|
|
|
# #
|
2018-03-23 22:14:50 +01:00
|
|
|
# Telize 2.0.0 #
|
|
|
|
# Copyright (c) 2013-2018, Frederic Cambus #
|
2017-06-05 22:20:42 +02:00
|
|
|
# https://www.telize.com #
|
2016-01-05 21:53:16 +01:00
|
|
|
# #
|
|
|
|
# Created: 2013-08-15 #
|
2018-03-23 22:14:50 +01:00
|
|
|
# Last Updated: 2018-03-15 #
|
2016-01-05 21:53:16 +01:00
|
|
|
# #
|
2017-06-05 22:20:42 +02:00
|
|
|
# Telize is released under the BSD 2-Clause license. #
|
2016-01-05 21:53:16 +01:00
|
|
|
# See LICENSE file for details. #
|
|
|
|
# #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
server {
|
|
|
|
# Configuration variables
|
|
|
|
set $cors "true";
|
|
|
|
set $cors_origin "*";
|
|
|
|
|
|
|
|
server_name 127.0.0.1;
|
|
|
|
|
|
|
|
# Uncomment when using Telize behind a load balancer
|
|
|
|
# set_real_ip_from 10.0.0.0/8; # Put your load balancer IP range here
|
|
|
|
# real_ip_header X-Forwarded-For;
|
|
|
|
|
|
|
|
charset_types application/json;
|
|
|
|
|
|
|
|
keepalive_timeout 0;
|
|
|
|
gzip off;
|
|
|
|
|
|
|
|
location ~ /ip$ {
|
|
|
|
charset off;
|
|
|
|
default_type text/plain;
|
|
|
|
|
2018-03-23 22:14:50 +01:00
|
|
|
add_header Cache-Control no-cache;
|
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
content_by_lua_block {
|
|
|
|
ngx.say(ngx.var.remote_addr)
|
|
|
|
}
|
2016-01-05 21:53:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
location ~ /jsonip$ {
|
|
|
|
charset utf-8;
|
|
|
|
default_type application/json;
|
|
|
|
|
2018-03-23 22:14:50 +01:00
|
|
|
if ($cors = "true") {
|
|
|
|
add_header Access-Control-Allow-Origin $cors_origin;
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
content_by_lua_block {
|
2018-03-23 22:14:50 +01:00
|
|
|
local cjson = require "cjson"
|
|
|
|
|
|
|
|
ngx.header["Cache-Control"] = "no-cache";
|
2016-01-05 21:53:16 +01:00
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
local json = cjson.encode({
|
|
|
|
ip = ngx.var.remote_addr
|
|
|
|
})
|
2016-01-05 21:53:16 +01:00
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
local callback = ngx.var.arg_callback
|
2016-01-05 21:53:16 +01:00
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
if callback then
|
|
|
|
ngx.say(callback, "(", json, ");")
|
|
|
|
else
|
|
|
|
ngx.say(json)
|
|
|
|
end
|
|
|
|
}
|
2016-01-05 21:53:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
location ~ /geoip/?(?<ip>.*) {
|
|
|
|
if ($ip = "") {
|
|
|
|
set $ip $remote_addr;
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
# Uncomment when using Telize behind a load balancer, and
|
|
|
|
# comment the directive setting X-Real-IP
|
2016-01-05 21:53:16 +01:00
|
|
|
# proxy_set_header X-Forwarded-For $ip;
|
|
|
|
|
|
|
|
proxy_set_header X-Real-IP $ip;
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
proxy_pass $scheme://127.0.0.1/jsonify?callback=$arg_callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
location /jsonify {
|
|
|
|
charset utf-8;
|
|
|
|
default_type application/json;
|
|
|
|
|
|
|
|
if ($cors = "true") {
|
|
|
|
add_header Access-Control-Allow-Origin $cors_origin;
|
|
|
|
}
|
|
|
|
|
2018-03-23 22:14:50 +01:00
|
|
|
set_real_ip_from 127.0.0.1;
|
|
|
|
|
|
|
|
access_log off;
|
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
content_by_lua_block {
|
2018-03-23 22:14:50 +01:00
|
|
|
local cjson = require "cjson"
|
2017-06-05 22:20:42 +02:00
|
|
|
|
|
|
|
ngx.header["Cache-Control"] = "no-cache";
|
|
|
|
|
|
|
|
-- Check for invalid IP addresses
|
|
|
|
if ngx.var.remote_addr == "127.0.0.1" then
|
|
|
|
ngx.status = ngx.HTTP_BAD_REQUEST
|
|
|
|
ngx.say(cjson.encode({
|
|
|
|
code = 401,
|
|
|
|
message = "Input string is not a valid IP address"
|
|
|
|
}))
|
|
|
|
ngx.exit(ngx.HTTP_OK)
|
|
|
|
end
|
|
|
|
|
|
|
|
local payload = {
|
|
|
|
ip = ngx.var.remote_addr,
|
2018-03-23 22:14:50 +01:00
|
|
|
continent_code = ngx.var.geoip2_continent_code,
|
|
|
|
country = ngx.var.geoip2_country,
|
|
|
|
country_code = ngx.var.geoip2_country_code,
|
|
|
|
country_code3 = ngx.var.geoip2_country_code3,
|
|
|
|
region = ngx.var.geoip2_region,
|
|
|
|
region_code = ngx.var.geoip2_region_code,
|
|
|
|
city = ngx.var.geoip2_city,
|
|
|
|
postal_code = ngx.var.geoip2_postal_code,
|
|
|
|
latitude = ngx.var.geoip2_latitude,
|
|
|
|
longitude = ngx.var.geoip2_longitude,
|
|
|
|
timezone = ngx.var.geoip2_timezone,
|
|
|
|
offset = ngx.var.geoip2_offset,
|
|
|
|
asn = ngx.var.geoip2_asn,
|
|
|
|
organization = ngx.var.geoip2_organization,
|
2017-06-05 22:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-- Validate payload
|
|
|
|
for item, value in pairs(payload) do
|
|
|
|
if payload[item] == "" then
|
|
|
|
payload[item] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Convert latitude and longitude to numeric values
|
|
|
|
if payload.latitude ~= nil and payload.longitude ~= nil then
|
|
|
|
payload.latitude = tonumber(payload.latitude)
|
|
|
|
payload.longitude = tonumber(payload.longitude)
|
|
|
|
end
|
2016-01-05 21:53:16 +01:00
|
|
|
|
2018-03-23 22:14:50 +01:00
|
|
|
-- Convert timezone offset to numeric value
|
|
|
|
if payload.offset ~= nil then
|
|
|
|
payload.offset = tonumber(payload.offset)
|
2016-01-05 21:53:16 +01:00
|
|
|
end
|
2017-06-05 22:20:42 +02:00
|
|
|
|
2018-03-23 22:14:50 +01:00
|
|
|
-- Convert ASN to numeric value
|
|
|
|
if payload.asn ~= nil then
|
|
|
|
payload.asn = tonumber(payload.asn)
|
2017-06-05 22:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local json = cjson.encode(payload)
|
|
|
|
|
2018-03-23 22:14:50 +01:00
|
|
|
local callback = ngx.var.arg_callback
|
|
|
|
|
2017-06-05 22:20:42 +02:00
|
|
|
if callback ~= "" then
|
|
|
|
ngx.say(callback, "(", json, ");")
|
|
|
|
else
|
|
|
|
ngx.say(json)
|
|
|
|
end
|
|
|
|
}
|
2016-01-05 21:53:16 +01:00
|
|
|
}
|
|
|
|
}
|