luci-app-wireguard: merge app and proto rpcd
authorlvoegl <lvoegl@tdt.de>
Tue, 31 Aug 2021 11:51:06 +0000 (13:51 +0200)
committerLukas Voegl <lvoegl@tdt.de>
Fri, 17 Sep 2021 12:15:56 +0000 (14:15 +0200)
Signed-off-by: lvoegl <lvoegl@tdt.de>
applications/luci-app-wireguard/Makefile
applications/luci-app-wireguard/htdocs/luci-static/resources/view/wireguard/status.js
applications/luci-app-wireguard/root/usr/libexec/rpcd/luci.wireguard [new file with mode: 0644]
applications/luci-app-wireguard/root/usr/libexec/rpcd/luci.wireguard_status [deleted file]
applications/luci-app-wireguard/root/usr/share/rpcd/acl.d/luci-app-wireguard.json
protocols/luci-proto-wireguard/Makefile
protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard [deleted file]

index ee260a982aa27b5de66afda1668013bde3cb4b1f..fe6a2c66efd814f1ea5928e73d8553842d24fca2 100644 (file)
@@ -7,7 +7,7 @@
 include $(TOPDIR)/rules.mk
 
 LUCI_TITLE:=WireGuard Status
-LUCI_DEPENDS:=+wireguard-tools +luci-proto-wireguard
+LUCI_DEPENDS:=+wireguard-tools
 LUCI_PKGARCH:=all
 
 include ../../luci.mk
index ca4ca9fd17aeaafeec3e589bb188cb87bc8c0c6b..8614db018ea10593a02a3cde90e0fae28d6fd0ab 100644 (file)
@@ -6,7 +6,7 @@
 
 
 var callGetWgInstances = rpc.declare({
-       object: 'luci.wireguard_status',
+       object: 'luci.wireguard',
        method: 'getWgInstances'
 });
 
