luci-proto-wireguard: Add generate key button
authorWojciech Jowsa <wojciech.jowsa@gmail.com>
Tue, 18 Aug 2020 20:51:07 +0000 (22:51 +0200)
committerJo-Philipp Wich <jo@mein.io>
Fri, 21 Aug 2020 09:03:53 +0000 (11:03 +0200)
Signed-off-by: Wojciech Jowsa <wojciech.jowsa@gmail.com>
[minor indentation fix, use bound section_id value, remove empty translation]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
protocols/luci-proto-wireguard/htdocs/luci-static/resources/protocol/wireguard.js
protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard [new file with mode: 0755]
protocols/luci-proto-wireguard/root/usr/share/rpcd/acl.d/luci-wireguard.json [new file with mode: 0644]

index e88c07c339ba532f4f319e600fbc9f31d92def6a..4690ecfc7ffa1f6226366c5635a8044e1bf08cce 100644 (file)
@@ -1,8 +1,16 @@
 'use strict';
+'require ui';
 'require uci';
+'require rpc';
 'require form';
 'require network';
 
+var generateKey = rpc.declare({
+       object: 'luci.wireguard',
+       method: 'generateKeyPair',
+       expect: { keys: {} }
+});
+
 function validateBase64(section_id, value) {
        if (value.length == 0)
                return true;
@@ -55,6 +63,18 @@ return network.registerProtocol('wireguard', {
                o.validate = validateBase64;
                o.rmempty = false;
 
+               o = s.taboption('general', form.Button, 'generate_key', _('Generate Key'));
+               o.inputstyle = 'apply';
+               o.onclick = ui.createHandlerFn(this, function(section_id, ev) {
+                        return generateKey().then(function(keypair) {
+                               var keyInput = document.getElementById('widget.cbid.network.%s.private_key'.format(section_id)),
+                                   changeEvent = new Event('change');
+
+                               keyInput.value = keypair.priv || '';
+                               keyInput.dispatchEvent(changeEvent);
+                       });
+               }, s.section);
+
                o = s.taboption('general', form.Value, 'listen_port', _('Listen Port'), _('Optional. UDP port used for outgoing and incoming packets.'));
                o.datatype = 'port';
                o.placeholder = _('random');
diff --git a/protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard b/protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard
new file mode 100755 (executable)
index 0000000..a6c951f
--- /dev/null
@@ -0,0 +1,26 @@
+#!/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
diff --git a/protocols/luci-proto-wireguard/root/usr/share/rpcd/acl.d/luci-wireguard.json b/protocols/luci-proto-wireguard/root/usr/share/rpcd/acl.d/luci-wireguard.json
new file mode 100644 (file)
index 0000000..4bbcb81
--- /dev/null
@@ -0,0 +1,10 @@
+{
+       "luci-proto-wireguard": {
+               "description": "Grant access to LuCI Wireguard procedures",
+               "write": {
+                       "ubus": {
+                               "luci.wireguard": [ "generateKeyPair" ]
+                       }
+               }
+       }
+}