github.com/eagleql/xray-core@v1.4.4/features/dns/localdns/client.go (about) 1 package localdns 2 3 import ( 4 "github.com/eagleql/xray-core/common/net" 5 "github.com/eagleql/xray-core/features/dns" 6 ) 7 8 // Client is an implementation of dns.Client, which queries localhost for DNS. 9 type Client struct{} 10 11 // Type implements common.HasType. 12 func (*Client) Type() interface{} { 13 return dns.ClientType() 14 } 15 16 // Start implements common.Runnable. 17 func (*Client) Start() error { return nil } 18 19 // Close implements common.Closable. 20 func (*Client) Close() error { return nil } 21 22 // LookupIP implements Client. 23 func (*Client) LookupIP(host string, option dns.IPOption) ([]net.IP, error) { 24 ips, err := net.LookupIP(host) 25 if err != nil { 26 return nil, err 27 } 28 parsedIPs := make([]net.IP, 0, len(ips)) 29 ipv4 := make([]net.IP, 0, len(ips)) 30 ipv6 := make([]net.IP, 0, len(ips)) 31 for _, ip := range ips { 32 parsed := net.IPAddress(ip) 33 if parsed != nil { 34 parsedIPs = append(parsedIPs, parsed.IP()) 35 } 36 if len(ip) == net.IPv4len { 37 ipv4 = append(ipv4, ip) 38 } 39 if len(ip) == net.IPv6len { 40 ipv6 = append(ipv6, ip) 41 } 42 } 43 switch { 44 case option.IPv4Enable && option.IPv6Enable: 45 if len(parsedIPs) > 0 { 46 return parsedIPs, nil 47 } 48 case option.IPv4Enable: 49 if len(ipv4) > 0 { 50 return ipv4, nil 51 } 52 case option.IPv6Enable: 53 if len(ipv6) > 0 { 54 return ipv6, nil 55 } 56 } 57 return nil, dns.ErrEmptyResponse 58 } 59 60 // New create a new dns.Client that queries localhost for DNS. 61 func New() *Client { 62 return &Client{} 63 }