From ca76ead1ba0c2fdcdb8fc202588700e3116d3966 Mon Sep 17 00:00:00 2001 From: Dirk Brenken Date: Mon, 1 Oct 2018 08:09:12 +0200 Subject: [PATCH] luci-app-wireguard: add QR Code support plus fixes * add optional QR Code support to status page (per wg Interface), submitted information via QR code are: - [Interface] (list of) Address / PrivateKey - [Peer] Endpoint / PublicKey / (list of) AllowedIPs - support multiple interfaces and peers * fix XHTML rendering errors with OpenWrt standard theme * fix logical glitch with empty descriptions, remove needless variables * left align the output for better viewing esp. with material theme * freshen up design a little bit * switch space indentation to tabs Signed-off-by: Dirk Brenken --- .../luasrc/controller/wireguard.lua | 2 +- .../luasrc/view/wireguard.htm | 440 ++++++++++-------- 2 files changed, 241 insertions(+), 201 deletions(-) diff --git a/applications/luci-app-wireguard/luasrc/controller/wireguard.lua b/applications/luci-app-wireguard/luasrc/controller/wireguard.lua index 68a82fe5cc..5a91960974 100644 --- a/applications/luci-app-wireguard/luasrc/controller/wireguard.lua +++ b/applications/luci-app-wireguard/luasrc/controller/wireguard.lua @@ -4,5 +4,5 @@ module("luci.controller.wireguard", package.seeall) function index() - entry({"admin", "status", "wireguard"}, template("wireguard"), _("WireGuard Status"), 92) + entry({"admin", "status", "wireguard"}, template("wireguard"), _("WireGuard Status"), 92) end diff --git a/applications/luci-app-wireguard/luasrc/view/wireguard.htm b/applications/luci-app-wireguard/luasrc/view/wireguard.htm index c25ef85caa..444533275e 100644 --- a/applications/luci-app-wireguard/luasrc/view/wireguard.htm +++ b/applications/luci-app-wireguard/luasrc/view/wireguard.htm @@ -1,224 +1,264 @@ <%# - Copyright 2016-2017 Dan Luedtke - Licensed to the public under the Apache License 2.0. + Copyright 2016-2017 Dan Luedtke + Licensed to the public under the Apache License 2.0. -%> <% - local data = { } - local last_device = "" + 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 = { } - } - 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 - 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 + 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 + 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%> -
-
-
<%:Configuration%>
-
-
-
-
-   -
-
- <%:Collecting data...%> -
-
-
-
- <%- - local cur = uci.cursor() - for pkey, peer in pairs(iface.peers) do - local desc, tmp_desc, pub_key = "", "", "" - cur:foreach("network", "wireguard_" .. ikey, function(s) - local 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 == peer.public_key and tmp_desc ~= "" then - desc = ': ' .. tmp_desc - end - end - end) - -%> -
-
<%:Peer%><%=desc%>
-
-
-
-
-
- ? -
-
- <%:Collecting data...%> -
-
-
-
- <%- - end - -%> -
- <%- +-%> +

<%: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%> -- 2.30.2