+json = require "json"
+
+_config = {}
+
+function _config.read(file)
+
+ local file = file or "/opt/squid-auth-helper/config.json"
+ local f = assert(io.open(file))
+ local cfg = json.decode(f:read("*all"))
+
+ _config.pmap = cfg["portmap"] or {}
+
+ _config.ident_timeout = 2
+ _config.ident_default = nil
+
+ if cfg["ident"] then
+ _config.ident_timeout = cfg["ident"]["timeout"] or _config.ident_timeout
+ _config.ident_default = cfg["ident"]["default"] or _config.ident_default
+ end
+
+ _config.ipmap = {}
+ _config.macmap = {}
+
+ if cfg["hosts"] then
+ if cfg["hosts"]["ip"] then
+ for k, v in pairs(cfg["hosts"]["ip"]) do
+ local k, n = string.gsub(k, "%.", "%%.")
+ local k, n = string.gsub(k,"%*",".*")
+ _config.ipmap["^"..k.."$"] = v
+ end
+ end
+ if cfg["hosts"]["mac"] then
+ for k, v in pairs(cfg["hosts"]["mac"]) do
+ local k, n = string.gsub(k,"%*",".*")
+ _config.macmap["^"..k.."$"] = v
+ end
+ end
+ end
+
+end
+
+function _config.map_port(port)
+ return _config.pmap[tostring(port)] or port
+end
+
+function _config.map_ip(ip)
+ for k,v in pairs(_config.ipmap) do
+ if string.match(ip,k) then
+ return v
+ end
+ end
+ return nil
+end
+
+function _config.map_mac(mac)
+ for k,v in pairs(_config.macmap) do
+ if string.match(mac,k) then
+ return v
+ end
+ end
+ return nil
+end
+
+return _config