Compare commits
5 Commits
v0.4.2
..
15f28fa7c8
| Author | SHA1 | Date | |
|---|---|---|---|
| 15f28fa7c8 | |||
| 6838d00a0a | |||
| e723066f7b | |||
| 3c6dee543a | |||
| 75d7b4389a |
+36
-45
@@ -27,71 +27,60 @@ var (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
// PrivateKey is a binary Wireguard Private Key
|
// PrivateKey is a binary Wireguard Private Key
|
||||||
PrivateKey [PrivateKeySize]byte
|
PrivateKey []byte
|
||||||
// PublicKey is a binary Wireguard Public Key
|
// PublicKey is a binary Wireguard Public Key
|
||||||
PublicKey [PublicKeySize]byte
|
PublicKey []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
func (key PrivateKey) String() string {
|
func (key PrivateKey) String() string {
|
||||||
switch {
|
return encodeKey(key)
|
||||||
case key.IsZero():
|
|
||||||
return ""
|
|
||||||
default:
|
|
||||||
return base64.StdEncoding.EncodeToString(key[:])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pub PublicKey) String() string {
|
func (pub PublicKey) String() string {
|
||||||
switch {
|
return encodeKey(pub)
|
||||||
case pub.IsZero():
|
|
||||||
return ""
|
|
||||||
default:
|
|
||||||
return base64.StdEncoding.EncodeToString(pub[:])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (key PrivateKey) IsZero() bool {
|
func (key PrivateKey) IsZero() bool {
|
||||||
var zero PrivateKey
|
return len(key) == 0
|
||||||
return key.Equal(zero)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (pub PublicKey) IsZero() bool {
|
func (pub PublicKey) IsZero() bool {
|
||||||
var zero PublicKey
|
return len(pub) == 0
|
||||||
return pub.Equal(zero)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal checks if two private keys are identical
|
// Equal checks if two private keys are identical
|
||||||
func (key PrivateKey) Equal(alter PrivateKey) bool {
|
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
|
// Equal checks if two public keys are identical
|
||||||
func (pub PublicKey) Equal(alter PublicKey) bool {
|
func (pub PublicKey) Equal(alter PublicKey) bool {
|
||||||
return bytes.Equal(pub[:], alter[:])
|
return bytes.Equal(pub, alter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrivateKeyFromBase64 decodes a base64-based string into
|
// PrivateKeyFromBase64 decodes a base64-based string into
|
||||||
// a [PrivateKey]
|
// a [PrivateKey]
|
||||||
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
||||||
b, err := decodeKey(data, PrivateKeySize)
|
b, err := decodeKey(data, PrivateKeySize)
|
||||||
if err != nil {
|
return b, err
|
||||||
var zero PrivateKey
|
|
||||||
return zero, err
|
|
||||||
}
|
|
||||||
return *(*[PrivateKeySize]byte)(b), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PublicKeyFromBase64 decodes a base64-based string into
|
// PublicKeyFromBase64 decodes a base64-based string into
|
||||||
// a [PublicKey]
|
// a [PublicKey]
|
||||||
func PublicKeyFromBase64(data string) (PublicKey, error) {
|
func PublicKeyFromBase64(data string) (PublicKey, error) {
|
||||||
b, err := decodeKey(data, PublicKeySize)
|
b, err := decodeKey(data, PublicKeySize)
|
||||||
if err != nil {
|
return b, err
|
||||||
var zero PublicKey
|
}
|
||||||
return zero, err
|
|
||||||
|
func encodeKey(b []byte) string {
|
||||||
|
switch {
|
||||||
|
case len(b) == 0:
|
||||||
|
return ""
|
||||||
|
default:
|
||||||
|
return base64.StdEncoding.EncodeToString(b)
|
||||||
}
|
}
|
||||||
return *(*[PublicKeySize]byte)(b), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeKey(data string, size int) ([]byte, error) {
|
func decodeKey(data string, size int) ([]byte, error) {
|
||||||
@@ -113,27 +102,27 @@ func NewPrivateKey() (PrivateKey, error) {
|
|||||||
|
|
||||||
_, err := rand.Read(s[:])
|
_, err := rand.Read(s[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var zero PrivateKey
|
return []byte{}, err
|
||||||
return zero, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply same clamping as wireguard-go/device/noise-helpers.go
|
// apply same clamping as wireguard-go/device/noise-helpers.go
|
||||||
s[0] &= 0xf8
|
s[0] &= 0xf8
|
||||||
s[31] = (s[31] & 0x7f) | 0x40
|
s[31] = (s[31] & 0x7f) | 0x40
|
||||||
|
|
||||||
return s, nil
|
return s[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public generates the corresponding PublicKey
|
// Public generates the corresponding PublicKey
|
||||||
func (key PrivateKey) Public() PublicKey {
|
func (key PrivateKey) Public() PublicKey {
|
||||||
var pub PublicKey
|
if len(key) != PrivateKeySize {
|
||||||
if !key.IsZero() {
|
return []byte{}
|
||||||
in := (*[PrivateKeySize]byte)(&key)
|
|
||||||
out := (*[PublicKeySize]byte)(&pub)
|
|
||||||
|
|
||||||
curve25519.ScalarBaseMult(out, in)
|
|
||||||
}
|
}
|
||||||
return pub
|
|
||||||
|
out := [PublicKeySize]byte{}
|
||||||
|
in := (*[PrivateKeySize]byte)(key)
|
||||||
|
|
||||||
|
curve25519.ScalarBaseMult(&out, in)
|
||||||
|
return out[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyPair holds a Key pair
|
// KeyPair holds a Key pair
|
||||||
@@ -169,13 +158,15 @@ func (kp *KeyPair) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyPair creates a new KeyPair for Wireguard
|
// NewKeyPair creates a new KeyPair for Wireguard
|
||||||
func NewKeyPair() (KeyPair, error) {
|
func NewKeyPair() (*KeyPair, error) {
|
||||||
var out KeyPair
|
|
||||||
|
|
||||||
key, err := NewPrivateKey()
|
key, err := NewPrivateKey()
|
||||||
if err == nil {
|
if err != nil {
|
||||||
out.PrivateKey = key
|
return nil, err
|
||||||
out.PublicKey = key.Public()
|
}
|
||||||
|
|
||||||
|
out := &KeyPair{
|
||||||
|
PrivateKey: key,
|
||||||
|
PublicKey: key.Public(),
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -82,12 +82,14 @@ func getRingZeroGatewayID(z *Zone) int {
|
|||||||
var firstNodeID, gatewayID int
|
var firstNodeID, gatewayID int
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
nodeID := p.ID()
|
||||||
|
|
||||||
if firstNodeID == 0 {
|
if firstNodeID == 0 {
|
||||||
firstNodeID = p.ID
|
firstNodeID = nodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.IsGateway() {
|
if _, found := p.getRingInfo(0); found {
|
||||||
gatewayID = p.ID
|
gatewayID = nodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
return gatewayID != 0
|
return gatewayID != 0
|
||||||
|
|||||||
+27
-8
@@ -2,23 +2,48 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Machine is a machine on a Zone
|
// A Machine is a machine on a Zone
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
|
||||||
zone *Zone
|
zone *Zone
|
||||||
ID int
|
id int
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||||
Rings []*RingInfo `toml:"rings,omitempty"`
|
RingAddresses []*RingInfo `toml:"rings,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) String() string {
|
func (m *Machine) String() string {
|
||||||
return m.Name
|
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
|
// FullName returns the Name of the machine including domain name
|
||||||
func (m *Machine) FullName() string {
|
func (m *Machine) FullName() string {
|
||||||
if domain := m.zone.zones.domain; domain != "" {
|
if domain := m.zone.zones.domain; domain != "" {
|
||||||
@@ -33,12 +58,6 @@ func (m *Machine) FullName() string {
|
|||||||
return m.Name
|
return m.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
||||||
return m.zone.zones.GetMachineByName(name)
|
return m.zone.zones.GetMachineByName(name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@@ -19,22 +17,13 @@ func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateTruncFile creates or truncates a file on the machine's config directory
|
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||||
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
func (m *Machine) CreateTruncFile(name string, args ...any) (fs.File, error) {
|
||||||
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
return m.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateFile creates a file on the machine's config directory
|
// CreateFile creates a file on the machine's config directory
|
||||||
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
func (m *Machine) CreateFile(name string, args ...any) (fs.File, error) {
|
||||||
return m.openWriter(name, os.O_CREATE, args...)
|
return m.OpenFile(name, os.O_RDWR|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
|
// RemoveFile deletes a file from the machine's config directory
|
||||||
@@ -59,19 +48,6 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
|||||||
return fs.ReadFile(base, fullName)
|
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 {
|
func (m *Machine) getFilename(name string, args ...any) string {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
name = fmt.Sprintf(name, args...)
|
name = fmt.Sprintf(name, args...)
|
||||||
|
|||||||
+10
-137
@@ -3,7 +3,6 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
@@ -11,122 +10,6 @@ import (
|
|||||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetWireguardKeys reads a wgN.key/wgN.pub files
|
|
||||||
func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
|
|
||||||
var (
|
|
||||||
data []byte
|
|
||||||
err error
|
|
||||||
out wireguard.KeyPair
|
|
||||||
)
|
|
||||||
|
|
||||||
data, err = m.ReadFile("wg%v.key", ring)
|
|
||||||
if err != nil {
|
|
||||||
// failed to read
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
out.PrivateKey, err = wireguard.PrivateKeyFromBase64(string(data))
|
|
||||||
if err != nil {
|
|
||||||
// bad key
|
|
||||||
err = core.Wrapf(err, "wg%v.key", ring)
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err = m.ReadFile("wg%v.pub", ring)
|
|
||||||
switch {
|
|
||||||
case os.IsNotExist(err):
|
|
||||||
// no wgN.pub is fine
|
|
||||||
case err != nil:
|
|
||||||
// failed to read
|
|
||||||
return out, err
|
|
||||||
default:
|
|
||||||
// good read
|
|
||||||
out.PublicKey, err = wireguard.PublicKeyFromBase64(string(data))
|
|
||||||
if err != nil {
|
|
||||||
// bad key
|
|
||||||
err = core.Wrapf(err, "wg%v.pub", ring)
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = out.Validate()
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) tryReadWireguardKeys(ring int) error {
|
|
||||||
kp, err := m.GetWireguardKeys(ring)
|
|
||||||
switch {
|
|
||||||
case os.IsNotExist(err):
|
|
||||||
// ignore
|
|
||||||
return nil
|
|
||||||
case err != nil:
|
|
||||||
// something went wrong
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
// import keys
|
|
||||||
ri := &RingInfo{
|
|
||||||
Ring: ring,
|
|
||||||
Keys: kp,
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.applyRingInfo(ring, ri)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
// GetWireguardConfig reads a wgN.conf file
|
||||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||||
data, err := m.ReadFile("wg%v.conf", ring)
|
data, err := m.ReadFile("wg%v.conf", ring)
|
||||||
@@ -178,9 +61,9 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
||||||
for _, ri := range m.Rings {
|
for _, ri := range m.RingAddresses {
|
||||||
if ri.Ring == ring {
|
if ri.Ring == ring {
|
||||||
return ri, ri.Enabled
|
return ri, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,10 +71,10 @@ func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
||||||
cur, _ := m.getRingInfo(ring)
|
cur, found := m.getRingInfo(ring)
|
||||||
if cur == nil {
|
if !found {
|
||||||
// first, append
|
// first, append
|
||||||
m.Rings = append(m.Rings, new)
|
m.RingAddresses = append(m.RingAddresses, new)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +86,8 @@ func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.Interfa
|
|||||||
ri := &RingInfo{
|
ri := &RingInfo{
|
||||||
Ring: ring,
|
Ring: ring,
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Keys: wireguard.KeyPair{
|
Address: data.Address,
|
||||||
|
Keys: &wireguard.KeyPair{
|
||||||
PrivateKey: data.PrivateKey,
|
PrivateKey: data.PrivateKey,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -223,7 +107,7 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
|||||||
ri := &RingInfo{
|
ri := &RingInfo{
|
||||||
Ring: ring,
|
Ring: ring,
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Keys: wireguard.KeyPair{
|
Keys: &wireguard.KeyPair{
|
||||||
PublicKey: pc.PublicKey,
|
PublicKey: pc.PublicKey,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -240,8 +124,8 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
|||||||
return fmt.Errorf("invalid %s", "zoneID")
|
return fmt.Errorf("invalid %s", "zoneID")
|
||||||
case nodeID == 0:
|
case nodeID == 0:
|
||||||
return fmt.Errorf("invalid %s", "nodeID")
|
return fmt.Errorf("invalid %s", "nodeID")
|
||||||
case m.ID != nodeID:
|
case m.ID() != nodeID:
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID, nodeID)
|
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID(), nodeID)
|
||||||
case m.zone.ID != 0 && m.zone.ID != zoneID:
|
case m.zone.ID != 0 && m.zone.ID != zoneID:
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
|
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
|
||||||
case m.zone.ID == 0:
|
case m.zone.ID == 0:
|
||||||
@@ -250,14 +134,3 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
|||||||
|
|
||||||
return nil
|
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,7 +3,6 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,32 +25,6 @@ func (m *Machine) updatePublicAddresses() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 {
|
func (m *Machine) scan() error {
|
||||||
for i := 0; i < RingsCount; i++ {
|
for i := 0; i < RingsCount; i++ {
|
||||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||||
|
|||||||
+43
-29
@@ -14,58 +14,75 @@ const (
|
|||||||
MaxNodeID = 0xff - 1
|
MaxNodeID = 0xff - 1
|
||||||
// RingsCount indicates how many wireguard rings we have
|
// RingsCount indicates how many wireguard rings we have
|
||||||
RingsCount = 2
|
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
|
// RingInfo contains represents the Wireguard endpoint details
|
||||||
// for a Machine on a particular ring
|
// for a Machine on a particular ring
|
||||||
type RingInfo struct {
|
type RingInfo struct {
|
||||||
Ring int `toml:"ring"`
|
Ring int `toml:"ring"`
|
||||||
Enabled bool `toml:"enabled,omitempty"`
|
Enabled bool `toml:"enabled,omitempty"`
|
||||||
Keys wireguard.KeyPair `toml:"keys,omitempty"`
|
Keys *wireguard.KeyPair `toml:"keys,omitempty"`
|
||||||
|
Address netip.Addr `toml:"address,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge attempts to combine two RingInfo structs
|
// Merge attempts to combine two RingInfo structs
|
||||||
func (ri *RingInfo) Merge(alter *RingInfo) error {
|
func (ri *RingInfo) Merge(alter *RingInfo) error {
|
||||||
switch {
|
switch {
|
||||||
case alter == nil:
|
|
||||||
return nil
|
|
||||||
case ri.Ring != alter.Ring:
|
case ri.Ring != alter.Ring:
|
||||||
// different ring
|
// different ring
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
|
return fmt.Errorf("invalid %s: %v ≠ %v", "ring", ri.Ring, alter.Ring)
|
||||||
case ri.Enabled && !alter.Enabled:
|
case ri.Enabled != alter.Enabled:
|
||||||
// can't disable via Merge
|
// different state
|
||||||
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
|
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):
|
case !canMergeKeyPairs(ri.Keys, alter.Keys):
|
||||||
// incompatible keypairs
|
// incompatible keypairs
|
||||||
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ri.unsafeMerge(alter)
|
switch {
|
||||||
}
|
case ri.Keys == nil:
|
||||||
|
// assign keypair
|
||||||
func (ri *RingInfo) unsafeMerge(alter *RingInfo) error {
|
ri.Keys = alter.Keys
|
||||||
// enable via Merge
|
case alter.Keys != nil:
|
||||||
if alter.Enabled {
|
// fill the gaps on our keypair
|
||||||
ri.Enabled = true
|
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 addressEqual(ri.Address, netip.Addr{}) {
|
||||||
if ri.Keys.PrivateKey.IsZero() {
|
// assign address
|
||||||
ri.Keys.PrivateKey = alter.Keys.PrivateKey
|
ri.Address = alter.Address
|
||||||
}
|
|
||||||
if ri.Keys.PublicKey.IsZero() {
|
|
||||||
ri.Keys.PublicKey = alter.Keys.PublicKey
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
|
func canMergeAddress(ip1, ip2 netip.Addr) bool {
|
||||||
|
var zero netip.Addr
|
||||||
|
|
||||||
switch {
|
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):
|
case !p1.PrivateKey.IsZero() && !p2.PrivateKey.IsZero() && !p1.PrivateKey.Equal(p2.PrivateKey):
|
||||||
return false
|
return false
|
||||||
case !p1.PublicKey.IsZero() && !p2.PublicKey.IsZero() && !p1.PublicKey.Equal(p2.PublicKey):
|
case !p1.PublicKey.IsZero() && !p2.PublicKey.IsZero() && !p1.PublicKey.Equal(p2.PublicKey):
|
||||||
@@ -79,7 +96,6 @@ func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
|
|||||||
// Wireguard ring
|
// Wireguard ring
|
||||||
type RingAddressEncoder struct {
|
type RingAddressEncoder struct {
|
||||||
ID int
|
ID int
|
||||||
Port uint16
|
|
||||||
Encode func(zoneID, nodeID int) (netip.Addr, bool)
|
Encode func(zoneID, nodeID int) (netip.Addr, bool)
|
||||||
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
|
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
|
||||||
}
|
}
|
||||||
@@ -88,14 +104,12 @@ var (
|
|||||||
// RingZero is a wg0 address encoder/decoder
|
// RingZero is a wg0 address encoder/decoder
|
||||||
RingZero = RingAddressEncoder{
|
RingZero = RingAddressEncoder{
|
||||||
ID: 0,
|
ID: 0,
|
||||||
Port: RingZeroPort,
|
|
||||||
Decode: ParseRingZeroAddress,
|
Decode: ParseRingZeroAddress,
|
||||||
Encode: RingZeroAddress,
|
Encode: RingZeroAddress,
|
||||||
}
|
}
|
||||||
// RingOne is a wg1 address encoder/decoder
|
// RingOne is a wg1 address encoder/decoder
|
||||||
RingOne = RingAddressEncoder{
|
RingOne = RingAddressEncoder{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
Port: RingOnePort,
|
|
||||||
Decode: ParseRingOneAddress,
|
Decode: ParseRingOneAddress,
|
||||||
Encode: RingOneAddress,
|
Encode: RingOneAddress,
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-9
@@ -93,8 +93,8 @@ func (m *Zones) scanSort() error {
|
|||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
sort.SliceStable(z.Machines, func(i, j int) bool {
|
sort.SliceStable(z.Machines, func(i, j int) bool {
|
||||||
id1 := z.Machines[i].ID
|
id1 := z.Machines[i].ID()
|
||||||
id2 := z.Machines[j].ID
|
id2 := z.Machines[j].ID()
|
||||||
return id1 < id2
|
return id1 < id2
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -102,9 +102,9 @@ func (m *Zones) scanSort() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
sort.SliceStable(p.Rings, func(i, j int) bool {
|
sort.SliceStable(p.RingAddresses, func(i, j int) bool {
|
||||||
ri1 := p.Rings[i]
|
ri1 := p.RingAddresses[i]
|
||||||
ri2 := p.Rings[j]
|
ri2 := p.RingAddresses[j]
|
||||||
|
|
||||||
return ri1.Ring < ri2.Ring
|
return ri1.Ring < ri2.Ring
|
||||||
})
|
})
|
||||||
@@ -129,10 +129,6 @@ func (z *Zone) scan() error {
|
|||||||
Name: e.Name(),
|
Name: e.Name(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
z.Machines = append(z.Machines, m)
|
z.Machines = append(z.Machines, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user