Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 202f2e6dfc | |||
| 20484a5061 | |||
| 45b25c63d4 | |||
| c0e2ae9bf1 | |||
| 080021b427 | |||
| 4514b44211 | |||
| 49b82ace71 | |||
| 2207e4a4a4 | |||
| 7ca01aa1e4 | |||
| 8b72667f4d | |||
| 49694eb7cb | |||
| 15a98c05ec | |||
| a005823d44 | |||
| 7af8484acc | |||
| 0f1f1ce968 | |||
| 5058f286c6 | |||
| 86075eb47f |
+53
-2
@@ -2,27 +2,78 @@ 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"
|
"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 toml representation of the config",
|
Short: "generates a text 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 := zones.New(cfg.Base, cfg.Domain)
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
enc := toml.NewEncoder(&buf)
|
switch encoding {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ 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
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
gopkg.in/gcfg.v1 v1.2.3
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -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=
|
||||||
@@ -88,6 +90,7 @@ 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=
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519"
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
@@ -50,6 +51,43 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON encodes the key for JSON, omiting empty.
|
||||||
|
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
|
return encodeKeyJSON(key.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON encodes the key for JSON, omiting 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, omiting empty.
|
||||||
|
func (key PrivateKey) MarshalYAML() (any, error) {
|
||||||
|
return encodeKeyYAML(key.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalYAML encodes the key for YAML, omiting 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
|
||||||
|
|||||||
+8
-29
@@ -1,23 +1,24 @@
|
|||||||
package zones
|
package zones
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/fs"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"path/filepath"
|
|
||||||
"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
|
ID int `toml:"id"`
|
||||||
Name string `toml:"name"`
|
Name string `toml:"-" json:"-" yaml:"-"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
||||||
Rings []*RingInfo `toml:"rings,omitempty"`
|
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// revive:enable:line-length-limit
|
||||||
|
|
||||||
func (m *Machine) String() string {
|
func (m *Machine) String() string {
|
||||||
return m.Name
|
return m.Name
|
||||||
}
|
}
|
||||||
@@ -36,28 +37,6 @@ func (m *Machine) FullName() string {
|
|||||||
return m.Name
|
return m.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsGateway tells if the Machine is a ring0 gateway
|
// IsGateway tells if the Machine is a ring0 gateway
|
||||||
func (m *Machine) IsGateway() bool {
|
func (m *Machine) IsGateway() bool {
|
||||||
_, ok := m.getRingInfo(0)
|
_, ok := m.getRingInfo(0)
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"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) (io.WriteCloser, error) {
|
||||||
|
return m.openWriter(name, 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) (io.WriteCloser, error) {
|
||||||
|
return m.openWriter(name, os.O_CREATE, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||||||
|
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.(io.WriteCloser), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteStringFile writes the given content to a file on the machine's config directory
|
||||||
|
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
|
||||||
|
f, err := m.CreateTruncFile(name, args...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
buf := bytes.NewBufferString(value)
|
||||||
|
_, err = buf.WriteTo(f)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
@@ -72,6 +73,60 @@ 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
|
||||||
|
// the machine's config directory
|
||||||
|
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
err = m.RemoveFile("wg%v.pub", ring)
|
||||||
|
switch {
|
||||||
|
case os.IsNotExist(err):
|
||||||
|
// ignore
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.RemoveFile("wg%v.key", ring)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// ignore
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// GetWireguardConfig reads a wgN.conf file
|
// GetWireguardConfig reads a wgN.conf file
|
||||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||||
data, err := m.ReadFile("wg%v.conf", ring)
|
data, err := m.ReadFile("wg%v.conf", ring)
|
||||||
@@ -195,3 +250,14 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
||||||
|
// config directory.
|
||||||
|
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||||
|
err := m.RemoveFile("wg%v.conf", ring)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
+16
-4
@@ -3,7 +3,9 @@ package zones
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/hack-pad/hackpadfs/os"
|
||||||
|
|
||||||
"darvaza.org/resolver"
|
"darvaza.org/resolver"
|
||||||
)
|
)
|
||||||
@@ -12,8 +14,8 @@ import (
|
|||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
|
|
||||||
ID int
|
ID int `toml:"id"`
|
||||||
Name string
|
Name string `toml:"name"`
|
||||||
|
|
||||||
Machines []*Machine `toml:"machines"`
|
Machines []*Machine `toml:"machines"`
|
||||||
}
|
}
|
||||||
@@ -104,5 +106,15 @@ 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)
|
dir, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
base, err := os.NewFS().Sub(dir[1:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewFS(base, domain)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user