|
|
|
@ -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 { |
|
|
|
|