Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b49f5e61a2 | |||
| 0b135ad636 | |||
| 0a5cdb313a | |||
| e2fc4e3b9d |
@@ -8,7 +8,7 @@ replace (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
asciigoat.org/ini v0.2.0
|
asciigoat.org/ini v0.1.0
|
||||||
darvaza.org/core v0.9.5
|
darvaza.org/core v0.9.5
|
||||||
darvaza.org/resolver v0.5.2
|
darvaza.org/resolver v0.5.2
|
||||||
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
|
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
|
||||||
@@ -23,7 +23,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
asciigoat.org/core v0.3.7 // indirect
|
asciigoat.org/core v0.3.6 // indirect
|
||||||
darvaza.org/slog/handlers/filter v0.4.4 // indirect
|
darvaza.org/slog/handlers/filter v0.4.4 // indirect
|
||||||
darvaza.org/slog/handlers/zerolog v0.4.4 // indirect
|
darvaza.org/slog/handlers/zerolog v0.4.4 // indirect
|
||||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
|
|||||||
+35
-5
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Config represents a ceph.conf file
|
// Config represents a ceph.conf file
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Global GlobalConfig `ini:"global"`
|
Global GlobalConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
|
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
|
||||||
@@ -24,10 +24,40 @@ func (*Config) WriteTo(w io.Writer) (int64, error) {
|
|||||||
|
|
||||||
// GlobalConfig represents the [global] section of a ceph.conf file
|
// GlobalConfig represents the [global] section of a ceph.conf file
|
||||||
type GlobalConfig struct {
|
type GlobalConfig struct {
|
||||||
FSID uuid.UUID `ini:"fsid"`
|
FSID uuid.UUID
|
||||||
Monitors []string `ini:"mon_host,comma"`
|
Monitors []string
|
||||||
MonitorsAddr []netip.Addr `ini:"mon_initial_members,comma"`
|
MonitorsAddr []netip.Addr
|
||||||
ClusterNetwork netip.Prefix `ini:"cluster_network"`
|
}
|
||||||
|
|
||||||
|
type intermediateConfig struct {
|
||||||
|
Global intermediateGlobalConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p intermediateConfig) Export() (*Config, error) {
|
||||||
|
var out Config
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// [global]
|
||||||
|
out.Global, err = p.Global.Export()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type intermediateGlobalConfig struct {
|
||||||
|
FSID uuid.UUID
|
||||||
|
MonInitialMembers string
|
||||||
|
MonHost string
|
||||||
|
ClusterNetwork string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (intermediateGlobalConfig) Export() (GlobalConfig, error) {
|
||||||
|
var out GlobalConfig
|
||||||
|
var err error
|
||||||
|
|
||||||
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfigFromReader parses the ceph.conf file
|
// NewConfigFromReader parses the ceph.conf file
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
package ceph
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/fs"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"asciigoat.org/ini/basic"
|
|
||||||
"darvaza.org/core"
|
|
||||||
)
|
|
||||||
|
|
||||||
func loadConfSection(out *Config, src *basic.Section) error {
|
|
||||||
switch src.Key {
|
|
||||||
case "global":
|
|
||||||
return loadGlobalConfSection(out, src)
|
|
||||||
default:
|
|
||||||
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadGlobalConfSection(out *Config, src *basic.Section) error {
|
|
||||||
var cfg GlobalConfig
|
|
||||||
|
|
||||||
for _, field := range src.Fields {
|
|
||||||
if err := loadGlobalConfField(&cfg, field); err != nil {
|
|
||||||
return core.Wrap(err, "global")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out.Global = cfg
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadGlobalConfField(_ *GlobalConfig, field basic.Field) error {
|
|
||||||
log.Printf("%s[%q] = %q", "global", field.Key, field.Value)
|
|
||||||
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
|
|
||||||
}
|
|
||||||
+106
-6
@@ -2,6 +2,7 @@ package wireguard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -78,7 +79,7 @@ type PeerConfig struct {
|
|||||||
Name string
|
Name string
|
||||||
PublicKey PublicKey
|
PublicKey PublicKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix `ini:",comma"`
|
AllowedIPs []netip.Prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndpointAddress is a host:port pair to reach the Peer
|
// EndpointAddress is a host:port pair to reach the Peer
|
||||||
@@ -106,11 +107,6 @@ 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"
|
// FromString sets the EndpointAddress from a given "[host]:port"
|
||||||
func (ep *EndpointAddress) FromString(s string) error {
|
func (ep *EndpointAddress) FromString(s string) error {
|
||||||
host, port, err := core.SplitHostPort(s)
|
host, port, err := core.SplitHostPort(s)
|
||||||
@@ -131,6 +127,98 @@ func (ep *EndpointAddress) FromString(s string) error {
|
|||||||
return nil
|
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) {
|
func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
||||||
var out []netip.Prefix
|
var out []netip.Prefix
|
||||||
|
|
||||||
@@ -147,6 +235,18 @@ func parseAllowedIPs(data string) ([]netip.Prefix, error) {
|
|||||||
return out, nil
|
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
|
// NewConfigFromReader parses a wgN.conf file
|
||||||
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
func NewConfigFromReader(r io.Reader) (*Config, error) {
|
||||||
doc, err := basic.Decode(r)
|
doc, err := basic.Decode(r)
|
||||||
|
|||||||
@@ -1,164 +0,0 @@
|
|||||||
package wireguard
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/fs"
|
|
||||||
"net/netip"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"asciigoat.org/ini/basic"
|
|
||||||
"darvaza.org/core"
|
|
||||||
)
|
|
||||||
|
|
||||||
func loadConfSection(out *Config, src *basic.Section) error {
|
|
||||||
switch src.Key {
|
|
||||||
case "Interface":
|
|
||||||
return loadInterfaceConfSection(out, src)
|
|
||||||
case "Peer":
|
|
||||||
return loadPeerConfSection(out, src)
|
|
||||||
default:
|
|
||||||
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
switch field.Key {
|
|
||||||
case "Address":
|
|
||||||
var zero netip.Addr
|
|
||||||
if cfg.Address != zero {
|
|
||||||
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 !cfg.PrivateKey.IsZero() {
|
|
||||||
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 !cfg.PublicKey.IsZero() {
|
|
||||||
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,30 +51,6 @@ 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.
|
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
return encodeKeyJSON(key.String())
|
return encodeKeyJSON(key.String())
|
||||||
|
|||||||
Reference in New Issue
Block a user