1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#!/usr/bin/env lua
local json = require "luci.jsonc"
local sys = require "luci.sys"
local io = require "io"
local uci = require "uci"
local fs = require "nixio.fs"
local methods = {
generateKeyPair = {
call = function()
local prv = sys.exec("wg genkey 2>/dev/null"):sub(1, -2)
local pub = sys.exec("echo '" .. prv .. "' | wg pubkey 2>/dev/null"):sub(1, -2)
return {keys = {priv = prv, pub = pub}}
end
},
generateQrCode = {
args = {privkey = "privkey", psk = "psk", allowed_ips = {"allowed_ips"}},
call = function(args)
local qr_code
if fs.access("/usr/bin/qrencode") then
local psk = args.psk
local listen_port = args.listen_port
local allowed_ips = args.allowed_ips
local pubkey = sys.exec("echo '" .. args.privkey .. "' | wg pubkey 2>/dev/null"):sub(1, -2)
local client_privkey = sys.exec("wg genkey 2>/dev/null"):sub(1, -2)
local iface_qr = {
"[Interface]",
"PrivateKey = " .. client_privkey,
}
local peer_qr = {
"[Peer]",
"PublicKey = " .. pubkey,
}
if not allowed_ips or next(allowed_ips) == nil then
allowed_ips = {"0.0.0.0/0", "::/0"}
end
table.insert(peer_qr, "AllowedIPs = " .. table.concat(allowed_ips, ", "))
if psk then
table.insert(peer_qr, "PresharedKey = " .. psk)
end
qr_enc = table.concat(iface_qr, "\n") .. "\n\n" .. table.concat(peer_qr, "\n")
qr_code = sys.exec("/usr/bin/qrencode --inline --8bit --type=SVG --output=- '" .. qr_enc .. "' 2>/dev/null")
end
return {qr_code = qr_code}
end
},
getWgInstances = {
call = function()
local data = {}
local last_device = ""
local qr_pubkey = {}
local wg_dump = io.popen("wg show all dump 2>/dev/null")
if wg_dump then
local line
for line in wg_dump:lines() do
local line = string.split(line, "\t")
if not (last_device == line[1]) then
last_device = line[1]
data[line[1]] = {
name = line[1],
public_key = line[3],
listen_port = line[4],
fwmark = line[5],
peers = {}
}
if not line[3] or line[3] == "" or line[3] == "(none)" then
qr_pubkey[line[1]] = ""
else
qr_pubkey[line[1]] = "PublicKey = " .. line[3]
end
else
local peer_name
local cur = uci.cursor()
cur:foreach(
"network",
"wireguard_" .. line[1],
function(s)
if s.public_key == line[2] then
peer_name = s.description
end
end
)
local peer = {
name = peer_name,
public_key = line[2],
endpoint = line[4],
allowed_ips = {},
latest_handshake = line[6],
transfer_rx = line[7],
transfer_tx = line[8],
persistent_keepalive = line[9]
}
if not (line[4] == "(none)") then
local ipkey, ipvalue
for ipkey, ipvalue in pairs(string.split(line[5], ",")) do
if #ipvalue > 0 then
table.insert(peer["allowed_ips"], ipvalue)
end
end
end
table.insert(data[line[1]].peers, peer)
end
end
end
return data
end
}
}
local function parseInput()
local parse = json.new()
local done, err
while true do
local chunk = io.read(4096)
if not chunk then
break
elseif not done and not err then
done, err = parse:parse(chunk)
end
end
if not done then
print(json.stringify({error = err or "Incomplete input"}))
os.exit(1)
end
return parse:get()
end
local function validateArgs(func, uargs)
local method = methods[func]
if not method then
print(json.stringify({error = "Method not found"}))
os.exit(1)
end
if type(uargs) ~= "table" then
print(json.stringify({error = "Invalid arguments"}))
os.exit(1)
end
uargs.ubus_rpc_session = nil
local k, v
local margs = method.args or {}
for k, v in pairs(uargs) do
if margs[k] == nil or (v ~= nil and type(v) ~= type(margs[k])) then
print(json.stringify({error = "Invalid arguments"}))
os.exit(1)
end
end
return method
end
if arg[1] == "list" then
local _, method, rv = nil, nil, {}
for _, method in pairs(methods) do
rv[_] = method.args or {}
end
print((json.stringify(rv):gsub(":%[%]", ":{}")))
elseif arg[1] == "call" then
local args = parseInput()
local method = validateArgs(arg[2], args)
local result, code = method.call(args)
print((json.stringify(result):gsub("^%[%]$", "{}")))
os.exit(code or 0)
end
|