github.com/imannamdari/v2ray-core/v5@v5.0.5/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/imannamdari/v2ray-core/v5" 10 "github.com/imannamdari/v2ray-core/v5/common/net" 11 "github.com/imannamdari/v2ray-core/v5/features/dns" 12 ) 13 14 type FakeDNSServer struct { 15 fakeDNSEngine dns.FakeDNSEngine 16 } 17 18 func NewFakeDNSServer(fakeDNSEngine dns.FakeDNSEngine) *FakeDNSServer { 19 return &FakeDNSServer{fakeDNSEngine: fakeDNSEngine} 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 !opt.FakeEnable { 28 return nil, nil // Returning empty ip record with no error will continue DNS lookup, effectively indicating that this server is disabled. 29 } 30 if f.fakeDNSEngine == nil { 31 if err := core.RequireFeatures(ctx, func(fd dns.FakeDNSEngine) { 32 f.fakeDNSEngine = fd 33 }); err != nil { 34 return nil, newError("Unable to locate a fake DNS Engine").Base(err).AtError() 35 } 36 } 37 var ips []net.Address 38 if fkr0, ok := f.fakeDNSEngine.(dns.FakeDNSEngineRev0); ok { 39 ips = fkr0.GetFakeIPForDomain3(domain, opt.IPv4Enable, opt.IPv6Enable) 40 } else { 41 ips = filterIP(f.fakeDNSEngine.GetFakeIPForDomain(domain), opt) 42 } 43 44 netIP, err := toNetIP(ips) 45 if err != nil { 46 return nil, newError("Unable to convert IP to net ip").Base(err).AtError() 47 } 48 49 newError(f.Name(), " got answer: ", domain, " -> ", ips).AtInfo().WriteToLog() 50 51 if len(netIP) > 0 { 52 return netIP, nil 53 } 54 return nil, dns.ErrEmptyResponse 55 } 56 57 func isFakeDNS(server Server) bool { 58 _, ok := server.(*FakeDNSServer) 59 return ok 60 }