Compare commits
10 Commits
v0.6.1
...
07fd7b450a
| Author | SHA1 | Date | |
|---|---|---|---|
| 07fd7b450a | |||
| 61d1ea85ee | |||
| 87258c7a9e | |||
| c5c99db732 | |||
| 7258b4a20e | |||
| 15f28fa7c8 | |||
| 6838d00a0a | |||
| e723066f7b | |||
| 3c6dee543a | |||
| 75d7b4389a |
@@ -8,6 +8,7 @@ require (
|
|||||||
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
|
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
|
||||||
darvaza.org/slog v0.5.2
|
darvaza.org/slog v0.5.2
|
||||||
github.com/burntSushi/toml v0.3.1
|
github.com/burntSushi/toml v0.3.1
|
||||||
|
github.com/hack-pad/hackpadfs v0.2.1
|
||||||
github.com/mgechev/revive v1.3.2
|
github.com/mgechev/revive v1.3.2
|
||||||
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
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
|
|||||||
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
||||||
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
|
||||||
|
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||||
|
|||||||
+14
-10
@@ -78,27 +78,31 @@ func genEnvZoneNodes(z *Zone) string {
|
|||||||
return strings.Join(s, " ")
|
return strings.Join(s, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRingZeroGatewayID(z *Zone) int {
|
func getRingZeroGatewayID(z *Zone) (int, error) {
|
||||||
var firstNodeID, gatewayID int
|
var gatewayID int
|
||||||
|
var first *Machine
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
nodeID := p.ID()
|
nodeID := p.ID()
|
||||||
|
|
||||||
if firstNodeID == 0 {
|
if first == nil {
|
||||||
firstNodeID = nodeID
|
first = p
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, found := p.getRingInfo(0); found {
|
if p.IsGateway() {
|
||||||
gatewayID = nodeID
|
gatewayID = nodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
return gatewayID != 0
|
return gatewayID != 0
|
||||||
})
|
})
|
||||||
|
|
||||||
switch {
|
if gatewayID == 0 {
|
||||||
case gatewayID == 0:
|
gatewayID = first.ID()
|
||||||
return firstNodeID
|
|
||||||
default:
|
if err := first.SetGateway(true); err != nil {
|
||||||
return gatewayID
|
return gatewayID, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return gatewayID, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-20
@@ -1,10 +1,7 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/fs"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -61,28 +58,34 @@ func (m *Machine) FullName() string {
|
|||||||
return m.Name
|
return m.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile reads a file from the machine's config directory
|
// IsGateway tells if the Machine is a ring0 gateway
|
||||||
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
func (m *Machine) IsGateway() bool {
|
||||||
base := m.zone.zones.dir
|
m.mu.Lock()
|
||||||
fullName := m.getFilename(name, args...)
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
return fs.ReadFile(base, fullName)
|
if ri, found := m.getRingInfo(0); found {
|
||||||
}
|
return ri.Enabled
|
||||||
|
|
||||||
func (m *Machine) getFilename(name string, args ...any) string {
|
|
||||||
if len(args) > 0 {
|
|
||||||
name = fmt.Sprintf(name, args...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s := []string{
|
return false
|
||||||
m.zone.Name,
|
|
||||||
m.Name,
|
|
||||||
name,
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.Join(s...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGateway enables/disables a Machine ring0 integration
|
||||||
|
func (m *Machine) SetGateway(enabled bool) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
ri, found := m.getRingInfo(0)
|
||||||
|
switch {
|
||||||
|
case !found && !enabled:
|
||||||
|
return nil
|
||||||
|
case !found:
|
||||||
|
return m.createRingInfo(0, true)
|
||||||
|
default:
|
||||||
|
ri.Enabled = enabled
|
||||||
|
return m.writeRingInfo(ri)
|
||||||
|
}
|
||||||
|
}
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
fs "github.com/hack-pad/hackpadfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenFile opens a file on the machine's config directory with the specified flags
|
||||||
|
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||||||
|
base := m.zone.zones.dir
|
||||||
|
fullName := m.getFilename(name, args...)
|
||||||
|
|
||||||
|
return fs.OpenFile(base, fullName, flags, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||||||
|
func (m *Machine) CreateTruncFile(name string, args ...any) (fs.File, error) {
|
||||||
|
return m.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateFile creates a file on the machine's config directory
|
||||||
|
func (m *Machine) CreateFile(name string, args ...any) (fs.File, error) {
|
||||||
|
return m.OpenFile(name, os.O_RDWR|os.O_CREATE, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveFile deletes a file from the machine's config directory
|
||||||
|
func (m *Machine) RemoveFile(name string, args ...any) error {
|
||||||
|
base := m.zone.zones.dir
|
||||||
|
fullName := m.getFilename(name, args...)
|
||||||
|
err := fs.Remove(base, fullName)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadFile reads a file from the machine's config directory
|
||||||
|
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||||||
|
base := m.zone.zones.dir
|
||||||
|
fullName := m.getFilename(name, args...)
|
||||||
|
|
||||||
|
return fs.ReadFile(base, fullName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) getFilename(name string, args ...any) string {
|
||||||
|
if len(args) > 0 {
|
||||||
|
name = fmt.Sprintf(name, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
s := []string{
|
||||||
|
m.zone.Name,
|
||||||
|
m.Name,
|
||||||
|
name,
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(s...)
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
@@ -134,3 +135,86 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Machine) createRingInfo(ring int, enabled bool) error {
|
||||||
|
keys, err := wireguard.NewKeyPair()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ri := &RingInfo{
|
||||||
|
Ring: ring,
|
||||||
|
Enabled: enabled,
|
||||||
|
Keys: keys,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.applyRingInfo(ring, ri)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.writeRingInfo(ri)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeRingInfo(ri *RingInfo) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if m == nil || ri == nil {
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.writeRingInfoPrivate(ri.Ring, ri.Keys.PrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.writeRingInfoPublic(ri.Ring, ri.Keys.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ri.Enabled {
|
||||||
|
return m.deleteRingInfoConf(ri.Ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeRingInfoPrivate(ring int, _ wireguard.PrivateKey) error {
|
||||||
|
f, err := m.CreateTruncFile("wg%v.key", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (m *Machine) writeRingInfoPublic(ring int, _ wireguard.PublicKey) error {
|
||||||
|
f, err := m.CreateTruncFile("wg%v.pub", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) deleteRingInfoConf(ring int) error {
|
||||||
|
err := m.RemoveFile("wg%v.conf", ring)
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case os.ErrNotExist:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+33
-2
@@ -3,7 +3,8 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
|
||||||
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
|
|
||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
)
|
)
|
||||||
@@ -32,6 +33,31 @@ func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||||
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||||
|
var err error
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.ID() == gatewayID {
|
||||||
|
found = true
|
||||||
|
err = p.SetGateway(enabled)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
case !found:
|
||||||
|
return fs.ErrNotExist
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -104,5 +130,10 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
|||||||
|
|
||||||
// New builds a [Zones] tree using the given directory
|
// New builds a [Zones] tree using the given directory
|
||||||
func New(dir, domain string) (*Zones, error) {
|
func New(dir, domain string) (*Zones, error) {
|
||||||
return NewFS(os.DirFS(dir), domain)
|
base, err := os.NewFS().Sub(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewFS(base, domain)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user