Merge pull request #6352 from nevumx/add-wireguard-peer-addresses-config
authorPaul Donald <newtwen@gmail.com>
Mon, 4 Dec 2023 16:34:40 +0000 (17:34 +0100)
committerPaul Donald <newtwen@gmail.com>
Mon, 4 Dec 2023 23:12:59 +0000 (00:12 +0100)
luci-proto-wireguard: adding an Addresses option to the wireguard peer config generator
(cherry picked from commit 9d746c75f4023bf3c4bcfe77eaa394fbf0188d95)

protocols/luci-proto-wireguard/htdocs/luci-static/resources/protocol/wireguard.js

index 96228cf4d7870617b64b04657b5a7f1e671aa36a..96014c5a7b4f288cef36f1f97d4476dbbdad2af7 100644 (file)
@@ -688,7 +688,7 @@ return network.registerProtocol('wireguard', {
 
                o.modalonly = true;
 
-               o.createPeerConfig = function(section_id, endpoint, ips) {
+               o.createPeerConfig = function(section_id, endpoint, ips, eips) {
                        var pub = s.formvalue(s.section, 'public_key'),
                            port = s.formvalue(s.section, 'listen_port') || '51820',
                            prv = this.section.formvalue(section_id, 'private_key'),
@@ -699,6 +699,7 @@ return network.registerProtocol('wireguard', {
                        return [
                                '[Interface]',
                                'PrivateKey = ' + prv,
+                               eips && eips.length ? 'Address = ' + eips.join(', ') : '# Address not defined',
                                eport ? 'ListenPort = ' + eport : '# ListenPort not defined',
                                '',
                                '[Peer]',
@@ -714,7 +715,8 @@ return network.registerProtocol('wireguard', {
                        var mapNode = ss.getActiveModalMap(),
                            headNode = mapNode.parentNode.querySelector('h4'),
                            configGenerator = this.createPeerConfig.bind(this, section_id),
-                           parent = this.map;
+                           parent = this.map,
+                               eips = this.section.formvalue(section_id, 'allowed_ips');
 
                        return Promise.all([
                                network.getWANNetworks(),
@@ -745,7 +747,7 @@ return network.registerProtocol('wireguard', {
 
                                var qrm, qrs, qro;
 
-                               qrm = new form.JSONMap({ config: { endpoint: hostnames[0], allowed_ips: ips } }, null, _('The generated configuration can be imported into a WireGuard client application to setup a connection towards this device.'));
+                               qrm = new form.JSONMap({ config: { endpoint: hostnames[0], allowed_ips: ips, addresses: eips } }, null, _('The generated configuration can be imported into a WireGuard client application to set up a connection towards this device.'));
                                qrm.parent = parent;
 
                                qrs = qrm.section(form.NamedSection, 'config');
@@ -755,9 +757,10 @@ return network.registerProtocol('wireguard', {
                                            conf = this.map.findElement('.client-config'),
                                            endpoint = this.section.getUIElement(section_id, 'endpoint'),
                                            ips = this.section.getUIElement(section_id, 'allowed_ips');
+                                           eips = this.section.getUIElement(section_id, 'addresses');
 
                                        if (this.isValid(section_id)) {
-                                               conf.firstChild.data = configGenerator(endpoint.getValue(), ips.getValue());
+                                               conf.firstChild.data = configGenerator(endpoint.getValue(), ips.getValue(), eips.getValue());
                                                code.style.opacity = '.5';
 
                                                invokeQREncode(conf.firstChild.data, code);
@@ -775,9 +778,15 @@ return network.registerProtocol('wireguard', {
                                ips.forEach(function(ip) { qro.value(ip) });
                                qro.onchange = handleConfigChange;
 
+                               qro = qrs.option(form.DynamicList, 'addresses', _('Addresses'), _('IP addresses for the peer to use inside the tunnel. Some clients require this setting.'));
+                               qro.datatype = 'ipaddr';
+                               qro.default = eips;
+                               eips.forEach(function(eip) { qro.value(eip) });
+                               qro.onchange = handleConfigChange;
+
                                qro = qrs.option(form.DummyValue, 'output');
                                qro.renderWidget = function() {
-                                       var peer_config = configGenerator(hostnames[0], ips);
+                                       var peer_config = configGenerator(hostnames[0], ips, eips);
 
                                        var node = E('div', {
                                                'style': 'display:flex;flex-wrap:wrap;align-items:center;gap:.5em;width:100%'