support querying either IPv4 or IPv6 dns

This commit is contained in:
Darien Raymond
2018-11-19 20:42:02 +01:00
parent 8d8eb0f35a
commit bb1efdebd1
10 changed files with 242 additions and 57 deletions
+13 -20
View File
@@ -8,29 +8,22 @@ import (
// Client is a V2Ray feature for querying DNS information.
type Client interface {
features.Feature
LookupIP(host string) ([]net.IP, error)
// LookupIP returns IP address for the given domain. IPs may contain IPv4 and/or IPv6 addresses.
LookupIP(domain string) ([]net.IP, error)
}
// IPv4Lookup is an optional feature for querying IPv4 addresses only.
type IPv4Lookup interface {
LookupIPv4(domain string) ([]net.IP, error)
}
// IPv6Lookup is an optional feature for querying IPv6 addresses only.
type IPv6Lookup interface {
LookupIPv6(domain string) ([]net.IP, error)
}
// ClientType returns the type of Client interface. Can be used for implementing common.HasType.
func ClientType() interface{} {
return (*Client)(nil)
}
// LocalClient is an implementation of Client, which queries localhost for DNS.
type LocalClient struct{}
// Type implements common.HasType.
func (LocalClient) Type() interface{} {
return ClientType()
}
// Start implements common.Runnable.
func (LocalClient) Start() error { return nil }
// Close implements common.Closable.
func (LocalClient) Close() error { return nil }
// LookupIP implements Client.
func (LocalClient) LookupIP(host string) ([]net.IP, error) {
return net.LookupIP(host)
}
+73
View File
@@ -0,0 +1,73 @@
package localdns
import (
"context"
"net"
"v2ray.com/core/features/dns"
)
// Client is an implementation of dns.Client, which queries localhost for DNS.
type Client struct {
resolver net.Resolver
}
// 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 (c *Client) LookupIP(host string) ([]net.IP, error) {
ipAddr, err := c.resolver.LookupIPAddr(context.Background(), host)
if err != nil {
return nil, err
}
ips := make([]net.IP, 0, len(ipAddr))
for _, addr := range ipAddr {
ips = append(ips, addr.IP)
}
return ips, nil
}
func (c *Client) LookupIPv4(host string) ([]net.IP, error) {
ips, err := c.LookupIP(host)
if err != nil {
return nil, err
}
var ipv4 []net.IP
for _, ip := range ips {
if len(ip) == net.IPv4len {
ipv4 = append(ipv4, ip)
}
}
return ipv4, nil
}
func (c *Client) LookupIPv6(host string) ([]net.IP, error) {
ips, err := c.LookupIP(host)
if err != nil {
return nil, err
}
var ipv6 []net.IP
for _, ip := range ips {
if len(ip) == net.IPv6len {
ipv6 = append(ipv6, ip)
}
}
return ipv6, nil
}
func New() *Client {
return &Client{
resolver: net.Resolver{
PreferGo: true,
},
}
}