prometheus-node-exporter-lua: add dawn exporter
[feed/packages.git] / net / wifidog-ng / files / wifidog-ng / ubus.lua
1 --[[
2 Copyright (C) 2018 Jianhui Zhao <jianhuizhao329@gmail.com>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 USA
18 --]]
19
20 local uci = require "uci"
21 local ubus = require "ubus"
22 local http = require "socket.http"
23 local auth = require "wifidog-ng.auth"
24 local config = require "wifidog-ng.config"
25
26
27 local M = {}
28
29 local conn = nil
30
31 local ubus_codes = {
32 ["INVALID_COMMAND"] = 1,
33 ["INVALID_ARGUMENT"] = 2,
34 ["METHOD_NOT_FOUND"] = 3,
35 ["NOT_FOUND"] = 4,
36 ["NO_DATA"] = 5,
37 ["PERMISSION_DENIED"] = 6,
38 ["TIMEOUT"] = 7,
39 ["NOT_SUPPORTED"] = 8,
40 ["UNKNOWN_ERROR"] = 9,
41 ["CONNECTION_FAILED"] = 10
42 }
43
44 local function reload_validated_domain()
45 local c = uci.cursor()
46
47 local file = io.open("/tmp/dnsmasq.d/wifidog-ng", "w")
48
49 c:foreach("wifidog-ng", "validated_domain", function(s)
50 file:write("ipset=/" .. s.domain .. "/wifidog-ng-ip\n")
51 end)
52 file:close()
53
54 os.execute("/etc/init.d/dnsmasq restart &")
55 end
56
57 local methods = {
58 ["wifidog-ng"] = {
59 roam = {
60 function(req, msg)
61 local cfg = config.get()
62
63 if not msg.ip or not msg.mac then
64 return ubus_codes["INVALID_ARGUMENT"]
65 end
66
67 local url = string.format("%s&stage=roam&ip=%s&mac=%s", cfg.auth_url, msg.ip, msg.mac)
68 local r = http.request(url) or ""
69 local token = r:match("token=(%w+)")
70 if token then
71 auth.new_term(msg.ip, msg.mac, token)
72 end
73 end, {ip = ubus.STRING, mac = ubus.STRING }
74 },
75 term = {
76 function(req, msg)
77 if msg.action == "show" then
78 conn:reply(req, {terms = auth.get_terms()});
79 return
80 end
81
82 if not msg.action or not msg.mac then
83 return ubus_codes["INVALID_ARGUMENT"]
84 end
85
86 if msg.action == "add" then
87 auth.allow_user(mac)
88 elseif msg.action == "del" then
89 auth.deny_user(mac)
90 end
91 end, {action = ubus.STRING, mac = ubus.STRING }
92 },
93 whitelist = {
94 function(req, msg)
95 if not msg.action or not msg.type or not msg.value then
96 return ubus_codes["INVALID_ARGUMENT"]
97 end
98
99 if msg.action == "add" then
100 config.add_whitelist(msg.type, msg.value)
101 if msg.type == "mac" then
102 auth.allow_user(msg.value)
103 end
104 elseif msg.action == "del" then
105 config.del_whitelist(msg.type, msg.value)
106 if msg.type == "mac" then
107 auth.deny_user(msg.value)
108 end
109 end
110
111 if msg.type == "domain" then
112 reload_validated_domain()
113 end
114 end, {action = ubus.STRING, type = ubus.STRING, value = ubus.STRING }
115 },
116 }
117 }
118
119 function M.init()
120 conn = ubus.connect()
121 if not conn then
122 error("Failed to connect to ubus")
123 end
124
125 conn:add(methods)
126 end
127
128 return M