<%# Copyright 2016-2017 Dan Luedtke Licensed to the public under the Apache License 2.0. -%> <% local data = { } local last_device = "" local qr_pubkey = { } local function qr_clean(qr_type, value) if not value or value == "" or value == "(none)" then return "" end if qr_type == "privkey" then return "PrivateKey = " ..value elseif qr_type == "pubkey" then return "PublicKey = " ..value end end local wg_dump = io.popen("wg show all dump 2>/dev/null") 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 = { } } qr_pubkey[line[1]] = qr_clean("pubkey", line[3]) 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) 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 qr_enc local qr_code local qr_privkey if fs.access("/usr/bin/qrencode") then qr_privkey = qr_clean("privkey", luci.sys.exec("wg genkey 2>/dev/null")) if qr_pubkey[ikey] and qr_privkey then qr_enc = "[Interface]\n" ..qr_privkey.. "\n[Peer]\n" ..qr_pubkey[ikey].. "\nAllowedIPs = 0.0.0.0/0, ::/0" qr_code = luci.sys.exec("/usr/bin/qrencode --inline --8bit --type=SVG --output=- '" ..qr_enc.. "' 2>/dev/null") else qr_code = "The QR-Code could not be generated, the wg interface setup is incomplete!" end else qr_code = "For QR-Code support please install the 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%>