Merge branch 'main' into next-amery

This commit is contained in:
2023-10-27 20:10:32 +00:00
10 changed files with 362 additions and 63 deletions
+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
}
+12
View File
@@ -0,0 +1,12 @@
package dns
import "errors"
var (
// ErrNoDNSProvider indicates a [libdns.Provider] wasn't assigned
// to the [Manager]
ErrNoDNSProvider = errors.New("dns provider not specified")
// ErrNoDomain indicates a domain wasn't specified
ErrNoDomain = errors.New("domain not specified")
)
+60 -3
View File
@@ -2,15 +2,16 @@ package dns
import (
"context"
"errors"
"io/fs"
"net/netip"
"strings"
"darvaza.org/core"
"darvaza.org/slog"
"git.jpi.io/amery/jpictl/pkg/cluster"
"github.com/libdns/libdns"
"golang.org/x/net/publicsuffix"
"git.jpi.io/amery/jpictl/pkg/cluster"
)
// Manager is a DNS Manager instance
@@ -71,7 +72,7 @@ func (mgr *Manager) setDefaults() error {
}
if mgr.domain == "" || mgr.suffix == "" {
return errors.New("domain not specified")
return ErrNoDomain
}
for _, opt := range opts {
@@ -120,6 +121,62 @@ func NewManager(opts ...ManagerOption) (*Manager, error) {
return mgr, nil
}
// GetRecords pulls all the address records on DNS for our domain,
// optionally only those matching the given names.
func (mgr *Manager) GetRecords(ctx context.Context, names ...string) ([]libdns.Record, error) {
if mgr.p == nil {
return nil, ErrNoDNSProvider
}
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
switch {
case err != nil:
// failed
return nil, err
case len(recs) == 0:
// empty
return []libdns.Record{}, nil
case mgr.suffix == "" && len(names) == 0:
// unfiltered
return recs, nil
default:
// filtered
recs = mgr.filterRecords(recs, names...)
return recs, nil
}
}
func (mgr *Manager) filterRecords(recs []libdns.Record, names ...string) []libdns.Record {
out := make([]libdns.Record, 0, len(recs))
for _, rr := range recs {
name, ok := mgr.matchSuffix(rr)
switch {
case !ok:
// skip, wrong subdomain
continue
case len(names) == 0:
// unfiltered, take it
case !core.SliceContains(names, name):
// skip, not one of the requested names
continue
}
out = append(out, rr)
}
return out
}
func (mgr *Manager) matchSuffix(rr libdns.Record) (string, bool) {
if mgr.suffix == "" {
// no suffix
return rr.Name, true
}
// remove suffix
return strings.CutSuffix(rr.Name, mgr.suffix)
}
// AddHost registers a host
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
active bool, addrs ...netip.Addr) error {
+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
+58
View File
@@ -0,0 +1,58 @@
package dns
import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
)
// Show shows current DNS entries
func (mgr *Manager) Show(ctx context.Context, names ...string) error {
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)
_, _ = 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
}
_, 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
}
+13 -13
View File
@@ -2,7 +2,6 @@ package dns
import (
"context"
"errors"
"net/netip"
"sort"
"strings"
@@ -48,18 +47,14 @@ func SortSyncAddrSlice(s []SyncAddr) []SyncAddr {
return s
}
// GetRecords pulls all the address records on DNS for our domain
func (mgr *Manager) GetRecords(ctx context.Context) ([]SyncAddrRecord, error) {
if mgr.p == nil {
return nil, errors.New("dns provider not specified")
}
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
// GetSyncRecords pulls all the address records on DNS for our domain
func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error) {
recs, err := mgr.GetRecords(ctx)
if err != nil {
return nil, err
}
return mgr.filteredRecords(recs)
return mgr.asSyncRecords(recs)
}
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
@@ -94,9 +89,9 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
return out, true, nil
}
func (mgr *Manager) filteredRecords(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 {
@@ -111,9 +106,14 @@ func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, err
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))
@@ -137,7 +137,7 @@ func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, err
// Sync updates all the address records on DNS for our domain
func (mgr *Manager) Sync(ctx context.Context) error {
current, err := mgr.GetRecords(ctx)
current, err := mgr.GetSyncRecords(ctx)
if err != nil {
return core.Wrap(err, "GetRecords")
}
+3 -9
View File
@@ -207,20 +207,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