|
|
|
@ -13,14 +13,21 @@ import (
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// GetWireguardKeys reads a wgN.key/wgN.pub files
|
|
|
|
|
func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) { |
|
|
|
|
func (m *Machine) GetWireguardKeys(ringID rings.RingID) (wireguard.KeyPair, error) { |
|
|
|
|
var ( |
|
|
|
|
data []byte |
|
|
|
|
err error |
|
|
|
|
out wireguard.KeyPair |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
data, err = m.ReadFile("wg%v.key", ring) |
|
|
|
|
ring, err := AsWireguardInterfaceID(ringID) |
|
|
|
|
if err != nil { |
|
|
|
|
// invalid ring
|
|
|
|
|
return out, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
keyFile, pubFile, _ := ring.Files() |
|
|
|
|
|
|
|
|
|
data, err = m.ReadFile(keyFile) |
|
|
|
|
if err != nil { |
|
|
|
|
// failed to read
|
|
|
|
|
return out, err |
|
|
|
@ -29,11 +36,11 @@ func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
|
|
|
|
|
out.PrivateKey, err = wireguard.PrivateKeyFromBase64(string(data)) |
|
|
|
|
if err != nil { |
|
|
|
|
// bad key
|
|
|
|
|
err = core.Wrap(err, "wg%v.key", ring) |
|
|
|
|
err = core.Wrap(err, keyFile) |
|
|
|
|
return out, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data, err = m.ReadFile("wg%v.pub", ring) |
|
|
|
|
data, err = m.ReadFile(pubFile) |
|
|
|
|
switch { |
|
|
|
|
case os.IsNotExist(err): |
|
|
|
|
// no wgN.pub is fine
|
|
|
|
@ -45,7 +52,7 @@ func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
|
|
|
|
|
out.PublicKey, err = wireguard.PublicKeyFromBase64(string(data)) |
|
|
|
|
if err != nil { |
|
|
|
|
// bad key
|
|
|
|
|
err = core.Wrap(err, "wg%v.pub", ring) |
|
|
|
|
err = core.Wrap(err, pubFile) |
|
|
|
|
return out, err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -54,8 +61,8 @@ func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
|
|
|
|
|
return out, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) tryReadWireguardKeys(ring int) error { |
|
|
|
|
kp, err := m.GetWireguardKeys(ring) |
|
|
|
|
func (m *Machine) tryReadWireguardKeys(ringID rings.RingID) error { |
|
|
|
|
kp, err := m.GetWireguardKeys(ringID) |
|
|
|
|
switch { |
|
|
|
|
case os.IsNotExist(err): |
|
|
|
|
// ignore
|
|
|
|
@ -66,20 +73,25 @@ func (m *Machine) tryReadWireguardKeys(ring int) error {
|
|
|
|
|
default: |
|
|
|
|
// import keys
|
|
|
|
|
ri := &RingInfo{ |
|
|
|
|
Ring: ring, |
|
|
|
|
Ring: MustWireguardInterfaceID(ringID), |
|
|
|
|
Keys: kp, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return m.applyRingInfo(ring, ri) |
|
|
|
|
return m.applyRingInfo(ringID, ri) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
|
|
|
|
// the machine's config directory
|
|
|
|
|
func (m *Machine) RemoveWireguardKeys(ring int) error { |
|
|
|
|
var err error |
|
|
|
|
func (m *Machine) RemoveWireguardKeys(ringID rings.RingID) error { |
|
|
|
|
ring, err := AsWireguardInterfaceID(ringID) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = m.RemoveFile("wg%v.pub", ring) |
|
|
|
|
keyFile, pubFile, _ := ring.Files() |
|
|
|
|
|
|
|
|
|
err = m.RemoveFile(pubFile) |
|
|
|
|
switch { |
|
|
|
|
case os.IsNotExist(err): |
|
|
|
|
// ignore
|
|
|
|
@ -87,7 +99,7 @@ func (m *Machine) RemoveWireguardKeys(ring int) error {
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = m.RemoveFile("wg%v.key", ring) |
|
|
|
|
err = m.RemoveFile(keyFile) |
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
// ignore
|
|
|
|
|
err = nil |
|
|
|
@ -97,8 +109,13 @@ func (m *Machine) RemoveWireguardKeys(ring int) error {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetWireguardConfig reads a wgN.conf file
|
|
|
|
|
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) { |
|
|
|
|
data, err := m.ReadFile("wg%v.conf", ring) |
|
|
|
|
func (m *Machine) GetWireguardConfig(ringID rings.RingID) (*wireguard.Config, error) { |
|
|
|
|
ring, err := AsWireguardInterfaceID(ringID) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data, err := m.ReadFile(ring.ConfFile()) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -107,7 +124,7 @@ func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
|
|
|
|
return wireguard.NewConfigFromReader(r) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) tryApplyWireguardConfig(ring int) error { |
|
|
|
|
func (m *Machine) tryApplyWireguardConfig(ring rings.RingID) error { |
|
|
|
|
wg, err := m.GetWireguardConfig(ring) |
|
|
|
|
switch { |
|
|
|
|
case os.IsNotExist(err): |
|
|
|
@ -119,7 +136,7 @@ func (m *Machine) tryApplyWireguardConfig(ring int) error {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) applyWireguardConfigNode(ring int, wg *wireguard.Config) error { |
|
|
|
|
func (m *Machine) applyWireguardConfigNode(ring rings.RingID, wg *wireguard.Config) error { |
|
|
|
|
addr := wg.GetAddress() |
|
|
|
|
if !core.IsZero(addr) { |
|
|
|
|
zoneID, nodeID, ok := Rings[ring].Decode(addr) |
|
|
|
@ -139,7 +156,7 @@ func (m *Machine) applyWireguardConfigNode(ring int, wg *wireguard.Config) error
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error { |
|
|
|
|
func (m *Machine) applyWireguardConfig(ring rings.RingID, wg *wireguard.Config) error { |
|
|
|
|
if err := m.applyWireguardConfigNode(ring, wg); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -153,7 +170,7 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
|
|
|
|
WithField("subsystem", "wireguard"). |
|
|
|
|
WithField("node", m.Name). |
|
|
|
|
WithField("peer", peer.Endpoint.Host). |
|
|
|
|
WithField("ring", ring). |
|
|
|
|
WithField("ring", MustWireguardInterfaceID(ring)). |
|
|
|
|
Print("ignoring unknown endpoint") |
|
|
|
|
case err != nil: |
|
|
|
|
return core.Wrap(err, "peer") |
|
|
|
@ -163,9 +180,9 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) { |
|
|
|
|
func (m *Machine) getRingInfo(ring rings.RingID) (*RingInfo, bool) { |
|
|
|
|
for _, ri := range m.Rings { |
|
|
|
|
if ri.Ring == ring { |
|
|
|
|
if ri.RingID() == ring { |
|
|
|
|
return ri, ri.Enabled |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -173,13 +190,13 @@ func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
|
|
|
|
|
return nil, false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error { |
|
|
|
|
func (m *Machine) applyRingInfo(ring rings.RingID, new *RingInfo) error { |
|
|
|
|
cur, _ := m.getRingInfo(ring) |
|
|
|
|
if cur == nil { |
|
|
|
|
// first, append
|
|
|
|
|
m.debug(). |
|
|
|
|
WithField("node", m.Name). |
|
|
|
|
WithField("ring", ring). |
|
|
|
|
WithField("ring", MustWireguardInterfaceID(ring)). |
|
|
|
|
Print("found") |
|
|
|
|
m.Rings = append(m.Rings, new) |
|
|
|
|
return nil |
|
|
|
@ -189,9 +206,11 @@ func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
|
|
|
|
|
return cur.Merge(new) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.InterfaceConfig) error { |
|
|
|
|
func (m *Machine) applyWireguardInterfaceConfig(ring rings.RingID, |
|
|
|
|
data wireguard.InterfaceConfig) error { |
|
|
|
|
//
|
|
|
|
|
ri := &RingInfo{ |
|
|
|
|
Ring: ring, |
|
|
|
|
Ring: MustWireguardInterfaceID(ring), |
|
|
|
|
Enabled: true, |
|
|
|
|
Keys: wireguard.KeyPair{ |
|
|
|
|
PrivateKey: data.PrivateKey, |
|
|
|
@ -201,7 +220,7 @@ func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.Interfa
|
|
|
|
|
return m.applyRingInfo(ring, ri) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) error { |
|
|
|
|
func (m *Machine) applyWireguardPeerConfig(ring rings.RingID, pc wireguard.PeerConfig) error { |
|
|
|
|
peer, found := m.getPeerByName(pc.Endpoint.Name()) |
|
|
|
|
switch { |
|
|
|
|
case !found: |
|
|
|
@ -213,7 +232,7 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
|
|
|
|
default: |
|
|
|
|
// apply RingInfo
|
|
|
|
|
ri := &RingInfo{ |
|
|
|
|
Ring: ring, |
|
|
|
|
Ring: MustWireguardInterfaceID(ring), |
|
|
|
|
Enabled: true, |
|
|
|
|
Keys: wireguard.KeyPair{ |
|
|
|
|
PublicKey: pc.PublicKey, |
|
|
|
@ -260,8 +279,13 @@ func (m *Machine) setRingDefaults(ri *RingInfo) error {
|
|
|
|
|
|
|
|
|
|
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
|
|
|
|
// config directory.
|
|
|
|
|
func (m *Machine) RemoveWireguardConfig(ring int) error { |
|
|
|
|
err := m.RemoveFile("wg%v.conf", ring) |
|
|
|
|
func (m *Machine) RemoveWireguardConfig(ringID rings.RingID) error { |
|
|
|
|
ring, err := AsWireguardInterfaceID(ringID) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = m.RemoveFile(ring.ConfFile()) |
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
err = nil |
|
|
|
|
} |
|
|
|
@ -269,7 +293,12 @@ func (m *Machine) RemoveWireguardConfig(ring int) error {
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) { |
|
|
|
|
func (m *Machine) createRingInfo(ringID rings.RingID, enabled bool) (*RingInfo, error) { |
|
|
|
|
ring, err := AsWireguardInterfaceID(ringID) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
keys, err := wireguard.NewKeyPair() |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
@ -281,7 +310,7 @@ func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
|
|
|
|
|
Keys: keys, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = m.applyRingInfo(ring, ri) |
|
|
|
|
err = m.applyRingInfo(ringID, ri) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|