Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a2cbdffd0 | |||
| 81aafbc083 |
@@ -1,7 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "git.jpi.io/amery/jpictl/pkg/zones"
|
|
||||||
|
|
||||||
// Config describes the repository
|
// Config describes the repository
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Base string
|
Base string
|
||||||
@@ -12,8 +10,3 @@ var cfg = &Config{
|
|||||||
Base: "./m",
|
Base: "./m",
|
||||||
Domain: "m.jpi.cloud",
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
+5
-54
@@ -2,76 +2,27 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/burntSushi/toml"
|
"github.com/burntSushi/toml"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encoder represents an object that encodes another internally
|
|
||||||
type Encoder interface {
|
|
||||||
Encode(any) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoding represents a type of [Encoder]
|
|
||||||
type Encoding int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// TOMLEncoding represents TOML encoding
|
|
||||||
TOMLEncoding Encoding = iota
|
|
||||||
// JSONEncoding represents JSON encoding
|
|
||||||
JSONEncoding
|
|
||||||
// YAMLEncoding represents YAML encoding
|
|
||||||
YAMLEncoding
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewJSONEncoder returns a JSON [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewJSONEncoder(w io.Writer) Encoder {
|
|
||||||
enc := json.NewEncoder(w)
|
|
||||||
enc.SetIndent(``, ` `)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewYAMLEncoder returns a YAML [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewYAMLEncoder(w io.Writer) Encoder {
|
|
||||||
enc := yaml.NewEncoder(w)
|
|
||||||
enc.SetIndent(2)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTOMLEncoder returns a TOML [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewTOMLEncoder(w io.Writer) Encoder {
|
|
||||||
enc := toml.NewEncoder(w)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
const encoding = YAMLEncoding
|
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
var dumpCmd = &cobra.Command{
|
var dumpCmd = &cobra.Command{
|
||||||
Use: "dump",
|
Use: "dump",
|
||||||
Short: "generates a text representation of the config",
|
Short: "generates a toml representation of the config",
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var enc Encoder
|
|
||||||
|
|
||||||
m, err := cfg.LoadZones()
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch encoding {
|
enc := toml.NewEncoder(&buf)
|
||||||
case JSONEncoding:
|
|
||||||
enc = NewJSONEncoder(&buf)
|
|
||||||
case YAMLEncoding:
|
|
||||||
enc = NewYAMLEncoder(&buf)
|
|
||||||
default:
|
|
||||||
enc = NewTOMLEncoder(&buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = enc.Encode(m); err != nil {
|
if err = enc.Encode(m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-11
@@ -4,6 +4,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
@@ -11,24 +13,15 @@ var envCmd = &cobra.Command{
|
|||||||
Use: "env",
|
Use: "env",
|
||||||
Short: "generates environment variables for shell scripts",
|
Short: "generates environment variables for shell scripts",
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
m, err := cfg.LoadZones()
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = m.Env(*envExport).WriteTo(os.Stdout)
|
return m.WriteEnv(os.Stdout)
|
||||||
return err
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command Flags
|
|
||||||
var (
|
|
||||||
envExport *bool
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(envCmd)
|
rootCmd.AddCommand(envCmd)
|
||||||
|
|
||||||
envExport = envCmd.PersistentFlags().BoolP("export", "e", false,
|
|
||||||
"export generated variables")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
@@ -13,7 +13,6 @@ require (
|
|||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
gopkg.in/gcfg.v1 v1.2.3
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -90,7 +90,6 @@ golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
|||||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||||
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||||
|
|||||||
@@ -1,44 +1,17 @@
|
|||||||
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]
|
|
||||||
{{if .Interface.Name}}# Name: {{.Interface.Name}}
|
|
||||||
{{end -}}
|
|
||||||
Address = {{.Interface.Address}}
|
|
||||||
PrivateKey = {{.Interface.PrivateKey}}
|
|
||||||
ListenPort = {{.Interface.ListenPort}}
|
|
||||||
{{- range .Peer }}
|
|
||||||
|
|
||||||
[Peer]
|
|
||||||
{{if .Name}}# Name: {{.Name}}
|
|
||||||
{{end -}}
|
|
||||||
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
|
||||||
@@ -55,20 +28,8 @@ 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 {
|
||||||
Name string
|
|
||||||
Address netip.Addr
|
Address netip.Addr
|
||||||
PrivateKey PrivateKey
|
PrivateKey PrivateKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
@@ -76,7 +37,6 @@ type InterfaceConfig struct {
|
|||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
Name string
|
|
||||||
PublicKey PublicKey
|
PublicKey PublicKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519"
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
@@ -51,43 +50,6 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
|
||||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
|
||||||
return encodeKeyJSON(key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
|
||||||
func (pub PublicKey) MarshalJSON() ([]byte, error) {
|
|
||||||
return encodeKeyJSON(pub.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeKeyJSON(s string) ([]byte, error) {
|
|
||||||
var out []byte
|
|
||||||
if s != "" {
|
|
||||||
out = []byte(fmt.Sprintf("%q", s))
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalYAML encodes the key for YAML, omitting empty.
|
|
||||||
func (key PrivateKey) MarshalYAML() (any, error) {
|
|
||||||
return encodeKeyYAML(key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalYAML encodes the key for YAML, omitting empty.
|
|
||||||
func (pub PublicKey) MarshalYAML() (any, error) {
|
|
||||||
return encodeKeyYAML(pub.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeKeyYAML(s string) (any, error) {
|
|
||||||
if s == "" {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (key PrivateKey) IsZero() bool {
|
func (key PrivateKey) IsZero() bool {
|
||||||
var zero PrivateKey
|
var zero PrivateKey
|
||||||
|
|||||||
+12
-34
@@ -7,23 +7,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Env is a shell environment factory for this cluster
|
// WriteEnv generates environment variables for shell scripts
|
||||||
type Env struct {
|
func (m *Zones) WriteEnv(w io.Writer) error {
|
||||||
ZoneIterator
|
|
||||||
|
|
||||||
export bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Env returns a shell environment factory
|
|
||||||
func (m *Zones) Env(export bool) *Env {
|
|
||||||
return &Env{
|
|
||||||
ZoneIterator: m,
|
|
||||||
export: export,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteTo generates environment variables for shell scripts
|
|
||||||
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
||||||
@@ -32,10 +17,11 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
return buf.WriteTo(w)
|
_, err := buf.WriteTo(w)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
||||||
zoneID := z.ID
|
zoneID := z.ID
|
||||||
|
|
||||||
// ZONE{zoneID}
|
// ZONE{zoneID}
|
||||||
@@ -56,7 +42,7 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args ...any) {
|
func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
|
||||||
var value string
|
var value string
|
||||||
|
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
@@ -66,13 +52,7 @@ func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args
|
|||||||
m.writeEnvVar(w, value, name, args...)
|
m.writeEnvVar(w, value, name, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
||||||
var prefix string
|
|
||||||
|
|
||||||
if m.export {
|
|
||||||
prefix = "export "
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
name = fmt.Sprintf(name, args...)
|
name = fmt.Sprintf(name, args...)
|
||||||
}
|
}
|
||||||
@@ -80,17 +60,15 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
|||||||
if name != "" {
|
if name != "" {
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
|
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func genEnvZones(m *Env) string {
|
func genEnvZones(m *Zones) string {
|
||||||
var s []string
|
s := make([]string, 0, len(m.Zones))
|
||||||
|
for _, z := range m.Zones {
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
s = append(s, fmt.Sprintf("%v", z.ID))
|
s = append(s, fmt.Sprintf("%v", z.ID))
|
||||||
return false
|
}
|
||||||
})
|
|
||||||
|
|
||||||
return strings.Join(s, " ")
|
return strings.Join(s, " ")
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-14
@@ -5,20 +5,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// revive:disable:line-length-limit
|
|
||||||
|
|
||||||
// A Machine is a machine on a Zone
|
// A Machine is a machine on a Zone
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
zone *Zone
|
zone *Zone
|
||||||
ID int `toml:"id"`
|
ID int
|
||||||
Name string `toml:"-" json:"-" yaml:"-"`
|
Name string `toml:"name"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||||
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
|
Rings []*RingInfo `toml:"rings,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// revive:enable:line-length-limit
|
|
||||||
|
|
||||||
func (m *Machine) String() string {
|
func (m *Machine) String() string {
|
||||||
return m.Name
|
return m.Name
|
||||||
}
|
}
|
||||||
@@ -58,12 +54,7 @@ func (m *Machine) SetGateway(enabled bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ri.Enabled = enabled
|
ri.Enabled = enabled
|
||||||
return m.SyncWireguardConfig(0)
|
return m.syncRingConfig(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) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
@@ -72,6 +73,38 @@ func (m *Machine) tryReadWireguardKeys(ring int) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
||||||
|
func (m *Machine) WriteWireguardKeys(ring int) error {
|
||||||
|
var err error
|
||||||
|
var key, pub string
|
||||||
|
var ri *RingInfo
|
||||||
|
|
||||||
|
ri, _ = m.getRingInfo(ring)
|
||||||
|
if ri != nil {
|
||||||
|
key = ri.Keys.PrivateKey.String()
|
||||||
|
pub = ri.Keys.PublicKey.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case key == "":
|
||||||
|
return fs.ErrNotExist
|
||||||
|
case pub == "":
|
||||||
|
pub = ri.Keys.PrivateKey.Public().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.WriteStringFile(key, "wg%v.key", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.WriteStringFile(pub, "wg%v.pub", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||||
// the machine's config directory
|
// the machine's config directory
|
||||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||||
@@ -229,6 +262,11 @@ 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) {
|
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
|
||||||
keys, err := wireguard.NewKeyPair()
|
keys, err := wireguard.NewKeyPair()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -248,3 +286,32 @@ func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
|
|||||||
|
|
||||||
return ri, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
+1
-169
@@ -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,174 +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)
|
|
||||||
_ 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{
|
|
||||||
Name: fmt.Sprintf("%s-%v", p.Name, r.ID),
|
|
||||||
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.Name = pp.PeerConfig.Name
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|||||||
+6
-50
@@ -11,7 +11,6 @@ func (m *Zones) scan() error {
|
|||||||
m.scanMachines,
|
m.scanMachines,
|
||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
m.scanGateways,
|
|
||||||
} {
|
} {
|
||||||
if err := fn(); err != nil {
|
if err := fn(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -93,7 +92,12 @@ func (m *Zones) scanSort() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
sort.Sort(z)
|
sort.SliceStable(z.Machines, func(i, j int) bool {
|
||||||
|
id1 := z.Machines[i].ID
|
||||||
|
id2 := z.Machines[j].ID
|
||||||
|
return id1 < id2
|
||||||
|
})
|
||||||
|
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -111,16 +115,6 @@ func (m *Zones) scanSort() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) scanGateways() error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
_, _, err = z.GetGateway()
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (z *Zone) scan() error {
|
func (z *Zone) scan() error {
|
||||||
// each directory is a machine
|
// each directory is a machine
|
||||||
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||||||
@@ -145,41 +139,3 @@ func (z *Zone) scan() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGateway returns the first gateway found, if none
|
|
||||||
// files will be created to enable the first [Machine] to
|
|
||||||
// be one
|
|
||||||
func (z *Zone) GetGateway() (*Machine, bool, error) {
|
|
||||||
var first *Machine
|
|
||||||
var gateway *Machine
|
|
||||||
|
|
||||||
z.zones.ForEachMachine(func(p *Machine) bool {
|
|
||||||
switch {
|
|
||||||
case p.IsGateway():
|
|
||||||
// found
|
|
||||||
gateway = p
|
|
||||||
case first == nil:
|
|
||||||
// remember
|
|
||||||
first = p
|
|
||||||
default:
|
|
||||||
// keep looking
|
|
||||||
}
|
|
||||||
|
|
||||||
return gateway != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case gateway != nil:
|
|
||||||
// found one
|
|
||||||
return gateway, false, nil
|
|
||||||
case first != nil:
|
|
||||||
// make one
|
|
||||||
if err := first.SetGateway(true); err != nil {
|
|
||||||
return first, false, err
|
|
||||||
}
|
|
||||||
return first, true, nil
|
|
||||||
default:
|
|
||||||
// Zone without nodes?
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
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.WriteWireguardKeys(ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.SyncWireguardConfig(ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,272 +0,0 @@
|
|||||||
package zones
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/fs"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ WireguardConfigPruner = (*Zones)(nil)
|
|
||||||
_ WireguardConfigPruner = (*Zone)(nil)
|
|
||||||
_ WireguardConfigPruner = (*Machine)(nil)
|
|
||||||
|
|
||||||
_ WireguardConfigWriter = (*Zones)(nil)
|
|
||||||
_ WireguardConfigWriter = (*Zone)(nil)
|
|
||||||
_ WireguardConfigWriter = (*Machine)(nil)
|
|
||||||
|
|
||||||
_ WireguardConfigSyncer = (*Zones)(nil)
|
|
||||||
_ WireguardConfigSyncer = (*Zone)(nil)
|
|
||||||
_ WireguardConfigSyncer = (*Machine)(nil)
|
|
||||||
|
|
||||||
_ WireguardKeysWriter = (*Zones)(nil)
|
|
||||||
_ WireguardKeysWriter = (*Zone)(nil)
|
|
||||||
_ WireguardKeysWriter = (*Machine)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
// A WireguardConfigPruner deletes wgN.conf on all machines under
|
|
||||||
// its scope with the specified ring disabled
|
|
||||||
type WireguardConfigPruner interface {
|
|
||||||
PruneWireguardConfig(ring int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled.
|
|
||||||
func (z *Zone) PruneWireguardConfig(ring int) error {
|
|
||||||
return pruneWireguardConfig(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
err = p.PruneWireguardConfig(ring)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
// ignore
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PruneWireguardConfig deletes the wgN.conf file if its
|
|
||||||
// presence on the ring is disabled
|
|
||||||
func (m *Machine) PruneWireguardConfig(ring int) error {
|
|
||||||
_, ok := m.getRingInfo(ring)
|
|
||||||
if !ok {
|
|
||||||
return m.RemoveWireguardConfig(ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// A WireguardConfigWriter rewrites all wgN.conf on all machines under
|
|
||||||
// its scope attached to that ring
|
|
||||||
type WireguardConfigWriter interface {
|
|
||||||
WriteWireguardConfig(ring int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
|
||||||
// attached to that ring
|
|
||||||
func (m *Zones) WriteWireguardConfig(ring int) error {
|
|
||||||
switch ring {
|
|
||||||
case 0:
|
|
||||||
return writeWireguardConfig(m, m, ring)
|
|
||||||
case 1:
|
|
||||||
var err error
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
err = writeWireguardConfig(m, z, ring)
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
return fs.ErrInvalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
|
||||||
// on the Zone attached to that ring
|
|
||||||
func (z *Zone) WriteWireguardConfig(ring int) error {
|
|
||||||
switch ring {
|
|
||||||
case 0:
|
|
||||||
return writeWireguardConfig(z.zones, z.zones, ring)
|
|
||||||
case 1:
|
|
||||||
return writeWireguardConfig(z.zones, z, ring)
|
|
||||||
default:
|
|
||||||
return fs.ErrInvalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
|
||||||
r, err := NewRing(z, m, ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
r.ForEachMachine(func(p *Machine) bool {
|
|
||||||
err = p.writeWireguardRingConfig(r)
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
|
|
||||||
// if enabled
|
|
||||||
func (m *Machine) WriteWireguardConfig(ring int) error {
|
|
||||||
r, err := NewRing(m.zone.zones, m.zone, ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.writeWireguardRingConfig(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
|
|
||||||
wg, err := r.ExportConfig(m)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
_, err = wg.WriteTo(f)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// A WireguardConfigSyncer updates all wgN.conf on all machines under
|
|
||||||
// its scope reflecting the state of the ring
|
|
||||||
type WireguardConfigSyncer interface {
|
|
||||||
SyncWireguardConfig(ring int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
||||||
// ring
|
|
||||||
func (m *Zones) SyncWireguardConfig(ring int) error {
|
|
||||||
switch ring {
|
|
||||||
case 0:
|
|
||||||
return syncWireguardConfig(m, m, ring)
|
|
||||||
case 1:
|
|
||||||
var err error
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
err = syncWireguardConfig(m, z, ring)
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
return fs.ErrInvalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
||||||
// ring
|
|
||||||
func (z *Zone) SyncWireguardConfig(ring int) error {
|
|
||||||
switch ring {
|
|
||||||
case 0:
|
|
||||||
return syncWireguardConfig(z.zones, z.zones, ring)
|
|
||||||
case 1:
|
|
||||||
return syncWireguardConfig(z.zones, z, ring)
|
|
||||||
default:
|
|
||||||
return fs.ErrInvalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
|
||||||
r, err := NewRing(z, m, ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
r.ForEachMachine(func(p *Machine) bool {
|
|
||||||
if _, ok := p.getRingInfo(ring); ok {
|
|
||||||
err = p.writeWireguardRingConfig(r)
|
|
||||||
} else {
|
|
||||||
err = p.RemoveWireguardConfig(ring)
|
|
||||||
}
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncWireguardConfig updates all wgN.conf files for the specified
|
|
||||||
// ring
|
|
||||||
func (m *Machine) SyncWireguardConfig(ring int) error {
|
|
||||||
return m.zone.SyncWireguardConfig(ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A WireguardKeysWriter writes the Wireguard Keys for all machines
|
|
||||||
// under its scope for the specified ring
|
|
||||||
type WireguardKeysWriter interface {
|
|
||||||
WriteWireguardKeys(ring int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
||||||
func (m *Zones) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(m, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
||||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
|
||||||
func (m *Machine) WriteWireguardKeys(ring int) error {
|
|
||||||
var err error
|
|
||||||
var key, pub string
|
|
||||||
var ri *RingInfo
|
|
||||||
|
|
||||||
ri, _ = m.getRingInfo(ring)
|
|
||||||
if ri != nil {
|
|
||||||
key = ri.Keys.PrivateKey.String()
|
|
||||||
pub = ri.Keys.PublicKey.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case key == "":
|
|
||||||
return fs.ErrNotExist
|
|
||||||
case pub == "":
|
|
||||||
pub = ri.Keys.PrivateKey.Public().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
+13
-92
@@ -4,102 +4,36 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/hack-pad/hackpadfs/os"
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
|
|
||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
_ MachineIterator = Machines(nil)
|
|
||||||
_ sort.Interface = Machines(nil)
|
|
||||||
|
|
||||||
_ 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Machines is a list of Machine objects
|
|
||||||
type Machines []*Machine
|
|
||||||
|
|
||||||
// ForEachMachine calls a function for each Machine in the list
|
|
||||||
// until instructed to terminate the loop
|
|
||||||
func (m Machines) ForEachMachine(fn func(*Machine) bool) {
|
|
||||||
for _, p := range m {
|
|
||||||
if fn(p) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Len returns the number of machines in the list
|
|
||||||
func (m Machines) Len() int {
|
|
||||||
return len(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Less implements sort.Interface to sort the list
|
|
||||||
func (m Machines) Less(i, j int) bool {
|
|
||||||
a, b := m[i], m[j]
|
|
||||||
za, zb := a.Zone(), b.Zone()
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case za == zb:
|
|
||||||
return a.ID < b.ID
|
|
||||||
default:
|
|
||||||
return za < zb
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap implements sort.Interface to sort the list
|
|
||||||
func (m Machines) Swap(i, j int) {
|
|
||||||
m[i], m[j] = m[j], m[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// FilterMachines produces a subset of the machines offered by the given
|
|
||||||
// iterator fulfilling a condition
|
|
||||||
func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) {
|
|
||||||
var out []*Machine
|
|
||||||
|
|
||||||
if cond == nil {
|
|
||||||
// unconditional
|
|
||||||
cond = func(*Machine) bool { return true }
|
|
||||||
}
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
if cond(p) {
|
|
||||||
out = append(out, p)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
return out, len(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zone represents one zone in a cluster
|
// Zone represents one zone in a cluster
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
|
|
||||||
ID int `toml:"id"`
|
ID int
|
||||||
Name string `toml:"name"`
|
Name string
|
||||||
|
|
||||||
Machines `toml:"machines"`
|
Machines []*Machine `toml:"machines"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zone) String() string {
|
func (z *Zone) String() string {
|
||||||
return z.Name
|
return z.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForEachMachine calls a function for each Machine in the zone
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
for _, p := range z.Machines {
|
||||||
|
if fn(p) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetGateway configures a machine to be the zone's ring0 gateway
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||||
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||||
var err error
|
var err error
|
||||||
@@ -125,19 +59,6 @@ func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GatewayIDs returns the list of IDs of machines that act as ring0 gateways
|
|
||||||
func (z *Zone) GatewayIDs() ([]int, int) {
|
|
||||||
var out []int
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
if p.IsGateway() {
|
|
||||||
out = append(out, p.ID)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
return out, len(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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