luci-proto-wireguard: fixed bug with incorrect peer name detection
authorthis-username-has-been-taken <119663930+this-username-has-been-taken@users.noreply.github.com>
Wed, 23 Oct 2024 10:15:33 +0000 (13:15 +0300)
committerPaul Donald <newtwen+github@gmail.com>
Wed, 23 Oct 2024 20:25:27 +0000 (22:25 +0200)
Fixed bug with incorrect peer name detection on `Status -> WireGuard`
page when more than one peer with the same public key exist:
1. Peers are now tested not only by public key, but also by
enabled/disabled status, peer host (both IP and FQDN are supported)
and port.
2. Added required `resolveip` dependency.

Closes #7342

Signed-off-by: @this-username-has-been-taken
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
protocols/luci-proto-wireguard/Makefile
protocols/luci-proto-wireguard/root/usr/share/rpcd/ucode/luci.wireguard

index 58aa34acd868dbff43ca18dc800c80d0505d2e84..25bf7a69dcbd6c9d58ba0ce84b1ce9b2499e328b 100644 (file)
@@ -7,7 +7,7 @@
 include $(TOPDIR)/rules.mk
 
 LUCI_TITLE:=Support for WireGuard VPN
-LUCI_DEPENDS:=+wireguard-tools +ucode +luci-lib-uqr
+LUCI_DEPENDS:=+wireguard-tools +ucode +luci-lib-uqr +resolveip
 LUCI_PKGARCH:=all
 
 PKG_LICENSE:=Apache-2.0
index add810c8ae8e693c99601e5ed3fc7ce1dd09d8bb..c6ae13c94b8a00711710516a6b7399869e25f858 100644 (file)
@@ -15,6 +15,19 @@ function command(cmd) {
        return trim(popen(cmd)?.read?.('all'));
 }
 
+function checkPeerHost(configHost, configPort, wgHost) {
+       const ips = popen(`resolveip ${configHost} 2>/dev/null`);
+       if (ips) {
+               for (let line = ips.read('line'); length(line); line = ips.read('line')) {
+                       const ip =  rtrim(line, '\n');
+                       if (ip + ":" + configPort == wgHost) {
+                               return true;
+                       }
+               }
+       }
+       return false;
+}
+
 
 const methods = {
        generatePsk: {
@@ -76,7 +89,7 @@ const methods = {
                                                let peer_name;
 
                                                uci.foreach('network', `wireguard_${last_device}`, (s) => {
-                                                       if (s.public_key == record[1])
+                                                       if (!s.disabled && s.public_key == record[1] && checkPeerHost(s.endpoint_host, s.endpoint_port, record[3]))
                                                                peer_name = s.description;
                                                });