From d0fe2199c46a3f23a9ad6420088449a792162e9d Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 03:09:53 +0000 Subject: [PATCH] ax(dns): add explicit health port aliases --- http_server.go | 2 +- serve.go | 56 +++++++++++++++++++++++++++++------------- service.go | 56 +++++++++++++++++++++++++++--------------- service_test.go | 65 +++++++++++++++++++++++++++++++++++++------------ 4 files changed, 126 insertions(+), 53 deletions(-) diff --git a/http_server.go b/http_server.go index 338727b..7cd99c9 100644 --- a/http_server.go +++ b/http_server.go @@ -72,7 +72,7 @@ func (service *Service) ServeHTTPHealth(bind string, port int) (*HealthServer, e bind = "127.0.0.1" } if port <= 0 { - port = service.resolveHTTPPort() + port = service.resolveHealthPort() } address := net.JoinHostPort(bind, strconv.Itoa(port)) diff --git a/serve.go b/serve.go index e61c805..3a9a2f8 100644 --- a/serve.go +++ b/serve.go @@ -184,6 +184,14 @@ func (service *Service) resolveServePort() int { // port := service.ResolveHTTPPort() // healthServer, err := service.ServeHTTPHealth("127.0.0.1", port) func (service *Service) ResolveHTTPPort() int { + return service.ResolveHealthPort() +} + +// ResolveHealthPort returns the health listener port used by `ServeHTTPHealth`. +// +// port := service.ResolveHealthPort() +// healthServer, err := service.ServeHTTPHealth("127.0.0.1", port) +func (service *Service) ResolveHealthPort() int { if service == nil { return DefaultHTTPPort } @@ -198,10 +206,23 @@ func (service *Service) ResolveHTTPPort() int { // port := service.HTTPListenPort() // server, err := service.ServeHTTPHealth("127.0.0.1", port) func (service *Service) HTTPListenPort() int { - if service == nil { - return DefaultHTTPPort - } - return service.ResolveHTTPPort() + return service.ResolveHealthPort() +} + +// HealthListenPort is the explicit alias for ResolveHealthPort. +// +// port := service.HealthListenPort() +// server, err := service.ServeHTTPHealth("127.0.0.1", port) +func (service *Service) HealthListenPort() int { + return service.ResolveHealthPort() +} + +// HealthPort returns the health listener port used by `ServeHTTPHealth`. +// +// port := service.HealthPort() +// healthServer, err := service.ServeHTTPHealth("127.0.0.1", port) +func (service *Service) HealthPort() int { + return service.ResolveHealthPort() } // HTTPPort is an explicit alias for ResolveHTTPPort. @@ -209,26 +230,27 @@ func (service *Service) HTTPListenPort() int { // port := service.HTTPPort() // healthServer, err := service.ServeHTTPHealth("127.0.0.1", port) func (service *Service) HTTPPort() int { - if service == nil { - return DefaultHTTPPort - } - return service.ResolveHTTPPort() + return service.ResolveHealthPort() } // resolveHTTPListenPort keeps internal callers aligned with explicit naming. func (service *Service) resolveHTTPListenPort() int { - if service == nil { - return DefaultHTTPPort - } - return service.HTTPListenPort() + return service.ResolveHealthPort() +} + +// resolveHealthListenPort keeps internal callers aligned with explicit naming. +func (service *Service) resolveHealthListenPort() int { + return service.ResolveHealthPort() } // resolveHTTPPort is a legacy compatibility helper. func (service *Service) resolveHTTPPort() int { - if service == nil { - return DefaultHTTPPort - } - return service.ResolveHTTPPort() + return service.ResolveHealthPort() +} + +// resolveHealthPort is a legacy compatibility helper. +func (service *Service) resolveHealthPort() int { + return service.ResolveHealthPort() } // Serve starts DNS over UDP and TCP. @@ -317,7 +339,7 @@ func (service *Service) ServeAll(bind string, dnsPort int, httpPort int) (*Servi dnsPort = service.resolveDNSListenPort() } if httpPort <= 0 { - httpPort = service.resolveHTTPListenPort() + httpPort = service.resolveHealthListenPort() } dnsServer, err := service.Serve(bind, dnsPort) diff --git a/service.go b/service.go index dbf8aee..1b5f9f2 100644 --- a/service.go +++ b/service.go @@ -131,13 +131,14 @@ type HealthResult struct { // snapshot := service.Describe() // fmt.Println(snapshot.Status, snapshot.ZoneApex, snapshot.TreeRoot) type ServiceDescription struct { - Status string `json:"status"` - Records int `json:"records"` - ZoneApex string `json:"zone_apex"` - TreeRoot string `json:"tree_root"` - DNSPort int `json:"dns_port"` - HTTPPort int `json:"http_port"` - RecordTTL string `json:"record_ttl"` + Status string `json:"status"` + Records int `json:"records"` + ZoneApex string `json:"zone_apex"` + TreeRoot string `json:"tree_root"` + DNSPort int `json:"dns_port"` + HealthPort int `json:"health_port"` + HTTPPort int `json:"http_port"` + RecordTTL string `json:"record_ttl"` } type Service struct { @@ -179,8 +180,14 @@ type ServiceOptions struct { RecordDiscoverer func() (map[string]NameRecords, error) FallbackRecordDiscoverer func() (map[string]NameRecords, error) // RecordTTL keeps forward records and the reverse index alive for the same duration. - RecordTTL time.Duration - DNSPort int + RecordTTL time.Duration + // DNSListenPort is the explicit port for the DNS listener. + DNSListenPort int + // HealthPort is the explicit port for the `/health` listener. + HealthPort int + // DNSPort is kept for compatibility with older call sites. + DNSPort int + // HTTPPort is kept for compatibility with older call sites. HTTPPort int HSDURL string HSDUsername string @@ -257,6 +264,15 @@ func NewService(options ServiceOptions) *Service { checkInterval = DefaultTreeRootCheckInterval } + dnsPort := options.DNSListenPort + if dnsPort <= 0 { + dnsPort = options.DNSPort + } + healthPort := options.HealthPort + if healthPort <= 0 { + healthPort = options.HTTPPort + } + chainAliasActionCaller := options.ChainAliasActionCaller if chainAliasActionCaller == nil { if actionCaller, ok := options.ActionRegistrar.(ActionCaller); ok { @@ -312,8 +328,8 @@ func NewService(options ServiceOptions) *Service { reverseIndex: buildReverseIndex(cached, options.RecordTTL), treeRoot: treeRoot, zoneApex: computeZoneApex(cached), - dnsPort: options.DNSPort, - httpPort: options.HTTPPort, + dnsPort: dnsPort, + httpPort: healthPort, recordTTL: options.RecordTTL, hsdClient: hsdClient, mainchainAliasClient: mainchainClient, @@ -1217,13 +1233,14 @@ func (service *Service) Snapshot() ServiceDescription { } return ServiceDescription{ - Status: "ready", - Records: len(service.records), - ZoneApex: service.zoneApex, - TreeRoot: treeRoot, - DNSPort: dnsPort, - HTTPPort: httpPort, - RecordTTL: service.recordTTL.String(), + Status: "ready", + Records: len(service.records), + ZoneApex: service.zoneApex, + TreeRoot: treeRoot, + DNSPort: dnsPort, + HealthPort: httpPort, + HTTPPort: httpPort, + RecordTTL: service.recordTTL.String(), } } @@ -1571,12 +1588,13 @@ func NormalizeName(name string) string { func (service *Service) String() string { snapshot := service.Describe() return fmt.Sprintf( - "dns.Service{status=%q records=%d zone_apex=%q tree_root=%q dns_port=%d http_port=%d record_ttl=%q}", + "dns.Service{status=%q records=%d zone_apex=%q tree_root=%q dns_port=%d health_port=%d http_port=%d record_ttl=%q}", snapshot.Status, snapshot.Records, snapshot.ZoneApex, snapshot.TreeRoot, snapshot.DNSPort, + snapshot.HealthPort, snapshot.HTTPPort, snapshot.RecordTTL, ) diff --git a/service_test.go b/service_test.go index 8ab17bb..8c7613d 100644 --- a/service_test.go +++ b/service_test.go @@ -679,8 +679,9 @@ func TestServiceDescribeReturnsSemanticSnapshot(t *testing.T) { A: []string{"10.10.10.11"}, }, }, - DNSPort: 1053, - HTTPPort: 5555, + DNSPort: 1053, + HealthPort: 5555, + HTTPPort: 4444, }) snapshot := service.Describe() @@ -696,7 +697,7 @@ func TestServiceDescribeReturnsSemanticSnapshot(t *testing.T) { if snapshot.TreeRoot == "" { t.Fatal("expected tree root in service snapshot") } - if snapshot.DNSPort != 1053 || snapshot.HTTPPort != 5555 { + if snapshot.DNSPort != 1053 || snapshot.HealthPort != 5555 || snapshot.HTTPPort != 5555 { t.Fatalf("expected configured ports in snapshot, got %#v", snapshot) } if snapshot.RecordTTL != "0s" { @@ -707,7 +708,7 @@ func TestServiceDescribeReturnsSemanticSnapshot(t *testing.T) { if !strings.Contains(description, "zone_apex=\"charon.lthn\"") { t.Fatalf("expected string description to include zone apex, got %q", description) } - if !strings.Contains(description, "dns_port=1053") || !strings.Contains(description, "http_port=5555") { + if !strings.Contains(description, "dns_port=1053") || !strings.Contains(description, "health_port=5555") || !strings.Contains(description, "http_port=5555") { t.Fatalf("expected string description to include configured ports, got %q", description) } } @@ -3175,22 +3176,23 @@ func TestServiceResolveServePortDefaultsToStandardDNSPort(t *testing.T) { customPort := 1053 customService := NewService(ServiceOptions{ - DNSPort: customPort, + DNSListenPort: customPort, + DNSPort: 5454, }) if customService.ResolveDNSPort() != customPort { - t.Fatalf("expected ResolveDNSPort to honor configured DNSPort, got %d", customService.ResolveDNSPort()) + t.Fatalf("expected ResolveDNSPort to honor configured DNSListenPort, got %d", customService.ResolveDNSPort()) } if customService.DNSPort() != customPort { - t.Fatalf("expected DNSPort alias to honor configured DNSPort, got %d", customService.DNSPort()) + t.Fatalf("expected DNSPort alias to honor configured DNSListenPort, got %d", customService.DNSPort()) } if customService.DNSListenPort() != customPort { - t.Fatalf("expected DNSListenPort to honor configured DNSPort, got %d", customService.DNSListenPort()) + t.Fatalf("expected DNSListenPort to honor configured DNSListenPort, got %d", customService.DNSListenPort()) } if customService.resolveServePort() != customPort { - t.Fatalf("expected resolveServePort helper to honor configured DNSPort, got %d", customService.resolveServePort()) + t.Fatalf("expected resolveServePort helper to honor configured DNSListenPort, got %d", customService.resolveServePort()) } if customService.resolveDNSListenPort() != customPort { - t.Fatalf("expected resolveDNSListenPort helper to honor configured DNSPort, got %d", customService.resolveDNSListenPort()) + t.Fatalf("expected resolveDNSListenPort helper to honor configured DNSListenPort, got %d", customService.resolveDNSListenPort()) } } @@ -3199,37 +3201,68 @@ func TestServiceResolveHTTPPortDefaultsToStandardHTTPPort(t *testing.T) { if service.ResolveHTTPPort() != DefaultHTTPPort { t.Fatalf("expected ResolveHTTPPort to default to %d, got %d", DefaultHTTPPort, service.ResolveHTTPPort()) } + if service.ResolveHealthPort() != DefaultHTTPPort { + t.Fatalf("expected ResolveHealthPort to default to %d, got %d", DefaultHTTPPort, service.ResolveHealthPort()) + } if service.HTTPPort() != DefaultHTTPPort { t.Fatalf("expected HTTPPort alias to return default %d, got %d", DefaultHTTPPort, service.HTTPPort()) } + if service.HealthPort() != DefaultHTTPPort { + t.Fatalf("expected HealthPort alias to return default %d, got %d", DefaultHTTPPort, service.HealthPort()) + } if service.HTTPListenPort() != DefaultHTTPPort { t.Fatalf("expected HTTPListenPort to return default %d, got %d", DefaultHTTPPort, service.HTTPListenPort()) } + if service.HealthListenPort() != DefaultHTTPPort { + t.Fatalf("expected HealthListenPort to return default %d, got %d", DefaultHTTPPort, service.HealthListenPort()) + } if service.resolveHTTPPort() != DefaultHTTPPort { t.Fatalf("expected resolveHTTPPort helper to return default %d, got %d", DefaultHTTPPort, service.resolveHTTPPort()) } + if service.resolveHealthPort() != DefaultHTTPPort { + t.Fatalf("expected resolveHealthPort helper to return default %d, got %d", DefaultHTTPPort, service.resolveHealthPort()) + } if service.resolveHTTPListenPort() != DefaultHTTPPort { t.Fatalf("expected resolveHTTPListenPort helper to return default %d, got %d", DefaultHTTPPort, service.resolveHTTPListenPort()) } + if service.resolveHealthListenPort() != DefaultHTTPPort { + t.Fatalf("expected resolveHealthListenPort helper to return default %d, got %d", DefaultHTTPPort, service.resolveHealthListenPort()) + } customPort := 5555 customService := NewService(ServiceOptions{ - HTTPPort: customPort, + HealthPort: customPort, + HTTPPort: 4444, }) if customService.ResolveHTTPPort() != customPort { - t.Fatalf("expected ResolveHTTPPort to honor configured HTTPPort, got %d", customService.ResolveHTTPPort()) + t.Fatalf("expected ResolveHTTPPort to honor configured HealthPort, got %d", customService.ResolveHTTPPort()) + } + if customService.ResolveHealthPort() != customPort { + t.Fatalf("expected ResolveHealthPort to honor configured HealthPort, got %d", customService.ResolveHealthPort()) } if customService.HTTPPort() != customPort { - t.Fatalf("expected HTTPPort alias to honor configured HTTPPort, got %d", customService.HTTPPort()) + t.Fatalf("expected HTTPPort alias to honor configured HealthPort, got %d", customService.HTTPPort()) + } + if customService.HealthPort() != customPort { + t.Fatalf("expected HealthPort alias to honor configured HealthPort, got %d", customService.HealthPort()) } if customService.HTTPListenPort() != customPort { - t.Fatalf("expected HTTPListenPort to honor configured HTTPPort, got %d", customService.HTTPListenPort()) + t.Fatalf("expected HTTPListenPort to honor configured HealthPort, got %d", customService.HTTPListenPort()) + } + if customService.HealthListenPort() != customPort { + t.Fatalf("expected HealthListenPort to honor configured HealthPort, got %d", customService.HealthListenPort()) } if customService.resolveHTTPPort() != customPort { - t.Fatalf("expected resolveHTTPPort helper to honor configured HTTPPort, got %d", customService.resolveHTTPPort()) + t.Fatalf("expected resolveHTTPPort helper to honor configured HealthPort, got %d", customService.resolveHTTPPort()) + } + if customService.resolveHealthPort() != customPort { + t.Fatalf("expected resolveHealthPort helper to honor configured HealthPort, got %d", customService.resolveHealthPort()) } if customService.resolveHTTPListenPort() != customPort { - t.Fatalf("expected resolveHTTPListenPort helper to honor configured HTTPPort, got %d", customService.resolveHTTPListenPort()) + t.Fatalf("expected resolveHTTPListenPort helper to honor configured HealthPort, got %d", customService.resolveHTTPListenPort()) + } + if customService.resolveHealthListenPort() != customPort { + t.Fatalf("expected resolveHealthListenPort helper to honor configured HealthPort, got %d", customService.resolveHealthListenPort()) } }