|
|
|
@ -26,7 +26,6 @@ type RingInfo struct {
|
|
|
|
|
Ring int `toml:"ring"` |
|
|
|
|
Enabled bool `toml:"enabled,omitempty"` |
|
|
|
|
Keys *wireguard.KeyPair `toml:"keys,omitempty"` |
|
|
|
|
Address netip.Addr `toml:"address,omitempty"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Merge attempts to combine two RingInfo structs
|
|
|
|
@ -38,9 +37,6 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
|
|
|
|
|
case ri.Enabled && !alter.Enabled: |
|
|
|
|
// can't disable via Merge
|
|
|
|
|
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) |
|
|
|
@ -69,29 +65,9 @@ func (ri *RingInfo) unsafeMerge(alter *RingInfo) error {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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: |
|
|
|
|