geoip.lua

changeset 20
fed4cf6b1f43
parent 19
fd7b581560d5
child 21
3a89f5722626
equal deleted inserted replaced
19:fd7b581560d5 20:fed4cf6b1f43
1 local log = require "util.logger".init("geoip"); 1 local log = require "util.logger".init("geoip");
2 2
3 local ok, err = pcall(function () 3 local ok, err = pcall(function ()
4 local city_db_path = os.getenv("GEOIP_CITY_DB"); 4 local city_db_path = os.getenv("GEOIP_CITY_DB");
5 if not city_db_path then return; end 5 if not city_db_path then
6 log("debug", "GEOIP_CITY_DB not set");
7 return;
8 end
6 9
10 log("debug", "Loading geoip database");
7 local city_db = assert(require "geoip.city".open(city_db_path)); 11 local city_db = assert(require "geoip.city".open(city_db_path));
8 12 log("debug", "Loaded geoip database successfully");
13
9 events.add_handler("new-client", function (info) 14 events.add_handler("new-client", function (info)
10 local ip = info.conn:ip(); 15 local ip = info.conn:ip();
11 local location = city_db:query_by_addr(ip); 16 local location = city_db:query_by_addr(ip);
12 if location then 17 if location then
13 local l = {}; 18 local l = {};
19 end 24 end
20 if location.country_name then 25 if location.country_name then
21 table.insert(l, location.country_name); 26 table.insert(l, location.country_name);
22 end 27 end
23 conn.location = table.concat(l, ", "); 28 conn.location = table.concat(l, ", ");
29 else
30 log("debug", "Failed to find location for IP %q: %s", tostring(ip), tostring(err));
24 end 31 end
25 end); 32 end);
26 end) 33 end)
27 34
28 if not ok then 35 if not ok then

mercurial