Compare commits

..

3 Commits

Author SHA1 Message Date
amery 70411a2aab dns/show: sort records
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-24 17:13:12 +00:00
amery c94fa1770c dns/show: refactor Record formatting
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-24 17:13:12 +00:00
amery b6750f8b28 dns/sync: remove redundant suffix check
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-24 17:13:12 +00:00
3 changed files with 76 additions and 17 deletions
+41
View File
@@ -38,6 +38,47 @@ 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 {
switch {
case a.Name < b.Name:
return true
case a.Name > b.Name:
return false
}
switch {
case a.Type < b.Type:
return true
case a.Type > b.Type:
return false
case a.Type == "A", a.Type == "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
+34 -8
View File
@@ -1,29 +1,55 @@
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 {
var buf bytes.Buffer
recs, err := mgr.GetRecords(ctx, names...)
if err != nil {
return core.Wrap(err, "GetRecords")
}
SortRecords(recs)
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(os.Stdout)
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
}
+1 -9
View File
@@ -4,7 +4,6 @@ import (
"context"
"net/netip"
"sort"
"strings"
"time"
"darvaza.org/core"
@@ -58,7 +57,7 @@ func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error
}
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
func (*Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
var out SyncAddr
var addr netip.Addr
@@ -67,13 +66,6 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
return out, false, nil
}
// skip entries not containing our suffix
if mgr.suffix != "" {
if !strings.HasSuffix(rr.Name, mgr.suffix) {
return out, false, nil
}
}
err := addr.UnmarshalText([]byte(rr.Value))
if err != nil {
// invalid address on A or AAAA record