3 local json = require "luci.jsonc"
4 local sys = require "luci.sys"
5 local io = require "io"
6 local uci = require "uci"
11 local prv = sys.exec("wg genkey 2>/dev/null"):sub(1, -2)
12 local pub = sys.exec("echo '" .. prv .. "' | wg pubkey 2>/dev/null"):sub(1, -2)
14 return {keys = {priv = prv, pub = pub}}
20 local last_device = ""
23 local wg_dump = io.popen("wg show all dump 2>/dev/null")
26 for line in wg_dump:lines() do
27 local line = string.split(line, "\t")
28 if not (last_device == line[1]) then
33 listen_port = line[4],
37 if not line[3] or line[3] == "" or line[3] == "(none)" then
38 qr_pubkey[line[1]] = ""
40 qr_pubkey[line[1]] = "PublicKey = " .. line[3]
44 local cur = uci.cursor()
48 "wireguard_" .. line[1],
50 if s.public_key == line[2] then
51 peer_name = s.description
63 latest_handshake = line[6],
64 transfer_rx = line[7],
65 transfer_tx = line[8],
66 persistent_keepalive = line[9]
70 if not (line[4] == "(none)") then
72 for ipkey, ipvalue in pairs(string.split(line[5], ",")) do
74 table.insert(data[line[1]].peers[peer_name]["allowed_ips"], ipvalue)
87 local function parseInput()
88 local parse = json.new()
92 local chunk = io.read(4096)
95 elseif not done and not err then
96 done, err = parse:parse(chunk)
101 print(json.stringify({error = err or "Incomplete input"}))
108 local function validateArgs(func, uargs)
109 local method = methods[func]
111 print(json.stringify({error = "Method not found"}))
115 if type(uargs) ~= "table" then
116 print(json.stringify({error = "Invalid arguments"}))
120 uargs.ubus_rpc_session = nil
123 local margs = method.args or {}
124 for k, v in pairs(uargs) do
125 if margs[k] == nil or (v ~= nil and type(v) ~= type(margs[k])) then
126 print(json.stringify({error = "Invalid arguments"}))
134 if arg[1] == "list" then
135 local _, method, rv = nil, nil, {}
136 for _, method in pairs(methods) do
137 rv[_] = method.args or {}
139 print((json.stringify(rv):gsub(":%[%]", ":{}")))
140 elseif arg[1] == "call" then
141 local args = parseInput()
142 local method = validateArgs(arg[2], args)
143 local result, code = method.call(args)
144 print((json.stringify(result):gsub("^%[%]$", "{}")))