forked from fcambus/telize
-
Notifications
You must be signed in to change notification settings - Fork 3
/
telize
108 lines (90 loc) · 3.18 KB
/
telize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
###############################################################################
# #
# Telize 1.02 (c) by Frederic Cambus 2013-2014 #
# http://www.telize.com #
# #
# Created: 2013/08/15 #
# Last Updated: 2014/09/01 #
# #
# Telize is released under the BSD 3-Clause license. #
# See LICENSE file for details. #
# #
###############################################################################
server {
# Configuration variables
set $cors "true";
set $cors_origin "*";
charset utf-8;
charset_types application/json;
keepalive_timeout 0;
location /v1/telize-service/status {
stub_status;
}
location /v1/telize-service/geoip {
default_type application/json;
if ($geoip_org ~* "^(AS[0-9]+) (.+)") {
set $asn $1;
set $isp $2;
}
if ($cors = "true") {
add_header Access-Control-Allow-Origin $cors_origin;
}
more_set_headers "Cache-Control: no-cache";
content_by_lua '
local cjson = require("cjson")
local iconv = require("iconv")
local cd = iconv.new("utf-8","iso-8859-15")
local callback = ngx.var.arg_callback
local args = {
ip = ngx.var.remote_addr,
country_code = ngx.var.geoip_city_country_code,
country_code3 = ngx.var.geoip_city_country_code3,
country = ngx.var.geoip_city_country_name,
region = ngx.var.geoip_region_name,
region_code = ngx.var.geoip_region,
city = ngx.var.geoip_city,
postal_code = ngx.var.geoip_postal_code,
continent_code = ngx.var.geoip_city_continent_code,
latitude = ngx.var.geoip_latitude,
longitude = ngx.var.geoip_longitude,
dma_code = ngx.var.geoip_dma_code,
area_code = ngx.var.geoip_area_code,
timezone = ngx.var.geoip_timezone,
offset = ngx.var.geoip_timezone_offset,
asn = ngx.var.asn,
isp = ngx.var.isp
}
-- Validate args
for item, value in pairs(args) do
if args[item] == "" then
args[item] = nil
elseif item == "latitude" or item == "longitude" then
args[item] = tonumber(value)
end
end
-- Convert city name to UTF-8 if it exists
if args.city ~= nil then
args.city = cd:iconv(args.city)
end
-- Convert region name to UTF-8 if it exists
if args.region ~= nil then
args.region = cd:iconv(args.region)
end
-- Convert isp name to UTF-8 if it exists
if args.isp ~= nil then
args.isp = cd:iconv(args.isp)
end
if args.ip == "127.0.0.1" then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say(cjson.encode({code = 401, message = "Invalid IP address"}))
ngx.exit(ngx.HTTP_OK)
end
local json = cjson.encode(args)
if (callback ~= nil and callback ~= "") then
ngx.header.content_type = "application/javascript"
ngx.say(callback, "(", json, ");")
else
ngx.say(json)
end';
}
}