1
0

wireguard.htm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <%#
  2. Copyright 2016-2017 Dan Luedtke <mail@danrl.com>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <%
  6. local data = { }
  7. local last_device = ""
  8. local wg_dump = io.popen("wg show all dump")
  9. if wg_dump then
  10. local line
  11. for line in wg_dump:lines() do
  12. local line = string.split(line, "\t")
  13. if not (last_device == line[1]) then
  14. last_device = line[1]
  15. data[line[1]] = {
  16. name = line[1],
  17. public_key = line[3],
  18. listen_port = line[4],
  19. fwmark = line[5],
  20. peers = { }
  21. }
  22. else
  23. local peer = {
  24. public_key = line[2],
  25. endpoint = line[4],
  26. allowed_ips = { },
  27. latest_handshake = line[6],
  28. transfer_rx = line[7],
  29. transfer_tx = line[8],
  30. persistent_keepalive = line[9]
  31. }
  32. if not (line[4] == '(none)') then
  33. for ipkey, ipvalue in pairs(string.split(line[5], ",")) do
  34. if #ipvalue > 0 then
  35. table.insert(peer['allowed_ips'], ipvalue)
  36. end
  37. end
  38. end
  39. table.insert(data[line[1]].peers, peer)
  40. end
  41. end
  42. end
  43. if luci.http.formvalue("status") == "1" then
  44. luci.http.prepare_content("application/json")
  45. luci.http.write_json(data)
  46. return
  47. end
  48. -%>
  49. <%+header%>
  50. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  51. <script type="text/javascript">//<![CDATA[
  52. function bytes_to_str(bytes) {
  53. bytes = parseFloat(bytes);
  54. if (bytes < 1) { return "0 B"; }
  55. var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
  56. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  57. return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
  58. };
  59. function timestamp_to_str(timestamp) {
  60. if (timestamp < 1) {
  61. return '<%:Never%>';
  62. }
  63. var now = new Date();
  64. var seconds = (now.getTime() / 1000) - timestamp;
  65. var ago = "";
  66. if (seconds < 60) {
  67. ago = parseInt(seconds) + '<%:s ago%>';
  68. } else if (seconds < 3600) {
  69. ago = parseInt(seconds / 60) + '<%:m ago%>';
  70. } else if (seconds < 86401) {
  71. ago = parseInt(seconds / 3600) + '<%:h ago%>';
  72. } else {
  73. ago = '<%:over a day ago%>';
  74. }
  75. var t = new Date(timestamp * 1000);
  76. return t.toUTCString() + ' (' + ago + ')';
  77. }
  78. XHR.poll(5, '<%=REQUEST_URI%>', { status: 1 },
  79. function(x, data) {
  80. for (var key in data) {
  81. if (!data.hasOwnProperty(key)) { continue; }
  82. var ifname = key;
  83. var iface = data[key];
  84. var s = "";
  85. if (iface.public_key == '(none)') {
  86. s += '<em><%:Interface does not have a public key!%></em>';
  87. } else {
  88. s += String.format(
  89. '<strong><%:Public Key%>: </strong>%s',
  90. iface.public_key
  91. );
  92. }
  93. if (iface.listen_port > 0) {
  94. s += String.format(
  95. '<br /><strong><%:Listen Port%>: </strong>%s',
  96. iface.listen_port
  97. );
  98. }
  99. if (iface.fwmark != 'off') {
  100. s += String.format(
  101. '<br /><strong><%:Firewall Mark%>: </strong>%s',
  102. iface.fwmark
  103. );
  104. }
  105. document.getElementById(ifname + "_info").innerHTML = s;
  106. for (var i = 0, ilen = iface.peers.length; i < ilen; i++) {
  107. var peer = iface.peers[i];
  108. var s = String.format(
  109. '<strong><%:Public Key%>: </strong>%s',
  110. peer.public_key
  111. );
  112. if (peer.endpoint != '(none)') {
  113. s += String.format(
  114. '<br /><strong><%:Endpoint%>: </strong>%s',
  115. peer.endpoint
  116. );
  117. }
  118. if (peer.allowed_ips.length > 0) {
  119. s += '<br /><strong><%:Allowed IPs%>:</strong>';
  120. for (var k = 0, klen = peer.allowed_ips.length; k < klen; k++) {
  121. s += '<br />&nbsp;&nbsp;&bull;&nbsp;' + peer.allowed_ips[k];
  122. }
  123. }
  124. if (peer.persistent_keepalive != 'off') {
  125. s += String.format(
  126. '<br /><strong><%:Persistent Keepalive%>: </strong>%ss',
  127. peer.persistent_keepalive
  128. );
  129. }
  130. var icon = '<img src="<%=resource%>/icons/tunnel_disabled.png" />';
  131. var now = new Date();
  132. if (((now.getTime() / 1000) - peer.latest_handshake) < 140) {
  133. icon = '<img src="<%=resource%>/icons/tunnel.png" />';
  134. }
  135. s += String.format(
  136. '<br /><strong><%:Latest Handshake%>: </strong>%s',
  137. timestamp_to_str(peer.latest_handshake)
  138. );
  139. s += String.format(
  140. '<br /><strong><%:Data Received%>: </strong>%s' +
  141. '<br /><strong><%:Data Transmitted%>: </strong>%s',
  142. bytes_to_str(peer.transfer_rx),
  143. bytes_to_str(peer.transfer_tx)
  144. );
  145. document.getElementById(ifname + "_" + peer.public_key + "_icon").innerHTML = icon;
  146. document.getElementById(ifname + "_" + peer.public_key + "_info").innerHTML = s;
  147. }
  148. }
  149. });
  150. //]]></script>
  151. <h2>WireGuard Status</h2>
  152. <fieldset class="cbi-section">
  153. <%-
  154. for ikey, iface in pairs(data) do
  155. -%>
  156. <legend><%:Interface%> <%=ikey%></legend>
  157. <table width="100%" cellspacing="10">
  158. <tr>
  159. <td width="33%" style="vertical-align:top"><%:Configuration%></td>
  160. <td>
  161. <table>
  162. <tr>
  163. <td id="<%=ikey%>_icon" style="width:16px; text-align:center; padding:3px">
  164. &nbsp;
  165. </td>
  166. <td id="<%=ikey%>_info" style="vertical-align:middle; padding: 3px">
  167. <em><%:Collecting data...%></em>
  168. </td>
  169. </tr></table>
  170. </td>
  171. </tr>
  172. <%-
  173. for pkey, peer in pairs(iface.peers) do
  174. -%>
  175. <tr>
  176. <td width="33%" style="vertical-align:top"><%:Peer%></td>
  177. <td>
  178. <table>
  179. <tr>
  180. <td id="<%=ikey%>_<%=peer.public_key%>_icon" style="width:16px; text-align:center; padding:3px">
  181. <img src="<%=resource%>/icons/tunnel_disabled.png" /><br />
  182. <small>?</small>
  183. </td>
  184. <td id="<%=ikey%>_<%=peer.public_key%>_info" style="vertical-align:middle; padding: 3px">
  185. <em><%:Collecting data...%></em>
  186. </td>
  187. </tr></table>
  188. </td>
  189. </tr>
  190. <%-
  191. end
  192. -%>
  193. </table>
  194. <%-
  195. end
  196. -%>
  197. </fieldset>
  198. <%+footer%>