Browse Source

wireguard: cleanup Config parser using BinaryKey

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/1/head
Alejandro Mery 10 months ago
parent
commit
f5ee63e5aa
  1. 14
      pkg/wireguard/config.go

14
pkg/wireguard/config.go

@ -1,7 +1,6 @@
package wireguard package wireguard
import ( import (
"encoding/base64"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -32,13 +31,13 @@ func (f *Config) Peers() int {
// InterfaceConfig represents the [Interface] section // InterfaceConfig represents the [Interface] section
type InterfaceConfig struct { type InterfaceConfig struct {
Address netip.Addr Address netip.Addr
PrivateKey []byte PrivateKey BinaryKey
ListenPort uint16 ListenPort uint16
} }
// PeerConfig represents a [Peer] section // PeerConfig represents a [Peer] section
type PeerConfig struct { type PeerConfig struct {
PublicKey []byte PublicKey BinaryKey
Endpoint EndpointAddress Endpoint EndpointAddress
AllowedIPs []netip.Prefix AllowedIPs []netip.Prefix
} }
@ -129,19 +128,19 @@ type interfaceConfig struct {
} }
func (p interfaceConfig) Export() (InterfaceConfig, error) { func (p interfaceConfig) Export() (InterfaceConfig, error) {
var err error
out := InterfaceConfig{ out := InterfaceConfig{
Address: p.Address, Address: p.Address,
ListenPort: p.ListenPort, ListenPort: p.ListenPort,
} }
b, err := base64.StdEncoding.DecodeString(p.PrivateKey) out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
if err != nil { if err != nil {
err = core.Wrap(err, "PrivateKey") err = core.Wrap(err, "PrivateKey")
return InterfaceConfig{}, err return InterfaceConfig{}, err
} }
out.PrivateKey = b
return out, nil return out, nil
} }
@ -163,8 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
} }
// PublicKey // PublicKey
s = v.Peer.PublicKey[i] out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
out.PublicKey, err = base64.StdEncoding.DecodeString(s)
if err != nil { if err != nil {
err = core.Wrap(err, "PublicKey") err = core.Wrap(err, "PublicKey")
return out, err return out, err

Loading…
Cancel
Save