Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 09678d8eb8 | |||
| 3662fc7510 | |||
| cade4b83bc | |||
| 2bff61d6f2 | |||
| b537a4438d |
+5
-10
@@ -15,24 +15,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
||||||
"PrefixJoin": func(a []netip.Prefix, sep string) string {
|
"StringsJoin": strings.Join,
|
||||||
s := make([]string, len(a))
|
|
||||||
for i, p := range a {
|
|
||||||
s[i] = p.String()
|
|
||||||
}
|
|
||||||
return strings.Join(s, sep)
|
|
||||||
},
|
|
||||||
}).Parse(`[Interface]
|
}).Parse(`[Interface]
|
||||||
Address = {{.Interface.Address}}
|
Address = {{.Interface.Address}}
|
||||||
PrivateKey = {{.Interface.PrivateKey}}
|
PrivateKey = {{.Interface.PrivateKey}}
|
||||||
ListenPort = {{.Interface.ListenPort}}
|
ListenPort = {{.Interface.ListenPort}}
|
||||||
{{- range .Peer }}
|
{{range .Peer}}
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
|
# {{.Endpoint.Name}}
|
||||||
PublicKey = {{.PublicKey}}
|
PublicKey = {{.PublicKey}}
|
||||||
Endpoint = {{.Endpoint}}
|
Endpoint = {{.Endpoint}}
|
||||||
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
AllowedIPs = {{ StringsJoin .AllowedIPs ", "}}
|
||||||
{{- end }}
|
{{end}}
|
||||||
`))
|
`))
|
||||||
|
|
||||||
// Config represents a wgN.conf file
|
// Config represents a wgN.conf file
|
||||||
|
|||||||
@@ -61,11 +61,6 @@ func (m *Machine) SetGateway(enabled bool) error {
|
|||||||
return m.SyncWireguardConfig(0)
|
return m.SyncWireguardConfig(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zone indicates the [Zone] this machine belongs to
|
|
||||||
func (m *Machine) Zone() int {
|
|
||||||
return m.zone.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -268,23 +268,22 @@ func (m *Machine) SyncWireguardConfig(ring int) error {
|
|||||||
return m.zone.SyncWireguardConfig(ring)
|
return m.zone.SyncWireguardConfig(ring)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteWireguardConfig ...
|
|
||||||
func (m *Machine) WriteWireguardConfig(ring int) error {
|
func (m *Machine) WriteWireguardConfig(ring int) error {
|
||||||
r, err := NewRing(m.zone, ring)
|
r, err := m.zone.GetRing(ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.writeWireguardRingConfig(r)
|
return m.writeWireguardRing(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
|
func (m *Machine) writeWireguardRing(r *Ring) error {
|
||||||
wg, err := r.ExportConfig(m)
|
wg, err := r.ExportConfig(m.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
|
f, err := m.CreateTruncFile("wg%v.conf", r.Ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-173
@@ -2,7 +2,6 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
@@ -176,178 +175,7 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
|||||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||||
return netip.Addr{}, false
|
return netip.Addr{}, false
|
||||||
default:
|
default:
|
||||||
a4 := [4]uint8{10, uint8(zoneID << 4), 0, uint8(nodeID)}
|
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
|
||||||
return netip.AddrFrom4(a4), true
|
return netip.AddrFrom4(a4), true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
_ MachineIterator = (*Ring)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
// A Ring describes all peers on a ring
|
|
||||||
type Ring struct {
|
|
||||||
RingAddressEncoder
|
|
||||||
|
|
||||||
Peers []*RingPeer
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddPeer adds a [Machine] to the ring
|
|
||||||
func (r *Ring) AddPeer(p *Machine) bool {
|
|
||||||
_, ok := p.getRingInfo(r.ID)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
rp := r.newPeer(p)
|
|
||||||
r.Peers = append(r.Peers, rp)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Ring) newPeer(p *Machine) *RingPeer {
|
|
||||||
nodeID := p.ID
|
|
||||||
zoneID := p.Zone()
|
|
||||||
|
|
||||||
ri, _ := p.getRingInfo(r.ID)
|
|
||||||
addr, _ := r.Encode(zoneID, nodeID)
|
|
||||||
|
|
||||||
rp := &RingPeer{
|
|
||||||
Node: p,
|
|
||||||
Address: addr,
|
|
||||||
PrivateKey: ri.Keys.PrivateKey,
|
|
||||||
PeerConfig: wireguard.PeerConfig{
|
|
||||||
PublicKey: ri.Keys.PublicKey,
|
|
||||||
Endpoint: wireguard.EndpointAddress{
|
|
||||||
Host: p.FullName(),
|
|
||||||
Port: r.Port,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.ID == 0 {
|
|
||||||
rp.AllowRingOneNetwork(zoneID)
|
|
||||||
rp.AllowIP(addr)
|
|
||||||
} else {
|
|
||||||
rp.AllowIP(addr)
|
|
||||||
|
|
||||||
if p.IsGateway() {
|
|
||||||
zones := p.zone.zones
|
|
||||||
rp.AllowOtherRingOneNetworks(zones, zoneID)
|
|
||||||
rp.AllowRingZeroGateways(zones)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return rp
|
|
||||||
}
|
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the ring
|
|
||||||
// until instructed to terminate the loop
|
|
||||||
func (r *Ring) ForEachMachine(fn func(*Machine) bool) {
|
|
||||||
for _, pp := range r.Peers {
|
|
||||||
if fn(pp.Node) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExportConfig ...
|
|
||||||
func (r *Ring) ExportConfig(p *Machine) (*wireguard.Config, error) {
|
|
||||||
cur, ok := r.getMachine(p)
|
|
||||||
if !ok {
|
|
||||||
return nil, fs.ErrNotExist
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &wireguard.Config{
|
|
||||||
Interface: wireguard.InterfaceConfig{
|
|
||||||
Address: cur.Address,
|
|
||||||
PrivateKey: cur.PrivateKey,
|
|
||||||
ListenPort: r.Port,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, pp := range r.Peers {
|
|
||||||
if pp.Node != p {
|
|
||||||
pc := pp.PeerConfig
|
|
||||||
out.Peer = append(out.Peer, pc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Ring) getMachine(p *Machine) (*RingPeer, bool) {
|
|
||||||
for _, pp := range r.Peers {
|
|
||||||
if pp.Node == p {
|
|
||||||
return pp, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// A RingPeer is a node on a [Ring]
|
|
||||||
type RingPeer struct {
|
|
||||||
Node *Machine
|
|
||||||
|
|
||||||
Address netip.Addr
|
|
||||||
PrivateKey wireguard.PrivateKey
|
|
||||||
PeerConfig wireguard.PeerConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllowIP ...
|
|
||||||
func (rp *RingPeer) AllowIP(addr netip.Addr) {
|
|
||||||
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs,
|
|
||||||
netip.PrefixFrom(addr, 32),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllowNetwork ...
|
|
||||||
func (rp *RingPeer) AllowNetwork(addr netip.Addr, bits int) {
|
|
||||||
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs,
|
|
||||||
netip.PrefixFrom(addr, bits),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllowRingOneNetwork ...
|
|
||||||
func (rp *RingPeer) AllowRingOneNetwork(zoneID int) {
|
|
||||||
addr, _ := RingOneAddress(zoneID, 0)
|
|
||||||
rp.AllowNetwork(addr, 12)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllowOtherRingOneNetworks ...
|
|
||||||
func (rp *RingPeer) AllowOtherRingOneNetworks(m *Zones, zoneID int) {
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
if z.ID != zoneID {
|
|
||||||
addr, _ := RingOneAddress(z.ID, 0)
|
|
||||||
rp.AllowNetwork(addr, 12)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllowRingZeroGateways ...
|
|
||||||
func (rp *RingPeer) AllowRingZeroGateways(m *Zones) {
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
switch {
|
|
||||||
case !p.IsGateway():
|
|
||||||
// skip regular nodes
|
|
||||||
default:
|
|
||||||
addr, _ := RingZeroAddress(p.Zone(), p.ID)
|
|
||||||
rp.AllowIP(addr)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRing ...
|
|
||||||
func NewRing(m MachineIterator, ring int) (*Ring, error) {
|
|
||||||
r := &Ring{
|
|
||||||
RingAddressEncoder: Rings[ring],
|
|
||||||
}
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
r.AddPeer(p)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|||||||
+56
-67
@@ -1,88 +1,39 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/wireguard"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type Ring struct {
|
||||||
_ machineRinger = (*Zone)(nil)
|
Ring int
|
||||||
_ machineRinger = (*Zones)(nil)
|
}
|
||||||
)
|
|
||||||
|
|
||||||
type machineRinger interface {
|
func (*Ring) ExportConfig(_ int) (*wireguard.Config, error) {
|
||||||
MachineIterator
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
PruneWireguardConfig(ring int) error
|
func (*Zone) GetRing(_ int) (*Ring, error) {
|
||||||
|
return &Ring{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
// ring
|
// ring
|
||||||
func (z *Zone) SyncWireguardConfig(ring int) error {
|
func (z *Zone) SyncWireguardConfig(ring int) error {
|
||||||
switch ring {
|
err := z.PruneWireguardConfig(ring)
|
||||||
case 0:
|
|
||||||
return syncWireguardConfig(z.zones, ring)
|
|
||||||
case 1:
|
|
||||||
return syncWireguardConfig(z, ring)
|
|
||||||
default:
|
|
||||||
return fs.ErrInvalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled.
|
|
||||||
func (z *Zone) PruneWireguardConfig(ring int) error {
|
|
||||||
return pruneWireguardConfig(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
||||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
||||||
// ring
|
|
||||||
func (m *Zones) SyncWireguardConfig(ring int) error {
|
|
||||||
switch ring {
|
|
||||||
case 0:
|
|
||||||
return syncWireguardConfig(m, ring)
|
|
||||||
case 1:
|
|
||||||
var err error
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
err = syncWireguardConfig(z, ring)
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
return fs.ErrInvalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled on all zones
|
|
||||||
func (m *Zones) PruneWireguardConfig(ring int) error {
|
|
||||||
return pruneWireguardConfig(m, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
||||||
func (m *Zones) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(m, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
func syncWireguardConfig(m machineRinger, ring int) error {
|
|
||||||
err := m.PruneWireguardConfig(ring)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := NewRing(m, ring)
|
r, err := z.GetRing(ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
if _, ok := p.getRingInfo(ring); ok {
|
if _, ok := p.getRingInfo(ring); ok {
|
||||||
err = p.writeWireguardRingConfig(r)
|
err = p.writeWireguardRing(r)
|
||||||
}
|
}
|
||||||
return err != nil
|
return err != nil
|
||||||
})
|
})
|
||||||
@@ -90,10 +41,12 @@ func syncWireguardConfig(m machineRinger, ring int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||||
|
// the corresponding ring disabled.
|
||||||
|
func (z *Zone) PruneWireguardConfig(ring int) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
_, ok := p.getRingInfo(ring)
|
_, ok := p.getRingInfo(ring)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = p.RemoveWireguardConfig(ring)
|
err = p.RemoveWireguardConfig(ring)
|
||||||
@@ -104,10 +57,11 @@ func pruneWireguardConfig(m MachineIterator, ring int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeWireguardKeys(m MachineIterator, ring int) error {
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
||||||
|
func (z *Zone) WriteWireguardKeys(ring int) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
err = p.WriteWireguardKeys(ring)
|
err = p.WriteWireguardKeys(ring)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// ignore
|
// ignore
|
||||||
@@ -119,3 +73,38 @@ func writeWireguardKeys(m MachineIterator, ring int) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||||
|
// the corresponding ring disabled on all zones
|
||||||
|
func (m *Zones) PruneWireguardConfig(ring int) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = z.PruneWireguardConfig(ring)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
||||||
|
func (m *Zones) WriteWireguardKeys(ring int) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = z.WriteWireguardKeys(ring)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
|
// ring
|
||||||
|
func (m *Zones) SyncWireguardConfig(ring int) error {
|
||||||
|
err := m.PruneWireguardConfig(ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,16 +10,6 @@ import (
|
|||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
_ MachineIterator = (*Zone)(nil)
|
|
||||||
_ MachineIterator = (*Zones)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
// A MachineIterator is a set of Machines we can iterate on
|
|
||||||
type MachineIterator interface {
|
|
||||||
ForEachMachine(func(*Machine) bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zone represents one zone in a cluster
|
// Zone represents one zone in a cluster
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
|
|||||||
Reference in New Issue
Block a user