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

215 lines
4.4 KiB
Go
Raw Normal View History

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