Compare commits

..

9 Commits

Author SHA1 Message Date
amery 9ab7594bcc Merge pull request 'jpictl: add initial dns add command' (#29)
Reviewed-on: #29
2023-10-27 18:56:11 +02:00
karasz 07d4f462a3 Merge pull request 'wireguard: fix KeyPair.Validate()' (#30)
Reviewed-on: #30
2023-10-27 18:11:16 +02:00
amery 142ea00577 wireguard: fix KeyPair.Validate()
PrivateKey and PublicKey are now fixed length arrays,
so testing for len 0 is invalid

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-27 15:55:27 +00:00
amery 052f89152c jpictl/dns: introduce add command to register new machines
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-27 15:52:50 +00:00
amery 557f156579 dns: refactor asSyncRecords()
for direct access of the unsorted map

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-27 15:52:48 +00:00
amery e857ff7456 Merge pull request 'dns: refactor record formatting and sort show results' (#28)
Reviewed-on: #28
2023-10-27 17:52:19 +02:00
amery 9da49f2d86 dns/show: sort records
v2: change Name to lower case and Type to upper case before comparing

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-26 18:47:29 +00:00
amery 356322bc94 dns/show: introduce writeRecords() helper
to print a whole []libdns.Record

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-26 18:41:32 +00:00
amery 7dac96f474 dns/show: refactor Record formatting
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-24 17:35:26 +00:00
6 changed files with 201 additions and 21 deletions
+35
View File
@@ -2,9 +2,11 @@ package main
import (
"context"
"net/netip"
"os"
"time"
"darvaza.org/core"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/cluster"
@@ -149,10 +151,43 @@ var dnsShowCmd = &cobra.Command{
},
}
var dnsAddCmd = &cobra.Command{
Use: "add <name> <address..>",
Short: "dns add registers a new machine on the public DNS",
Args: cobra.MinimumNArgs(2),
PreRun: setVerbosity,
RunE: func(cmd *cobra.Command, args []string) error {
var addrs []netip.Addr
for _, s := range args[1:] {
addr, err := core.ParseAddr(s)
switch {
case err != nil:
return core.Wrap(err, s)
case !addr.IsValid(), addr.IsUnspecified(), addr.IsPrivate(), addr.IsMulticast():
return core.Wrap(core.ErrInvalid, s)
default:
addrs = append(addrs, addr)
}
}
mgr, err := newDNSManagerCommand(cmd, true, true)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
defer cancel()
return mgr.Add(ctx, args[0], addrs...)
},
}
func init() {
rootCmd.AddCommand(dnsCmd)
dnsCmd.AddCommand(dnsWriteCmd)
dnsCmd.AddCommand(dnsSyncCmd)
dnsCmd.AddCommand(dnsShowCmd)
dnsCmd.AddCommand(dnsAddCmd)
}
+69
View File
@@ -0,0 +1,69 @@
package dns
import (
"context"
"net/netip"
"os"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
)
// Add adds a machine to the DNS records
func (mgr *Manager) Add(ctx context.Context, name string, addrs ...netip.Addr) error {
// TODO: validate name
cur, err := mgr.GetRecords(ctx, name)
if err != nil {
return core.Wrap(err, "GetRecords")
}
// merge []SyncAddr for name
s := mgr.asSyncRecordsMap(cur)[name+mgr.suffix]
for _, addr := range addrs {
s = AppendSyncAddr(s, addr)
}
return mgr.addSyncAddr(ctx, name, s)
}
func (mgr *Manager) addSyncAddr(ctx context.Context, name string, s []SyncAddr) error {
var recs []libdns.Record
for _, a := range s {
recs = append(recs, libdns.Record{
ID: a.ID,
Name: name + mgr.suffix,
Type: core.IIf(a.Addr.Is6(), "AAAA", "A"),
TTL: time.Second,
Value: a.Addr.String(),
})
}
SortRecords(recs)
err := writeRecords(recs, os.Stdout)
if err != nil {
return err
}
_, err = mgr.p.SetRecords(ctx, mgr.domain, recs)
return err
}
// AppendSyncAddr appends a [netip.Addr] to a [SyncAddr] slice
// if the address is new.
func AppendSyncAddr(s []SyncAddr, addr netip.Addr) []SyncAddr {
for _, se := range s {
if se.Addr.Compare(addr) == 0 {
// found
return s
}
}
s = append(s, SyncAddr{
Addr: addr,
TTL: time.Second,
})
return s
}
+48
View File
@@ -6,6 +6,7 @@ import (
"io"
"net/netip"
"sort"
"strings"
"time"
"darvaza.org/core"
@@ -38,6 +39,53 @@ func SortAddrRecords(s []AddrRecord) []AddrRecord {
return s
}
// SortRecords sorts a slice of [libdns.Record], by Name, Type and Value
func SortRecords(s []libdns.Record) []libdns.Record {
sort.Slice(s, func(i, j int) bool {
return lessRecord(s[i], s[j])
})
return s
}
func lessRecord(a, b libdns.Record) bool {
aName := strings.ToLower(a.Name)
bName := strings.ToLower(b.Name)
switch {
case aName < bName:
return true
case aName > bName:
return false
}
aType := strings.ToUpper(a.Type)
bType := strings.ToUpper(b.Type)
switch {
case aType < bType:
return true
case aType > bType:
return false
case aType == "A", aType == "AAAA":
// IP Addresses
var aa, ba netip.Addr
switch {
case aa.UnmarshalText([]byte(a.Value)) != nil:
// bad address on a
return true
case ba.UnmarshalText([]byte(b.Value)) != nil:
// bad address on b
return false
default:
return aa.Less(ba)
}
default:
// text
return a.Value < b.Value
}
}
// SortRegions sorts regions. first by length those 3-character
// or shorter, and then by length. It's mostly aimed at
// supporting ISO-3166 order
+37 -8
View File
@@ -1,11 +1,15 @@
package dns
import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
)
// Show shows current DNS entries
@@ -15,15 +19,40 @@ func (mgr *Manager) Show(ctx context.Context, names ...string) error {
return core.Wrap(err, "GetRecords")
}
SortRecords(recs)
return writeRecords(recs, os.Stdout)
}
func writeRecords(recs []libdns.Record, w io.Writer) error {
var buf bytes.Buffer
for _, rr := range recs {
_, _ = fmt.Printf("%s\t%v\tIN\t%s\t%s\t; %s\n",
rr.Name,
int(rr.TTL/time.Second),
rr.Type,
rr.Value,
rr.ID)
_ = fmtRecord(&buf, rr)
_, _ = buf.WriteRune('\n')
}
_, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs))
_, err := buf.WriteTo(w)
return err
}
func fmtRecord(w io.Writer, rr libdns.Record) error {
ttl := int(rr.TTL / time.Second)
if ttl < 1 {
ttl = 1
}
_, _ = fmt.Printf("; %v records\n", len(recs))
return nil
_, err := fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s",
rr.Name,
ttl,
rr.Type,
rr.Value)
if err == nil {
if rr.ID != "" {
_, err = fmt.Fprintf(w, "\t; %s", rr.ID)
}
}
return err
}
+9 -4
View File
@@ -54,7 +54,7 @@ func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error
return nil, err
}
return mgr.filteredSyncRecords(recs)
return mgr.asSyncRecords(recs)
}
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
@@ -89,9 +89,9 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
return out, true, nil
}
func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr {
// filter and convert
cache := make(map[string][]SyncAddr)
out := make(map[string][]SyncAddr)
for _, rr := range recs {
addr, ok, err := mgr.AsSyncAddr(rr)
switch {
@@ -106,9 +106,14 @@ func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord,
Print()
case ok:
// store
cache[rr.Name] = append(cache[rr.Name], addr)
out[rr.Name] = append(out[rr.Name], addr)
}
}
return out
}
func (mgr *Manager) asSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
cache := mgr.asSyncRecordsMap(recs)
// prepare records
out := make([]SyncAddrRecord, len(cache))
+3 -9
View File
@@ -183,20 +183,14 @@ type KeyPair struct {
// Validate checks the PublicKey matches the PrivateKey,
// and sets the PublicKey if missing
func (kp *KeyPair) Validate() error {
keyLen := len(kp.PrivateKey)
pubLen := len(kp.PublicKey)
switch {
case keyLen != PrivateKeySize:
// bad private key
case kp.PrivateKey.IsZero():
// no private key
return ErrInvalidPrivateKey
case pubLen == 0:
case kp.PublicKey.IsZero():
// no public key, set it
kp.PublicKey = kp.PrivateKey.Public()
return nil
case pubLen != PublicKeySize:
// bad public key
return ErrInvalidPublicKey
case !kp.PrivateKey.Public().Equal(kp.PublicKey):
// wrong public key
return ErrInvalidPublicKey