From 86fc04acddafecbf49e8348134d09af5a0ecbf1e Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 23:08:14 +0000 Subject: [PATCH] feat(dns): default health HTTP port to 5554 Co-Authored-By: Virgil --- http_server.go | 3 +++ serve.go | 11 +++++++++-- service.go | 1 + service_test.go | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/http_server.go b/http_server.go index 0b3d011..c1c832b 100644 --- a/http_server.go +++ b/http_server.go @@ -64,6 +64,9 @@ func (service *Service) ServeHTTPHealth(bind string, port int) (*HealthServer, e if bind == "" { bind = "127.0.0.1" } + if port <= 0 { + port = service.resolveHTTPPort() + } address := net.JoinHostPort(bind, strconv.Itoa(port)) listener, err := net.Listen("tcp", address) diff --git a/serve.go b/serve.go index 7b83909..f79d196 100644 --- a/serve.go +++ b/serve.go @@ -113,6 +113,13 @@ func (server *DNSServer) Close() error { return err } +func (service *Service) resolveHTTPPort() int { + if service == nil || service.httpPort <= 0 { + return DefaultHTTPPort + } + return service.httpPort +} + // Serve starts DNS over UDP and TCP. // // srv, err := service.Serve("0.0.0.0", 53) @@ -168,8 +175,8 @@ func (service *Service) ServeAll(bind string, dnsPort int, httpPort int) (*Servi if dnsPort == 0 { dnsPort = service.dnsPort } - if httpPort == 0 { - httpPort = service.httpPort + if httpPort <= 0 { + httpPort = service.resolveHTTPPort() } dnsServer, err := service.Serve(bind, dnsPort) diff --git a/service.go b/service.go index 9af31b2..d7b4bab 100644 --- a/service.go +++ b/service.go @@ -19,6 +19,7 @@ import ( // fmt.Println(dns.DefaultTreeRootCheckInterval) const DefaultTreeRootCheckInterval = 15 * time.Second const DefaultDNSPort = 53 +const DefaultHTTPPort = 5554 // NameRecords stores the cached DNS records for one name. // diff --git a/service_test.go b/service_test.go index f45283c..cb943ef 100644 --- a/service_test.go +++ b/service_test.go @@ -2250,6 +2250,21 @@ func TestServiceResolveServePortDefaultsToStandardDNSPort(t *testing.T) { } } +func TestServiceResolveHTTPPortDefaultsToStandardHTTPPort(t *testing.T) { + service := NewService(ServiceOptions{}) + if service.resolveHTTPPort() != DefaultHTTPPort { + t.Fatalf("expected resolve HTTP port to default to %d, got %d", DefaultHTTPPort, service.resolveHTTPPort()) + } + + customPort := 5555 + customService := NewService(ServiceOptions{ + HTTPPort: customPort, + }) + if customService.resolveHTTPPort() != customPort { + t.Fatalf("expected resolveHTTPPort to honor configured HTTPPort, got %d", customService.resolveHTTPPort()) + } +} + func TestServiceActionNamesExposeAllRFCActions(t *testing.T) { service := NewService(ServiceOptions{}) -- 2.45.3