Merge pull request #1038 from danrl/luci-app-wireguard
luci-app-wireguard: performance+ (no more sys.exec calls)
This commit is contained in:
commit
829344d676
1 changed files with 104 additions and 175 deletions
|
@ -4,135 +4,45 @@
|
||||||
-%>
|
-%>
|
||||||
|
|
||||||
<%
|
<%
|
||||||
function strip(s)
|
local data = { }
|
||||||
return (s:gsub("^%s*(.-)%s*$", "%1"))
|
local last_device = ""
|
||||||
end
|
|
||||||
|
|
||||||
function is_valid(s)
|
local wg_dump = io.popen("wg show all dump")
|
||||||
return (string.len(strip(s)) > 0)
|
if wg_dump then
|
||||||
end
|
local line
|
||||||
|
for line in wg_dump:lines() do
|
||||||
function peer_add(peers, public_key)
|
local line = string.split(line, "\t")
|
||||||
table.insert(peers, {
|
if not (last_device == line[1]) then
|
||||||
public_key = public_key,
|
last_device = line[1]
|
||||||
endpoint = "",
|
data[line[1]] = {
|
||||||
transfer_rx = 0,
|
name = line[1],
|
||||||
transfer_tx = 0,
|
public_key = line[3],
|
||||||
latest_handshake = -1,
|
listen_port = line[5],
|
||||||
persistent_keepalive = "",
|
fwmark = line[6],
|
||||||
allowed_ips = { }
|
peers = { }
|
||||||
})
|
}
|
||||||
end
|
else
|
||||||
|
local peer = {
|
||||||
function peer_set_transfer(peers, public_key, rx, tx)
|
public_key = line[2],
|
||||||
for key, peer in pairs(peers) do
|
endpoint = line[3],
|
||||||
if peer.public_key == public_key then
|
allowed_ips = { },
|
||||||
peers[key].transfer_rx = rx
|
latest_handshake = line[5],
|
||||||
peers[key].transfer_tx = tx
|
transfer_rx = line[6],
|
||||||
break
|
transfer_tx = line[7],
|
||||||
end
|
persistent_keepalive = line[8]
|
||||||
end
|
}
|
||||||
end
|
if not (line[4] == '(none)') then
|
||||||
|
for ipkey, ipvalue in pairs(string.split(line[4], ",")) do
|
||||||
function peer_set_latest_handshake(peers, public_key, latest_handshake)
|
if #ipvalue > 0 then
|
||||||
for key, peer in pairs(peers) do
|
table.insert(peer['allowed_ips'], ipvalue)
|
||||||
if peer.public_key == public_key then
|
end
|
||||||
peers[key].latest_handshake = latest_handshake
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function peer_set_endpoint(peers, public_key, endpoint)
|
|
||||||
for key, peer in pairs(peers) do
|
|
||||||
if peer.public_key == public_key then
|
|
||||||
peers[key].endpoint = endpoint
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function peer_set_persistent_keepalive(peers, public_key, persistent_keepalive)
|
|
||||||
for key, peer in pairs(peers) do
|
|
||||||
if peer.public_key == public_key then
|
|
||||||
peers[key].persistent_keepalive = persistent_keepalive
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function peer_set_allowed_ips(peers, public_key, allowed_ips)
|
|
||||||
for key, peer in pairs(peers) do
|
|
||||||
if peer.public_key == public_key then
|
|
||||||
for ipkey, ipvalue in pairs(string.split(allowed_ips, " ")) do
|
|
||||||
if is_valid(ipvalue) then
|
|
||||||
table.insert(peers[key].allowed_ips, strip(ipvalue))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
break
|
table.insert(data[line[1]].peers, peer)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local data = { }
|
|
||||||
|
|
||||||
local wg_ifaces = luci.sys.exec("wg show interfaces")
|
|
||||||
for key, name in pairs(string.split(wg_ifaces, "\n")) do
|
|
||||||
if not is_valid(name) then break end
|
|
||||||
name = strip(name)
|
|
||||||
local public_key = luci.sys.exec("wg show \"" .. name .. "\" public-key")
|
|
||||||
local listening_port = luci.sys.exec("wg show \"" .. name .. "\" listen-port")
|
|
||||||
|
|
||||||
local peers = { }
|
|
||||||
local wg_peers = luci.sys.exec("wg show \"" .. name .. "\" peers")
|
|
||||||
for key, public_key in pairs(string.split(wg_peers, "\n")) do
|
|
||||||
if not is_valid(public_key) then break end
|
|
||||||
peer_add(peers, public_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
local wg_endpoints = luci.sys.exec("wg show \"" .. name .. "\" endpoints")
|
|
||||||
for key, endpoint in pairs(string.split(wg_endpoints, "\n")) do
|
|
||||||
if not is_valid(endpoint) then break end
|
|
||||||
local ln = string.split(strip(endpoint), "\t")
|
|
||||||
peer_set_endpoint(peers, ln[1], strip(ln[2]))
|
|
||||||
end
|
|
||||||
|
|
||||||
local wg_allowed_ips = luci.sys.exec("wg show \"" .. name .. "\" allowed-ips")
|
|
||||||
for key, allowed_ips in pairs(string.split(wg_allowed_ips, "\n")) do
|
|
||||||
if not is_valid(allowed_ips) then break end
|
|
||||||
local ln = string.split(strip(allowed_ips), "\t", 2)
|
|
||||||
peer_set_allowed_ips(peers, ln[1], strip(ln[2]))
|
|
||||||
end
|
|
||||||
|
|
||||||
local wg_persistent_keepalives = luci.sys.exec("wg show \"" .. name .. "\" persistent-keepalive")
|
|
||||||
for key, persistent_keepalive in pairs(string.split(wg_persistent_keepalives, "\n")) do
|
|
||||||
if not is_valid(persistent_keepalive) then break end
|
|
||||||
local ln = string.split(strip(persistent_keepalive), "\t")
|
|
||||||
peer_set_persistent_keepalive(peers, strip(ln[1]), strip(ln[2]))
|
|
||||||
end
|
|
||||||
|
|
||||||
local wg_latest_handshakes = luci.sys.exec("wg show \"" .. name .. "\" latest-handshakes")
|
|
||||||
for key, latest_handshake in pairs(string.split(wg_latest_handshakes, "\n")) do
|
|
||||||
if not is_valid(latest_handshake) then break end
|
|
||||||
local ln = string.split(strip(latest_handshake), "\t")
|
|
||||||
peer_set_latest_handshake(peers, strip(ln[1]), tonumber(ln[2]))
|
|
||||||
end
|
|
||||||
|
|
||||||
local wg_transfers = luci.sys.exec("wg show \"" .. name .. "\" transfer")
|
|
||||||
for key, transfer in pairs(string.split(wg_transfers, "\n")) do
|
|
||||||
if not is_valid(transfer) then break end
|
|
||||||
local ln = string.split(strip(transfer), "\t")
|
|
||||||
peer_set_transfer(peers, ln[1], strip(ln[2]), strip(ln[3]))
|
|
||||||
end
|
|
||||||
|
|
||||||
table.insert(data, {
|
|
||||||
name = name,
|
|
||||||
public_key = strip(public_key),
|
|
||||||
listening_port = tonumber(strip(listening_port)),
|
|
||||||
peers = peers
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
if luci.http.formvalue("status") == "1" then
|
if luci.http.formvalue("status") == "1" then
|
||||||
luci.http.prepare_content("application/json")
|
luci.http.prepare_content("application/json")
|
||||||
luci.http.write_json(data)
|
luci.http.write_json(data)
|
||||||
|
@ -146,80 +56,102 @@
|
||||||
<script type="text/javascript">//<![CDATA[
|
<script type="text/javascript">//<![CDATA[
|
||||||
|
|
||||||
function bytes_to_str(bytes) {
|
function bytes_to_str(bytes) {
|
||||||
var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
|
bytes = parseFloat(bytes);
|
||||||
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
|
if (bytes < 1) { return "0 B"; }
|
||||||
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
|
var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
|
||||||
|
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
|
||||||
|
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
|
||||||
};
|
};
|
||||||
|
|
||||||
function timestamp_to_str(timestamp) {
|
function timestamp_to_str(timestamp) {
|
||||||
|
if (timestamp < 1) {
|
||||||
|
return '<%:Never%>';
|
||||||
|
}
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
var seconds = (now.getTime() / 1000) - timestamp;
|
var seconds = (now.getTime() / 1000) - timestamp;
|
||||||
if (seconds < 60){
|
var ago = "";
|
||||||
return parseInt(seconds) + '<%:s ago%>';
|
if (seconds < 60) {
|
||||||
}
|
ago = parseInt(seconds) + '<%:s ago%>';
|
||||||
else if (seconds < 3600){
|
} else if (seconds < 3600) {
|
||||||
return parseInt(seconds / 60) + '<%:m ago%>';
|
ago = parseInt(seconds / 60) + '<%:m ago%>';
|
||||||
}
|
} else if (seconds < 86401) {
|
||||||
else if (seconds < 86400){
|
ago = parseInt(seconds / 3600) + '<%:h ago%>';
|
||||||
return parseInt(seconds / 3600) + '<%:h ago%>';
|
|
||||||
} else {
|
} else {
|
||||||
return '<%:over a day ago%>';
|
ago = '<%:over a day ago%>';
|
||||||
}
|
}
|
||||||
|
var t = new Date(timestamp * 1000);
|
||||||
|
return t.toUTCString() + ' (' + ago + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
XHR.poll(5, '<%=REQUEST_URI%>', { status: 1 },
|
XHR.poll(5, '<%=REQUEST_URI%>', { status: 1 },
|
||||||
function(x, data) {
|
function(x, data) {
|
||||||
for (var i = 0, ilen = data.length; i < ilen; i++) {
|
for (var key in data) {
|
||||||
var iface = data[i];
|
if (!data.hasOwnProperty(key)) { continue; }
|
||||||
var ifid = iface['public_key'] + "_";
|
var ifname = key;
|
||||||
var s = String.format(
|
var iface = data[key];
|
||||||
'<strong><%:Public Key%>: </strong>%s' +
|
var s = "";
|
||||||
'<br /><strong><%:Listening Port%>: </strong>%<%:s%>',
|
if (iface.public_key == '(none)') {
|
||||||
iface['public_key'],
|
s += '<em><%:Interface does not have a public key!%></em>';
|
||||||
iface['listening_port']
|
} else {
|
||||||
);
|
s += String.format(
|
||||||
document.getElementById(ifid + "info").innerHTML = s;
|
|
||||||
for (var j = 0, jlen = iface['peers'].length; j < jlen; j++) {
|
|
||||||
var peer = iface['peers'][j];
|
|
||||||
var pid = ifid + peer['public_key'] + "_";
|
|
||||||
s = String.format(
|
|
||||||
'<strong><%:Public Key%>: </strong>%s',
|
'<strong><%:Public Key%>: </strong>%s',
|
||||||
peer['public_key']
|
iface.public_key
|
||||||
);
|
);
|
||||||
if (peer['endpoint'] != '(none)') {
|
}
|
||||||
|
if (iface.listen_port > 0) {
|
||||||
|
s += String.format(
|
||||||
|
'<br /><strong><%:Listen Port%>: </strong>%s',
|
||||||
|
iface.listen_port
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (iface.fwmark != 'off') {
|
||||||
|
s += String.format(
|
||||||
|
'<br /><strong><%:Firewall Mark%>: </strong>%s',
|
||||||
|
iface.fwmark
|
||||||
|
);
|
||||||
|
}
|
||||||
|
document.getElementById(ifname + "_info").innerHTML = s;
|
||||||
|
for (var i = 0, ilen = iface.peers.length; i < ilen; i++) {
|
||||||
|
var peer = iface.peers[i];
|
||||||
|
var s = String.format(
|
||||||
|
'<strong><%:Public Key%>: </strong>%s',
|
||||||
|
peer.public_key
|
||||||
|
);
|
||||||
|
if (peer.endpoint != '(none)') {
|
||||||
s += String.format(
|
s += String.format(
|
||||||
'<br /><strong><%:Endpoint%>: </strong>%s',
|
'<br /><strong><%:Endpoint%>: </strong>%s',
|
||||||
peer['endpoint']
|
peer.endpoint
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (peer['allowed_ips'].length > 0) {
|
if (peer.allowed_ips.length > 0) {
|
||||||
s += '<br /><strong><%:Allowed IPs%>:</strong>';
|
s += '<br /><strong><%:Allowed IPs%>:</strong>';
|
||||||
for (var k = 0, klen = peer['allowed_ips'].length; k < klen; k++) {
|
for (var k = 0, klen = peer.allowed_ips.length; k < klen; k++) {
|
||||||
s += '<br /> • ' + peer['allowed_ips'][k];
|
s += '<br /> • ' + peer.allowed_ips[k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (peer['persistent_keepalive'] != 'off') {
|
if (peer.persistent_keepalive != 'off') {
|
||||||
s += String.format(
|
s += String.format(
|
||||||
'<br /><strong><%:Persistent Keepalive%>: </strong>%ss',
|
'<br /><strong><%:Persistent Keepalive%>: </strong>%ss',
|
||||||
peer['persistent_keepalive']
|
peer.persistent_keepalive
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
var icon = '<img src="<%=resource%>/icons/tunnel_disabled.png" />';
|
var icon = '<img src="<%=resource%>/icons/tunnel_disabled.png" />';
|
||||||
if (((now.getTime() / 1000) - peer['latest_handshake']) < 140) {
|
var now = new Date();
|
||||||
|
if (((now.getTime() / 1000) - peer.latest_handshake) < 140) {
|
||||||
icon = '<img src="<%=resource%>/icons/tunnel.png" />';
|
icon = '<img src="<%=resource%>/icons/tunnel.png" />';
|
||||||
s += String.format(
|
|
||||||
'<br /><strong><%:Latest Handshake%>: </strong>%s',
|
|
||||||
timestamp_to_str(peer['latest_handshake'])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
s += String.format(
|
||||||
|
'<br /><strong><%:Latest Handshake%>: </strong>%s',
|
||||||
|
timestamp_to_str(peer.latest_handshake)
|
||||||
|
);
|
||||||
s += String.format(
|
s += String.format(
|
||||||
'<br /><strong><%:Data Received%>: </strong>%s' +
|
'<br /><strong><%:Data Received%>: </strong>%s' +
|
||||||
'<br /><strong><%:Data Transmitted%>: </strong>%s',
|
'<br /><strong><%:Data Transmitted%>: </strong>%s',
|
||||||
bytes_to_str(peer['transfer_rx']),
|
bytes_to_str(peer.transfer_rx),
|
||||||
bytes_to_str(peer['transfer_tx'])
|
bytes_to_str(peer.transfer_tx)
|
||||||
);
|
);
|
||||||
document.getElementById(pid + "icon").innerHTML = icon;
|
document.getElementById(ifname + "_" + peer.public_key + "_icon").innerHTML = icon;
|
||||||
document.getElementById(pid + "info").innerHTML = s;
|
document.getElementById(ifname + "_" + peer.public_key + "_info").innerHTML = s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -227,42 +159,39 @@
|
||||||
|
|
||||||
<h2>WireGuard Status</h2>
|
<h2>WireGuard Status</h2>
|
||||||
|
|
||||||
|
|
||||||
<fieldset class="cbi-section">
|
<fieldset class="cbi-section">
|
||||||
<%-
|
<%-
|
||||||
for key, iface in pairs(data) do
|
for ikey, iface in pairs(data) do
|
||||||
local ifid = iface.public_key .. "_"
|
|
||||||
-%>
|
-%>
|
||||||
<legend><%:Interface%> <%=iface.name%></legend>
|
<legend><%:Interface%> <%=ikey%></legend>
|
||||||
<table width="100%" cellspacing="10">
|
<table width="100%" cellspacing="10">
|
||||||
<tr>
|
<tr>
|
||||||
<td width="33%" style="vertical-align:top"><%:Configuration%></td>
|
<td width="33%" style="vertical-align:top"><%:Configuration%></td>
|
||||||
<td>
|
<td>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td id="<%=ifid%>icon" style="width:16px; text-align:center; padding:3px">
|
<td id="<%=ikey%>_icon" style="width:16px; text-align:center; padding:3px">
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td id="<%=ifid%>info" style="vertical-align:middle; padding: 3px">
|
<td id="<%=ikey%>_info" style="vertical-align:middle; padding: 3px">
|
||||||
<em><%:Collecting data...%></em>
|
<em><%:Collecting data...%></em>
|
||||||
</td>
|
</td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<%-
|
<%-
|
||||||
for key, peer in pairs(iface.peers) do
|
for pkey, peer in pairs(iface.peers) do
|
||||||
local pid = ifid .. peer.public_key .. "_"
|
|
||||||
-%>
|
-%>
|
||||||
<tr>
|
<tr>
|
||||||
<td width="33%" style="vertical-align:top"><%:Peer%></td>
|
<td width="33%" style="vertical-align:top"><%:Peer%></td>
|
||||||
<td>
|
<td>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td id="<%=pid%>icon" style="width:16px; text-align:center; padding:3px">
|
<td id="<%=ikey%>_<%=peer.public_key%>_icon" style="width:16px; text-align:center; padding:3px">
|
||||||
<img src="<%=resource%>/icons/tunnel_disabled.png" /><br />
|
<img src="<%=resource%>/icons/tunnel_disabled.png" /><br />
|
||||||
<small>?</small>
|
<small>?</small>
|
||||||
</td>
|
</td>
|
||||||
<td id="<%=pid%>info" style="vertical-align:middle; padding: 3px">
|
<td id="<%=ikey%>_<%=peer.public_key%>_info" style="vertical-align:middle; padding: 3px">
|
||||||
<em><%:Collecting data...%></em>
|
<em><%:Collecting data...%></em>
|
||||||
</td>
|
</td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
|
|
Loading…
Reference in a new issue