Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| acf9e0e81d | |||
| 3b43e0c9ea | |||
| 9762e78f5e | |||
| 3534e7b755 | |||
| b80dc84a26 | |||
| c0ef6ae9c4 | |||
| 58867031ea | |||
| b95d1f1878 | |||
| d38c909b0b |
@@ -23,12 +23,16 @@ var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap
|
|||||||
return strings.Join(s, sep)
|
return strings.Join(s, sep)
|
||||||
},
|
},
|
||||||
}).Parse(`[Interface]
|
}).Parse(`[Interface]
|
||||||
|
{{if .Interface.Name}}# Name: {{.Interface.Name}}
|
||||||
|
{{end -}}
|
||||||
Address = {{.Interface.Address}}
|
Address = {{.Interface.Address}}
|
||||||
PrivateKey = {{.Interface.PrivateKey}}
|
PrivateKey = {{.Interface.PrivateKey}}
|
||||||
ListenPort = {{.Interface.ListenPort}}
|
ListenPort = {{.Interface.ListenPort}}
|
||||||
{{- range .Peer }}
|
{{- range .Peer }}
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
|
{{if .Name}}# Name: {{.Name}}
|
||||||
|
{{end -}}
|
||||||
PublicKey = {{.PublicKey}}
|
PublicKey = {{.PublicKey}}
|
||||||
Endpoint = {{.Endpoint}}
|
Endpoint = {{.Endpoint}}
|
||||||
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
||||||
@@ -64,6 +68,7 @@ func (f *Config) WriteTo(w io.Writer) (int64, error) {
|
|||||||
|
|
||||||
// InterfaceConfig represents the [Interface] section
|
// InterfaceConfig represents the [Interface] section
|
||||||
type InterfaceConfig struct {
|
type InterfaceConfig struct {
|
||||||
|
Name string
|
||||||
Address netip.Addr
|
Address netip.Addr
|
||||||
PrivateKey PrivateKey
|
PrivateKey PrivateKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
@@ -71,6 +76,7 @@ type InterfaceConfig struct {
|
|||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
|
Name string
|
||||||
PublicKey PublicKey
|
PublicKey PublicKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
@@ -73,38 +72,6 @@ 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+"\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
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||||
// the machine's config directory
|
// the machine's config directory
|
||||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
@@ -179,3 +180,170 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
|||||||
return netip.AddrFrom4(a4), true
|
return netip.AddrFrom4(a4), true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Ring)(nil)
|
||||||
|
_ ZoneIterator = (*Ring)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Ring describes all peers on a ring
|
||||||
|
type Ring struct {
|
||||||
|
RingAddressEncoder
|
||||||
|
ZoneIterator
|
||||||
|
|
||||||
|
Peers []*RingPeer
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPeer adds a [Machine] to the ring
|
||||||
|
func (r *Ring) AddPeer(p *Machine) bool {
|
||||||
|
ri, ok := p.getRingInfo(r.ID)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeID := p.ID
|
||||||
|
zoneID := p.Zone()
|
||||||
|
addr, _ := r.Encode(zoneID, nodeID)
|
||||||
|
|
||||||
|
rp := &RingPeer{
|
||||||
|
Node: p,
|
||||||
|
Address: addr,
|
||||||
|
PrivateKey: ri.Keys.PrivateKey,
|
||||||
|
PeerConfig: wireguard.PeerConfig{
|
||||||
|
Name: fmt.Sprintf("%s-%v", p.Name, r.ID),
|
||||||
|
PublicKey: ri.Keys.PublicKey,
|
||||||
|
Endpoint: wireguard.EndpointAddress{
|
||||||
|
Host: p.FullName(),
|
||||||
|
Port: r.Port,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case r.ID == 0:
|
||||||
|
r.setRingZeroAllowedIPs(rp)
|
||||||
|
case p.IsGateway():
|
||||||
|
r.setRingOneGatewayAllowedIPs(rp)
|
||||||
|
default:
|
||||||
|
r.setRingOneNodeAllowedIPs(rp)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Peers = append(r.Peers, rp)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Ring) setRingZeroAllowedIPs(rp *RingPeer) {
|
||||||
|
zoneID, _, _ := r.Decode(rp.Address)
|
||||||
|
|
||||||
|
// everyone on ring0 is a gateway to ring1
|
||||||
|
addr, _ := RingOneAddress(zoneID, 0)
|
||||||
|
rp.AllowCIDR(addr, 12)
|
||||||
|
|
||||||
|
// peer
|
||||||
|
rp.AllowCIDR(rp.Address, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Ring) setRingOneGatewayAllowedIPs(rp *RingPeer) {
|
||||||
|
zoneID, _, _ := r.Decode(rp.Address)
|
||||||
|
|
||||||
|
// peer
|
||||||
|
rp.AllowCIDR(rp.Address, 32)
|
||||||
|
|
||||||
|
// ring1 gateways connect to all other ring1 networks
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
if z.ID != zoneID {
|
||||||
|
addr, _ := r.Encode(z.ID, 0)
|
||||||
|
rp.AllowCIDR(addr, 12)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
// ring1 gateways also connect to all ring0 addresses
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.IsGateway() {
|
||||||
|
addr, _ := RingZeroAddress(z.ID, p.ID)
|
||||||
|
rp.AllowCIDR(addr, 32)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Ring) setRingOneNodeAllowedIPs(rp *RingPeer) {
|
||||||
|
// only to the peer itself
|
||||||
|
rp.AllowCIDR(rp.Address, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachMachine calls a function for each Machine in the ring
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (r *Ring) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
for _, pp := range r.Peers {
|
||||||
|
if fn(pp.Node) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExportConfig builds a wgN.conf for the specified machine on the ring
|
||||||
|
func (r *Ring) ExportConfig(p *Machine) (*wireguard.Config, error) {
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
out := &wireguard.Config{
|
||||||
|
Interface: wireguard.InterfaceConfig{
|
||||||
|
ListenPort: r.Port,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pp := range r.Peers {
|
||||||
|
switch {
|
||||||
|
case pp.Node == p:
|
||||||
|
// current
|
||||||
|
found = true
|
||||||
|
out.Interface.Name = pp.PeerConfig.Name
|
||||||
|
out.Interface.Address = pp.Address
|
||||||
|
out.Interface.PrivateKey = pp.PrivateKey
|
||||||
|
default:
|
||||||
|
// peer
|
||||||
|
pc := pp.PeerConfig
|
||||||
|
out.Peer = append(out.Peer, pc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return nil, fs.ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// A RingPeer is a node on a [Ring]
|
||||||
|
type RingPeer struct {
|
||||||
|
Node *Machine
|
||||||
|
|
||||||
|
Address netip.Addr
|
||||||
|
PrivateKey wireguard.PrivateKey
|
||||||
|
PeerConfig wireguard.PeerConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllowCIDR allows an IP range via this peer
|
||||||
|
func (rp *RingPeer) AllowCIDR(addr netip.Addr, bits int) {
|
||||||
|
cidr := netip.PrefixFrom(addr, bits)
|
||||||
|
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs, cidr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRing composes a new Ring for Wireguard setup
|
||||||
|
func NewRing(z ZoneIterator, m MachineIterator, ring int) (*Ring, error) {
|
||||||
|
r := &Ring{
|
||||||
|
RingAddressEncoder: Rings[ring],
|
||||||
|
ZoneIterator: z,
|
||||||
|
}
|
||||||
|
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
r.AddPeer(p)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|||||||
+2
-2
@@ -18,12 +18,12 @@ func (m *Zones) SyncAllWireguard() error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
for ring := 0; ring < RingsCount; ring++ {
|
for ring := 0; ring < RingsCount; ring++ {
|
||||||
err = m.PruneWireguardConfig(ring)
|
err = m.WriteWireguardKeys(ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.WriteWireguardKeys(ring)
|
err = m.SyncWireguardConfig(ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,265 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ WireguardConfigPruner = (*Zones)(nil)
|
||||||
|
_ WireguardConfigPruner = (*Zone)(nil)
|
||||||
|
_ WireguardConfigPruner = (*Machine)(nil)
|
||||||
|
|
||||||
|
_ WireguardConfigWriter = (*Zones)(nil)
|
||||||
|
_ WireguardConfigWriter = (*Zone)(nil)
|
||||||
|
_ WireguardConfigWriter = (*Machine)(nil)
|
||||||
|
|
||||||
|
_ WireguardConfigSyncer = (*Zones)(nil)
|
||||||
|
_ WireguardConfigSyncer = (*Zone)(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 WireguardConfigWriter rewrites all wgN.conf on all machines under
|
||||||
|
// its scope attached to that ring
|
||||||
|
type WireguardConfigWriter interface {
|
||||||
|
WriteWireguardConfig(ring int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||||
|
// attached to that ring
|
||||||
|
func (m *Zones) WriteWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return writeWireguardConfig(m, m, ring)
|
||||||
|
case 1:
|
||||||
|
var err error
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = writeWireguardConfig(m, z, ring)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||||
|
// on the Zone attached to that ring
|
||||||
|
func (z *Zone) WriteWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return writeWireguardConfig(z.zones, z.zones, ring)
|
||||||
|
case 1:
|
||||||
|
return writeWireguardConfig(z.zones, z, ring)
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
||||||
|
r, err := NewRing(z, m, ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.writeWireguardRingConfig(r)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
|
||||||
|
// if enabled
|
||||||
|
func (m *Machine) WriteWireguardConfig(ring int) error {
|
||||||
|
r, err := NewRing(m.zone.zones, m.zone, ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.writeWireguardRingConfig(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
|
||||||
|
wg, err := r.ExportConfig(m)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = wg.WriteTo(f)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// A WireguardConfigSyncer updates all wgN.conf on all machines under
|
||||||
|
// its scope reflecting the state of the ring
|
||||||
|
type WireguardConfigSyncer interface {
|
||||||
|
SyncWireguardConfig(ring int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
|
// ring
|
||||||
|
func (m *Zones) SyncWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return syncWireguardConfig(m, m, ring)
|
||||||
|
case 1:
|
||||||
|
var err error
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = syncWireguardConfig(m, z, ring)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
|
// ring
|
||||||
|
func (z *Zone) SyncWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return syncWireguardConfig(z.zones, z.zones, ring)
|
||||||
|
case 1:
|
||||||
|
return syncWireguardConfig(z.zones, z, ring)
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
||||||
|
r, err := NewRing(z, m, ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if _, ok := p.getRingInfo(ring); ok {
|
||||||
|
err = p.writeWireguardRingConfig(r)
|
||||||
|
} else {
|
||||||
|
err = p.RemoveWireguardConfig(ring)
|
||||||
|
}
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package zones
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled.
|
|
||||||
func (z *Zone) PruneWireguardConfig(ring int) error {
|
|
||||||
return pruneWireguardConfig(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
||||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
||||||
func (m *Zones) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(m, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
_, ok := p.getRingInfo(ring)
|
|
||||||
if !ok {
|
|
||||||
err = p.RemoveWireguardConfig(ring)
|
|
||||||
}
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
_ MachineIterator = (*Zone)(nil)
|
_ MachineIterator = (*Zone)(nil)
|
||||||
_ MachineIterator = (*Zones)(nil)
|
_ MachineIterator = (*Zones)(nil)
|
||||||
|
_ ZoneIterator = (*Zones)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
// A MachineIterator is a set of Machines we can iterate on
|
// A MachineIterator is a set of Machines we can iterate on
|
||||||
@@ -20,6 +21,11 @@ type MachineIterator interface {
|
|||||||
ForEachMachine(func(*Machine) bool)
|
ForEachMachine(func(*Machine) bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A ZoneIterator is a set of Zones we can iterate on
|
||||||
|
type ZoneIterator interface {
|
||||||
|
ForEachZone(func(*Zone) bool)
|
||||||
|
}
|
||||||
|
|
||||||
// Zone represents one zone in a cluster
|
// Zone represents one zone in a cluster
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
|
|||||||
Reference in New Issue
Block a user