From 657b4b787f247d6f6c5180ca4766b2c97dba6ecf Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sat, 9 Dec 2023 17:23:08 +0000 Subject: [PATCH] fix(custom): read wireguard presharedkey from peer section --- .../configuration/sources/files/wireguard.go | 16 +++++++++++----- .../sources/files/wireguard_test.go | 14 ++------------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/internal/configuration/sources/files/wireguard.go b/internal/configuration/sources/files/wireguard.go index 95adcf4f..84b09ebb 100644 --- a/internal/configuration/sources/files/wireguard.go +++ b/internal/configuration/sources/files/wireguard.go @@ -43,6 +43,17 @@ func (s *Source) readWireguard() (wireguard settings.Wireguard, err error) { return wireguard, fmt.Errorf("getting interface section: %w", err) } + peerSection, err := iniFile.GetSection("Peer") + if err == nil { + wireguard.PreSharedKey, err = parseINIWireguardKey(peerSection, "PresharedKey") + if err != nil { + return wireguard, fmt.Errorf("parsing peer section: %w", err) + } + } else if !regexINISectionNotExist.MatchString(err.Error()) { + // can never happen + return wireguard, fmt.Errorf("getting peer section: %w", err) + } + return wireguard, nil } @@ -53,11 +64,6 @@ func parseWireguardInterfaceSection(interfaceSection *ini.Section, return err // error is already wrapped correctly } - wireguard.PreSharedKey, err = parseINIWireguardKey(interfaceSection, "PresharedKey") - if err != nil { - return err // error is already wrapped correctly - } - wireguard.Addresses, err = parseINIWireguardAddress(interfaceSection) if err != nil { return err // error is already wrapped correctly diff --git a/internal/configuration/sources/files/wireguard_test.go b/internal/configuration/sources/files/wireguard_test.go index 352ba059..7236e2f3 100644 --- a/internal/configuration/sources/files/wireguard_test.go +++ b/internal/configuration/sources/files/wireguard_test.go @@ -63,11 +63,11 @@ PrivateKey = x fileContent: ` [Interface] PrivateKey = QOlCgyA/Sn/c/+YNTIEohrjm8IZV+OZ2AUFIoX20sk8= -PresharedKey = YJ680VN+dGrdsWNjSFqZ6vvwuiNhbq502ZL3G7Q3o3g= Address = 10.38.22.35/32 DNS = 193.138.218.74 [Peer] +PresharedKey = YJ680VN+dGrdsWNjSFqZ6vvwuiNhbq502ZL3G7Q3o3g= `, wireguard: settings.Wireguard{ PrivateKey: ptrTo("QOlCgyA/Sn/c/+YNTIEohrjm8IZV+OZ2AUFIoX20sk8="), @@ -119,14 +119,6 @@ PrivateKey = x`, "wgtypes: failed to parse base64-encoded key: " + "illegal base64 data at input byte 0", }, - "pre shared key error": { - iniData: `[Interface] -PresharedKey = x -`, - errMessage: "parsing PresharedKey: x: " + - "wgtypes: failed to parse base64-encoded key: " + - "illegal base64 data at input byte 0", - }, "address error": { iniData: `[Interface] Address = x @@ -137,12 +129,10 @@ Address = x iniData: ` [Interface] PrivateKey = QOlCgyA/Sn/c/+YNTIEohrjm8IZV+OZ2AUFIoX20sk8= -PresharedKey = YJ680VN+dGrdsWNjSFqZ6vvwuiNhbq502ZL3G7Q3o3g= Address = 10.38.22.35/32 `, wireguard: settings.Wireguard{ - PrivateKey: ptrTo("QOlCgyA/Sn/c/+YNTIEohrjm8IZV+OZ2AUFIoX20sk8="), - PreSharedKey: ptrTo("YJ680VN+dGrdsWNjSFqZ6vvwuiNhbq502ZL3G7Q3o3g="), + PrivateKey: ptrTo("QOlCgyA/Sn/c/+YNTIEohrjm8IZV+OZ2AUFIoX20sk8="), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{10, 38, 22, 35}), 32), },