From 743435d6e6feb1d5144ce004c35a11afa0d768bf Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Thu, 12 Dec 2024 06:56:05 +0000 Subject: [PATCH] Core: Fix startup error when `dns` exists but `fakedns` doesn't Fixes https://github.com/XTLS/Xray-core/issues/4155 --- app/dns/nameserver.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/dns/nameserver.go b/app/dns/nameserver.go index ecba9aff..de6e1686 100644 --- a/app/dns/nameserver.go +++ b/app/dns/nameserver.go @@ -35,7 +35,7 @@ type Client struct { var errExpectedIPNonMatch = errors.New("expectIPs not match") // NewServer creates a name server object according to the network destination url. -func NewServer(dest net.Destination, dispatcher routing.Dispatcher, queryStrategy QueryStrategy, fd dns.FakeDNSEngine) (Server, error) { +func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dispatcher, queryStrategy QueryStrategy) (Server, error) { if address := dest.Address; address.Family().IsDomain() { u, err := url.Parse(address.Domain()) if err != nil { @@ -55,6 +55,10 @@ func NewServer(dest net.Destination, dispatcher routing.Dispatcher, queryStrateg case strings.EqualFold(u.Scheme, "tcp+local"): // DNS-over-TCP Local mode return NewTCPLocalNameServer(u, queryStrategy) case strings.EqualFold(u.String(), "fakedns"): + var fd dns.FakeDNSEngine + core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) { // FakeDNSEngine is optional + fd = fdns + }) return NewFakeDNSServer(fd), nil } } @@ -78,13 +82,9 @@ func NewClient( ) (*Client, error) { client := &Client{} - var fd dns.FakeDNSEngine err := core.RequireFeatures(ctx, func(dispatcher routing.Dispatcher) error { - core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) { // FakeDNSEngine is optional - fd = fdns - }) // Create a new server for each client for now - server, err := NewServer(ns.Address.AsDestination(), dispatcher, ns.GetQueryStrategy(), fd) + server, err := NewServer(ctx, ns.Address.AsDestination(), dispatcher, ns.GetQueryStrategy()) if err != nil { return errors.New("failed to create nameserver").Base(err).AtWarning() }