|
|
|
package zones
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ WireguardConfigPruner = (*Zones)(nil)
|
|
|
|
_ WireguardConfigPruner = (*Zone)(nil)
|
|
|
|
_ WireguardConfigPruner = (*Machine)(nil)
|
|
|
|
|
|
|
|
_ WireguardKeysWriter = (*Zones)(nil)
|
|
|
|
_ WireguardKeysWriter = (*Zone)(nil)
|
|
|
|
_ WireguardKeysWriter = (*Machine)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// A WireguardConfigPruner deletes wgN.conf on all machines under
|
|
|
|
// its scope with the specified ring disabled
|
|
|
|
type WireguardConfigPruner interface {
|
|
|
|
PruneWireguardConfig(ring int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
|
|
// the corresponding ring disabled on all zones
|
|
|
|
func (m *Zones) PruneWireguardConfig(ring int) error {
|
|
|
|
return pruneWireguardConfig(m, ring)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
|
|
// the corresponding ring disabled.
|
|
|
|
func (z *Zone) PruneWireguardConfig(ring int) error {
|
|
|
|
return pruneWireguardConfig(z, ring)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
m.ForEachMachine(func(p *Machine) bool {
|
|
|
|
err = p.zone.PruneWireguardConfig(ring)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// ignore
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err != nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PruneWireguardConfig deletes the wgN.conf file if its
|
|
|
|
// presence on the ring is disabled
|
|
|
|
func (m *Machine) PruneWireguardConfig(ring int) error {
|
|
|
|
_, ok := m.getRingInfo(ring)
|
|
|
|
if !ok {
|
|
|
|
return m.RemoveWireguardConfig(ring)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A WireguardKeysWriter writes the Wireguard Keys for all machines
|
|
|
|
// under its scope for the specified ring
|
|
|
|
type WireguardKeysWriter interface {
|
|
|
|
WriteWireguardKeys(ring int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
|
|
func (m *Zones) WriteWireguardKeys(ring int) error {
|
|
|
|
return writeWireguardKeys(m, ring)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
|
|
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
|
|
return writeWireguardKeys(z, ring)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeWireguardKeys(m MachineIterator, ring int) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
m.ForEachMachine(func(p *Machine) bool {
|
|
|
|
err = p.WriteWireguardKeys(ring)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// ignore
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err != nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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+"\n", "wg%v.key", ring)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|