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