2017-02-16 18:40:26 +00:00
|
|
|
<%#
|
|
|
|
Copyright 2016-2017 Dan Luedtke <mail@danrl.com>
|
|
|
|
Licensed to the public under the Apache License 2.0.
|
|
|
|
-%>
|
|
|
|
|
|
|
|
<%
|
2017-02-24 08:26:03 +00:00
|
|
|
local data = { }
|
|
|
|
local last_device = ""
|
|
|
|
|
|
|
|
local wg_dump = io.popen("wg show all dump")
|
|
|
|
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[5],
|
|
|
|
fwmark = line[6],
|
|
|
|
peers = { }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
local peer = {
|
|
|
|
public_key = line[2],
|
|
|
|
endpoint = line[3],
|
|
|
|
allowed_ips = { },
|
|
|
|
latest_handshake = line[5],
|
|
|
|
transfer_rx = line[6],
|
|
|
|
transfer_tx = line[7],
|
|
|
|
persistent_keepalive = line[8]
|
|
|
|
}
|
|
|
|
if not (line[4] == '(none)') then
|
|
|
|
for ipkey, ipvalue in pairs(string.split(line[4], ",")) do
|
|
|
|
if #ipvalue > 0 then
|
|
|
|
table.insert(peer['allowed_ips'], ipvalue)
|
|
|
|
end
|
2017-02-16 18:40:26 +00:00
|
|
|
end
|
|
|
|
end
|
2017-02-24 08:26:03 +00:00
|
|
|
table.insert(data[line[1]].peers, peer)
|
2017-02-16 18:40:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if luci.http.formvalue("status") == "1" then
|
|
|
|
luci.http.prepare_content("application/json")
|
|
|
|
luci.http.write_json(data)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-%>
|
|
|
|
|
|
|
|
<%+header%>
|
|
|
|
|
|
|
|
<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
|
|
|
|
<script type="text/javascript">//<![CDATA[
|
|
|
|
|
|
|
|
function bytes_to_str(bytes) {
|
2017-02-24 08:26:03 +00:00
|
|
|
bytes = parseFloat(bytes);
|
|
|
|
if (bytes < 1) { return "0 B"; }
|
|
|
|
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];
|
2017-02-16 18:40:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function timestamp_to_str(timestamp) {
|
2017-02-24 08:26:03 +00:00
|
|
|
if (timestamp < 1) {
|
|
|
|
return '<%:Never%>';
|
|
|
|
}
|
2017-02-16 18:40:26 +00:00
|
|
|
var now = new Date();
|
|
|
|
var seconds = (now.getTime() / 1000) - timestamp;
|
2017-02-24 08:26:03 +00:00
|
|
|
var ago = "";
|
|
|
|
if (seconds < 60) {
|
|
|
|
ago = parseInt(seconds) + '<%:s ago%>';
|
|
|
|
} else if (seconds < 3600) {
|
|
|
|
ago = parseInt(seconds / 60) + '<%:m ago%>';
|
|
|
|
} else if (seconds < 86401) {
|
|
|
|
ago = parseInt(seconds / 3600) + '<%:h ago%>';
|
2017-02-16 18:40:26 +00:00
|
|
|
} else {
|
2017-02-24 08:26:03 +00:00
|
|
|
ago = '<%:over a day ago%>';
|
2017-02-16 18:40:26 +00:00
|
|
|
}
|
2017-02-24 08:26:03 +00:00
|
|
|
var t = new Date(timestamp * 1000);
|
|
|
|
return t.toUTCString() + ' (' + ago + ')';
|
2017-02-16 18:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XHR.poll(5, '<%=REQUEST_URI%>', { status: 1 },
|
|
|
|
function(x, data) {
|
2017-02-24 08:26:03 +00:00
|
|
|
for (var key in data) {
|
|
|
|
if (!data.hasOwnProperty(key)) { continue; }
|
|
|
|
var ifname = key;
|
|
|
|
var iface = data[key];
|
|
|
|
var s = "";
|
|
|
|
if (iface.public_key == '(none)') {
|
|
|
|
s += '<em><%:Interface does not have a public key!%></em>';
|
|
|
|
} else {
|
|
|
|
s += String.format(
|
2017-02-16 18:40:26 +00:00
|
|
|
'<strong><%:Public Key%>: </strong>%s',
|
2017-02-24 08:26:03 +00:00
|
|
|
iface.public_key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (iface.listen_port > 0) {
|
|
|
|
s += String.format(
|
|
|
|
'<br /><strong><%:Listen Port%>: </strong>%s',
|
|
|
|
iface.listen_port
|
2017-02-16 18:40:26 +00:00
|
|
|
);
|
2017-02-24 08:26:03 +00:00
|
|
|
}
|
|
|
|
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)') {
|
2017-02-16 18:40:26 +00:00
|
|
|
s += String.format(
|
|
|
|
'<br /><strong><%:Endpoint%>: </strong>%s',
|
2017-02-24 08:26:03 +00:00
|
|
|
peer.endpoint
|
2017-02-16 18:40:26 +00:00
|
|
|
);
|
|
|
|
}
|
2017-02-24 08:26:03 +00:00
|
|
|
if (peer.allowed_ips.length > 0) {
|
2017-02-16 18:40:26 +00:00
|
|
|
s += '<br /><strong><%:Allowed IPs%>:</strong>';
|
2017-02-24 08:26:03 +00:00
|
|
|
for (var k = 0, klen = peer.allowed_ips.length; k < klen; k++) {
|
|
|
|
s += '<br /> • ' + peer.allowed_ips[k];
|
2017-02-16 18:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-24 08:26:03 +00:00
|
|
|
if (peer.persistent_keepalive != 'off') {
|
2017-02-16 18:40:26 +00:00
|
|
|
s += String.format(
|
|
|
|
'<br /><strong><%:Persistent Keepalive%>: </strong>%ss',
|
2017-02-24 08:26:03 +00:00
|
|
|
peer.persistent_keepalive
|
2017-02-16 18:40:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
var icon = '<img src="<%=resource%>/icons/tunnel_disabled.png" />';
|
2017-02-24 08:26:03 +00:00
|
|
|
var now = new Date();
|
|
|
|
if (((now.getTime() / 1000) - peer.latest_handshake) < 140) {
|
2017-02-16 18:40:26 +00:00
|
|
|
icon = '<img src="<%=resource%>/icons/tunnel.png" />';
|
|
|
|
}
|
2017-02-24 08:26:03 +00:00
|
|
|
s += String.format(
|
|
|
|
'<br /><strong><%:Latest Handshake%>: </strong>%s',
|
|
|
|
timestamp_to_str(peer.latest_handshake)
|
|
|
|
);
|
2017-02-16 18:40:26 +00:00
|
|
|
s += String.format(
|
|
|
|
'<br /><strong><%:Data Received%>: </strong>%s' +
|
|
|
|
'<br /><strong><%:Data Transmitted%>: </strong>%s',
|
2017-02-24 08:26:03 +00:00
|
|
|
bytes_to_str(peer.transfer_rx),
|
|
|
|
bytes_to_str(peer.transfer_tx)
|
2017-02-16 18:40:26 +00:00
|
|
|
);
|
2017-02-24 08:26:03 +00:00
|
|
|
document.getElementById(ifname + "_" + peer.public_key + "_icon").innerHTML = icon;
|
|
|
|
document.getElementById(ifname + "_" + peer.public_key + "_info").innerHTML = s;
|
2017-02-16 18:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//]]></script>
|
|
|
|
|
|
|
|
<h2>WireGuard Status</h2>
|
|
|
|
|
|
|
|
<fieldset class="cbi-section">
|
|
|
|
<%-
|
2017-02-24 08:26:03 +00:00
|
|
|
for ikey, iface in pairs(data) do
|
2017-02-16 18:40:26 +00:00
|
|
|
-%>
|
2017-02-24 08:26:03 +00:00
|
|
|
<legend><%:Interface%> <%=ikey%></legend>
|
2017-02-16 18:40:26 +00:00
|
|
|
<table width="100%" cellspacing="10">
|
|
|
|
<tr>
|
|
|
|
<td width="33%" style="vertical-align:top"><%:Configuration%></td>
|
|
|
|
<td>
|
|
|
|
<table>
|
|
|
|
<tr>
|
2017-02-24 08:26:03 +00:00
|
|
|
<td id="<%=ikey%>_icon" style="width:16px; text-align:center; padding:3px">
|
2017-02-16 18:40:26 +00:00
|
|
|
|
|
|
|
</td>
|
2017-02-24 08:26:03 +00:00
|
|
|
<td id="<%=ikey%>_info" style="vertical-align:middle; padding: 3px">
|
2017-02-16 18:40:26 +00:00
|
|
|
<em><%:Collecting data...%></em>
|
|
|
|
</td>
|
|
|
|
</tr></table>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<%-
|
2017-02-24 08:26:03 +00:00
|
|
|
for pkey, peer in pairs(iface.peers) do
|
2017-02-16 18:40:26 +00:00
|
|
|
-%>
|
|
|
|
<tr>
|
|
|
|
<td width="33%" style="vertical-align:top"><%:Peer%></td>
|
|
|
|
<td>
|
|
|
|
<table>
|
|
|
|
<tr>
|
2017-02-24 08:26:03 +00:00
|
|
|
<td id="<%=ikey%>_<%=peer.public_key%>_icon" style="width:16px; text-align:center; padding:3px">
|
2017-02-16 18:40:26 +00:00
|
|
|
<img src="<%=resource%>/icons/tunnel_disabled.png" /><br />
|
|
|
|
<small>?</small>
|
|
|
|
</td>
|
2017-02-24 08:26:03 +00:00
|
|
|
<td id="<%=ikey%>_<%=peer.public_key%>_info" style="vertical-align:middle; padding: 3px">
|
2017-02-16 18:40:26 +00:00
|
|
|
<em><%:Collecting data...%></em>
|
|
|
|
</td>
|
|
|
|
</tr></table>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<%-
|
|
|
|
end
|
|
|
|
-%>
|
|
|
|
</table>
|
|
|
|
<%-
|
|
|
|
end
|
|
|
|
-%>
|
|
|
|
</fieldset>
|
|
|
|
|
|
|
|
<%+footer%>
|