Compare commits
2 Commits
v0.3.1
..
7b8f46fd7f
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b8f46fd7f | |||
| 34f0b910c8 |
@@ -10,7 +10,6 @@ require (
|
|||||||
github.com/burntSushi/toml v0.3.1
|
github.com/burntSushi/toml v0.3.1
|
||||||
github.com/mgechev/revive v1.3.2
|
github.com/mgechev/revive v1.3.2
|
||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
gopkg.in/gcfg.v1 v1.2.3
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -70,8 +70,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
|||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
|
||||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
|
||||||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
|
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
|
||||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
|||||||
+2
-61
@@ -2,11 +2,8 @@ package wireguard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -19,10 +16,6 @@ const (
|
|||||||
var (
|
var (
|
||||||
// ErrInvalidKeySize indicates the key size is wrong
|
// ErrInvalidKeySize indicates the key size is wrong
|
||||||
ErrInvalidKeySize = errors.New("invalid key size")
|
ErrInvalidKeySize = errors.New("invalid key size")
|
||||||
// ErrInvalidPrivateKey indicates the private key is invalid
|
|
||||||
ErrInvalidPrivateKey = errors.New("invalid private key")
|
|
||||||
// ErrInvalidPublicKey indicates the public key is invalid
|
|
||||||
ErrInvalidPublicKey = errors.New("invalid public key")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -96,34 +89,9 @@ func decodeKey(data string, size int) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPrivateKey creates a new PrivateKey
|
func NewPrivateKey() (PrivateKey, error)
|
||||||
func NewPrivateKey() (PrivateKey, error) {
|
|
||||||
var s [PrivateKeySize]byte
|
|
||||||
|
|
||||||
_, err := rand.Read(s[:])
|
func (PrivateKey) Public() PublicKey
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// apply same clamping as wireguard-go/device/noise-helpers.go
|
|
||||||
s[0] &= 0xf8
|
|
||||||
s[31] = (s[31] & 0x7f) | 0x40
|
|
||||||
|
|
||||||
return s[:], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Public generates the corresponding PublicKey
|
|
||||||
func (key PrivateKey) Public() PublicKey {
|
|
||||||
if len(key) != PrivateKeySize {
|
|
||||||
return []byte{}
|
|
||||||
}
|
|
||||||
|
|
||||||
out := [PublicKeySize]byte{}
|
|
||||||
in := (*[PrivateKeySize]byte)(key)
|
|
||||||
|
|
||||||
curve25519.ScalarBaseMult(&out, in)
|
|
||||||
return out[:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// KeyPair holds a Key pair
|
// KeyPair holds a Key pair
|
||||||
type KeyPair struct {
|
type KeyPair struct {
|
||||||
@@ -131,33 +99,6 @@ type KeyPair struct {
|
|||||||
PublicKey PublicKey
|
PublicKey PublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks the PublicKey matches the PrivateKey,
|
|
||||||
// and sets the PublicKey if missing
|
|
||||||
func (kp *KeyPair) Validate() error {
|
|
||||||
keyLen := len(kp.PrivateKey)
|
|
||||||
pubLen := len(kp.PublicKey)
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case keyLen != PrivateKeySize:
|
|
||||||
// bad private key
|
|
||||||
return ErrInvalidPrivateKey
|
|
||||||
case pubLen == 0:
|
|
||||||
// no public key, set it
|
|
||||||
kp.PublicKey = kp.PrivateKey.Public()
|
|
||||||
return nil
|
|
||||||
case pubLen != PublicKeySize:
|
|
||||||
// bad public key
|
|
||||||
return ErrInvalidPublicKey
|
|
||||||
case !kp.PrivateKey.Public().Equal(kp.PublicKey):
|
|
||||||
// wrong public key
|
|
||||||
return ErrInvalidPublicKey
|
|
||||||
default:
|
|
||||||
// correct public key
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewKeyPair creates a new KeyPair for Wireguard
|
|
||||||
func NewKeyPair() (*KeyPair, error) {
|
func NewKeyPair() (*KeyPair, error) {
|
||||||
key, err := NewPrivateKey()
|
key, err := NewPrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+1
-1
@@ -88,7 +88,7 @@ func getRingZeroGatewayID(z *Zone) int {
|
|||||||
firstNodeID = nodeID
|
firstNodeID = nodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, found := p.getRingInfo(0); found {
|
if p.Gateway() {
|
||||||
gatewayID = nodeID
|
gatewayID = nodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 != "" {
|
||||||
|
|||||||
+15
-71
@@ -10,77 +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
|
|
||||||
key wireguard.PrivateKey
|
|
||||||
pub wireguard.PublicKey
|
|
||||||
)
|
|
||||||
|
|
||||||
data, err = m.ReadFile("wg%v.key", ring)
|
|
||||||
if err != nil {
|
|
||||||
// failed to read
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
key, err = wireguard.PrivateKeyFromBase64(string(data))
|
|
||||||
if err != nil {
|
|
||||||
// bad key
|
|
||||||
err = core.Wrapf(err, "wg%v.key", ring)
|
|
||||||
return nil, 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 nil, err
|
|
||||||
default:
|
|
||||||
// good read
|
|
||||||
pub, err = wireguard.PublicKeyFromBase64(string(data))
|
|
||||||
if err != nil {
|
|
||||||
// bad key
|
|
||||||
err = core.Wrapf(err, "wg%v.pub", ring)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
kp := &wireguard.KeyPair{
|
|
||||||
PrivateKey: key,
|
|
||||||
PublicKey: pub,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = kp.Validate(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return kp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
@@ -205,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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,15 +25,6 @@ func (m *Machine) updatePublicAddresses() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) init() error {
|
|
||||||
for i := 0; i < RingsCount; i++ {
|
|
||||||
if err := m.tryReadWireguardKeys(i); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 {
|
||||||
|
|||||||
+3
-12
@@ -31,9 +31,9 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
|
|||||||
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):
|
case !canMergeAddress(ri.Address, alter.Address):
|
||||||
// different address
|
// different address
|
||||||
return fmt.Errorf("invalid %s: %v ≠ %v", "address", ri.Address, alter.Address)
|
return fmt.Errorf("invalid %s: %v ≠ %v", "address", ri.Address, alter.Address)
|
||||||
@@ -42,15 +42,6 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
|
|||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ri *RingInfo) unsafeMerge(alter *RingInfo) error {
|
|
||||||
// enable via Merge
|
|
||||||
if alter.Enabled {
|
|
||||||
ri.Enabled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case ri.Keys == nil:
|
case ri.Keys == nil:
|
||||||
// assign keypair
|
// assign keypair
|
||||||
|
|||||||
@@ -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