2 Copyright
2016-
2017 Dan Luedtke
<mail@danrl.com
>
3 Licensed to the public under the Apache License
2.0.
10 local wg_dump = io.popen(
"wg show all dump")
13 for line in wg_dump:lines() do
14 local line = string.split(line,
"\t")
15 if not (last_device == line[
1]) then
20 listen_port = line[
4],
29 latest_handshake = line[
6],
30 transfer_rx = line[
7],
31 transfer_tx = line[
8],
32 persistent_keepalive = line[
9]
34 if not (line[
4] == '(none)') then
35 for ipkey, ipvalue in pairs(string.split(line[
5],
",")) do
37 table.insert(peer['allowed_ips'], ipvalue)
41 table.insert(data[line[
1]].peers, peer)
46 if luci.http.formvalue(
"status") ==
"1" then
47 luci.http.prepare_content(
"application/json")
48 luci.http.write_json(data)
55 <script type=
"text/javascript" src=
"<%=resource%>/cbi.js"></script>
56 <script type=
"text/javascript">//<![CDATA[
58 function bytes_to_str(bytes) {
59 bytes = parseFloat(bytes);
60 if (bytes <
1) { return
"0 B"; }
61 var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
62 var i = parseInt(Math.floor(Math.log(bytes) / Math.log(
1024)));
63 return Math.round(bytes / Math.pow(
1024, i),
2) + ' ' + sizes[i];
66 function timestamp_to_str(timestamp) {
71 var seconds = (now.getTime() /
1000) - timestamp;
74 ago = parseInt(seconds) + '<%:s ago%
>';
75 } else if (seconds <
3600) {
76 ago = parseInt(seconds /
60) + '<%:m ago%
>';
77 } else if (seconds <
86401) {
78 ago = parseInt(seconds /
3600) + '<%:h ago%
>';
80 ago = '<%:over a day ago%
>';
82 var t = new Date(timestamp *
1000);
83 return t.toUTCString() + ' (' + ago + ')';
86 XHR.poll(
5, '<%=REQUEST_URI%
>', { status:
1 },
88 for (var key in data) {
89 if (!data.hasOwnProperty(key)) { continue; }
91 var iface = data[key];
93 if (iface.public_key == '(none)') {
94 s += '
<em><%:Interface does not have a public key!%
></em>';
97 '
<strong><%:Public Key%
>:
</strong>%s',
101 if (iface.listen_port
> 0) {
103 '
<br /><strong><%:Listen Port%
>:
</strong>%s',
107 if (iface.fwmark != 'off') {
109 '
<br /><strong><%:Firewall Mark%
>:
</strong>%s',
113 document.getElementById(ifname +
"_info").innerHTML = s;
114 for (var i =
0, ilen = iface.peers.length; i < ilen; i++) {
115 var peer = iface.peers[i];
116 var s = String.format(
117 '
<strong><%:Public Key%
>:
</strong>%s',
120 if (peer.endpoint != '(none)') {
122 '
<br /><strong><%:Endpoint%
>:
</strong>%s',
126 if (peer.allowed_ips.length
> 0) {
127 s += '
<br /><strong><%:Allowed IPs%
>:
</strong>';
128 for (var k =
0, klen = peer.allowed_ips.length; k < klen; k++) {
129 s += '
<br /> • ' + peer.allowed_ips[k];
132 if (peer.persistent_keepalive != 'off') {
134 '
<br /><strong><%:Persistent Keepalive%
>:
</strong>%ss',
135 peer.persistent_keepalive
138 var icon = '
<img src=
"<%=resource%>/icons/tunnel_disabled.png" />';
139 var now = new Date();
140 if (((now.getTime() /
1000) - peer.latest_handshake) <
140) {
141 icon = '
<img src=
"<%=resource%>/icons/tunnel.png" />';
144 '
<br /><strong><%:Latest Handshake%
>:
</strong>%s',
145 timestamp_to_str(peer.latest_handshake)
148 '
<br /><strong><%:Data Received%
>:
</strong>%s' +
149 '
<br /><strong><%:Data Transmitted%
>:
</strong>%s',
150 bytes_to_str(peer.transfer_rx),
151 bytes_to_str(peer.transfer_tx)
153 document.getElementById(ifname +
"_" + peer.public_key +
"_icon").innerHTML = icon;
154 document.getElementById(ifname +
"_" + peer.public_key +
"_info").innerHTML = s;
160 <h2>WireGuard Status
</h2>
162 <fieldset class=
"cbi-section">
164 for ikey, iface in pairs(data) do
166 <legend><%:Interface%
> <%=ikey%
></legend>
167 <table width=
"100%" cellspacing=
"10">
169 <td width=
"33%" style=
"vertical-align:top"><%:Configuration%
></td>
173 <td id=
"<%=ikey%>_icon" style=
"width:16px; text-align:center; padding:3px">
176 <td id=
"<%=ikey%>_info" style=
"vertical-align:middle; padding: 3px">
177 <em><%:Collecting data...%
></em>
183 for pkey, peer in pairs(iface.peers) do
186 <td width=
"33%" style=
"vertical-align:top"><%:Peer%
></td>
190 <td id=
"<%=ikey%>_<%=peer.public_key%>_icon" style=
"width:16px; text-align:center; padding:3px">
191 <img src=
"<%=resource%>/icons/tunnel_disabled.png" /><br />
194 <td id=
"<%=ikey%>_<%=peer.public_key%>_info" style=
"vertical-align:middle; padding: 3px">
195 <em><%:Collecting data...%
></em>