Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7dd3ea8f96 | |||
| 07b4a22752 | |||
| 609f48a2d1 | |||
| d1f7d225ae | |||
| dfbb358187 | |||
| 26c49dff72 | |||
| 2043708949 | |||
| 311ae572da | |||
| 4ca77b0ac0 | |||
| 1859c8e04b |
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
import "git.jpi.io/amery/jpictl/pkg/zones"
|
||||
|
||||
// Config describes the repository
|
||||
type Config struct {
|
||||
Base string
|
||||
@@ -10,3 +12,8 @@ var cfg = &Config{
|
||||
Base: "./m",
|
||||
Domain: "m.jpi.cloud",
|
||||
}
|
||||
|
||||
// LoadZones loads all zones and machines in the config directory
|
||||
func (cfg *Config) LoadZones() (*zones.Zones, error) {
|
||||
return zones.New(cfg.Base, cfg.Domain)
|
||||
}
|
||||
|
||||
+1
-3
@@ -9,8 +9,6 @@ import (
|
||||
"github.com/burntSushi/toml"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||
)
|
||||
|
||||
// Encoder represents an object that encodes another internally
|
||||
@@ -60,7 +58,7 @@ var dumpCmd = &cobra.Command{
|
||||
var buf bytes.Buffer
|
||||
var enc Encoder
|
||||
|
||||
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||
m, err := cfg.LoadZones()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-3
@@ -4,8 +4,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||
)
|
||||
|
||||
// Command
|
||||
@@ -13,7 +11,7 @@ var envCmd = &cobra.Command{
|
||||
Use: "env",
|
||||
Short: "generates environment variables for shell scripts",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||
m, err := cfg.LoadZones()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Command
|
||||
var writeCmd = &cobra.Command{
|
||||
Use: "write",
|
||||
Short: "rewrites all config files",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
m, err := cfg.LoadZones()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.SyncAll()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(writeCmd)
|
||||
}
|
||||
@@ -1,17 +1,40 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"darvaza.org/core"
|
||||
"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
|
||||
type Config struct {
|
||||
Interface InterfaceConfig
|
||||
@@ -28,6 +51,17 @@ func (f *Config) Peers() int {
|
||||
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
|
||||
type InterfaceConfig struct {
|
||||
Address netip.Addr
|
||||
|
||||
@@ -43,6 +43,11 @@ func (m *Machine) IsGateway() bool {
|
||||
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) {
|
||||
return m.zone.zones.GetMachineByName(name)
|
||||
}
|
||||
|
||||
@@ -92,12 +92,12 @@ func (m *Machine) WriteWireguardKeys(ring int) error {
|
||||
pub = ri.Keys.PrivateKey.Public().String()
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(key, "wg%v.key", ring)
|
||||
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.WriteStringFile(pub, "wg%v.pub", ring)
|
||||
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -175,7 +175,7 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||
return netip.Addr{}, false
|
||||
default:
|
||||
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
|
||||
a4 := [4]uint8{10, uint8(zoneID << 4), 0, uint8(nodeID)}
|
||||
return netip.AddrFrom4(a4), true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package zones
|
||||
|
||||
// SyncAll updates all config files
|
||||
func (m *Zones) SyncAll() error {
|
||||
for _, fn := range []func() error{
|
||||
m.SyncAllWireguard,
|
||||
} {
|
||||
if err := fn(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncAllWireguard updates all wireguard config files
|
||||
func (m *Zones) SyncAllWireguard() error {
|
||||
var err error
|
||||
|
||||
for ring := 0; ring < RingsCount; ring++ {
|
||||
err = m.PruneWireguardConfig(ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.WriteWireguardKeys(ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package zones
|
||||
|
||||
import "os"
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
_, ok := p.getRingInfo(ring)
|
||||
if !ok {
|
||||
err = p.RemoveWireguardConfig(ring)
|
||||
}
|
||||
return err != nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func writeWireguardKeys(m MachineIterator, ring int) error {
|
||||
var err error
|
||||
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
err = p.WriteWireguardKeys(ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err != nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -10,6 +10,16 @@ import (
|
||||
"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
|
||||
type Zone struct {
|
||||
zones *Zones
|
||||
|
||||
Reference in New Issue
Block a user