diff --git a/applications/luci-app-wireguard/root/usr/libexec/rpcd/luci.wireguard b/applications/luci-app-wireguard/root/usr/libexec/rpcd/luci.wireguard
new file mode 100644 (file)
index 0000000..7354ad4
--- /dev/null
@@ -0,0 +1,146 @@
+#!/usr/bin/env lua
+
+local json = require "luci.jsonc"
+local sys = require "luci.sys"
+local io = require "io"
+local uci = require "uci"
+
+local methods = {
+       generateKeyPair = {
+               call = function()
+                       local prv = sys.exec("wg genkey 2>/dev/null"):sub(1, -2)
+                       local pub = sys.exec("echo '" .. prv .. "' | wg pubkey 2>/dev/null"):sub(1, -2)
+
+                       return {keys = {priv = prv, pub = pub}}
+               end
+       },
+       getWgInstances = {
+               call = function()
+                       local data = {}
+                       local last_device = ""
+                       local qr_pubkey = {}
+
+                       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 = {}
+                                               }
+                                               if not line[3] or line[3] == "" or line[3] == "(none)" then
+                                                       qr_pubkey[line[1]] = ""
+                                               else
+                                                       qr_pubkey[line[1]] = "PublicKey = " .. line[3]
+                                               end
+                                       else
+                                               local peer_name
+                                               local cur = uci.cursor()
+
+                                               cur:foreach(
+                                                       "network",
+                                                       "wireguard_" .. line[1],
+                                                       function(s)
+                                                               if s.public_key == line[2] then
+                                                                       peer_name = s.description
+                                                               end
+                                                       end
+                                               )
+
+                                               table.insert(
+                                                       data[line[1]].peers,
+                                                       {
+                                                               name = peer_name,
+                                                               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(data[line[1]].peers[peer_name]["allowed_ips"], ipvalue)
+                                                               end
+                                                       end
+                                               end
+                                       end
+                               end
+                       end
+
+                       return data
+               end
+       }
+}
+
+local function parseInput()
+       local parse = json.new()
+       local done, err
+
+       while true do
+               local chunk = io.read(4096)
+               if not chunk then
+                       break
+               elseif not done and not err then
+                       done, err = parse:parse(chunk)
+               end
+       end
+
+       if not done then
+               print(json.stringify({error = err or "Incomplete input"}))
+               os.exit(1)
+       end
+
+       return parse:get()
+end
+
+local function validateArgs(func, uargs)
+       local method = methods[func]
+       if not method then
+               print(json.stringify({error = "Method not found"}))
+               os.exit(1)
+       end
+
+       if type(uargs) ~= "table" then
+               print(json.stringify({error = "Invalid arguments"}))
+               os.exit(1)
+       end
+
+       uargs.ubus_rpc_session = nil
+
+       local k, v
+       local margs = method.args or {}
+       for k, v in pairs(uargs) do
+               if margs[k] == nil or (v ~= nil and type(v) ~= type(margs[k])) then
+                       print(json.stringify({error = "Invalid arguments"}))
+                       os.exit(1)
+               end
+       end
+
+       return method
+end
+
+if arg[1] == "list" then
+       local _, method, rv = nil, nil, {}
+       for _, method in pairs(methods) do
+               rv[_] = method.args or {}
+       end
+       print((json.stringify(rv):gsub(":%[%]", ":{}")))
+elseif arg[1] == "call" then
+       local args = parseInput()
+       local method = validateArgs(arg[2], args)
+       local result, code = method.call(args)
+       print((json.stringify(result):gsub("^%[%]$", "{}")))
+       os.exit(code or 0)
+end
diff --git a/applications/luci-app-wireguard/root/usr/libexec/rpcd/luci.wireguard_status b/applications/luci-app-wireguard/root/usr/libexec/rpcd/luci.wireguard_status
deleted file mode 100644 (file)
index 892e74d..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-#!/usr/bin/env lua
-
-local json = require "luci.jsonc"
-local sys = require "luci.sys"
-local io = require "io"
-local uci = require "uci"
-
-local methods = {
-       getWgInstances = {
-               call = function()
-                       local data = {}
-                       local last_device = ""
-                       local qr_pubkey = {}
-
-                       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 = {}
-                                               }
-                                               if not line[3] or line[3] == "" or line[3] == "(none)" then
-                                                       qr_pubkey[line[1]] = ""
-                                               else
-                                                       qr_pubkey[line[1]] = "PublicKey = " .. line[3]
-                                               end
-                                       else
-                                               local peer_name
-                                               local cur = uci.cursor()
-
-                                               cur:foreach(
-                                                       "network",
-                                                       "wireguard_" .. line[1],
-                                                       function(s)
-                                                               if s.public_key == line[2] then
-                                                                       peer_name = s.description
-                                                               end
-                                                       end
-                                               )
-
-                                               table.insert(
-                                                       data[line[1]].peers,
-                                                       {
-                                                               name = peer_name,
-                                                               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(data[line[1]].peers[peer_name]["allowed_ips"], ipvalue)
-                                                               end
-                                                       end
-                                               end
-                                       end
-                               end
-                       end
-
-                       return data
-               end
-       }
-}
-
-local function parseInput()
-       local parse = json.new()
-       local done, err
-
-       while true do
-               local chunk = io.read(4096)
-               if not chunk then
-                       break
-               elseif not done and not err then
-                       done, err = parse:parse(chunk)
-               end
-       end
-
-       if not done then
-               print(json.stringify({error = err or "Incomplete input"}))
-               os.exit(1)
-       end
-
-       return parse:get()
-end
-
-local function validateArgs(func, uargs)
-       local method = methods[func]
-       if not method then
-               print(json.stringify({error = "Method not found"}))
-               os.exit(1)
-       end
-
-       if type(uargs) ~= "table" then
-               print(json.stringify({error = "Invalid arguments"}))
-               os.exit(1)
-       end
-
-       uargs.ubus_rpc_session = nil
-
-       local k, v
-       local margs = method.args or {}
-       for k, v in pairs(uargs) do
-               if margs[k] == nil or (v ~= nil and type(v) ~= type(margs[k])) then
-                       print(json.stringify({error = "Invalid arguments"}))
-                       os.exit(1)
-               end
-       end
-
-       return method
-end
-
-if arg[1] == "list" then
-       local _, method, rv = nil, nil, {}
-       for _, method in pairs(methods) do
-               rv[_] = method.args or {}
-       end
-       print((json.stringify(rv):gsub(":%[%]", ":{}")))
-elseif arg[1] == "call" then
-       local args = parseInput()
-       local method = validateArgs(arg[2], args)
-       local result, code = method.call(args)
-       print((json.stringify(result):gsub("^%[%]$", "{}")))
-       os.exit(code or 0)
-end
index f0938e5b0048e23da912cf9295abf9a0dd2148d4..cd38bcf8ec5334b5b381851332cb56212580529a 100644 (file)
@@ -3,7 +3,7 @@
                "description": "Grant access to LuCI app wireguard",
                "read": {
                        "ubus": {
-                               "luci.wireguard_status": [
+                               "luci.wireguard": [
                                        "getWgInstances"
                                ]
                        }
index f88b90bbfa504303c95c52988849c117a5bd929e..45ec1921cc91f8c1fcbd565d0b71330e34688426 100644 (file)
@@ -7,7 +7,7 @@
 include $(TOPDIR)/rules.mk
 
 LUCI_TITLE:=Support for WireGuard VPN
-LUCI_DEPENDS:=+wireguard-tools
+LUCI_DEPENDS:=+wireguard-tools +luci-app-wireguard
 LUCI_PKGARCH:=all
 
 include ../../luci.mk
diff --git a/protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard b/protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard
deleted file mode 100755 (executable)
index a6c951f..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/sh
-
-. /usr/share/libubox/jshn.sh
-
-case "$1" in
-       list)
-               json_init
-               json_add_object "generateKeyPair"
-               json_close_object
-               json_dump
-       ;;
-       call)
-               case "$2" in
-                       generateKeyPair)
-                               prv=$(wg genkey)
-                               pub=$(echo $prv | wg pubkey)
-                               json_init
-                               json_add_object "keys"
-                               json_add_string "priv" "$prv"
-                               json_add_string "pub" "$pub"
-                               json_close_object
-                               json_dump
-                       ;;
-               esac
-       ;;
-esac