luci-proto-wireguard: display interface public key 5400/head
authorPaul Dee <itsascambutmailmeanyway@gmail.com>
Thu, 11 Nov 2021 00:31:43 +0000 (01:31 +0100)
committerPaul Dee <itsascambutmailmeanyway@gmail.com>
Thu, 11 Nov 2021 00:31:43 +0000 (01:31 +0100)
Signed-off-by: Paul Dee <itsascambutmailmeanyway@gmail.com>
protocols/luci-proto-wireguard/htdocs/luci-static/resources/protocol/wireguard.js
protocols/luci-proto-wireguard/root/usr/libexec/rpcd/luci.wireguard
protocols/luci-proto-wireguard/root/usr/share/rpcd/acl.d/luci-wireguard.json

index 5795eaebec45b859b769d5b0cc5833ded4f4fdfd..78b3207d9e09487017f11bed845bbd96cf0f73a7 100644 (file)
@@ -11,6 +11,13 @@ var generateKey = rpc.declare({
        expect: { keys: {} }
 });
 
+var getPublicAndPrivateKeyFromPrivate = rpc.declare({
+       object: 'luci.wireguard',
+       method: 'getPublicAndPrivateKeyFromPrivate',
+       params: ['privkey'],
+       expect: { keys: {} }
+});
+
 var generateQrCode = rpc.declare({
        object: 'luci.wireguard',
        method: 'generateQrCode',
@@ -88,14 +95,34 @@ return network.registerProtocol('wireguard', {
                o.validate = validateBase64;
                o.rmempty = false;
 
+               var sections = uci.sections('network');
+               var serverName = this.getIfname();
+               var server = findSection(sections, serverName);
+
+               o = s.taboption('general', form.Value, 'public_key', _('Public Key'), _('Base64-encoded public key of this interface for sharing.'));
+               o.rmempty = false;
+               o.write = function() {/* write nothing */};
+
+               o.load = function(s) {
+                       return getPublicAndPrivateKeyFromPrivate(server.private_key).then(
+                               function(keypair) {
+                                       return keypair.pub || '';
+                               }, 
+                               function(error){
+                                       return _('Error getting PublicKey');
+                       }, this)
+               };
+
                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');
+                                       changeEvent = new Event('change'),
+                                       pubKeyInput = document.getElementById('widget.cbid.network.%s.public_key'.format(section_id));
 
                                keyInput.value = keypair.priv || '';
+                               pubKeyInput.value = keypair.pub || '';
                                keyInput.dispatchEvent(changeEvent);
                        });
                }, s.section);
index 681e98e5c4dd14381536b0f9ec607a33228519d9..bbe91cdd6a8d7629eb1a6f8df53e329b937de4a6 100755 (executable)
@@ -16,6 +16,14 @@ local methods = {
                        return {keys = {priv = prv, pub = pub}}
                end
        },
+       getPublicAndPrivateKeyFromPrivate = {
+               args = {privkey = "privkey"},
+               call = function(args)
+                       local pubkey = sys.exec("echo %s | wg pubkey 2>/dev/null" % util.shellquote(args.privkey)):sub(1, -2)
+
+                       return {keys = {priv = args.privkey, pub = pubkey}}
+               end
+       },
        generateQrCode = {
                args = {privkey = "privkey", psk = "psk", allowed_ips = {"allowed_ips"}},
                call = function(args)
index 04877d4f490464c8f51c0c882e5ebdc45736a6c3..aa949b9d1486de07275fefcba3f1f71cd74dc876 100644 (file)
@@ -5,6 +5,7 @@
                        "ubus": {
                                "luci.wireguard": [
                                        "generateKeyPair",
+                                       "getPublicAndPrivateKeyFromPrivate",
                                        "generateQrCode"
                                ]
                        }