github.com/xmplusdev/xray-core@v1.8.10/app/dns/nameserver_fakedns.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/xmplusdev/xray-core/common/net"
     7  	"github.com/xmplusdev/xray-core/core"
     8  	"github.com/xmplusdev/xray-core/features/dns"
     9  )
    10  
    11  type FakeDNSServer struct {
    12  	fakeDNSEngine dns.FakeDNSEngine
    13  }
    14  
    15  func NewFakeDNSServer() *FakeDNSServer {
    16  	return &FakeDNSServer{}
    17  }
    18  
    19  func (FakeDNSServer) Name() string {
    20  	return "FakeDNS"
    21  }
    22  
    23  func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, opt dns.IPOption, _ bool) ([]net.IP, error) {
    24  	if f.fakeDNSEngine == nil {
    25  		if err := core.RequireFeatures(ctx, func(fd dns.FakeDNSEngine) {
    26  			f.fakeDNSEngine = fd
    27  		}); err != nil {
    28  			return nil, newError("Unable to locate a fake DNS Engine").Base(err).AtError()
    29  		}
    30  	}
    31  	var ips []net.Address
    32  	if fkr0, ok := f.fakeDNSEngine.(dns.FakeDNSEngineRev0); ok {
    33  		ips = fkr0.GetFakeIPForDomain3(domain, opt.IPv4Enable, opt.IPv6Enable)
    34  	} else {
    35  		ips = f.fakeDNSEngine.GetFakeIPForDomain(domain)
    36  	}
    37  
    38  	netIP, err := toNetIP(ips)
    39  	if err != nil {
    40  		return nil, newError("Unable to convert IP to net ip").Base(err).AtError()
    41  	}
    42  
    43  	newError(f.Name(), " got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
    44  
    45  	if len(netIP) > 0 {
    46  		return netIP, nil
    47  	}
    48  	return nil, dns.ErrEmptyResponse
    49  }