<%# Copyright 2016-2017 Dan Luedtke Licensed to the public under the Apache License 2.0. -%> <% local uci = uci.cursor() local data = { } local last_device = "" local enc = { } 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[4], fwmark = line[5], peers = { } } local s = uci:get_list("network", line[1], "addresses") local address = "" local key, value for key, value in pairs(s) do if address ~= "" then address = address.. ", " ..value else address = value end end enc[line[1]] = "[Interface]\nPrivateKey = " ..line[2].. "\nAddress = " ..address else local peer = { 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) enc[line[1]] = enc[line[1]].. "\n\n[Peer]\nEndpoint = " ..line[4].. "\nPublicKey = " ..line[2].. "\nAllowedIPs = " ..line[5] end end end if luci.http.formvalue("status") == "1" then luci.http.prepare_content("application/json") luci.http.write_json(data) return end -%> <%+header%>

WireGuard Status

<%- local ikey, iface for ikey, iface in pairs(data) do -%>

<%:Interface%> <%=ikey%>

<%- local qrcode if fs.access("/usr/bin/qrencode") then if enc[ikey]:sub(26,31) ~= "(none)" then qrcode = luci.sys.exec("/usr/bin/qrencode --inline --8bit --type=SVG --output=- '" ..enc[ikey].. "'") end else qrcode = "For QR-Code support please install package 'qrencode'!" end -%>
<%:Configuration%>
 
<%:Collecting data...%>
<%- local cur = uci.cursor() local pkey, peer for pkey, peer in pairs(iface.peers) do local desc cur:foreach("network", "wireguard_" .. ikey, function(s) local key, value, tmp_desc, pub_key for key, value in pairs(s) do if key == "description" then tmp_desc = value end if value == peer.public_key then pub_key = value end if pub_key and tmp_desc then desc = ': ' ..tmp_desc end end end) -%>
<%:Peer%><%=desc%>
?
<%:Collecting data...%>
<%- end -%>
<%- end -%>
<%+footer%>