diff --git a/action.go b/action.go index 6eebd74..6bff52f 100644 --- a/action.go +++ b/action.go @@ -274,10 +274,13 @@ func (service *Service) handleReverseLookup(ctx context.Context, values map[stri func (service *Service) handleServe(ctx context.Context, values map[string]any) (any, bool, error) { _ = ctx bind, _ := stringActionValueOptional(values, "bind") - port, err := intActionValue(values, "port") + port, portProvided, err := intActionValueOptional(values, "port") if err != nil { return nil, false, err } + if !portProvided { + port = service.dnsPort + } result, err := service.Serve(bind, port) if err != nil { return nil, false, err @@ -337,3 +340,18 @@ func intActionValue(values map[string]any, key string) (int, error) { return 0, fmt.Errorf("%w: %s", errActionMissingValue, key) } } + +func intActionValueOptional(values map[string]any, key string) (int, bool, error) { + if values == nil { + return 0, false, nil + } + _, exists := values[key] + if !exists { + return 0, false, nil + } + value, err := intActionValue(values, key) + if err != nil { + return 0, false, err + } + return value, true, nil +} diff --git a/go.mod b/go.mod index 3a60f50..92f2952 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,6 @@ go 1.22 require github.com/miekg/dns v1.1.62 -require github.com/patrickmn/go-cache v2.1.0+incompatible - require ( golang.org/x/mod v0.18.0 // indirect golang.org/x/net v0.27.0 // indirect diff --git a/go.sum b/go.sum index 26556ce..95e8194 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/miekg/dns v1.1.62 h1:cN8OuEF1/x5Rq6Np+h1epln8OiyPWV+lROx9LxcGgIQ= github.com/miekg/dns v1.1.62/go.mod h1:mvDlcItzm+br7MToIKqkglaGhlFMHJ9DTNNWONWXbNQ= -github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= -github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= diff --git a/service_test.go b/service_test.go index 3f98ebb..c61abb1 100644 --- a/service_test.go +++ b/service_test.go @@ -2105,6 +2105,45 @@ func TestServiceHandleActionResolveAndTXTAndAll(t *testing.T) { } } +func TestServiceHandleActionServeDefaultsPortFromServiceConfiguration(t *testing.T) { + desiredPort := pickFreeTCPPort(t) + service := NewService(ServiceOptions{ + DNSPort: desiredPort, + Records: map[string]NameRecords{ + "gateway.charon.lthn": { + A: []string{"10.10.10.10"}, + }, + }, + }) + + payload, ok, err := service.HandleAction(ActionServe, map[string]any{ + "bind": "127.0.0.1", + }) + if err != nil { + t.Fatalf("expected serve action to default port when omitted: %v", err) + } + if !ok { + t.Fatal("expected serve action to succeed with omitted port") + } + dnsServer, ok := payload.(*DNSServer) + if !ok { + t.Fatalf("expected DNSServer payload, got %T", payload) + } + if dnsServer == nil { + t.Fatal("expected dns server from serve action") + } + + _, port, err := net.SplitHostPort(dnsServer.DNSAddress()) + if err != nil { + t.Fatalf("expected service address to include port: %v", err) + } + if port != strconv.Itoa(desiredPort) { + t.Fatalf("expected configured DNS port %d, got %q", desiredPort, port) + } + + _ = dnsServer.Close() +} + func TestServiceActionNamesExposeAllRFCActions(t *testing.T) { service := NewService(ServiceOptions{})