Core: Fix startup error when dns exists but fakedns doesn't

Fixes https://github.com/XTLS/Xray-core/issues/4155
This commit is contained in:
RPRX 2024-12-12 06:56:05 +00:00 committed by GitHub
parent 8cd9a74376
commit 743435d6e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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()
}