Compare commits
19 Commits
v0.5.0
...
f4b3c3b341
| Author | SHA1 | Date | |
|---|---|---|---|
| f4b3c3b341 | |||
| 67b2b9be29 | |||
| 3f6cf543d2 | |||
| 1fd67780b1 | |||
| 6c1e9d0989 | |||
| 78110464b4 | |||
| 1d980a9d6e | |||
| ab3390dc1b | |||
| 1bca1f7da1 | |||
| 5e5958d22e | |||
| 45447275a7 | |||
| 51d4748a6f | |||
| e03e5e0d05 | |||
| a655603343 | |||
| c291b218a4 | |||
| 3911a51ccf | |||
| db62adfb9c | |||
| 4599eca7d9 | |||
| 312dbe2269 |
Vendored
+4
@@ -1,8 +1,12 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"asciigoat",
|
||||
"ceph",
|
||||
"cyclomatic",
|
||||
"darvaza",
|
||||
"gofrs",
|
||||
"jpictl",
|
||||
"Wrapf",
|
||||
"zerolog"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -17,5 +17,6 @@ var cfg = &Config{
|
||||
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
|
||||
return zones.New(cfg.Base, cfg.Domain,
|
||||
zones.ResolvePublicAddresses(resolve),
|
||||
zones.WithLogger(log),
|
||||
)
|
||||
}
|
||||
|
||||
+5
-1
@@ -16,7 +16,11 @@ var envCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = m.Env(*envExport).WriteTo(os.Stdout)
|
||||
env, err := m.Env(*envExport)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = env.WriteTo(os.Stdout)
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"darvaza.org/slog"
|
||||
)
|
||||
|
||||
// TODO: make log level configurable via flags
|
||||
var (
|
||||
log = zerolog.New(nil, slog.Debug)
|
||||
)
|
||||
|
||||
@@ -2,19 +2,21 @@ module git.jpi.io/amery/jpictl
|
||||
|
||||
go 1.19
|
||||
|
||||
replace asciigoat.org/ini => ../../../asciigoat.org/ini
|
||||
|
||||
require (
|
||||
asciigoat.org/ini v0.2.5
|
||||
darvaza.org/core v0.9.8
|
||||
darvaza.org/resolver v0.5.4
|
||||
darvaza.org/sidecar v0.0.2
|
||||
darvaza.org/slog v0.5.3
|
||||
darvaza.org/slog/handlers/discard v0.4.5
|
||||
github.com/burntSushi/toml v0.3.1
|
||||
github.com/gofrs/uuid/v5 v5.0.0
|
||||
github.com/hack-pad/hackpadfs v0.2.1
|
||||
github.com/mgechev/revive v1.3.3
|
||||
github.com/spf13/cobra v1.7.0
|
||||
golang.org/x/crypto v0.12.0
|
||||
gopkg.in/gcfg.v1 v1.2.3
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -43,5 +45,4 @@ require (
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
golang.org/x/tools v0.12.0 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
|
||||
asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
|
||||
asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE=
|
||||
asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU=
|
||||
darvaza.org/core v0.9.8 h1:luLxgfUc2pzuusYPo/Z/dC/qr9XZPKpSQw8/kS7zNUM=
|
||||
darvaza.org/core v0.9.8/go.mod h1:Dbme64naxeshQfxcVJX9ZT7AiGyIY8kldfuELVtf8mw=
|
||||
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs=
|
||||
@@ -10,6 +8,8 @@ darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0=
|
||||
darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo=
|
||||
darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0=
|
||||
darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0=
|
||||
darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g=
|
||||
darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8=
|
||||
darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc=
|
||||
darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g=
|
||||
darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4=
|
||||
@@ -98,10 +98,6 @@ golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||
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/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+14
-110
@@ -2,7 +2,6 @@ package wireguard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/netip"
|
||||
@@ -10,8 +9,8 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
"darvaza.org/core"
|
||||
"gopkg.in/gcfg.v1"
|
||||
)
|
||||
|
||||
var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap{
|
||||
@@ -107,6 +106,11 @@ func (ep EndpointAddress) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalText loads an endpoint address from text data
|
||||
func (ep *EndpointAddress) UnmarshalText(b []byte) error {
|
||||
return ep.FromString(string(b))
|
||||
}
|
||||
|
||||
// FromString sets the EndpointAddress from a given "[host]:port"
|
||||
func (ep *EndpointAddress) FromString(s string) error {
|
||||
host, port, err := core.SplitHostPort(s)
|
||||
@@ -127,98 +131,6 @@ func (ep *EndpointAddress) FromString(s string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type intermediateConfig struct {
|
||||
Interface interfaceConfig
|
||||
Peer peersConfig
|
||||
}
|
||||
|
||||
func (v *intermediateConfig) Export() (*Config, error) {
|
||||
var out Config
|
||||
var err error
|
||||
|
||||
// Interface
|
||||
out.Interface, err = v.Interface.Export()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Peers
|
||||
peers, ok := v.PeersCount()
|
||||
if !ok {
|
||||
return nil, errors.New("inconsistent Peer data")
|
||||
}
|
||||
|
||||
for i := 0; i < peers; i++ {
|
||||
p, err := v.ExportPeer(i)
|
||||
if err != nil {
|
||||
err = core.Wrapf(err, "Peer[%v]:", i)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out.Peer = append(out.Peer, p)
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
type interfaceConfig struct {
|
||||
Address netip.Addr
|
||||
PrivateKey string
|
||||
ListenPort uint16
|
||||
}
|
||||
|
||||
func (p interfaceConfig) Export() (InterfaceConfig, error) {
|
||||
var err error
|
||||
|
||||
out := InterfaceConfig{
|
||||
Address: p.Address,
|
||||
ListenPort: p.ListenPort,
|
||||
}
|
||||
|
||||
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PrivateKey")
|
||||
return InterfaceConfig{}, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type peersConfig struct {
|
||||
PublicKey []string
|
||||
Endpoint []string
|
||||
AllowedIPs []string
|
||||
}
|
||||
|
||||
func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
|
||||
var out PeerConfig
|
||||
|
||||
// Endpoint
|
||||
s := v.Peer.Endpoint[i]
|
||||
err := out.Endpoint.FromString(s)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "Endpoint")
|
||||
return out, err
|
||||
}
|
||||
|
||||
// PublicKey
|
||||
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i])
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "PublicKey")
|
||||
return out, err
|
||||
}
|
||||
|
||||
// AllowedIPs
|
||||
s = v.Peer.AllowedIPs[i]
|
||||
out.AllowedIPs, err = parseAllowedIPs(s)
|
||||
if err != nil {
|
||||
err = core.Wrap(err, "AllowedIPs")
|
||||
return out, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
||||
var out []netip.Prefix
|
||||
|
||||
@@ -235,25 +147,17 @@ func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (v *intermediateConfig) PeersCount() (int, bool) {
|
||||
c0 := len(v.Peer.Endpoint)
|
||||
c1 := len(v.Peer.PublicKey)
|
||||
c2 := len(v.Peer.AllowedIPs)
|
||||
|
||||
if c0 != c1 || c1 != c2 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return c0, true
|
||||
}
|
||||
|
||||
// NewConfigFromReader parses a wgN.conf file
|
||||
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
||||
temp := &intermediateConfig{}
|
||||
|
||||
if err := gcfg.ReadInto(temp, r); err != nil {
|
||||
doc, err := basic.Decode(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return temp.Export()
|
||||
cfg, err := newConfigFromDocument(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"strconv"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
type sectionHandler func(*Config, *basic.Section) error
|
||||
|
||||
var sectionMap = map[string]func(*Config, *basic.Section) error{
|
||||
"Interface": loadInterfaceConfSection,
|
||||
"Peer": loadPeerConfSection,
|
||||
}
|
||||
|
||||
func loadConfSection(out *Config, src *basic.Section) error {
|
||||
h, ok := sectionMap[src.Key]
|
||||
if !ok {
|
||||
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
|
||||
}
|
||||
|
||||
return h(out, src)
|
||||
}
|
||||
|
||||
func loadInterfaceConfSection(out *Config, src *basic.Section) error {
|
||||
var cfg InterfaceConfig
|
||||
|
||||
for _, field := range src.Fields {
|
||||
if err := loadInterfaceConfField(&cfg, field); err != nil {
|
||||
return core.Wrap(err, "Interface")
|
||||
}
|
||||
}
|
||||
|
||||
out.Interface = cfg
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadPeerConfSection(out *Config, src *basic.Section) error {
|
||||
var cfg PeerConfig
|
||||
|
||||
for _, field := range src.Fields {
|
||||
if err := loadPeerConfField(&cfg, field); err != nil {
|
||||
return core.Wrapf(err, "Peer[%v]", len(out.Peer))
|
||||
}
|
||||
}
|
||||
|
||||
out.Peer = append(out.Peer, cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// revive:disable:cyclomatic
|
||||
// revive:disable:cognitive-complexity
|
||||
|
||||
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error {
|
||||
// revive:enable:cyclomatic
|
||||
// revive:enable:cognitive-complexity
|
||||
|
||||
// TODO: refactor when asciigoat's ini parser learns to do reflection
|
||||
switch field.Key {
|
||||
case "Address":
|
||||
if !core.IsZero(cfg.Address) {
|
||||
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.Address.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "PrivateKey":
|
||||
if !core.IsZero(cfg.PrivateKey) {
|
||||
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.PrivateKey.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "ListenPort":
|
||||
if cfg.ListenPort > 0 {
|
||||
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
u64, err := strconv.ParseUint(field.Value, 10, 16)
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
case u64 == 0:
|
||||
return core.Wrapf(fs.ErrInvalid, "invalid %q value", field.Key)
|
||||
default:
|
||||
cfg.ListenPort = uint16(u64)
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return core.Wrapf(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||
}
|
||||
}
|
||||
|
||||
// revive:disable:cyclomatic
|
||||
// revive:disable:cognitive-complexity
|
||||
|
||||
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error {
|
||||
// revive:enable:cyclomatic
|
||||
// revive:enable:cognitive-complexity
|
||||
|
||||
switch field.Key {
|
||||
case "PublicKey":
|
||||
if !core.IsZero(cfg.PublicKey) {
|
||||
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.PublicKey.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "Endpoint":
|
||||
if cfg.Endpoint.String() != "" {
|
||||
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.Endpoint.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
case "AllowedIPs":
|
||||
s, err := parseAllowedIPs(field.Value)
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
case len(s) > 0:
|
||||
cfg.AllowedIPs = append(cfg.AllowedIPs, s...)
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return core.Wrapf(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newConfigFromDocument(doc *basic.Document) (*Config, error) {
|
||||
var out Config
|
||||
|
||||
if len(doc.Global) > 0 {
|
||||
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range doc.Sections {
|
||||
src := &doc.Sections[i]
|
||||
if err := loadConfSection(&out, src); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
@@ -51,6 +51,30 @@ func (pub PublicKey) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalText loads the value from base64
|
||||
func (key *PrivateKey) UnmarshalText(b []byte) error {
|
||||
v, err := PrivateKeyFromBase64(string(b))
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
*key = v
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalText loads the value from base64
|
||||
func (pub *PublicKey) UnmarshalText(b []byte) error {
|
||||
v, err := PublicKeyFromBase64(string(b))
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
*pub = v
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||
return encodeKeyJSON(key.String())
|
||||
|
||||
+6
-2
@@ -14,8 +14,12 @@ import (
|
||||
// GetCephFSID returns our Ceph's FSID
|
||||
func (m *Zones) GetCephFSID() (uuid.UUID, error) {
|
||||
if core.IsZero(m.CephFSID) {
|
||||
// TODO: generate one
|
||||
return uuid.Nil, nil
|
||||
// generate one
|
||||
v, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
m.CephFSID = v
|
||||
}
|
||||
return m.CephFSID, nil
|
||||
}
|
||||
|
||||
+17
-3
@@ -11,15 +11,24 @@ import (
|
||||
type Env struct {
|
||||
ZoneIterator
|
||||
|
||||
export bool
|
||||
cephFSID string
|
||||
export bool
|
||||
}
|
||||
|
||||
// Env returns a shell environment factory
|
||||
func (m *Zones) Env(export bool) *Env {
|
||||
return &Env{
|
||||
func (m *Zones) Env(export bool) (*Env, error) {
|
||||
fsid, err := m.GetCephFSID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
env := &Env{
|
||||
ZoneIterator: m,
|
||||
cephFSID: fsid.String(),
|
||||
export: export,
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// Zones returns the list of Zone IDs
|
||||
@@ -38,7 +47,12 @@ func (m *Env) Zones() []int {
|
||||
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if m.cephFSID != "" {
|
||||
m.writeEnvVar(&buf, m.cephFSID, "FSID")
|
||||
}
|
||||
|
||||
m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
|
||||
|
||||
m.ForEachZone(func(z *Zone) bool {
|
||||
m.writeEnvZone(&buf, z)
|
||||
return false
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package zones
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrInvalidName indicates the name isn't valid
|
||||
ErrInvalidName = errors.New("invalid name")
|
||||
|
||||
// ErrUnknownNode indicates there is a reference to a node
|
||||
// we don't have on the tree
|
||||
ErrUnknownNode = errors.New("node does not exist")
|
||||
|
||||
// ErrInvalidNode indicates the nodes can't be used for
|
||||
// the intended purpose
|
||||
ErrInvalidNode = errors.New("invalid node")
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
package zones
|
||||
|
||||
import "darvaza.org/slog"
|
||||
|
||||
type logger interface {
|
||||
withDebug() (slog.Logger, bool)
|
||||
withInfo() (slog.Logger, bool)
|
||||
|
||||
debug() slog.Logger
|
||||
info() slog.Logger
|
||||
warn(error) slog.Logger
|
||||
error(error) slog.Logger
|
||||
}
|
||||
|
||||
var (
|
||||
_ logger = (*Zones)(nil)
|
||||
)
|
||||
|
||||
func (z *Zones) withDebug() (slog.Logger, bool) {
|
||||
return z.debug().WithEnabled()
|
||||
}
|
||||
|
||||
func (z *Zones) withInfo() (slog.Logger, bool) {
|
||||
return z.debug().WithEnabled()
|
||||
}
|
||||
|
||||
func (z *Zones) debug() slog.Logger {
|
||||
return z.log.Debug()
|
||||
}
|
||||
|
||||
func (z *Zones) info() slog.Logger {
|
||||
return z.log.Info()
|
||||
}
|
||||
|
||||
func (z *Zones) warn(err error) slog.Logger {
|
||||
l := z.log.Warn()
|
||||
if err != nil {
|
||||
l = l.WithField(slog.ErrorFieldName, err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (z *Zones) error(err error) slog.Logger {
|
||||
l := z.log.Error()
|
||||
if err != nil {
|
||||
l = l.WithField(slog.ErrorFieldName, err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
// A Machine is a machine on a Zone
|
||||
type Machine struct {
|
||||
zone *Zone
|
||||
logger
|
||||
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"-" json:"-" yaml:"-"`
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package zones
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -135,7 +136,11 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
|
||||
}
|
||||
|
||||
for _, peer := range wg.Peer {
|
||||
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
|
||||
err := m.applyWireguardPeerConfig(ring, peer)
|
||||
switch {
|
||||
case errors.Is(err, ErrUnknownNode):
|
||||
// ignore unknown peers
|
||||
case err != nil:
|
||||
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
|
||||
return err
|
||||
}
|
||||
@@ -183,8 +188,10 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
||||
switch {
|
||||
case !found:
|
||||
// unknown
|
||||
return core.Wrap(ErrUnknownNode, pc.Endpoint.Host)
|
||||
case ring == 1 && m.zone != peer.zone:
|
||||
// invalid zone
|
||||
return core.Wrap(ErrInvalidNode, peer.Name)
|
||||
default:
|
||||
// apply RingInfo
|
||||
ri := &RingInfo{
|
||||
@@ -197,8 +204,6 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
|
||||
|
||||
return peer.applyRingInfo(ring, ri)
|
||||
}
|
||||
|
||||
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
|
||||
}
|
||||
|
||||
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
|
||||
@@ -4,7 +4,10 @@ import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
// LookupNetIP uses the DNS Resolver to get the public addresses associated
|
||||
@@ -30,21 +33,32 @@ func (m *Machine) UpdatePublicAddresses() error {
|
||||
|
||||
func (m *Machine) init() error {
|
||||
if err := m.setID(); err != nil {
|
||||
return err
|
||||
return core.Wrap(err, m.Name)
|
||||
}
|
||||
|
||||
for i := 0; i < RingsCount; i++ {
|
||||
if err := m.tryReadWireguardKeys(i); err != nil {
|
||||
return err
|
||||
return core.Wrap(err, m.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Machine) setID() error {
|
||||
zoneName := m.zone.Name
|
||||
suffix := m.Name[len(zoneName)+1:]
|
||||
|
||||
l := len(zoneName)
|
||||
switch {
|
||||
case len(m.Name) < l+2:
|
||||
return ErrInvalidName
|
||||
case !strings.HasPrefix(m.Name, zoneName):
|
||||
return ErrInvalidName
|
||||
case m.Name[l] != '-':
|
||||
return ErrInvalidName
|
||||
}
|
||||
|
||||
suffix := m.Name[l+1:]
|
||||
id, err := strconv.ParseInt(suffix, 10, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+26
-1
@@ -5,10 +5,12 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"darvaza.org/slog/handlers/discard"
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
)
|
||||
|
||||
// A ScanOption preconfigures the Zones before scanning
|
||||
// A ScanOption pre-configures the Zones before scanning
|
||||
type ScanOption func(*Zones, *ScanOptions) error
|
||||
|
||||
// ScanOptions contains flags used by the initial scan
|
||||
@@ -17,6 +19,10 @@ type ScanOptions struct {
|
||||
// pre-populate Machine.PublicAddresses during the
|
||||
// initial scan
|
||||
DontResolvePublicAddresses bool
|
||||
|
||||
// Logger specifies the logger to be used. otherwise
|
||||
// the scanner will be mute
|
||||
slog.Logger
|
||||
}
|
||||
|
||||
// ResolvePublicAddresses instructs the scanner to use
|
||||
@@ -55,6 +61,19 @@ func WithResolver(h resolver.Resolver) ScanOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger specifies what to use for logging
|
||||
func WithLogger(log slog.Logger) ScanOption {
|
||||
return func(m *Zones, opt *ScanOptions) error {
|
||||
if log == nil {
|
||||
log = discard.New()
|
||||
}
|
||||
|
||||
opt.Logger = log
|
||||
m.log = log
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Zones) setDefaults(opt *ScanOptions) error {
|
||||
if m.resolver == nil {
|
||||
h := resolver.NewCloudflareLookuper()
|
||||
@@ -64,6 +83,12 @@ func (m *Zones) setDefaults(opt *ScanOptions) error {
|
||||
}
|
||||
}
|
||||
|
||||
if opt.Logger == nil {
|
||||
if err := WithLogger(nil)(m, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+24
-11
@@ -3,6 +3,8 @@ package zones
|
||||
import (
|
||||
"io/fs"
|
||||
"sort"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
func (m *Zones) scan(opts *ScanOptions) error {
|
||||
@@ -31,22 +33,32 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
z := &Zone{
|
||||
zones: m,
|
||||
Name: e.Name(),
|
||||
z, err := m.newZone(e.Name())
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, e.Name())
|
||||
case z.Machines.Len() > 0:
|
||||
m.Zones = append(m.Zones, z)
|
||||
}
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Zones = append(m.Zones, z)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Zones) newZone(name string) (*Zone, error) {
|
||||
z := &Zone{
|
||||
zones: m,
|
||||
logger: m,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
if err := z.scan(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return z, nil
|
||||
}
|
||||
|
||||
func (m *Zones) scanMachines(opts *ScanOptions) error {
|
||||
var err error
|
||||
m.ForEachMachine(func(p *Machine) bool {
|
||||
@@ -132,8 +144,9 @@ func (z *Zone) scan() error {
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
m := &Machine{
|
||||
zone: z,
|
||||
Name: e.Name(),
|
||||
zone: z,
|
||||
logger: z,
|
||||
Name: e.Name(),
|
||||
}
|
||||
|
||||
if err := m.init(); err != nil {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sort"
|
||||
|
||||
"darvaza.org/resolver"
|
||||
"darvaza.org/slog"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
)
|
||||
|
||||
@@ -87,6 +88,7 @@ func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int)
|
||||
// Zone represents one zone in a cluster
|
||||
type Zone struct {
|
||||
zones *Zones
|
||||
logger
|
||||
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"name"`
|
||||
@@ -141,6 +143,7 @@ func (z *Zone) GatewayIDs() ([]int, int) {
|
||||
// Zones represents all zones in a cluster
|
||||
type Zones struct {
|
||||
dir fs.FS
|
||||
log slog.Logger
|
||||
resolver resolver.Resolver
|
||||
domain string
|
||||
|
||||
|
||||
Reference in New Issue
Block a user