1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00

consume context in local nameserver.

This commit is contained in:
Darien Raymond 2018-06-26 15:16:45 +02:00
parent 9cfb2bfd51
commit 2fb77d6911
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 50 additions and 16 deletions

View File

@ -2,31 +2,34 @@ package dns
import (
"context"
"time"
"v2ray.com/core/common/net"
)
var (
multiQuestionDNS = map[net.Address]bool{
net.IPAddress([]byte{8, 8, 8, 8}): true,
net.IPAddress([]byte{8, 8, 4, 4}): true,
net.IPAddress([]byte{9, 9, 9, 9}): true,
}
)
type ARecord struct {
IPs []net.IP
Expire time.Time
}
type NameServer interface {
QueryIP(ctx context.Context, domain string) ([]net.IP, error)
}
type LocalNameServer struct {
resolver net.Resolver
}
func (*LocalNameServer) QueryIP(ctx context.Context, domain string) ([]net.IP, error) {
return net.LookupIP(domain)
func (s *LocalNameServer) QueryIP(ctx context.Context, domain string) ([]net.IP, error) {
ipAddr, err := s.resolver.LookupIPAddr(ctx, domain)
if err != nil {
return nil, err
}
var ips []net.IP
for _, addr := range ipAddr {
ips = append(ips, addr.IP)
}
return ips, nil
}
func NewLocalNameServer() *LocalNameServer {
return &LocalNameServer{
resolver: net.Resolver{
PreferGo: true,
},
}
}

View File

@ -0,0 +1,21 @@
package dns_test
import (
"context"
"testing"
"time"
. "v2ray.com/core/app/dns"
. "v2ray.com/ext/assert"
)
func TestLocalNameServer(t *testing.T) {
assert := With(t)
s := NewLocalNameServer()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
ips, err := s.QueryIP(ctx, "google.com")
cancel()
assert(err, IsNil)
assert(len(ips), GreaterThan, 0)
}

View File

@ -16,6 +16,14 @@ import (
"v2ray.com/core/transport/internet/udp"
)
var (
multiQuestionDNS = map[net.Address]bool{
net.IPAddress([]byte{8, 8, 8, 8}): true,
net.IPAddress([]byte{8, 8, 4, 4}): true,
net.IPAddress([]byte{9, 9, 9, 9}): true,
}
)
type IPRecord struct {
IP net.IP
Expire time.Time

View File

@ -51,3 +51,5 @@ type TCPListener = net.TCPListener
type UnixListener = net.UnixListener
var ResolveUnixAddr = net.ResolveUnixAddr
type Resolver = net.Resolver