Compare commits

..

2 Commits

Author SHA1 Message Date
amery fa21ec9cda Merge branch 'pr-amery-dns' into next-amery 2023-10-24 17:36:05 +00:00
amery 36347abaab dns/show: sort records
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-24 17:35:26 +00:00
5 changed files with 12 additions and 129 deletions
-35
View File
@@ -2,11 +2,9 @@ package main
import (
"context"
"net/netip"
"os"
"time"
"darvaza.org/core"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/cluster"
@@ -151,43 +149,10 @@ 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)
}
-67
View File
@@ -1,67 +0,0 @@
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
}
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
}
+5 -12
View File
@@ -6,7 +6,6 @@ import (
"io"
"net/netip"
"sort"
"strings"
"time"
"darvaza.org/core"
@@ -48,25 +47,19 @@ func SortRecords(s []libdns.Record) []libdns.Record {
}
func lessRecord(a, b libdns.Record) bool {
aName := strings.ToLower(a.Name)
bName := strings.ToLower(b.Name)
switch {
case aName < bName:
case a.Name < b.Name:
return true
case aName > bName:
case a.Name > b.Name:
return false
}
aType := strings.ToUpper(a.Type)
bType := strings.ToUpper(b.Type)
switch {
case aType < bType:
case a.Type < b.Type:
return true
case aType > bType:
case a.Type > b.Type:
return false
case aType == "A", aType == "AAAA":
case a.Type == "A", a.Type == "AAAA":
// IP Addresses
var aa, ba netip.Addr
+3 -6
View File
@@ -14,17 +14,14 @@ import (
// Show shows current DNS entries
func (mgr *Manager) Show(ctx context.Context, names ...string) error {
var buf bytes.Buffer
recs, err := mgr.GetRecords(ctx, names...)
if err != nil {
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 {
_ = fmtRecord(&buf, rr)
@@ -32,7 +29,7 @@ func writeRecords(recs []libdns.Record, w io.Writer) error {
}
_, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs))
_, err := buf.WriteTo(w)
_, err = buf.WriteTo(os.Stdout)
return err
}
+4 -9
View File
@@ -54,7 +54,7 @@ func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error
return nil, err
}
return mgr.asSyncRecords(recs)
return mgr.filteredSyncRecords(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) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr {
func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
// filter and convert
out := make(map[string][]SyncAddr)
cache := make(map[string][]SyncAddr)
for _, rr := range recs {
addr, ok, err := mgr.AsSyncAddr(rr)
switch {
@@ -106,14 +106,9 @@ func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr
Print()
case ok:
// store
out[rr.Name] = append(out[rr.Name], addr)
cache[rr.Name] = append(cache[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))