geoip.lua

Mon, 04 Jan 2016 17:03:19 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 17:03:19 +0000
changeset 18
8050134f35b3
parent 16
d35376a53644
child 19
fd7b581560d5
permissions
-rw-r--r--

clients: Include request in the new-client event

pcall(function ()
	local city_db_path = os.getenv("GEOIP_CITY_DB");
	if not city_db_path then return; end
	
	local city_db = assert(require "geoip.city".open(city_db_path));
	
	events.add_handler("new-client", function (info)
		local ip = info.conn:ip();
		local location = city_db:query_by_addr(ip);
		if location then
			local l = {};
			if location.city_name then
				table.insert(l, location.city_name);
			end
			if location.region then
				table.insert(l, location.region);
			end
			if location.country_name then
				table.insert(l, location.country_name);
			end
			conn.location = table.concat(l, ", ");
		end
	end);
end)

mercurial