Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ab2c9d068 | |||
| 098482a890 | |||
| 0a48a40796 | |||
| 3c24e24d71 |
@@ -1,2 +1 @@
|
||||
.tmp
|
||||
.version
|
||||
|
||||
+2
-5
@@ -2,8 +2,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -14,9 +12,8 @@ const (
|
||||
|
||||
var (
|
||||
rootCmd = &cobra.Command{
|
||||
Use: CmdName,
|
||||
Short: "control tool for jpi.cloud",
|
||||
Version: version,
|
||||
Use: CmdName,
|
||||
Short: "control tool for jpi.cloud",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
//go:generate sh -c "git describe | tr -d '\r\n' > .version"
|
||||
//go:embed .version
|
||||
var version string
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Returns jpictl's version",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
_, _ = fmt.Fprintf(os.Stdout, "%s\n", version)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
if version == "" {
|
||||
version = "undetermined"
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
@@ -2,6 +2,8 @@ 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.12.0
|
||||
@@ -17,7 +19,6 @@ require (
|
||||
github.com/spf13/cobra v1.8.0
|
||||
golang.org/x/crypto v0.20.0
|
||||
golang.org/x/net v0.21.0
|
||||
gopkg.in/gcfg.v1 v1.2.3
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -50,5 +51,4 @@ require (
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/tools v0.18.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/cache/x/simplelru v0.1.8 h1:rvFucut4wKYbsYc994yR3P0M08NqlsvZxr5G4QK82tw=
|
||||
darvaza.org/cache/x/simplelru v0.1.8/go.mod h1:Mv1isOJTcXYK+aK0AvUe+/3KpRTXDsYga6rdTS/upNs=
|
||||
darvaza.org/core v0.12.0 h1:LLtYh9RZJSd0sgPTDocofCc4H5jbutSUQgPbKt+8gcI=
|
||||
@@ -115,10 +113,6 @@ golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
|
||||
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
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=
|
||||
|
||||
@@ -39,9 +39,6 @@ func writeGlobalToBuffer(w *bytes.Buffer, c *GlobalConfig) {
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "mon_initial_members", strings.Join(c.Monitors, ", "))
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "mon_host", joinAddrs(c.MonitorsAddr, ", "))
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "cluster_network", c.ClusterNetwork.String())
|
||||
|
||||
_, _ = fmt.Fprintf(w, "\n; %s\n", "don't rewrite labels on startup")
|
||||
_, _ = fmt.Fprintf(w, "%s = %s\n", "osd_class_update_on_start", "false")
|
||||
}
|
||||
|
||||
func joinAddrs(addrs []netip.Addr, sep string) string {
|
||||
|
||||
+12
-47
@@ -2,10 +2,9 @@ package ceph
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/netip"
|
||||
"log"
|
||||
|
||||
"asciigoat.org/ini/basic"
|
||||
"asciigoat.org/ini/parser"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
@@ -36,61 +35,27 @@ func loadGlobalConfSection(out *Config, src *basic.Section) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// revive:disable:cyclomatic
|
||||
// revive:disable:cognitive-complexity
|
||||
|
||||
func loadGlobalConfField(cfg *GlobalConfig, field basic.Field) error {
|
||||
// revive:enable:cyclomatic
|
||||
// revive:enable:cognitive-complexity
|
||||
log.Printf("%s[%q] = %q", "global", field.Key, field.Value)
|
||||
|
||||
// TODO: refactor when asciigoat's ini parser learns to do reflection
|
||||
key, value := field.Key, field.Value
|
||||
|
||||
switch field.Key {
|
||||
switch key {
|
||||
case "fsid":
|
||||
if !core.IsZero(cfg.FSID) {
|
||||
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
|
||||
}
|
||||
|
||||
err := cfg.FSID.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return configFieldHandler(&cfg.FSID, key, value)
|
||||
case "mon_host":
|
||||
entries, _ := parser.SplitCommaArray(field.Value)
|
||||
for _, s := range entries {
|
||||
var addr netip.Addr
|
||||
|
||||
if err := addr.UnmarshalText([]byte(s)); err != nil {
|
||||
return core.Wrap(err, field.Key)
|
||||
}
|
||||
|
||||
cfg.MonitorsAddr = append(cfg.MonitorsAddr, addr)
|
||||
}
|
||||
return nil
|
||||
return configFieldHandler(&cfg.MonitorsAddr, key, value)
|
||||
case "mon_initial_members":
|
||||
entries, _ := parser.SplitCommaArray(field.Value)
|
||||
cfg.Monitors = append(cfg.Monitors, entries...)
|
||||
return nil
|
||||
return configFieldHandler(&cfg.Monitors, key, value)
|
||||
case "cluster_network":
|
||||
if !core.IsZero(cfg.ClusterNetwork) {
|
||||
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
|
||||
return err
|
||||
}
|
||||
|
||||
err := cfg.ClusterNetwork.UnmarshalText([]byte(field.Value))
|
||||
switch {
|
||||
case err != nil:
|
||||
return core.Wrap(err, field.Key)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
return configFieldHandler(&cfg.ClusterNetwork, key, value)
|
||||
default:
|
||||
return core.Wrap(fs.ErrNotExist, "field %q unknown", key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func configFieldHandler(vi any, key, value string) error
|
||||
|
||||
func newConfigFromDocument(doc *basic.Document) (*Config, error) {
|
||||
var out Config
|
||||
|
||||
|
||||
+10
-113
@@ -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{
|
||||
@@ -79,7 +78,7 @@ type PeerConfig struct {
|
||||
Name string
|
||||
PublicKey PublicKey
|
||||
Endpoint EndpointAddress
|
||||
AllowedIPs []netip.Prefix
|
||||
AllowedIPs []netip.Prefix `ini:",comma"`
|
||||
}
|
||||
|
||||
// EndpointAddress is a host:port pair to reach the Peer
|
||||
@@ -132,100 +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.Wrap(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,
|
||||
}
|
||||
|
||||
if p.PrivateKey != "" {
|
||||
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
|
||||
|
||||
@@ -242,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,105 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"log"
|
||||
|
||||
"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.Wrap(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.Wrap(err, "Peer[%v]", len(out.Peer))
|
||||
}
|
||||
}
|
||||
|
||||
out.Peer = append(out.Peer, cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func configFieldHandler(vi any, key, value string) error
|
||||
|
||||
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error {
|
||||
log.Printf("%s[%q] = %q", "Interface", field.Key, field.Value)
|
||||
|
||||
key, value := field.Key, field.Value
|
||||
|
||||
switch key {
|
||||
case "Address":
|
||||
return configFieldHandler(&cfg.Address, key, value)
|
||||
case "PrivateKey":
|
||||
return configFieldHandler(&cfg.PrivateKey, key, value)
|
||||
case "ListenPort":
|
||||
return configFieldHandler(&cfg.ListenPort, key, value)
|
||||
default:
|
||||
return core.Wrap(fs.ErrInvalid, "unknown field %q", key)
|
||||
}
|
||||
}
|
||||
|
||||
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error {
|
||||
log.Printf("%s[%q] = %q", "Peer", field.Key, field.Value)
|
||||
|
||||
key, value := field.Key, field.Value
|
||||
|
||||
switch key {
|
||||
case "PublicKey":
|
||||
return configFieldHandler(&cfg.PublicKey, key, value)
|
||||
case "Endpoint":
|
||||
return configFieldHandler(&cfg.Endpoint, key, value)
|
||||
case "AllowedIPs":
|
||||
return configFieldHandler(&cfg.AllowedIPs, key, value)
|
||||
default:
|
||||
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user