Compare commits
4 Commits
v0.1.0
...
7b8f46fd7f
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b8f46fd7f | |||
| 34f0b910c8 | |||
| 1d8c818ec4 | |||
| 2f51a463b2 |
@@ -31,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 BinaryKey
|
PrivateKey PrivateKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
PublicKey BinaryKey
|
PublicKey PublicKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
}
|
}
|
||||||
@@ -135,7 +135,7 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
|||||||
ListenPort: p.ListenPort,
|
ListenPort: p.ListenPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
|
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PrivateKey")
|
err = core.Wrap(err, "PrivateKey")
|
||||||
return InterfaceConfig{}, err
|
return InterfaceConfig{}, err
|
||||||
@@ -162,7 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey
|
// PublicKey
|
||||||
out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
|
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = core.Wrap(err, "PublicKey")
|
err = core.Wrap(err, "PublicKey")
|
||||||
return out, err
|
return out, err
|
||||||
|
|||||||
+92
-15
@@ -3,34 +3,111 @@ package wireguard
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BinaryKey is a binary blob
|
const (
|
||||||
type BinaryKey []byte
|
// PrivateKeySize is the length in bytes of a Wireguard Private Key
|
||||||
|
PrivateKeySize = 32
|
||||||
|
// PublicKeySize is the length in bytes of a Wireguard Public Key
|
||||||
|
PublicKeySize = 32
|
||||||
|
)
|
||||||
|
|
||||||
func (k BinaryKey) String() string {
|
var (
|
||||||
return base64.StdEncoding.EncodeToString(k)
|
// ErrInvalidKeySize indicates the key size is wrong
|
||||||
|
ErrInvalidKeySize = errors.New("invalid key size")
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// PrivateKey is a binary Wireguard Private Key
|
||||||
|
PrivateKey []byte
|
||||||
|
// PublicKey is a binary Wireguard Public Key
|
||||||
|
PublicKey []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func (key PrivateKey) String() string {
|
||||||
|
return encodeKey(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pub PublicKey) String() string {
|
||||||
|
return encodeKey(pub)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (k BinaryKey) IsZero() bool {
|
func (key PrivateKey) IsZero() bool {
|
||||||
return len(k) == 0
|
return len(key) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal checks if two keys are identical
|
// IsZero tells if the key hasn't been set
|
||||||
func (k BinaryKey) Equal(alter BinaryKey) bool {
|
func (pub PublicKey) IsZero() bool {
|
||||||
return bytes.Equal(k, alter)
|
return len(pub) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// BinaryKeyFromBase64 decodes a base64-based string into
|
// Equal checks if two private keys are identical
|
||||||
// a [BinaryKey]
|
func (key PrivateKey) Equal(alter PrivateKey) bool {
|
||||||
func BinaryKeyFromBase64(data string) (BinaryKey, error) {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivateKeyFromBase64 decodes a base64-based string into
|
||||||
|
// a [PrivateKey]
|
||||||
|
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
|
||||||
|
b, err := decodeKey(data, PrivateKeySize)
|
||||||
|
return b, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeKey(data string, size int) ([]byte, error) {
|
||||||
b, err := base64.StdEncoding.DecodeString(data)
|
b, err := base64.StdEncoding.DecodeString(data)
|
||||||
return BinaryKey(b), err
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return []byte{}, err
|
||||||
|
case len(b) != size:
|
||||||
|
err = ErrInvalidKeySize
|
||||||
|
return []byte{}, err
|
||||||
|
default:
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPrivateKey() (PrivateKey, error)
|
||||||
|
|
||||||
|
func (PrivateKey) Public() PublicKey
|
||||||
|
|
||||||
// KeyPair holds a Key pair
|
// KeyPair holds a Key pair
|
||||||
type KeyPair struct {
|
type KeyPair struct {
|
||||||
PrivateKey BinaryKey
|
PrivateKey PrivateKey
|
||||||
PublicKey BinaryKey
|
PublicKey PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewKeyPair() (*KeyPair, error) {
|
||||||
|
key, err := NewPrivateKey()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &KeyPair{
|
||||||
|
PrivateKey: key,
|
||||||
|
PublicKey: key.Public(),
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-19
@@ -31,25 +31,7 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
|||||||
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
|
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
|
||||||
|
|
||||||
// ZONE{zoneID}_GW
|
// ZONE{zoneID}_GW
|
||||||
firstNodeID := 0
|
gatewayID := getRingZeroGatewayID(z)
|
||||||
gatewayID := 0
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
nodeID := p.ID()
|
|
||||||
|
|
||||||
if firstNodeID == 0 {
|
|
||||||
firstNodeID = nodeID
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, found := p.getRingInfo(0); found {
|
|
||||||
gatewayID = nodeID
|
|
||||||
}
|
|
||||||
|
|
||||||
return gatewayID != 0
|
|
||||||
})
|
|
||||||
|
|
||||||
if gatewayID == 0 {
|
|
||||||
gatewayID = firstNodeID
|
|
||||||
}
|
|
||||||
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
||||||
|
|
||||||
// ZONE{zoneID}_IP
|
// ZONE{zoneID}_IP
|
||||||
@@ -95,3 +77,28 @@ func genEnvZoneNodes(z *Zone) string {
|
|||||||
}
|
}
|
||||||
return strings.Join(s, " ")
|
return strings.Join(s, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRingZeroGatewayID(z *Zone) int {
|
||||||
|
var firstNodeID, gatewayID int
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
nodeID := p.ID()
|
||||||
|
|
||||||
|
if firstNodeID == 0 {
|
||||||
|
firstNodeID = nodeID
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.Gateway() {
|
||||||
|
gatewayID = nodeID
|
||||||
|
}
|
||||||
|
|
||||||
|
return gatewayID != 0
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case gatewayID == 0:
|
||||||
|
return firstNodeID
|
||||||
|
default:
|
||||||
|
return gatewayID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,6 +47,34 @@ func (m *Machine) ID() int {
|
|||||||
return m.id
|
return m.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gateway tells if the Machine is a ring0 gateway
|
||||||
|
func (m *Machine) Gateway() bool {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
if ri, found := m.getRingInfo(0); found {
|
||||||
|
return ri.Enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) SetGateway(enable bool) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
ri, found := m.getRingInfo(0)
|
||||||
|
switch {
|
||||||
|
case !found && !enable:
|
||||||
|
return nil
|
||||||
|
case !found:
|
||||||
|
return m.createRingInfo(0, true)
|
||||||
|
default:
|
||||||
|
ri.Enabled = enable
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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 != "" {
|
||||||
|
|||||||
@@ -134,3 +134,18 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Machine) createRingInfo(ring int, enabled bool) error {
|
||||||
|
keys, err := wireguard.NewKeyPair()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ri := &RingInfo{
|
||||||
|
Ring: ring,
|
||||||
|
Enabled: enabled,
|
||||||
|
Keys: keys,
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.applyRingInfo(ring, ri)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user