v2ray-core/app/dns/server/nameserver.go

215 lines
4.4 KiB
Go
Raw Normal View History

2016-12-16 15:20:12 +00:00
package server
2016-05-16 06:09:28 +00:00
import (
"context"
2016-05-16 06:09:28 +00:00
"net"
"sync"
"time"
"github.com/miekg/dns"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app/dispatcher"
2017-02-10 15:42:24 +00:00
"v2ray.com/core/app/log"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet/udp"
2016-05-16 06:09:28 +00:00
)
const (
2016-05-17 00:30:00 +00:00
DefaultTTL = uint32(3600)
CleanupInterval = time.Second * 120
CleanupThreshold = 512
2016-05-16 06:09:28 +00:00
)
2016-06-01 20:09:24 +00:00
var (
pseudoDestination = v2net.UDPDestination(v2net.LocalHostIP, v2net.Port(53))
)
2016-05-16 06:09:28 +00:00
type ARecord struct {
IPs []net.IP
Expire time.Time
}
type NameServer interface {
QueryA(domain string) <-chan *ARecord
}
type PendingRequest struct {
expire time.Time
response chan<- *ARecord
}
type UDPNameServer struct {
sync.Mutex
2016-05-17 00:30:00 +00:00
address v2net.Destination
requests map[uint16]*PendingRequest
2017-01-27 13:45:16 +00:00
udpServer *udp.Dispatcher
2016-05-17 00:30:00 +00:00
nextCleanup time.Time
2016-05-16 06:09:28 +00:00
}
2017-01-13 12:53:44 +00:00
func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.Interface) *UDPNameServer {
2016-05-16 06:09:28 +00:00
s := &UDPNameServer{
2016-11-13 13:33:00 +00:00
address: address,
requests: make(map[uint16]*PendingRequest),
2017-01-27 13:45:16 +00:00
udpServer: udp.NewDispatcher(dispatcher),
2016-05-16 06:09:28 +00:00
}
return s
}
2016-08-24 09:17:42 +00:00
// Private: Visible for testing.
2016-11-27 20:39:09 +00:00
func (v *UDPNameServer) Cleanup() {
2016-05-17 00:30:00 +00:00
expiredRequests := make([]uint16, 0, 16)
now := time.Now()
2016-11-27 20:39:09 +00:00
v.Lock()
for id, r := range v.requests {
2016-05-17 00:30:00 +00:00
if r.expire.Before(now) {
expiredRequests = append(expiredRequests, id)
close(r.response)
2016-05-16 06:09:28 +00:00
}
}
2016-05-17 00:30:00 +00:00
for _, id := range expiredRequests {
2016-11-27 20:39:09 +00:00
delete(v.requests, id)
2016-05-17 00:30:00 +00:00
}
2016-11-27 20:39:09 +00:00
v.Unlock()
2016-05-17 00:30:00 +00:00
expiredRequests = nil
2016-05-16 06:09:28 +00:00
}
2016-08-24 09:17:42 +00:00
// Private: Visible for testing.
2016-11-27 20:39:09 +00:00
func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
2016-05-16 06:09:28 +00:00
var id uint16
2016-11-27 20:39:09 +00:00
v.Lock()
if len(v.requests) > CleanupThreshold && v.nextCleanup.Before(time.Now()) {
v.nextCleanup = time.Now().Add(CleanupInterval)
go v.Cleanup()
2016-05-17 00:30:00 +00:00
}
2016-05-16 06:09:28 +00:00
for {
2017-04-27 09:54:15 +00:00
id = dice.RollUint16()
2016-11-27 20:39:09 +00:00
if _, found := v.requests[id]; found {
2016-05-16 06:09:28 +00:00
continue
}
2017-04-08 23:43:25 +00:00
log.Trace(newError("add pending request id ", id).AtDebug())
2016-11-27 20:39:09 +00:00
v.requests[id] = &PendingRequest{
2016-06-01 20:09:24 +00:00
expire: time.Now().Add(time.Second * 8),
2016-05-16 06:09:28 +00:00
response: response,
}
break
}
2016-11-27 20:39:09 +00:00
v.Unlock()
2016-05-16 06:09:28 +00:00
return id
}
2016-08-24 09:17:42 +00:00
// Private: Visible for testing.
func (v *UDPNameServer) HandleResponse(payload *buf.Buffer) {
2016-05-16 06:09:28 +00:00
msg := new(dns.Msg)
2016-12-05 14:19:14 +00:00
err := msg.Unpack(payload.Bytes())
2016-05-16 06:09:28 +00:00
if err != nil {
2017-04-08 23:43:25 +00:00
log.Trace(newError("failed to parse DNS response").Base(err).AtWarning())
2016-05-16 06:09:28 +00:00
return
}
record := &ARecord{
IPs: make([]net.IP, 0, 16),
}
id := msg.Id
ttl := DefaultTTL
2017-04-08 23:43:25 +00:00
log.Trace(newError("handling response for id ", id, " content: ", msg.String()).AtDebug())
2016-05-16 06:09:28 +00:00
2016-11-27 20:39:09 +00:00
v.Lock()
request, found := v.requests[id]
2016-05-16 06:09:28 +00:00
if !found {
2016-11-27 20:39:09 +00:00
v.Unlock()
2016-05-16 06:09:28 +00:00
return
}
2016-11-27 20:39:09 +00:00
delete(v.requests, id)
v.Unlock()
2016-05-16 06:09:28 +00:00
for _, rr := range msg.Answer {
2016-05-16 18:53:18 +00:00
switch rr := rr.(type) {
case *dns.A:
record.IPs = append(record.IPs, rr.A)
if rr.Hdr.Ttl < ttl {
ttl = rr.Hdr.Ttl
}
case *dns.AAAA:
record.IPs = append(record.IPs, rr.AAAA)
if rr.Hdr.Ttl < ttl {
ttl = rr.Hdr.Ttl
2016-05-16 06:09:28 +00:00
}
}
}
record.Expire = time.Now().Add(time.Second * time.Duration(ttl))
request.response <- record
close(request.response)
}
2016-12-09 10:35:27 +00:00
func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *buf.Buffer {
2016-12-11 20:43:16 +00:00
2016-05-16 06:09:28 +00:00
msg := new(dns.Msg)
2016-06-01 20:09:24 +00:00
msg.Id = id
2016-05-16 06:09:28 +00:00
msg.RecursionDesired = true
msg.Question = []dns.Question{
2016-07-24 11:44:29 +00:00
{
2016-05-16 06:09:28 +00:00
Name: dns.Fqdn(domain),
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
2016-05-16 18:53:18 +00:00
}}
2016-05-16 06:09:28 +00:00
2016-12-11 20:43:16 +00:00
buffer := buf.New()
buffer.AppendSupplier(func(b []byte) (int, error) {
writtenBuffer, err := msg.PackBuffer(b)
return len(writtenBuffer), err
})
2016-05-16 06:09:28 +00:00
2016-06-01 20:09:24 +00:00
return buffer
}
2016-11-27 20:39:09 +00:00
func (v *UDPNameServer) QueryA(domain string) <-chan *ARecord {
2016-06-01 20:09:24 +00:00
response := make(chan *ARecord, 1)
2016-11-27 20:39:09 +00:00
id := v.AssignUnusedID(response)
2016-06-01 20:09:24 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*8)
v.udpServer.Dispatch(ctx, v.address, v.BuildQueryA(domain, id), v.HandleResponse)
2016-06-01 20:09:24 +00:00
go func() {
for i := 0; i < 2; i++ {
time.Sleep(time.Second)
2016-11-27 20:39:09 +00:00
v.Lock()
_, found := v.requests[id]
v.Unlock()
2016-06-01 20:09:24 +00:00
if found {
v.udpServer.Dispatch(ctx, v.address, v.BuildQueryA(domain, id), v.HandleResponse)
2016-06-01 20:09:24 +00:00
} else {
break
}
}
cancel()
2016-06-01 20:09:24 +00:00
}()
2016-05-16 06:09:28 +00:00
return response
}
2016-05-16 16:05:01 +00:00
type LocalNameServer struct {
}
2016-11-27 20:39:09 +00:00
func (v *LocalNameServer) QueryA(domain string) <-chan *ARecord {
2016-05-16 18:53:18 +00:00
response := make(chan *ARecord, 1)
2016-05-16 16:05:01 +00:00
go func() {
defer close(response)
ips, err := net.LookupIP(domain)
if err != nil {
2017-04-08 23:43:25 +00:00
log.Trace(newError("failed to lookup IPs for domain ", domain).Base(err))
2016-05-16 16:05:01 +00:00
return
}
response <- &ARecord{
IPs: ips,
Expire: time.Now().Add(time.Second * time.Duration(DefaultTTL)),
}
}()
return response
}