1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/features/dns/localdns/client.go

72 lines
1.5 KiB
Go
Raw Normal View History

package localdns
import (
2018-11-22 13:07:49 -05:00
"v2ray.com/core/common/net"
"v2ray.com/core/features/dns"
)
// Client is an implementation of dns.Client, which queries localhost for DNS.
type Client struct{}
// Type implements common.HasType.
func (*Client) Type() interface{} {
return dns.ClientType()
}
// Start implements common.Runnable.
func (*Client) Start() error { return nil }
// Close implements common.Closable.
func (*Client) Close() error { return nil }
// LookupIP implements Client.
func (*Client) LookupIP(host string) ([]net.IP, error) {
2018-12-06 14:11:45 -05:00
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
}
parsedIPs := make([]net.IP, 0, len(ips))
2018-12-06 15:34:05 -05:00
for _, ip := range ips {
2018-12-06 14:11:45 -05:00
parsed := net.IPAddress(ip)
if parsed != nil {
parsedIPs = append(parsedIPs, parsed.IP())
}
}
return parsedIPs, nil
}
2018-11-22 16:49:45 -05:00
// LookupIPv4 implements IPv4Lookup.
func (c *Client) LookupIPv4(host string) ([]net.IP, error) {
ips, err := c.LookupIP(host)
if err != nil {
return nil, err
}
2018-12-06 14:11:45 -05:00
ipv4 := make([]net.IP, 0, len(ips))
for _, ip := range ips {
2018-12-06 14:11:45 -05:00
if len(ip) == net.IPv4len {
ipv4 = append(ipv4, ip)
}
}
return ipv4, nil
}
2018-11-22 16:49:45 -05:00
// LookupIPv6 implements IPv6Lookup.
func (c *Client) LookupIPv6(host string) ([]net.IP, error) {
ips, err := c.LookupIP(host)
if err != nil {
return nil, err
}
2018-12-06 14:11:45 -05:00
ipv6 := make([]net.IP, 0, len(ips))
for _, ip := range ips {
2018-12-06 14:11:45 -05:00
if len(ip) == net.IPv6len {
ipv6 = append(ipv6, ip)
}
}
return ipv6, nil
}
2018-11-22 16:49:45 -05:00
// New create a new dns.Client that queries localhost for DNS.
func New() *Client {
return &Client{}
}