Первый вариант. Внешний ACL-helper для идентификации пользователей прозрачного squid...
[lua-squid-acl-helper.git] / src / config.lua
1 json = require "json"
2
3 _config = {}
4
5 function _config.read(file)
6
7   local file = file or "/opt/squid-auth-helper/config.json"
8   local f = assert(io.open(file))
9   local cfg = json.decode(f:read("*all"))
10
11   _config.pmap = cfg["portmap"] or {}  
12
13   _config.ident_timeout = 2
14   _config.ident_default = nil
15
16   if cfg["ident"] then
17     _config.ident_timeout = cfg["ident"]["timeout"] or _config.ident_timeout
18     _config.ident_default = cfg["ident"]["default"] or _config.ident_default
19   end
20
21   _config.ipmap = {}
22   _config.macmap = {}
23   
24   if cfg["hosts"] then
25     if cfg["hosts"]["ip"] then
26       for k, v in pairs(cfg["hosts"]["ip"]) do
27         local k, n = string.gsub(k, "%.", "%%.")
28         local k, n = string.gsub(k,"%*",".*")
29         _config.ipmap["^"..k.."$"] = v 
30       end
31     end
32     if cfg["hosts"]["mac"] then
33       for k, v in pairs(cfg["hosts"]["mac"]) do
34         local k, n = string.gsub(k,"%*",".*")
35         _config.macmap["^"..k.."$"] = v 
36       end
37     end 
38   end
39   
40 end
41
42 function _config.map_port(port)
43   return _config.pmap[tostring(port)] or port
44 end
45
46 function _config.map_ip(ip)
47   for k,v in pairs(_config.ipmap) do
48     if string.match(ip,k) then
49       return v
50     end  
51   end
52   return nil
53 end
54
55 function _config.map_mac(mac)
56   for k,v in pairs(_config.macmap) do
57     if string.match(mac,k) then
58       return v
59     end  
60   end
61   return nil
62 end
63
64 return _config