Compare commits
3 Commits
v0.6.8
...
440dcde50a
| Author | SHA1 | Date | |
|---|---|---|---|
| 440dcde50a | |||
| c578990f8c | |||
| b0f4be7047 |
+45
-19
@@ -72,6 +72,29 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// revive:disable:flag-parameter
|
||||||
|
func newDNSManagerCommand(_ *cobra.Command,
|
||||||
|
resolve bool, withCredentials bool) (*dns.Manager, error) {
|
||||||
|
// revive:enable:flag-parameter
|
||||||
|
var cred dns.Provider
|
||||||
|
|
||||||
|
if withCredentials {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
cred, err = dns.DefaultDNSProvider()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := cfg.LoadZones(resolve)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return newDNSManager(m, cred)
|
||||||
|
}
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
var dnsCmd = &cobra.Command{
|
var dnsCmd = &cobra.Command{
|
||||||
Use: "dns",
|
Use: "dns",
|
||||||
@@ -81,13 +104,8 @@ var dnsWriteCmd = &cobra.Command{
|
|||||||
Use: "write",
|
Use: "write",
|
||||||
Short: "dns write generates public DNS records",
|
Short: "dns write generates public DNS records",
|
||||||
PreRun: setVerbosity,
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
m, err := cfg.LoadZones(true)
|
mgr, err := newDNSManagerCommand(cmd, true, false)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
mgr, err := newDNSManager(m, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -101,18 +119,8 @@ var dnsSyncCmd = &cobra.Command{
|
|||||||
Use: "sync",
|
Use: "sync",
|
||||||
Short: "dns sync updates public DNS records",
|
Short: "dns sync updates public DNS records",
|
||||||
PreRun: setVerbosity,
|
PreRun: setVerbosity,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
cred, err := dns.DefaultDNSProvider()
|
mgr, err := newDNSManagerCommand(cmd, true, true)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
m, err := cfg.LoadZones(true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
mgr, err := newDNSManager(m, cred)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -124,9 +132,27 @@ var dnsSyncCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dnsShowCmd = &cobra.Command{
|
||||||
|
Use: "show [<name>...]",
|
||||||
|
Short: "dns show lists entries on DNS for our domain",
|
||||||
|
PreRun: setVerbosity,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
mgr, err := newDNSManagerCommand(cmd, true, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return mgr.Show(ctx, args...)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(dnsCmd)
|
rootCmd.AddCommand(dnsCmd)
|
||||||
|
|
||||||
dnsCmd.AddCommand(dnsWriteCmd)
|
dnsCmd.AddCommand(dnsWriteCmd)
|
||||||
dnsCmd.AddCommand(dnsSyncCmd)
|
dnsCmd.AddCommand(dnsSyncCmd)
|
||||||
|
dnsCmd.AddCommand(dnsShowCmd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
@@ -2,15 +2,16 @@ package dns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
"darvaza.org/slog"
|
"darvaza.org/slog"
|
||||||
"git.jpi.io/amery/jpictl/pkg/cluster"
|
"github.com/libdns/libdns"
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manager is a DNS Manager instance
|
// Manager is a DNS Manager instance
|
||||||
@@ -71,7 +72,7 @@ func (mgr *Manager) setDefaults() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if mgr.domain == "" || mgr.suffix == "" {
|
if mgr.domain == "" || mgr.suffix == "" {
|
||||||
return errors.New("domain not specified")
|
return ErrNoDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -120,6 +121,62 @@ func NewManager(opts ...ManagerOption) (*Manager, error) {
|
|||||||
return mgr, nil
|
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
|
// AddHost registers a host
|
||||||
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
|
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
|
||||||
active bool, addrs ...netip.Addr) error {
|
active bool, addrs ...netip.Addr) error {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = fmt.Printf("; %v records\n", len(recs))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+6
-11
@@ -2,7 +2,6 @@ package dns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -48,18 +47,14 @@ func SortSyncAddrSlice(s []SyncAddr) []SyncAddr {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecords pulls all the address records on DNS for our domain
|
// GetSyncRecords pulls all the address records on DNS for our domain
|
||||||
func (mgr *Manager) GetRecords(ctx context.Context) ([]SyncAddrRecord, error) {
|
func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error) {
|
||||||
if mgr.p == nil {
|
recs, err := mgr.GetRecords(ctx)
|
||||||
return nil, errors.New("dns provider not specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return mgr.filteredRecords(recs)
|
return mgr.filteredSyncRecords(recs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
|
||||||
@@ -94,7 +89,7 @@ func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
|
|||||||
return out, true, nil
|
return out, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
func (mgr *Manager) filteredSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
|
||||||
// filter and convert
|
// filter and convert
|
||||||
cache := make(map[string][]SyncAddr)
|
cache := make(map[string][]SyncAddr)
|
||||||
for _, rr := range recs {
|
for _, rr := range recs {
|
||||||
@@ -137,7 +132,7 @@ func (mgr *Manager) filteredRecords(recs []libdns.Record) ([]SyncAddrRecord, err
|
|||||||
|
|
||||||
// Sync updates all the address records on DNS for our domain
|
// Sync updates all the address records on DNS for our domain
|
||||||
func (mgr *Manager) Sync(ctx context.Context) error {
|
func (mgr *Manager) Sync(ctx context.Context) error {
|
||||||
current, err := mgr.GetRecords(ctx)
|
current, err := mgr.GetSyncRecords(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return core.Wrap(err, "GetRecords")
|
return core.Wrap(err, "GetRecords")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user