Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 29dfde5e46 | |||
| 93aac4c04c |
@@ -1,7 +1,6 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||
@@ -25,74 +24,6 @@ type RingInfo struct {
|
||||
Address netip.Addr `toml:"address,omitempty"`
|
||||
}
|
||||
|
||||
// Merge attempts to combine two RingInfo structs
|
||||
func (ri *RingInfo) Merge(alter *RingInfo) error {
|
||||
switch {
|
||||
case ri.Ring != alter.Ring:
|
||||
// different ring
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
|
||||
case ri.Enabled != alter.Enabled:
|
||||
// different state
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "enabled", ri.Enabled, alter.Enabled)
|
||||
case !canMergeAddress(ri.Address, alter.Address):
|
||||
// different address
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "address", ri.Address, alter.Address)
|
||||
case !canMergeKeyPairs(ri.Keys, alter.Keys):
|
||||
// incompatible keypairs
|
||||
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
||||
}
|
||||
|
||||
switch {
|
||||
case ri.Keys == nil:
|
||||
// assign keypair
|
||||
ri.Keys = alter.Keys
|
||||
case alter.Keys != nil:
|
||||
// fill the gaps on our keypair
|
||||
if ri.Keys.PrivateKey.IsZero() {
|
||||
ri.Keys.PrivateKey = alter.Keys.PrivateKey
|
||||
}
|
||||
|
||||
if ri.Keys.PublicKey.IsZero() {
|
||||
ri.Keys.PublicKey = alter.Keys.PublicKey
|
||||
}
|
||||
}
|
||||
|
||||
if addressEqual(ri.Address, netip.Addr{}) {
|
||||
// assign address
|
||||
ri.Address = alter.Address
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func canMergeAddress(ip1, ip2 netip.Addr) bool {
|
||||
var zero netip.Addr
|
||||
|
||||
switch {
|
||||
case addressEqual(ip1, zero) || addressEqual(ip2, zero) || addressEqual(ip1, ip2):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func addressEqual(ip1, ip2 netip.Addr) bool {
|
||||
return ip1.Compare(ip2) == 0
|
||||
}
|
||||
|
||||
func canMergeKeyPairs(p1, p2 *wireguard.KeyPair) bool {
|
||||
switch {
|
||||
case p1 == nil || p2 == nil:
|
||||
return true
|
||||
case !p1.PrivateKey.IsZero() && !p2.PrivateKey.IsZero() && !p1.PrivateKey.Equal(p2.PrivateKey):
|
||||
return false
|
||||
case !p1.PublicKey.IsZero() && !p2.PublicKey.IsZero() && !p1.PublicKey.Equal(p2.PublicKey):
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// RingAddressEncoder provides encoder/decoder access for a particular
|
||||
// Wireguard ring
|
||||
type RingAddressEncoder struct {
|
||||
|
||||
+4
-3
@@ -31,9 +31,10 @@ func (m *Zones) scan() error {
|
||||
|
||||
func (m *Zones) scanMachines() error {
|
||||
var err error
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.scan()
|
||||
return err != nil
|
||||
m.ForEachMachine(func(p *Machine) {
|
||||
if err == nil {
|
||||
err = p.scan()
|
||||
}
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
+4
-28
@@ -32,45 +32,21 @@ type Zones struct {
|
||||
}
|
||||
|
||||
// ForEachMachine calls a function for each Machine in the cluster
|
||||
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||||
func (m *Zones) ForEachMachine(fn func(*Machine)) {
|
||||
for _, z := range m.Zones {
|
||||
for _, p := range z.Machines {
|
||||
if fn(p) {
|
||||
// terminate
|
||||
return
|
||||
}
|
||||
fn(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ForEachZone calls a function for each Zone in the cluster
|
||||
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
||||
func (m *Zones) ForEachZone(fn func(*Zone)) {
|
||||
for _, p := range m.Zones {
|
||||
if fn(p) {
|
||||
// terminate
|
||||
return
|
||||
}
|
||||
fn(p)
|
||||
}
|
||||
}
|
||||
|
||||
// GetMachineByName looks for a machine with the specified
|
||||
// name on any zone
|
||||
func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
|
||||
var out *Machine
|
||||
|
||||
if name != "" {
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
if p.Name == name {
|
||||
out = p
|
||||
}
|
||||
|
||||
return out != nil
|
||||
})
|
||||
}
|
||||
|
||||
return out, out != nil
|
||||
}
|
||||
|
||||
// NewFS builds a [Zones] tree using the given directory
|
||||
func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
||||
lockuper := resolver.NewCloudflareLookuper()
|
||||
|
||||
Reference in New Issue
Block a user