Skip to content

Commit

Permalink
fix(custom): read wireguard presharedkey from peer section
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 9, 2023
1 parent 32d6453 commit 657b4b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
16 changes: 11 additions & 5 deletions internal/configuration/sources/files/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
14 changes: 2 additions & 12 deletions internal/configuration/sources/files/wireguard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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="),
Expand Down Expand Up @@ -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
Expand All @@ -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),
},
Expand Down

0 comments on commit 657b4b7

Please sign in to comment.