Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b95d1f1878 | |||
| d38c909b0b | |||
| 7dd3ea8f96 | |||
| 07b4a22752 | |||
| 609f48a2d1 |
@@ -1,17 +1,40 @@
|
|||||||
package wireguard
|
package wireguard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
"gopkg.in/gcfg.v1"
|
"gopkg.in/gcfg.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
||||||
|
"PrefixJoin": func(a []netip.Prefix, sep string) string {
|
||||||
|
s := make([]string, len(a))
|
||||||
|
for i, p := range a {
|
||||||
|
s[i] = p.String()
|
||||||
|
}
|
||||||
|
return strings.Join(s, sep)
|
||||||
|
},
|
||||||
|
}).Parse(`[Interface]
|
||||||
|
Address = {{.Interface.Address}}
|
||||||
|
PrivateKey = {{.Interface.PrivateKey}}
|
||||||
|
ListenPort = {{.Interface.ListenPort}}
|
||||||
|
{{- range .Peer }}
|
||||||
|
|
||||||
|
[Peer]
|
||||||
|
PublicKey = {{.PublicKey}}
|
||||||
|
Endpoint = {{.Endpoint}}
|
||||||
|
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
||||||
|
{{- end }}
|
||||||
|
`))
|
||||||
|
|
||||||
// Config represents a wgN.conf file
|
// Config represents a wgN.conf file
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Interface InterfaceConfig
|
Interface InterfaceConfig
|
||||||
@@ -28,6 +51,17 @@ func (f *Config) Peers() int {
|
|||||||
return len(f.Peer)
|
return len(f.Peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
|
||||||
|
func (f *Config) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
if err := configTemplate.Execute(&buf, f); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.WriteTo(w)
|
||||||
|
}
|
||||||
|
|
||||||
// InterfaceConfig represents the [Interface] section
|
// InterfaceConfig represents the [Interface] section
|
||||||
type InterfaceConfig struct {
|
type InterfaceConfig struct {
|
||||||
Address netip.Addr
|
Address netip.Addr
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ func (m *Machine) IsGateway() bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ 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"
|
||||||
@@ -179,3 +180,168 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
|||||||
return netip.AddrFrom4(a4), true
|
return netip.AddrFrom4(a4), true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Ring)(nil)
|
||||||
|
_ ZoneIterator = (*Ring)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// A Ring describes all peers on a ring
|
||||||
|
type Ring struct {
|
||||||
|
RingAddressEncoder
|
||||||
|
ZoneIterator
|
||||||
|
|
||||||
|
Peers []*RingPeer
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPeer adds a [Machine] to the ring
|
||||||
|
func (r *Ring) AddPeer(p *Machine) bool {
|
||||||
|
ri, ok := p.getRingInfo(r.ID)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeID := p.ID
|
||||||
|
zoneID := p.Zone()
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case r.ID == 0:
|
||||||
|
r.setRingZeroAllowedIPs(rp)
|
||||||
|
case p.IsGateway():
|
||||||
|
r.setRingOneGatewayAllowedIPs(rp)
|
||||||
|
default:
|
||||||
|
r.setRingOneNodeAllowedIPs(rp)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Peers = append(r.Peers, rp)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Ring) setRingZeroAllowedIPs(rp *RingPeer) {
|
||||||
|
zoneID, _, _ := r.Decode(rp.Address)
|
||||||
|
|
||||||
|
// everyone on ring0 is a gateway to ring1
|
||||||
|
addr, _ := RingOneAddress(zoneID, 0)
|
||||||
|
rp.AllowCIDR(addr, 12)
|
||||||
|
|
||||||
|
// peer
|
||||||
|
rp.AllowCIDR(rp.Address, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Ring) setRingOneGatewayAllowedIPs(rp *RingPeer) {
|
||||||
|
zoneID, _, _ := r.Decode(rp.Address)
|
||||||
|
|
||||||
|
// peer
|
||||||
|
rp.AllowCIDR(rp.Address, 32)
|
||||||
|
|
||||||
|
// ring1 gateways connect to all other ring1 networks
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
if z.ID != zoneID {
|
||||||
|
addr, _ := r.Encode(z.ID, 0)
|
||||||
|
rp.AllowCIDR(addr, 12)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
// ring1 gateways also connect to all ring0 addresses
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.IsGateway() {
|
||||||
|
addr, _ := RingZeroAddress(z.ID, p.ID)
|
||||||
|
rp.AllowCIDR(addr, 32)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Ring) setRingOneNodeAllowedIPs(rp *RingPeer) {
|
||||||
|
// only to the peer itself
|
||||||
|
rp.AllowCIDR(rp.Address, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 builds a wgN.conf for the specified machine on the ring
|
||||||
|
func (r *Ring) ExportConfig(p *Machine) (*wireguard.Config, error) {
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
out := &wireguard.Config{
|
||||||
|
Interface: wireguard.InterfaceConfig{
|
||||||
|
ListenPort: r.Port,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pp := range r.Peers {
|
||||||
|
switch {
|
||||||
|
case pp.Node == p:
|
||||||
|
// current
|
||||||
|
found = true
|
||||||
|
out.Interface.Address = pp.Address
|
||||||
|
out.Interface.PrivateKey = pp.PrivateKey
|
||||||
|
default:
|
||||||
|
// peer
|
||||||
|
pc := pp.PeerConfig
|
||||||
|
out.Peer = append(out.Peer, pc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return nil, fs.ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// A RingPeer is a node on a [Ring]
|
||||||
|
type RingPeer struct {
|
||||||
|
Node *Machine
|
||||||
|
|
||||||
|
Address netip.Addr
|
||||||
|
PrivateKey wireguard.PrivateKey
|
||||||
|
PeerConfig wireguard.PeerConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllowCIDR allows an IP range via this peer
|
||||||
|
func (rp *RingPeer) AllowCIDR(addr netip.Addr, bits int) {
|
||||||
|
cidr := netip.PrefixFrom(addr, bits)
|
||||||
|
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs, cidr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRing composes a new Ring for Wireguard setup
|
||||||
|
func NewRing(z ZoneIterator, m MachineIterator, ring int) (*Ring, error) {
|
||||||
|
r := &Ring{
|
||||||
|
RingAddressEncoder: Rings[ring],
|
||||||
|
ZoneIterator: z,
|
||||||
|
}
|
||||||
|
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
r.AddPeer(p)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|||||||
+23
-29
@@ -5,9 +5,29 @@ import "os"
|
|||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||||
// the corresponding ring disabled.
|
// the corresponding ring disabled.
|
||||||
func (z *Zone) PruneWireguardConfig(ring int) error {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 pruneWireguardConfig(m MachineIterator, ring int) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
m.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)
|
||||||
@@ -18,11 +38,10 @@ func (z *Zone) PruneWireguardConfig(ring int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
func writeWireguardKeys(m MachineIterator, ring int) error {
|
||||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
err = p.WriteWireguardKeys(ring)
|
err = p.WriteWireguardKeys(ring)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// ignore
|
// ignore
|
||||||
@@ -34,28 +53,3 @@ func (z *Zone) WriteWireguardKeys(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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -10,6 +10,22 @@ import (
|
|||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Zone)(nil)
|
||||||
|
_ MachineIterator = (*Zones)(nil)
|
||||||
|
_ ZoneIterator = (*Zones)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// A MachineIterator is a set of Machines we can iterate on
|
||||||
|
type MachineIterator interface {
|
||||||
|
ForEachMachine(func(*Machine) bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A ZoneIterator is a set of Zones we can iterate on
|
||||||
|
type ZoneIterator interface {
|
||||||
|
ForEachZone(func(*Zone) 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