feat(dns): add explicit DNS service constructor aliases

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 23:45:05 +00:00
parent 8857ed4e51
commit 958a799c45
3 changed files with 67 additions and 4 deletions

View file

@ -25,10 +25,10 @@ var (
)
const (
actionArgBind = "bind"
actionArgIP = "ip"
actionArgName = "name"
actionArgPort = "port"
actionArgBind = "bind"
actionArgIP = "ip"
actionArgName = "name"
actionArgPort = "port"
actionArgHealthPort = "health_port"
)
@ -192,6 +192,8 @@ func (service *Service) RegisterActions(registrar ActionRegistrar) {
// NewServiceWithRegistrar builds a DNS service and registers its actions in one step.
//
// Deprecated: use NewDNSServiceWithRegistrar for a more explicit constructor name.
//
// service := dns.NewServiceWithRegistrar(dns.ServiceConfiguration{}, registrar)
// // registrar now exposes dns.resolve, dns.resolve.txt, dns.resolve.all, dns.reverse, dns.serve, dns.health, dns.discover
func NewServiceWithRegistrar(options ServiceOptions, registrar ActionRegistrar) *Service {
@ -201,6 +203,14 @@ func NewServiceWithRegistrar(options ServiceOptions, registrar ActionRegistrar)
return NewService(options)
}
// NewDNSServiceWithRegistrar builds a DNS service and registers its actions in one step.
//
// service := dns.NewDNSServiceWithRegistrar(dns.ServiceConfiguration{}, registrar)
// // registrar now exposes dns.resolve, dns.resolve.txt, dns.resolve.all, dns.reverse, dns.serve, dns.health, dns.discover
func NewDNSServiceWithRegistrar(options ServiceOptions, registrar ActionRegistrar) *Service {
return NewServiceWithRegistrar(options, registrar)
}
// HandleAction executes a DNS action by name.
//
// payload, ok, err := service.HandleAction(ActionResolve, map[string]any{

View file

@ -176,6 +176,8 @@ type Options = ServiceOptions
// NewService builds a DNS service from cached records and optional discovery hooks.
//
// Deprecated: use NewDNSService for a more explicit constructor name.
//
// service := dns.NewService(dns.ServiceConfig{
// Records: map[string]dns.NameRecords{
// "gateway.charon.lthn": {A: []string{"10.10.10.10"}},
@ -265,6 +267,17 @@ func NewService(options ServiceOptions) *Service {
return service
}
// NewDNSService builds a DNS service from cached records and optional discovery hooks.
//
// service := dns.NewDNSService(dns.ServiceConfiguration{
// Records: map[string]dns.NameRecords{
// "gateway.charon.lthn": {A: []string{"10.10.10.10"}},
// },
// })
func NewDNSService(options ServiceOptions) *Service {
return NewService(options)
}
func (service *Service) resolveHSDClient(client *HSDClient) (*HSDClient, error) {
if client != nil {
return client, nil

View file

@ -130,6 +130,46 @@ func TestServiceOptionsAliasBuildsService(t *testing.T) {
}
}
func TestNewDNSServiceAliasToExistingConstructor(t *testing.T) {
service := NewDNSService(ServiceOptions{
Records: map[string]NameRecords{
"gateway.charon.lthn": {
A: []string{"10.10.10.10"},
},
},
})
result, ok := service.ResolveAddress("gateway.charon.lthn")
if !ok {
t.Fatal("expected service constructed from NewDNSService alias to resolve")
}
if len(result.Addresses) != 1 || result.Addresses[0] != "10.10.10.10" {
t.Fatalf("unexpected resolve result from NewDNSService: %#v", result.Addresses)
}
}
func TestNewDNSServiceWithRegistrarAliasRegistersActions(t *testing.T) {
recorder := &actionRecorder{}
service := NewDNSServiceWithRegistrar(ServiceOptions{}, recorder)
if service == nil {
t.Fatal("expected service instance from NewDNSServiceWithRegistrar")
}
if len(recorder.names) != len(service.ActionNames()) {
t.Fatalf("expected %d registered action names, got %d", len(service.ActionNames()), len(recorder.names))
}
expected := map[string]struct{}{}
for _, name := range service.ActionNames() {
expected[name] = struct{}{}
}
for _, name := range recorder.names {
if _, ok := expected[name]; !ok {
t.Fatalf("unexpected action name registered by NewDNSServiceWithRegistrar: %q", name)
}
}
}
func TestServiceResolveUsesMostSpecificWildcard(t *testing.T) {
service := NewService(ServiceOptions{
Records: map[string]NameRecords{