diff --git a/action.go b/action.go index 74486a4..a42356e 100644 --- a/action.go +++ b/action.go @@ -289,7 +289,7 @@ func (service *Service) handleServe(ctx context.Context, values map[string]any) return nil, false, err } if !portProvided { - port = service.dnsPort + port = service.resolveServePort() } result, err := service.Serve(bind, port) if err != nil { @@ -298,6 +298,13 @@ func (service *Service) handleServe(ctx context.Context, values map[string]any) return result, true, nil } +func (service *Service) resolveServePort() int { + if service == nil || service.dnsPort <= 0 { + return DefaultDNSPort + } + return service.dnsPort +} + func stringActionValue(values map[string]any, key string) (string, error) { if values == nil { return "", errActionMissingValue diff --git a/service.go b/service.go index 11160b4..74fff66 100644 --- a/service.go +++ b/service.go @@ -18,6 +18,7 @@ import ( // service := dns.NewService(dns.ServiceOptions{}) // fmt.Println(dns.DefaultTreeRootCheckInterval) const DefaultTreeRootCheckInterval = 15 * time.Second +const DefaultDNSPort = 53 // NameRecords stores the cached DNS records for one name. // diff --git a/service_test.go b/service_test.go index 07ebe73..5b25b8f 100644 --- a/service_test.go +++ b/service_test.go @@ -2233,6 +2233,21 @@ func TestServiceHandleActionServeDefaultsPortFromServiceConfiguration(t *testing _ = dnsServer.Close() } +func TestServiceResolveServePortDefaultsToStandardDNSPort(t *testing.T) { + service := NewService(ServiceOptions{}) + if service.resolveServePort() != DefaultDNSPort { + t.Fatalf("expected serve to default to standard DNS port %d, got %d", DefaultDNSPort, service.resolveServePort()) + } + + customPort := 1053 + customService := NewService(ServiceOptions{ + DNSPort: customPort, + }) + if customService.resolveServePort() != customPort { + t.Fatalf("expected resolveServePort to honor configured DNSPort, got %d", customService.resolveServePort()) + } +} + func TestServiceActionNamesExposeAllRFCActions(t *testing.T) { service := NewService(ServiceOptions{}) @@ -2636,8 +2651,8 @@ func TestServiceDiscoverAliasesFallsBackToMainchainAliasRPCUsingHSDURL(t *testin defer server.Close() service := NewService(ServiceOptions{ - HSDURL: server.URL, - Records: map[string]NameRecords{}, + HSDURL: server.URL, + Records: map[string]NameRecords{}, }) if err := service.DiscoverAliases(context.Background()); err != nil {