Compare commits
14 Commits
7cf3ee04f5
..
v0.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| fdb0f0324f | |||
| 9aef92f32d | |||
| e5baf53758 | |||
| 0fe451eed0 | |||
| cb5ea80e66 | |||
| f7da9519fa | |||
| 589fb2f0e1 | |||
| f5ee63e5aa | |||
| 0de2e3f4d9 | |||
| c92873f07d | |||
| 4d25ea1d16 | |||
| 0d14510958 | |||
| c3b47ba812 | |||
| 15f5aab449 |
@@ -1,7 +1,6 @@
|
|||||||
package wireguard
|
package wireguard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -32,13 +31,13 @@ func (f *Config) Peers() int {
|
|||||||
// InterfaceConfig represents the [Interface] section
|
// InterfaceConfig represents the [Interface] section
|
||||||
type InterfaceConfig struct {
|
type InterfaceConfig struct {
|
||||||
Address netip.Addr
|
Address netip.Addr
|
||||||
PrivateKey []byte
|
PrivateKey BinaryKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
PublicKey []byte
|
PublicKey BinaryKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
}
|
}
|
||||||
@@ -129,19 +128,19 @@ type interfaceConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
out := InterfaceConfig{
|
out := InterfaceConfig{
|
||||||
Address: p.Address,
|
Address: p.Address,
|
||||||
ListenPort: p.ListenPort,
|
ListenPort: p.ListenPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := base64.StdEncoding.DecodeString(p.PrivateKey)
|
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PrivateKey")
|
err = core.Wrap(err, "PrivateKey")
|
||||||
return InterfaceConfig{}, err
|
return InterfaceConfig{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
out.PrivateKey = b
|
|
||||||
|
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,8 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey
|
// PublicKey
|
||||||
s = v.Peer.PublicKey[i]
|
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
|
||||||
out.PublicKey, err = base64.StdEncoding.DecodeString(s)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PublicKey")
|
err = core.Wrap(err, "PublicKey")
|
||||||
return out, err
|
return out, err
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package wireguard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BinaryKey is a binary blob
|
||||||
|
type BinaryKey []byte
|
||||||
|
|
||||||
|
func (k BinaryKey) String() string {
|
||||||
|
return base64.StdEncoding.EncodeToString(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsZero tells if the key hasn't been set
|
||||||
|
func (k BinaryKey) IsZero() bool {
|
||||||
|
return len(k) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal checks if two keys are identical
|
||||||
|
func (k BinaryKey) Equal(alter BinaryKey) bool {
|
||||||
|
return bytes.Equal(k, alter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BinaryKeyFromBase64 decodes a base64-based string into
|
||||||
|
// a [BinaryKey]
|
||||||
|
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
|
||||||
|
b, err := base64.StdEncoding.DecodeString(data)
|
||||||
|
return BinaryKey(b), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyPair holds a Key pair
|
||||||
|
type KeyPair struct {
|
||||||
|
PrivateKey BinaryKey
|
||||||
|
PublicKey BinaryKey
|
||||||
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package zones
|
|
||||||
|
|
||||||
import "net/netip"
|
|
||||||
|
|
||||||
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
|
|
||||||
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
|
||||||
if addr.IsValid() {
|
|
||||||
a4 := addr.As4()
|
|
||||||
|
|
||||||
if a4[0] == 10 && a4[1] == 0 {
|
|
||||||
return int(a4[2]), int(a4[3]), true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0, 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// RingZeroAddress returns a wg0 IP address
|
|
||||||
func RingZeroAddress(zoneID, nodeID int) netip.Addr {
|
|
||||||
c := zoneID
|
|
||||||
d := nodeID
|
|
||||||
|
|
||||||
return netip.AddrFrom4([4]byte{
|
|
||||||
10, 0, uint8(c), uint8(d),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
|
|
||||||
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
|
||||||
if addr.IsValid() {
|
|
||||||
a4 := addr.As4()
|
|
||||||
|
|
||||||
if a4[0] == 10 && a4[2] == 0 {
|
|
||||||
zoneID = int(a4[1] >> 4)
|
|
||||||
nodeID = int(a4[3])
|
|
||||||
return zoneID, nodeID, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0, 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// RingOneAddress returns a wg1 IP address
|
|
||||||
func RingOneAddress(zoneID, nodeID int) netip.Addr {
|
|
||||||
b := zoneID << 4
|
|
||||||
d := nodeID
|
|
||||||
|
|
||||||
return netip.AddrFrom4([4]byte{
|
|
||||||
10, uint8(b), 0, uint8(d),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -16,9 +16,10 @@ type Machine struct {
|
|||||||
|
|
||||||
zone *Zone
|
zone *Zone
|
||||||
id int
|
id int
|
||||||
Name string
|
Name string `toml:"name"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr
|
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||||
|
RingAddresses []*RingInfo `toml:"rings,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) String() string {
|
func (m *Machine) String() string {
|
||||||
@@ -81,3 +82,7 @@ func (m *Machine) getFilename(name string, args ...any) string {
|
|||||||
|
|
||||||
return filepath.Join(s...)
|
return filepath.Join(s...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
||||||
|
return m.zone.zones.GetMachineByName(name)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetWireguardConfig reads a wgN.conf file
|
||||||
|
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||||
|
data, err := m.ReadFile("wg%v.conf", ring)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r := bytes.NewReader(data)
|
||||||
|
return wireguard.NewConfigFromReader(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) tryApplyWireguardConfig(ring int) error {
|
||||||
|
wg, err := m.GetWireguardConfig(ring)
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
return nil
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return m.applyWireguardConfig(ring, wg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
||||||
|
addr := wg.GetAddress()
|
||||||
|
zoneID, nodeID, ok := Rings[ring].Decode(addr)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%s: invalid wg%v address: %s", m.Name, ring, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
|
||||||
|
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
|
||||||
|
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, peer := range wg.Peer {
|
||||||
|
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
|
||||||
|
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
||||||
|
var cur *RingInfo
|
||||||
|
|
||||||
|
for _, ri := range m.RingAddresses {
|
||||||
|
if ri.Ring == ring {
|
||||||
|
cur = ri
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cur == nil {
|
||||||
|
// first, append
|
||||||
|
m.RingAddresses = append(m.RingAddresses, new)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// extra, merge
|
||||||
|
return cur.Merge(new)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.InterfaceConfig) error {
|
||||||
|
ri := &RingInfo{
|
||||||
|
Ring: ring,
|
||||||
|
Enabled: true,
|
||||||
|
Address: data.Address,
|
||||||
|
Keys: &wireguard.KeyPair{
|
||||||
|
PrivateKey: data.PrivateKey,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.applyRingInfo(ring, ri)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) error {
|
||||||
|
peer, found := m.getPeerByName(pc.Endpoint.Name())
|
||||||
|
switch {
|
||||||
|
case !found:
|
||||||
|
// unknown
|
||||||
|
case ring == 1 && m.zone != peer.zone:
|
||||||
|
// invalid zone
|
||||||
|
default:
|
||||||
|
// apply RingInfo
|
||||||
|
ri := &RingInfo{
|
||||||
|
Ring: ring,
|
||||||
|
Enabled: true,
|
||||||
|
Keys: &wireguard.KeyPair{
|
||||||
|
PublicKey: pc.PublicKey,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return peer.applyRingInfo(ring, ri)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||||
|
switch {
|
||||||
|
case zoneID == 0:
|
||||||
|
return fmt.Errorf("invalid %s", "zoneID")
|
||||||
|
case nodeID == 0:
|
||||||
|
return fmt.Errorf("invalid %s", "nodeID")
|
||||||
|
case m.ID() != nodeID:
|
||||||
|
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID(), nodeID)
|
||||||
|
case m.zone.ID != 0 && m.zone.ID != zoneID:
|
||||||
|
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
|
||||||
|
case m.zone.ID == 0:
|
||||||
|
m.zone.ID = zoneID
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -26,5 +26,11 @@ func (m *Machine) updatePublicAddresses() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) scan() error {
|
func (m *Machine) scan() error {
|
||||||
|
for i := 0; i < RingsCount; i++ {
|
||||||
|
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return m.updatePublicAddresses()
|
return m.updatePublicAddresses()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,195 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MaxZoneID indicates the highest ID allowed for a Zone
|
||||||
|
MaxZoneID = 0xf
|
||||||
|
// MaxNodeID indicates the highest Machine ID allowed within a Zone
|
||||||
|
MaxNodeID = 0xff - 1
|
||||||
|
// RingsCount indicates how many wireguard rings we have
|
||||||
|
RingsCount = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// RingInfo contains represents the Wireguard endpoint details
|
||||||
|
// for a Machine on a particular ring
|
||||||
|
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
|
||||||
|
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 {
|
||||||
|
ID int
|
||||||
|
Encode func(zoneID, nodeID int) (netip.Addr, bool)
|
||||||
|
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// RingZero is a wg0 address encoder/decoder
|
||||||
|
RingZero = RingAddressEncoder{
|
||||||
|
ID: 0,
|
||||||
|
Decode: ParseRingZeroAddress,
|
||||||
|
Encode: RingZeroAddress,
|
||||||
|
}
|
||||||
|
// RingOne is a wg1 address encoder/decoder
|
||||||
|
RingOne = RingAddressEncoder{
|
||||||
|
ID: 1,
|
||||||
|
Decode: ParseRingOneAddress,
|
||||||
|
Encode: RingOneAddress,
|
||||||
|
}
|
||||||
|
// Rings provides indexed access to the ring address encoders
|
||||||
|
Rings = [RingsCount]RingAddressEncoder{
|
||||||
|
RingZero,
|
||||||
|
RingOne,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
|
||||||
|
//
|
||||||
|
// 0 is reserved, and only allowed when composing CIDRs.
|
||||||
|
func ValidZoneID(zoneID int) bool {
|
||||||
|
switch {
|
||||||
|
case zoneID < 0 || zoneID > MaxZoneID:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
|
||||||
|
// nodeID is unique within a Zone.
|
||||||
|
// 0 is reserved, and only allowed when composing CIDRs.
|
||||||
|
func ValidNodeID(nodeID int) bool {
|
||||||
|
switch {
|
||||||
|
case nodeID < 0 || nodeID > MaxNodeID:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
|
||||||
|
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
|
||||||
|
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
||||||
|
if addr.IsValid() {
|
||||||
|
a4 := addr.As4()
|
||||||
|
|
||||||
|
if a4[0] == 10 && a4[1] == 0 {
|
||||||
|
return int(a4[2]), int(a4[3]), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// RingZeroAddress returns a wg0 IP address
|
||||||
|
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||||
|
switch {
|
||||||
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||||
|
return netip.Addr{}, false
|
||||||
|
default:
|
||||||
|
a4 := [4]uint8{10, 0, uint8(zoneID), uint8(nodeID)}
|
||||||
|
return netip.AddrFrom4(a4), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
|
||||||
|
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
|
||||||
|
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
|
||||||
|
if addr.IsValid() {
|
||||||
|
a4 := addr.As4()
|
||||||
|
|
||||||
|
if a4[0] == 10 && a4[2] == 0 {
|
||||||
|
zoneID = int(a4[1] >> 4)
|
||||||
|
nodeID = int(a4[3])
|
||||||
|
return zoneID, nodeID, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// RingOneAddress returns a wg1 IP address
|
||||||
|
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||||
|
switch {
|
||||||
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||||
|
return netip.Addr{}, false
|
||||||
|
default:
|
||||||
|
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
|
||||||
|
return netip.AddrFrom4(a4), true
|
||||||
|
}
|
||||||
|
}
|
||||||
+81
-5
@@ -2,9 +2,25 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *Zones) scan() error {
|
func (m *Zones) scan() error {
|
||||||
|
for _, fn := range []func() error{
|
||||||
|
m.scanDirectory,
|
||||||
|
m.scanMachines,
|
||||||
|
m.scanZoneIDs,
|
||||||
|
m.scanSort,
|
||||||
|
} {
|
||||||
|
if err := fn(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Zones) scanDirectory() error {
|
||||||
// each directory is a zone
|
// each directory is a zone
|
||||||
entries, err := fs.ReadDir(m.dir, ".")
|
entries, err := fs.ReadDir(m.dir, ".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -26,19 +42,79 @@ func (m *Zones) scan() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.scanMachines()
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanMachines() error {
|
func (m *Zones) scanMachines() error {
|
||||||
var err error
|
var err error
|
||||||
m.ForEachMachine(func(p *Machine) {
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
if err == nil {
|
err = p.scan()
|
||||||
err = p.scan()
|
return err != nil
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Zones) scanZoneIDs() error {
|
||||||
|
var hasMissing bool
|
||||||
|
var lastZoneID int
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
switch {
|
||||||
|
case z.ID == 0:
|
||||||
|
hasMissing = true
|
||||||
|
case z.ID > lastZoneID:
|
||||||
|
lastZoneID = z.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if hasMissing {
|
||||||
|
next := lastZoneID + 1
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
if z.ID == 0 {
|
||||||
|
z.ID, next = next, next+1
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Zones) scanSort() error {
|
||||||
|
sort.SliceStable(m.Zones, func(i, j int) bool {
|
||||||
|
id1 := m.Zones[i].ID
|
||||||
|
id2 := m.Zones[j].ID
|
||||||
|
return id1 < id2
|
||||||
|
})
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
sort.SliceStable(z.Machines, func(i, j int) bool {
|
||||||
|
id1 := z.Machines[i].ID()
|
||||||
|
id2 := z.Machines[j].ID()
|
||||||
|
return id1 < id2
|
||||||
|
})
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
sort.SliceStable(p.RingAddresses, func(i, j int) bool {
|
||||||
|
ri1 := p.RingAddresses[i]
|
||||||
|
ri2 := p.RingAddresses[j]
|
||||||
|
|
||||||
|
return ri1.Ring < ri2.Ring
|
||||||
|
})
|
||||||
|
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (z *Zone) scan() error {
|
func (z *Zone) scan() error {
|
||||||
// each directory is a machine
|
// each directory is a machine
|
||||||
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||||||
|
|||||||
+28
-4
@@ -32,21 +32,45 @@ type Zones struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the cluster
|
// ForEachMachine calls a function for each Machine in the cluster
|
||||||
func (m *Zones) ForEachMachine(fn func(*Machine)) {
|
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||||||
for _, z := range m.Zones {
|
for _, z := range m.Zones {
|
||||||
for _, p := range z.Machines {
|
for _, p := range z.Machines {
|
||||||
fn(p)
|
if fn(p) {
|
||||||
|
// terminate
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForEachZone calls a function for each Zone in the cluster
|
// ForEachZone calls a function for each Zone in the cluster
|
||||||
func (m *Zones) ForEachZone(fn func(*Zone)) {
|
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
||||||
for _, p := range m.Zones {
|
for _, p := range m.Zones {
|
||||||
fn(p)
|
if fn(p) {
|
||||||
|
// terminate
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// NewFS builds a [Zones] tree using the given directory
|
||||||
func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
||||||
lockuper := resolver.NewCloudflareLookuper()
|
lockuper := resolver.NewCloudflareLookuper()
|
||||||
|
|||||||
Reference in New Issue
Block a user