|
| 1 | +############################################################################### |
| 2 | +# # |
| 3 | +# Telize 1.05 # |
| 4 | +# Copyright (c) 2013-2016, Frederic Cambus # |
| 5 | +# http://www.telize.com # |
| 6 | +# # |
| 7 | +# Created: 2013-08-15 # |
| 8 | +# Last Updated: 2016-01-05 # |
| 9 | +# # |
| 10 | +# Telize is released under the BSD 3-Clause license. # |
| 11 | +# See LICENSE file for details. # |
| 12 | +# # |
| 13 | +############################################################################### |
| 14 | + |
| 15 | +server { |
| 16 | + # Configuration variables |
| 17 | + set $cors "true"; |
| 18 | + set $cors_origin "*"; |
| 19 | + |
| 20 | + server_name 127.0.0.1; |
| 21 | + |
| 22 | + # Uncomment when using Telize behind a load balancer |
| 23 | + # set_real_ip_from 10.0.0.0/8; # Put your load balancer IP range here |
| 24 | + # real_ip_header X-Forwarded-For; |
| 25 | + |
| 26 | + charset_types application/json; |
| 27 | + |
| 28 | + keepalive_timeout 0; |
| 29 | + gzip off; |
| 30 | + |
| 31 | + ## Deny illegal Host headers |
| 32 | + if ($http_referer ~* ^(rwandair.com|m.rwandair.com|stage-rwandair.mobiashara.com)$ ) { |
| 33 | + return 444; |
| 34 | + } |
| 35 | + |
| 36 | + location ~ /ip$ { |
| 37 | + add_header Cache-Control no-cache; |
| 38 | + |
| 39 | + charset off; |
| 40 | + default_type text/plain; |
| 41 | + |
| 42 | + echo $remote_addr; |
| 43 | + } |
| 44 | + |
| 45 | + location ~ /jsonip$ { |
| 46 | + charset utf-8; |
| 47 | + default_type application/json; |
| 48 | + |
| 49 | + content_by_lua ' |
| 50 | + local cjson = require("cjson") |
| 51 | + |
| 52 | + local json = cjson.encode({ |
| 53 | + ip = ngx.var.remote_addr |
| 54 | + }) |
| 55 | + |
| 56 | + local callback = ngx.var.arg_callback |
| 57 | + |
| 58 | + if callback then |
| 59 | + ngx.say(callback, "(", json, ");") |
| 60 | + else |
| 61 | + ngx.say(json) |
| 62 | + end'; |
| 63 | + } |
| 64 | + |
| 65 | + location ~ /geoip/?(?<ip>.*) { |
| 66 | + if ($ip = "") { |
| 67 | + set $ip $remote_addr; |
| 68 | + } |
| 69 | + |
| 70 | + # Uncomment when using Telize behind a load balancer, and comment the directive setting X-Real-IP |
| 71 | + # proxy_set_header X-Forwarded-For $ip; |
| 72 | + |
| 73 | + proxy_set_header X-Real-IP $ip; |
| 74 | + proxy_set_header Host $host; |
| 75 | + proxy_pass $scheme://127.0.0.1/jsonify?callback=$arg_callback; |
| 76 | + } |
| 77 | + |
| 78 | + location /jsonify { |
| 79 | + set_real_ip_from 127.0.0.1; |
| 80 | + |
| 81 | + access_log off; |
| 82 | + |
| 83 | + charset utf-8; |
| 84 | + default_type application/json; |
| 85 | + |
| 86 | + if ($cors = "true") { |
| 87 | + add_header Access-Control-Allow-Origin $cors_origin; |
| 88 | + } |
| 89 | + |
| 90 | + more_set_headers "Cache-Control: no-cache"; |
| 91 | + |
| 92 | + content_by_lua ' |
| 93 | + local cjson = require("cjson") |
| 94 | + local iconv = require("iconv") |
| 95 | + local cd = iconv.new("utf-8","iso-8859-15") |
| 96 | + |
| 97 | + -- Check for invalid IP addresses |
| 98 | + if ngx.var.remote_addr == "127.0.0.1" then |
| 99 | + ngx.status = ngx.HTTP_BAD_REQUEST |
| 100 | + ngx.say(cjson.encode({code = 401, message = "Input string is not a valid IP address"})) |
| 101 | + ngx.exit(ngx.HTTP_OK) |
| 102 | + end |
| 103 | + |
| 104 | + local payload = { |
| 105 | + ip = ngx.var.remote_addr, |
| 106 | + country_code = ngx.var.geoip_city_country_code, |
| 107 | + country_code3 = ngx.var.geoip_city_country_code3, |
| 108 | + country = ngx.var.geoip_city_country_name, |
| 109 | + region = ngx.var.geoip_region_name, |
| 110 | + region_code = ngx.var.geoip_region, |
| 111 | + city = ngx.var.geoip_city, |
| 112 | + postal_code = ngx.var.geoip_postal_code, |
| 113 | + continent_code = ngx.var.geoip_city_continent_code, |
| 114 | + latitude = ngx.var.geoip_latitude, |
| 115 | + longitude = ngx.var.geoip_longitude, |
| 116 | + dma_code = ngx.var.geoip_dma_code, |
| 117 | + area_code = ngx.var.geoip_area_code, |
| 118 | + organization = ngx.var.geoip_org, |
| 119 | + timezone = ngx.var.geoip_timezone, |
| 120 | + offset = ngx.var.geoip_timezone_offset, |
| 121 | + } |
| 122 | + |
| 123 | + local callback = ngx.var.arg_callback |
| 124 | + |
| 125 | + -- Validate payload |
| 126 | + for item, value in pairs(payload) do |
| 127 | + if payload[item] == "" then |
| 128 | + payload[item] = nil |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + -- Convert latitude and longitude to numeric values |
| 133 | + if payload.latitude ~= nil and payload.longitude ~= nil then |
| 134 | + payload.latitude = tonumber(payload.latitude) |
| 135 | + payload.longitude = tonumber(payload.longitude) |
| 136 | + end |
| 137 | + |
| 138 | + -- Convert city name to UTF-8 if it exists |
| 139 | + if payload.city ~= nil then |
| 140 | + payload.city = cd:iconv(payload.city) |
| 141 | + end |
| 142 | + |
| 143 | + -- Convert region name to UTF-8 if it exists |
| 144 | + if payload.region ~= nil then |
| 145 | + payload.region = cd:iconv(payload.region) |
| 146 | + end |
| 147 | + |
| 148 | + -- Convert organization name to UTF-8 if it exists |
| 149 | + if payload.organization ~= nil then |
| 150 | + payload.organization = cd:iconv(payload.organization) |
| 151 | + end |
| 152 | + |
| 153 | + local json = cjson.encode(payload) |
| 154 | + |
| 155 | + if callback ~= "" then |
| 156 | + ngx.say(callback, "(", json, ");") |
| 157 | + else |
| 158 | + ngx.say(json) |
| 159 | + end'; |
| 160 | + } |
| 161 | +} |
0 commit comments