From 49694eb7cb3fef06717542740897d7bb29c5380e Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 25 Aug 2023 14:41:35 +0000 Subject: [PATCH] zones: Machine.WriteWireguardKeys() Signed-off-by: Alejandro Mery --- pkg/zones/machine_rings.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/zones/machine_rings.go b/pkg/zones/machine_rings.go index 72919b4..593e7d3 100644 --- a/pkg/zones/machine_rings.go +++ b/pkg/zones/machine_rings.go @@ -3,6 +3,7 @@ package zones import ( "bytes" "fmt" + "io/fs" "os" "darvaza.org/core" @@ -72,6 +73,38 @@ func (m *Machine) tryReadWireguardKeys(ring int) error { } } +// WriteWireguardKeys writes the wgN.key/wgN.pub files +func (m *Machine) WriteWireguardKeys(ring int) error { + var err error + var key, pub string + var ri *RingInfo + + ri, _ = m.getRingInfo(ring) + if ri != nil { + key = ri.Keys.PrivateKey.String() + pub = ri.Keys.PublicKey.String() + } + + switch { + case key == "": + return fs.ErrNotExist + case pub == "": + pub = ri.Keys.PrivateKey.Public().String() + } + + err = m.WriteStringFile(key, "wg%v.key", ring) + if err != nil { + return err + } + + err = m.WriteStringFile(pub, "wg%v.pub", ring) + if err != nil { + return err + } + + return nil +} + // GetWireguardConfig reads a wgN.conf file func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) { data, err := m.ReadFile("wg%v.conf", ring)