Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2207e4a4a4 | |||
| 7ca01aa1e4 | |||
| 8b72667f4d | |||
| 49694eb7cb | |||
| 15a98c05ec | |||
| a005823d44 | |||
| 7af8484acc | |||
| 0f1f1ce968 | |||
| 5058f286c6 | |||
| 86075eb47f | |||
| c81b782b26 | |||
| 0f62ee2e53 | |||
| 30a7bceda3 | |||
| 60e2687d04 | |||
| 1419e55d5b | |||
| ffdacb833b | |||
| aca0a5e834 |
@@ -8,6 +8,7 @@ require (
|
||||
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
|
||||
darvaza.org/slog v0.5.2
|
||||
github.com/burntSushi/toml v0.3.1
|
||||
github.com/hack-pad/hackpadfs v0.2.1
|
||||
github.com/mgechev/revive v1.3.2
|
||||
github.com/spf13/cobra v1.7.0
|
||||
golang.org/x/crypto v0.12.0
|
||||
|
||||
@@ -26,6 +26,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
|
||||
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
||||
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
|
||||
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
|
||||
+45
-36
@@ -27,60 +27,71 @@ var (
|
||||
|
||||
type (
|
||||
// PrivateKey is a binary Wireguard Private Key
|
||||
PrivateKey []byte
|
||||
PrivateKey [PrivateKeySize]byte
|
||||
// PublicKey is a binary Wireguard Public Key
|
||||
PublicKey []byte
|
||||
PublicKey [PublicKeySize]byte
|
||||
)
|
||||
|
||||
func (key PrivateKey) String() string {
|
||||
return encodeKey(key)
|
||||
switch {
|
||||
case key.IsZero():
|
||||
return ""
|
||||
default:
|
||||
return base64.StdEncoding.EncodeToString(key[:])
|
||||
}
|
||||
}
|
||||
|
||||
func (pub PublicKey) String() string {
|
||||
return encodeKey(pub)
|
||||
switch {
|
||||
case pub.IsZero():
|
||||
return ""
|
||||
default:
|
||||
return base64.StdEncoding.EncodeToString(pub[:])
|
||||
}
|
||||
}
|
||||
|
||||
// IsZero tells if the key hasn't been set
|
||||
func (key PrivateKey) IsZero() bool {
|
||||
return len(key) == 0
|
||||
var zero PrivateKey
|
||||
return key.Equal(zero)
|
||||
}
|
||||
|
||||
// IsZero tells if the key hasn't been set
|
||||
func (pub PublicKey) IsZero() bool {
|
||||
return len(pub) == 0
|
||||
var zero PublicKey
|
||||
return pub.Equal(zero)
|
||||
}
|
||||
|
||||
// Equal checks if two private keys are identical
|
||||
func (key PrivateKey) Equal(alter PrivateKey) bool {
|
||||
return bytes.Equal(key, alter)
|
||||
return bytes.Equal(key[:], alter[:])
|
||||
}
|
||||
|
||||
// Equal checks if two public keys are identical
|
||||
func (pub PublicKey) Equal(alter PublicKey) bool {
|
||||
return bytes.Equal(pub, alter)
|
||||
return bytes.Equal(pub[:], alter[:])
|
||||
}
|
||||
|
||||
// PrivateKeyFromBase64 decodes a base64-based string into
|
||||
// a [PrivateKey]
|
||||
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
||||
b, err := decodeKey(data, PrivateKeySize)
|
||||
return b, err
|
||||
if err != nil {
|
||||
var zero PrivateKey
|
||||
return zero, err
|
||||
}
|
||||
return *(*[PrivateKeySize]byte)(b), nil
|
||||
}
|
||||
|
||||
// PublicKeyFromBase64 decodes a base64-based string into
|
||||
// a [PublicKey]
|
||||
func PublicKeyFromBase64(data string) (PublicKey, error) {
|
||||
b, err := decodeKey(data, PublicKeySize)
|
||||
return b, err
|
||||
}
|
||||
|
||||
func encodeKey(b []byte) string {
|
||||
switch {
|
||||
case len(b) == 0:
|
||||
return ""
|
||||
default:
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
if err != nil {
|
||||
var zero PublicKey
|
||||
return zero, err
|
||||
}
|
||||
return *(*[PublicKeySize]byte)(b), nil
|
||||
}
|
||||
|
||||
func decodeKey(data string, size int) ([]byte, error) {
|
||||
@@ -102,27 +113,27 @@ func NewPrivateKey() (PrivateKey, error) {
|
||||
|
||||
_, err := rand.Read(s[:])
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
var zero PrivateKey
|
||||
return zero, err
|
||||
}
|
||||
|
||||
// apply same clamping as wireguard-go/device/noise-helpers.go
|
||||
s[0] &= 0xf8
|
||||
s[31] = (s[31] & 0x7f) | 0x40
|
||||
|
||||
return s[:], nil
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Public generates the corresponding PublicKey
|
||||
func (key PrivateKey) Public() PublicKey {
|
||||
if len(key) != PrivateKeySize {
|
||||
return []byte{}
|
||||
var pub PublicKey
|
||||
if !key.IsZero() {
|
||||
in := (*[PrivateKeySize]byte)(&key)
|
||||
out := (*[PublicKeySize]byte)(&pub)
|
||||
|
||||
curve25519.ScalarBaseMult(out, in)
|
||||
}
|
||||
|
||||
out := [PublicKeySize]byte{}
|
||||
in := (*[PrivateKeySize]byte)(key)
|
||||
|
||||
curve25519.ScalarBaseMult(&out, in)
|
||||
return out[:]
|
||||
return pub
|
||||
}
|
||||
|
||||
// KeyPair holds a Key pair
|
||||
@@ -158,15 +169,13 @@ func (kp *KeyPair) Validate() error {
|
||||
}
|
||||
|
||||
// NewKeyPair creates a new KeyPair for Wireguard
|
||||
func NewKeyPair() (*KeyPair, error) {
|
||||
key, err := NewPrivateKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func NewKeyPair() (KeyPair, error) {
|
||||
var out KeyPair
|
||||
|
||||
out := &KeyPair{
|
||||
PrivateKey: key,
|
||||
PublicKey: key.Public(),
|
||||
key, err := NewPrivateKey()
|
||||
if err == nil {
|
||||
out.PrivateKey = key
|
||||
out.PublicKey = key.Public()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
+3
-5
@@ -82,14 +82,12 @@ func getRingZeroGatewayID(z *Zone) int {
|
||||
var firstNodeID, gatewayID int
|
||||
|
||||
z.ForEachMachine(func(p *Machine) bool {
|
||||
nodeID := p.ID()
|
||||
|
||||
if firstNodeID == 0 {
|
||||
firstNodeID = nodeID
|
||||
firstNodeID = p.ID
|
||||
}
|
||||
|
||||
if _, found := p.getRingInfo(0); found {
|
||||
gatewayID = nodeID
|
||||
if p.IsGateway() {
|
||||
gatewayID = p.ID
|
||||
}
|
||||
|
||||
return gatewayID != 0
|
||||
|
||||
+6
-50
@@ -1,52 +1,24 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/netip"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A Machine is a machine on a Zone
|
||||
type Machine struct {
|
||||
mu sync.Mutex
|
||||
|
||||
zone *Zone
|
||||
id int
|
||||
ID int
|
||||
Name string `toml:"name"`
|
||||
|
||||
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||
RingAddresses []*RingInfo `toml:"rings,omitempty"`
|
||||
Rings []*RingInfo `toml:"rings,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Machine) String() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// ID return the index within the [Zone] associated to this [Machine]
|
||||
func (m *Machine) ID() int {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.id == 0 {
|
||||
zoneName := m.zone.Name
|
||||
|
||||
s := m.Name[len(zoneName)+1:]
|
||||
|
||||
id, err := strconv.ParseInt(s, 10, 8)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m.id = int(id)
|
||||
}
|
||||
|
||||
return m.id
|
||||
}
|
||||
|
||||
// FullName returns the Name of the machine including domain name
|
||||
func (m *Machine) FullName() string {
|
||||
if domain := m.zone.zones.domain; domain != "" {
|
||||
@@ -61,26 +33,10 @@ func (m *Machine) FullName() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// ReadFile reads a file from the machine's config directory
|
||||
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
base := m.zone.zones.dir
|
||||
fullName := m.getFilename(name, args...)
|
||||
|
||||
return fs.ReadFile(base, fullName)
|
||||
}
|
||||
|
||||
func (m *Machine) getFilename(name string, args ...any) string {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
s := []string{
|
||||
m.zone.Name,
|
||||
m.Name,
|
||||
name,
|
||||
}
|
||||
|
||||
return filepath.Join(s...)
|
||||
// IsGateway tells if the Machine is a ring0 gateway
|
||||
func (m *Machine) IsGateway() bool {
|
||||
_, ok := m.getRingInfo(0)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package zones
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
fs "github.com/hack-pad/hackpadfs"
|
||||
)
|
||||
|
||||
// OpenFile opens a file on the machine's config directory with the specified flags
|
||||
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||||
base := m.zone.zones.dir
|
||||
fullName := m.getFilename(name, args...)
|
||||
|
||||
return fs.OpenFile(base, fullName, flags, 0644)
|
||||
}
|
||||
|
||||
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||||
}
|
||||
|
||||
// CreateFile creates a file on the machine's config directory
|
||||
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||||
return m.openWriter(name, os.O_CREATE, args...)
|
||||
}
|
||||
|
||||
func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||||
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f.(io.WriteCloser), nil
|
||||
}
|
||||
|
||||
// RemoveFile deletes a file from the machine's config directory
|
||||
func (m *Machine) RemoveFile(name string, args ...any) error {
|
||||
base := m.zone.zones.dir
|
||||
fullName := m.getFilename(name, args...)
|
||||
err := fs.Remove(base, fullName)
|
||||
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// ReadFile reads a file from the machine's config directory
|
||||
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||||
base := m.zone.zones.dir
|
||||
fullName := m.getFilename(name, args...)
|
||||
|
||||
return fs.ReadFile(base, fullName)
|
||||
}
|
||||
|
||||
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
||||
f, err := m.CreateTruncFile(name, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buf := bytes.NewBufferString(value)
|
||||
_, err = buf.WriteTo(f)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Machine) getFilename(name string, args ...any) string {
|
||||
if len(args) > 0 {
|
||||
name = fmt.Sprintf(name, args...)
|
||||
}
|
||||
|
||||
s := []string{
|
||||
m.zone.Name,
|
||||
m.Name,
|
||||
name,
|
||||
}
|
||||
|
||||
return filepath.Join(s...)
|
||||
}
|
||||
+85
-29
@@ -3,6 +3,7 @@ package zones
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"darvaza.org/core"
|
||||
@@ -11,25 +12,24 @@ import (
|
||||
)
|
||||
|
||||
// GetWireguardKeys reads a wgN.key/wgN.pub files
|
||||
func (m *Machine) GetWireguardKeys(ring int) (*wireguard.KeyPair, error) {
|
||||
func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
key wireguard.PrivateKey
|
||||
pub wireguard.PublicKey
|
||||
out wireguard.KeyPair
|
||||
)
|
||||
|
||||
data, err = m.ReadFile("wg%v.key", ring)
|
||||
if err != nil {
|
||||
// failed to read
|
||||
return nil, err
|
||||
return out, err
|
||||
}
|
||||
|
||||
key, err = wireguard.PrivateKeyFromBase64(string(data))
|
||||
out.PrivateKey, err = wireguard.PrivateKeyFromBase64(string(data))
|
||||
if err != nil {
|
||||
// bad key
|
||||
err = core.Wrapf(err, "wg%v.key", ring)
|
||||
return nil, err
|
||||
return out, err
|
||||
}
|
||||
|
||||
data, err = m.ReadFile("wg%v.pub", ring)
|
||||
@@ -38,27 +38,19 @@ func (m *Machine) GetWireguardKeys(ring int) (*wireguard.KeyPair, error) {
|
||||
// no wgN.pub is fine
|
||||
case err != nil:
|
||||
// failed to read
|
||||
return nil, err
|
||||
return out, err
|
||||
default:
|
||||
// good read
|
||||
pub, err = wireguard.PublicKeyFromBase64(string(data))
|
||||
out.PublicKey, err = wireguard.PublicKeyFromBase64(string(data))
|
||||
if err != nil {
|
||||
// bad key
|
||||
err = core.Wrapf(err, "wg%v.pub", ring)
|
||||
return nil, err
|
||||
return out, err
|
||||
}
|
||||
}
|
||||
|
||||
kp := &wireguard.KeyPair{
|
||||
PrivateKey: key,
|
||||
PublicKey: pub,
|
||||
}
|
||||
|
||||
if err = kp.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return kp, nil
|
||||
err = out.Validate()
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (m *Machine) tryReadWireguardKeys(ring int) error {
|
||||
@@ -81,6 +73,60 @@ 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, "wg%v.key", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(pub, "wg%v.pub", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||
// the machine's config directory
|
||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||
var err error
|
||||
|
||||
err = m.RemoveFile("wg%v.pub", ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// ignore
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.RemoveFile("wg%v.key", ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWireguardConfig reads a wgN.conf file
|
||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||
data, err := m.ReadFile("wg%v.conf", ring)
|
||||
@@ -132,9 +178,9 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
||||
}
|
||||
|
||||
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
||||
for _, ri := range m.RingAddresses {
|
||||
for _, ri := range m.Rings {
|
||||
if ri.Ring == ring {
|
||||
return ri, true
|
||||
return ri, ri.Enabled
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,10 +188,10 @@ func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
||||
}
|
||||
|
||||
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
||||
cur, found := m.getRingInfo(ring)
|
||||
if !found {
|
||||
cur, _ := m.getRingInfo(ring)
|
||||
if cur == nil {
|
||||
// first, append
|
||||
m.RingAddresses = append(m.RingAddresses, new)
|
||||
m.Rings = append(m.Rings, new)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -157,8 +203,7 @@ func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.Interfa
|
||||
ri := &RingInfo{
|
||||
Ring: ring,
|
||||
Enabled: true,
|
||||
Address: data.Address,
|
||||
Keys: &wireguard.KeyPair{
|
||||
Keys: wireguard.KeyPair{
|
||||
PrivateKey: data.PrivateKey,
|
||||
},
|
||||
}
|
||||
@@ -178,7 +223,7 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
||||
ri := &RingInfo{
|
||||
Ring: ring,
|
||||
Enabled: true,
|
||||
Keys: &wireguard.KeyPair{
|
||||
Keys: wireguard.KeyPair{
|
||||
PublicKey: pc.PublicKey,
|
||||
},
|
||||
}
|
||||
@@ -195,8 +240,8 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
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.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:
|
||||
@@ -205,3 +250,14 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
||||
// config directory.
|
||||
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||
err := m.RemoveFile("wg%v.conf", ring)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package zones
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -26,6 +27,10 @@ func (m *Machine) updatePublicAddresses() error {
|
||||
}
|
||||
|
||||
func (m *Machine) init() error {
|
||||
if err := m.setID(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryReadWireguardKeys(i); err != nil {
|
||||
return err
|
||||
@@ -34,6 +39,19 @@ func (m *Machine) init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) setID() error {
|
||||
zoneName := m.zone.Name
|
||||
suffix := m.Name[len(zoneName)+1:]
|
||||
|
||||
id, err := strconv.ParseInt(suffix, 10, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.ID = int(id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) scan() error {
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
|
||||
+18
-41
@@ -14,29 +14,31 @@ const (
|
||||
MaxNodeID = 0xff - 1
|
||||
// RingsCount indicates how many wireguard rings we have
|
||||
RingsCount = 2
|
||||
// RingZeroPort is the port wireguard uses for ring0
|
||||
RingZeroPort = 51800
|
||||
// RingOnePort is the port wireguard uses for ring1
|
||||
RingOnePort = 51810
|
||||
)
|
||||
|
||||
// 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"`
|
||||
Ring int `toml:"ring"`
|
||||
Enabled bool `toml:"enabled,omitempty"`
|
||||
Keys wireguard.KeyPair `toml:"keys,omitempty"`
|
||||
}
|
||||
|
||||
// Merge attempts to combine two RingInfo structs
|
||||
func (ri *RingInfo) Merge(alter *RingInfo) error {
|
||||
switch {
|
||||
case alter == nil:
|
||||
return nil
|
||||
case ri.Ring != alter.Ring:
|
||||
// different ring
|
||||
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
|
||||
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)
|
||||
@@ -51,47 +53,19 @@ func (ri *RingInfo) unsafeMerge(alter *RingInfo) error {
|
||||
ri.Enabled = true
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
// fill the gaps on our keypair
|
||||
if ri.Keys.PrivateKey.IsZero() {
|
||||
ri.Keys.PrivateKey = alter.Keys.PrivateKey
|
||||
}
|
||||
|
||||
if addressEqual(ri.Address, netip.Addr{}) {
|
||||
// assign address
|
||||
ri.Address = alter.Address
|
||||
if ri.Keys.PublicKey.IsZero() {
|
||||
ri.Keys.PublicKey = alter.Keys.PublicKey
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func canMergeAddress(ip1, ip2 netip.Addr) bool {
|
||||
var zero netip.Addr
|
||||
|
||||
func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
|
||||
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):
|
||||
@@ -105,6 +79,7 @@ func canMergeKeyPairs(p1, p2 *wireguard.KeyPair) bool {
|
||||
// Wireguard ring
|
||||
type RingAddressEncoder struct {
|
||||
ID int
|
||||
Port uint16
|
||||
Encode func(zoneID, nodeID int) (netip.Addr, bool)
|
||||
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
|
||||
}
|
||||
@@ -113,12 +88,14 @@ var (
|
||||
// RingZero is a wg0 address encoder/decoder
|
||||
RingZero = RingAddressEncoder{
|
||||
ID: 0,
|
||||
Port: RingZeroPort,
|
||||
Decode: ParseRingZeroAddress,
|
||||
Encode: RingZeroAddress,
|
||||
}
|
||||
// RingOne is a wg1 address encoder/decoder
|
||||
RingOne = RingAddressEncoder{
|
||||
ID: 1,
|
||||
Port: RingOnePort,
|
||||
Decode: ParseRingOneAddress,
|
||||
Encode: RingOneAddress,
|
||||
}
|
||||
|
||||
+5
-5
@@ -93,8 +93,8 @@ func (m *Zones) scanSort() error {
|
||||
|
||||
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()
|
||||
id1 := z.Machines[i].ID
|
||||
id2 := z.Machines[j].ID
|
||||
return id1 < id2
|
||||
})
|
||||
|
||||
@@ -102,9 +102,9 @@ func (m *Zones) scanSort() error {
|
||||
})
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
sort.SliceStable(p.RingAddresses, func(i, j int) bool {
|
||||
ri1 := p.RingAddresses[i]
|
||||
ri2 := p.RingAddresses[j]
|
||||
sort.SliceStable(p.Rings, func(i, j int) bool {
|
||||
ri1 := p.Rings[i]
|
||||
ri2 := p.Rings[j]
|
||||
|
||||
return ri1.Ring < ri2.Ring
|
||||
})
|
||||
|
||||
+14
-2
@@ -3,7 +3,9 @@ package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
)
|
||||
@@ -104,5 +106,15 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
||||
|
||||
// New builds a [Zones] tree using the given directory
|
||||
func New(dir, domain string) (*Zones, error) {
|
||||
return NewFS(os.DirFS(dir), domain)
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base, err := os.NewFS().Sub(dir[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewFS(base, domain)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user