Compare commits
3 Commits
c92873f07d
...
589fb2f0e1
| Author | SHA1 | Date | |
|---|---|---|---|
| 589fb2f0e1 | |||
| f5ee63e5aa | |||
| 0de2e3f4d9 |
@@ -1,7 +1,6 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -32,13 +31,13 @@ func (f *Config) Peers() int {
|
||||
// InterfaceConfig represents the [Interface] section
|
||||
type InterfaceConfig struct {
|
||||
Address netip.Addr
|
||||
PrivateKey []byte
|
||||
PrivateKey BinaryKey
|
||||
ListenPort uint16
|
||||
}
|
||||
|
||||
// PeerConfig represents a [Peer] section
|
||||
type PeerConfig struct {
|
||||
PublicKey []byte
|
||||
PublicKey BinaryKey
|
||||
Endpoint EndpointAddress
|
||||
AllowedIPs []netip.Prefix
|
||||
}
|
||||
@@ -129,19 +128,19 @@ type interfaceConfig struct {
|
||||
}
|
||||
|
||||
func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
||||
var err error
|
||||
|
||||
out := InterfaceConfig{
|
||||
Address: p.Address,
|
||||
ListenPort: p.ListenPort,
|
||||
}
|
||||
|
||||
b, err := base64.StdEncoding.DecodeString(p.PrivateKey)
|
||||
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PrivateKey")
|
||||
return InterfaceConfig{}, err
|
||||
}
|
||||
|
||||
out.PrivateKey = b
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -163,8 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
||||
}
|
||||
|
||||
// PublicKey
|
||||
s = v.Peer.PublicKey[i]
|
||||
out.PublicKey, err = base64.StdEncoding.DecodeString(s)
|
||||
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PublicKey")
|
||||
return out, err
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
@@ -16,6 +17,11 @@ 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) {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
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
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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 {
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryApplyWireguardConfig(i); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.updatePublicAddresses()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user