@@ -39,6 +39,24 @@ func (m *Machine) IsGateway() bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGateway enables/disables a Machine ring0 integration
|
||||||
|
func (m *Machine) SetGateway(enabled bool) error {
|
||||||
|
ri, found := m.getRingInfo(0)
|
||||||
|
switch {
|
||||||
|
case !found && !enabled:
|
||||||
|
return nil
|
||||||
|
case !found:
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if ri, err = m.createRingInfo(0, false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ri.Enabled = enabled
|
||||||
|
return m.syncRingConfig(0)
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -261,3 +261,57 @@ func (m *Machine) RemoveWireguardConfig(ring int) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*Machine) syncRingConfig(_ int) error {
|
||||||
|
// _, err := m.getRingNodes(ring)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
|
||||||
|
keys, err := wireguard.NewKeyPair()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ri := &RingInfo{
|
||||||
|
Ring: ring,
|
||||||
|
Enabled: enabled,
|
||||||
|
Keys: keys,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.applyRingInfo(ring, ri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ri, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeRingInfo(ri *RingInfo) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if m == nil || ri == nil {
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.WriteWireguardKeys(ri.Ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ri.Enabled {
|
||||||
|
return m.RemoveWireguardConfig(ri.Ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
|
||||||
|
f, err := m.CreateTruncFile("wg%v.conf", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,6 +34,31 @@ func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||||
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||||
|
var err error
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.ID == gatewayID {
|
||||||
|
found = true
|
||||||
|
err = p.SetGateway(enabled)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
case !found:
|
||||||
|
return fs.ErrNotExist
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Zones represents all zones in a cluster
|
// Zones represents all zones in a cluster
|
||||||
type Zones struct {
|
type Zones struct {
|
||||||
dir fs.FS
|
dir fs.FS
|
||||||
|
|||||||
Reference in New Issue
Block a user