github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/dns/fakedns.go (about) 1 //go:build !confonly 2 // +build !confonly 3 4 package dns 5 6 import ( 7 fakedns "github.com/v2fly/v2ray-core/v5/app/dns/fakedns" 8 "github.com/v2fly/v2ray-core/v5/common/net" 9 "github.com/v2fly/v2ray-core/v5/features/dns" 10 ) 11 12 // FakeDNSClient is an implementation of dns.Client with FakeDNS enabled. 13 type FakeDNSClient struct { 14 *DNS 15 } 16 17 // LookupIP implements dns.Client. 18 func (s *FakeDNSClient) LookupIP(domain string) ([]net.IP, error) { 19 return s.lookupIPInternal(domain, dns.IPOption{IPv4Enable: true, IPv6Enable: true, FakeEnable: true}) 20 } 21 22 // LookupIPv4 implements dns.IPv4Lookup. 23 func (s *FakeDNSClient) LookupIPv4(domain string) ([]net.IP, error) { 24 return s.lookupIPInternal(domain, dns.IPOption{IPv4Enable: true, FakeEnable: true}) 25 } 26 27 // LookupIPv6 implements dns.IPv6Lookup. 28 func (s *FakeDNSClient) LookupIPv6(domain string) ([]net.IP, error) { 29 return s.lookupIPInternal(domain, dns.IPOption{IPv6Enable: true, FakeEnable: true}) 30 } 31 32 // FakeDNSEngine is an implementation of dns.FakeDNSEngine based on a fully functional DNS. 33 type FakeDNSEngine struct { 34 dns *DNS 35 fakeHolders *fakedns.HolderMulti 36 fakeDefault *fakedns.HolderMulti 37 } 38 39 // Type implements common.HasType. 40 func (*FakeDNSEngine) Type() interface{} { 41 return dns.FakeDNSEngineType() 42 } 43 44 // Start implements common.Runnable. 45 func (f *FakeDNSEngine) Start() error { 46 return f.fakeHolders.Start() 47 } 48 49 // Close implements common.Closable. 50 func (f *FakeDNSEngine) Close() error { 51 return f.fakeHolders.Close() 52 } 53 54 // GetFakeIPForDomain implements dns.FakeDNSEngine. 55 func (f *FakeDNSEngine) GetFakeIPForDomain(domain string) []net.Address { 56 return f.GetFakeIPForDomain3(domain, true, true) 57 } 58 59 // GetDomainFromFakeDNS implements dns.FakeDNSEngine. 60 func (f *FakeDNSEngine) GetDomainFromFakeDNS(ip net.Address) string { 61 return f.fakeHolders.GetDomainFromFakeDNS(ip) 62 } 63 64 // IsIPInIPPool implements dns.FakeDNSEngineRev0. 65 func (f *FakeDNSEngine) IsIPInIPPool(ip net.Address) bool { 66 return f.fakeHolders.IsIPInIPPool(ip) 67 } 68 69 // GetFakeIPForDomain3 implements dns.FakeDNSEngineRev0. 70 func (f *FakeDNSEngine) GetFakeIPForDomain3(domain string, IPv4 bool, IPv6 bool) []net.Address { // nolint: gocritic 71 option := dns.IPOption{IPv4Enable: IPv4, IPv6Enable: IPv6, FakeEnable: true} 72 for _, client := range f.dns.sortClients(domain, option) { 73 fakeServer, ok := client.fakeDNS.(*FakeDNSServer) 74 if !ok { 75 continue 76 } 77 fakeEngine, ok := fakeServer.fakeDNSEngine.(dns.FakeDNSEngineRev0) 78 if !ok { 79 return filterIP(fakeServer.fakeDNSEngine.GetFakeIPForDomain(domain), option) 80 } 81 return fakeEngine.GetFakeIPForDomain3(domain, IPv4, IPv6) 82 } 83 if f.fakeDefault != nil { 84 return f.fakeDefault.GetFakeIPForDomain3(domain, IPv4, IPv6) 85 } 86 return nil 87 }