2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-19 07:13:02 -05:00
|
|
|
"encoding/binary"
|
2018-06-26 09:04:47 -04:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
2018-06-26 09:04:47 -04:00
|
|
|
"v2ray.com/core/common"
|
2019-02-21 07:43:48 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2018-06-26 09:04:47 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2018-12-29 03:03:32 -05:00
|
|
|
"v2ray.com/core/common/protocol/dns"
|
2019-01-05 15:43:22 -05:00
|
|
|
udp_proto "v2ray.com/core/common/protocol/udp"
|
2018-11-02 10:01:33 -04:00
|
|
|
"v2ray.com/core/common/session"
|
2018-07-01 06:38:40 -04:00
|
|
|
"v2ray.com/core/common/signal/pubsub"
|
2018-06-26 09:04:47 -04:00
|
|
|
"v2ray.com/core/common/task"
|
2019-02-21 07:43:48 -05:00
|
|
|
dns_feature "v2ray.com/core/features/dns"
|
2018-11-02 10:01:33 -04:00
|
|
|
"v2ray.com/core/features/routing"
|
2018-06-26 09:04:47 -04:00
|
|
|
"v2ray.com/core/transport/internet/udp"
|
|
|
|
)
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
type record struct {
|
|
|
|
A *IPRecord
|
|
|
|
AAAA *IPRecord
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
type IPRecord struct {
|
2019-02-21 07:43:48 -05:00
|
|
|
IP []net.Address
|
2018-06-26 09:04:47 -04:00
|
|
|
Expire time.Time
|
2019-02-21 07:43:48 -05:00
|
|
|
RCode dnsmessage.RCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IPRecord) getIPs() ([]net.Address, error) {
|
|
|
|
if r == nil || r.Expire.Before(time.Now()) {
|
|
|
|
return nil, errRecordNotFound
|
|
|
|
}
|
|
|
|
if r.RCode != dnsmessage.RCodeSuccess {
|
|
|
|
return nil, dns_feature.RCodeError(r.RCode)
|
|
|
|
}
|
|
|
|
return r.IP, nil
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2018-07-01 11:15:29 -04:00
|
|
|
type pendingRequest struct {
|
2019-02-21 07:43:48 -05:00
|
|
|
domain string
|
|
|
|
expire time.Time
|
|
|
|
recType dnsmessage.Type
|
2018-07-01 11:15:29 -04:00
|
|
|
}
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
var (
|
|
|
|
errRecordNotFound = errors.New("record not found")
|
|
|
|
)
|
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
type ClassicNameServer struct {
|
|
|
|
sync.RWMutex
|
|
|
|
address net.Destination
|
2019-02-21 07:43:48 -05:00
|
|
|
ips map[string]record
|
2018-07-01 11:15:29 -04:00
|
|
|
requests map[uint16]pendingRequest
|
2018-07-01 06:38:40 -04:00
|
|
|
pub *pubsub.Service
|
2018-06-26 09:04:47 -04:00
|
|
|
udpServer *udp.Dispatcher
|
|
|
|
cleanup *task.Periodic
|
|
|
|
reqID uint32
|
2018-06-26 17:23:59 -04:00
|
|
|
clientIP net.IP
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
func NewClassicNameServer(address net.Destination, dispatcher routing.Dispatcher, clientIP net.IP) *ClassicNameServer {
|
2018-06-26 09:04:47 -04:00
|
|
|
s := &ClassicNameServer{
|
2018-07-03 15:38:02 -04:00
|
|
|
address: address,
|
2019-02-21 07:43:48 -05:00
|
|
|
ips: make(map[string]record),
|
2018-07-03 15:38:02 -04:00
|
|
|
requests: make(map[uint16]pendingRequest),
|
|
|
|
clientIP: clientIP,
|
|
|
|
pub: pubsub.NewService(),
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
s.cleanup = &task.Periodic{
|
|
|
|
Interval: time.Minute,
|
|
|
|
Execute: s.Cleanup,
|
|
|
|
}
|
2018-07-03 15:38:02 -04:00
|
|
|
s.udpServer = udp.NewDispatcher(dispatcher, s.HandleResponse)
|
2018-06-26 09:04:47 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:29:09 -05:00
|
|
|
func (s *ClassicNameServer) Name() string {
|
|
|
|
return s.address.String()
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
func (s *ClassicNameServer) Cleanup() error {
|
|
|
|
now := time.Now()
|
|
|
|
s.Lock()
|
2018-08-29 17:00:01 -04:00
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
if len(s.ips) == 0 && len(s.requests) == 0 {
|
|
|
|
return newError("nothing to do. stopping...")
|
|
|
|
}
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
for domain, record := range s.ips {
|
|
|
|
if record.A != nil && record.A.Expire.Before(now) {
|
|
|
|
record.A = nil
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
if record.AAAA != nil && record.AAAA.Expire.Before(now) {
|
|
|
|
record.AAAA = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if record.A == nil && record.AAAA == nil {
|
2018-06-26 09:04:47 -04:00
|
|
|
delete(s.ips, domain)
|
2019-02-21 07:43:48 -05:00
|
|
|
} else {
|
|
|
|
s.ips[domain] = record
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.ips) == 0 {
|
2019-02-21 07:43:48 -05:00
|
|
|
s.ips = make(map[string]record)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2018-07-01 11:15:29 -04:00
|
|
|
for id, req := range s.requests {
|
|
|
|
if req.expire.Before(now) {
|
|
|
|
delete(s.requests, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.requests) == 0 {
|
|
|
|
s.requests = make(map[uint16]pendingRequest)
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:04:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:43:22 -05:00
|
|
|
func (s *ClassicNameServer) HandleResponse(ctx context.Context, packet *udp_proto.Packet) {
|
|
|
|
payload := packet.Payload
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
var parser dnsmessage.Parser
|
|
|
|
header, err := parser.Start(payload.Bytes())
|
|
|
|
if err != nil {
|
2018-06-26 09:04:47 -04:00
|
|
|
newError("failed to parse DNS response").Base(err).AtWarning().WriteToLog()
|
|
|
|
return
|
|
|
|
}
|
2018-11-19 08:13:20 -05:00
|
|
|
if err := parser.SkipAllQuestions(); err != nil {
|
|
|
|
newError("failed to skip questions in DNS response").Base(err).AtWarning().WriteToLog()
|
|
|
|
return
|
|
|
|
}
|
2018-06-26 09:04:47 -04:00
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
id := header.ID
|
2018-07-01 11:15:29 -04:00
|
|
|
s.Lock()
|
|
|
|
req, f := s.requests[id]
|
|
|
|
if f {
|
|
|
|
delete(s.requests, id)
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
if !f {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := req.domain
|
2019-02-21 07:43:48 -05:00
|
|
|
recType := req.recType
|
2018-06-26 09:04:47 -04:00
|
|
|
|
|
|
|
now := time.Now()
|
2019-02-21 07:43:48 -05:00
|
|
|
ipRecord := &IPRecord{
|
|
|
|
RCode: header.RCode,
|
|
|
|
Expire: now.Add(time.Second * 600),
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:04:33 -05:00
|
|
|
L:
|
2018-11-19 07:13:02 -05:00
|
|
|
for {
|
|
|
|
header, err := parser.AnswerHeader()
|
|
|
|
if err != nil {
|
2018-11-22 11:16:44 -05:00
|
|
|
if err != dnsmessage.ErrSectionDone {
|
|
|
|
newError("failed to parse answer section for domain: ", domain).Base(err).WriteToLog()
|
|
|
|
}
|
2018-11-19 07:13:02 -05:00
|
|
|
break
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2018-11-19 07:13:02 -05:00
|
|
|
ttl := header.TTL
|
2018-06-26 09:04:47 -04:00
|
|
|
if ttl == 0 {
|
2018-07-01 06:38:40 -04:00
|
|
|
ttl = 600
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
expire := now.Add(time.Duration(ttl) * time.Second)
|
|
|
|
if ipRecord.Expire.After(expire) {
|
|
|
|
ipRecord.Expire = expire
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.Type != recType {
|
2019-02-21 09:04:33 -05:00
|
|
|
if err := parser.SkipAnswer(); err != nil {
|
|
|
|
newError("failed to skip answer").Base(err).WriteToLog()
|
|
|
|
break L
|
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
switch header.Type {
|
|
|
|
case dnsmessage.TypeA:
|
|
|
|
ans, err := parser.AResource()
|
|
|
|
if err != nil {
|
2018-11-22 11:16:44 -05:00
|
|
|
newError("failed to parse A record for domain: ", domain).Base(err).WriteToLog()
|
2019-02-21 09:04:33 -05:00
|
|
|
break L
|
2018-11-19 07:13:02 -05:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
ipRecord.IP = append(ipRecord.IP, net.IPAddress(ans.A[:]))
|
2018-11-19 07:13:02 -05:00
|
|
|
case dnsmessage.TypeAAAA:
|
|
|
|
ans, err := parser.AAAAResource()
|
|
|
|
if err != nil {
|
2018-11-22 11:16:44 -05:00
|
|
|
newError("failed to parse A record for domain: ", domain).Base(err).WriteToLog()
|
2019-02-21 09:04:33 -05:00
|
|
|
break L
|
2018-11-19 07:13:02 -05:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
ipRecord.IP = append(ipRecord.IP, net.IPAddress(ans.AAAA[:]))
|
2018-11-19 07:13:02 -05:00
|
|
|
default:
|
2018-11-22 11:16:44 -05:00
|
|
|
if err := parser.SkipAnswer(); err != nil {
|
|
|
|
newError("failed to skip answer").Base(err).WriteToLog()
|
2019-02-21 09:04:33 -05:00
|
|
|
break L
|
2018-11-22 11:16:44 -05:00
|
|
|
}
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
var rec record
|
|
|
|
switch recType {
|
|
|
|
case dnsmessage.TypeA:
|
|
|
|
rec.A = ipRecord
|
|
|
|
case dnsmessage.TypeAAAA:
|
|
|
|
rec.AAAA = ipRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(domain) > 0 && (rec.A != nil || rec.AAAA != nil) {
|
|
|
|
s.updateIP(domain, rec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNewer(baseRec *IPRecord, newRec *IPRecord) bool {
|
|
|
|
if newRec == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if baseRec == nil {
|
|
|
|
return true
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
return baseRec.Expire.Before(newRec.Expire)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
func (s *ClassicNameServer) updateIP(domain string, newRec record) {
|
2018-06-26 09:04:47 -04:00
|
|
|
s.Lock()
|
|
|
|
|
|
|
|
newError("updating IP records for domain:", domain).AtDebug().WriteToLog()
|
2019-02-21 07:43:48 -05:00
|
|
|
rec := s.ips[domain]
|
|
|
|
|
|
|
|
updated := false
|
|
|
|
if isNewer(rec.A, newRec.A) {
|
|
|
|
rec.A = newRec.A
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
if isNewer(rec.AAAA, newRec.AAAA) {
|
|
|
|
rec.AAAA = newRec.AAAA
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
|
|
|
s.ips[domain] = rec
|
|
|
|
s.pub.Publish(domain, nil)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2018-08-29 17:00:01 -04:00
|
|
|
|
|
|
|
s.Unlock()
|
|
|
|
common.Must(s.cleanup.Start())
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
func (s *ClassicNameServer) getMsgOptions() *dnsmessage.Resource {
|
2018-06-26 17:23:59 -04:00
|
|
|
if len(s.clientIP) == 0 {
|
2018-06-26 11:14:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
var netmask int
|
|
|
|
var family uint16
|
2018-06-26 11:14:51 -04:00
|
|
|
|
2018-06-26 17:23:59 -04:00
|
|
|
if len(s.clientIP) == 4 {
|
2018-11-19 07:13:02 -05:00
|
|
|
family = 1
|
|
|
|
netmask = 24 // 24 for IPV4, 96 for IPv6
|
2018-06-26 17:23:59 -04:00
|
|
|
} else {
|
2018-11-19 07:13:02 -05:00
|
|
|
family = 2
|
|
|
|
netmask = 96
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint16(b[0:], family)
|
|
|
|
b[2] = byte(netmask)
|
|
|
|
b[3] = 0
|
|
|
|
switch family {
|
|
|
|
case 1:
|
|
|
|
ip := s.clientIP.To4().Mask(net.CIDRMask(netmask, net.IPv4len*8))
|
|
|
|
needLength := (netmask + 8 - 1) / 8 // division rounding up
|
|
|
|
b = append(b, ip[:needLength]...)
|
|
|
|
case 2:
|
|
|
|
ip := s.clientIP.Mask(net.CIDRMask(netmask, net.IPv6len*8))
|
|
|
|
needLength := (netmask + 8 - 1) / 8 // division rounding up
|
|
|
|
b = append(b, ip[:needLength]...)
|
2018-06-26 11:14:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
const EDNS0SUBNET = 0x08
|
|
|
|
|
|
|
|
opt := new(dnsmessage.Resource)
|
|
|
|
common.Must(opt.Header.SetEDNS0(1350, 0xfe00, true))
|
2018-06-26 11:14:51 -04:00
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
opt.Body = &dnsmessage.OPTResource{
|
|
|
|
Options: []dnsmessage.Option{
|
|
|
|
{
|
|
|
|
Code: EDNS0SUBNET,
|
|
|
|
Data: b,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
2018-07-01 11:15:29 -04:00
|
|
|
}
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
func (s *ClassicNameServer) addPendingRequest(domain string, recType dnsmessage.Type) uint16 {
|
2018-07-01 11:15:29 -04:00
|
|
|
id := uint16(atomic.AddUint32(&s.reqID, 1))
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
s.requests[id] = pendingRequest{
|
2019-02-21 07:43:48 -05:00
|
|
|
domain: domain,
|
|
|
|
expire: time.Now().Add(time.Second * 8),
|
|
|
|
recType: recType,
|
2018-07-01 11:15:29 -04:00
|
|
|
}
|
2018-06-26 11:14:51 -04:00
|
|
|
|
2018-07-01 11:15:29 -04:00
|
|
|
return id
|
2018-06-26 11:14:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
func (s *ClassicNameServer) buildMsgs(domain string, option IPOption) []*dnsmessage.Message {
|
2018-11-19 07:13:02 -05:00
|
|
|
qA := dnsmessage.Question{
|
|
|
|
Name: dnsmessage.MustNewName(domain),
|
|
|
|
Type: dnsmessage.TypeA,
|
|
|
|
Class: dnsmessage.ClassINET,
|
2018-06-26 11:14:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
qAAAA := dnsmessage.Question{
|
|
|
|
Name: dnsmessage.MustNewName(domain),
|
|
|
|
Type: dnsmessage.TypeAAAA,
|
|
|
|
Class: dnsmessage.ClassINET,
|
2018-06-26 11:14:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
var msgs []*dnsmessage.Message
|
2018-06-26 09:04:47 -04:00
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
if option.IPv4Enable {
|
2018-11-19 07:13:02 -05:00
|
|
|
msg := new(dnsmessage.Message)
|
2019-02-21 07:43:48 -05:00
|
|
|
msg.Header.ID = s.addPendingRequest(domain, dnsmessage.TypeA)
|
2018-11-19 07:13:02 -05:00
|
|
|
msg.Header.RecursionDesired = true
|
|
|
|
msg.Questions = []dnsmessage.Question{qA}
|
2018-06-26 11:14:51 -04:00
|
|
|
if opt := s.getMsgOptions(); opt != nil {
|
2018-11-19 07:13:02 -05:00
|
|
|
msg.Additionals = append(msg.Additionals, *opt)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
if option.IPv6Enable {
|
2018-11-19 07:13:02 -05:00
|
|
|
msg := new(dnsmessage.Message)
|
2019-02-21 07:43:48 -05:00
|
|
|
msg.Header.ID = s.addPendingRequest(domain, dnsmessage.TypeAAAA)
|
2018-11-19 07:13:02 -05:00
|
|
|
msg.Header.RecursionDesired = true
|
|
|
|
msg.Questions = []dnsmessage.Question{qAAAA}
|
2018-06-26 11:14:51 -04:00
|
|
|
if opt := s.getMsgOptions(); opt != nil {
|
2018-11-19 07:13:02 -05:00
|
|
|
msg.Additionals = append(msg.Additionals, *opt)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs
|
|
|
|
}
|
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, option IPOption) {
|
2018-09-10 16:12:07 -04:00
|
|
|
newError("querying DNS for: ", domain).AtDebug().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
msgs := s.buildMsgs(domain, option)
|
2018-06-26 09:04:47 -04:00
|
|
|
|
|
|
|
for _, msg := range msgs {
|
2018-12-29 03:03:32 -05:00
|
|
|
b, err := dns.PackMessage(msg)
|
2018-06-26 09:04:47 -04:00
|
|
|
common.Must(err)
|
2019-01-16 14:32:41 -05:00
|
|
|
udpCtx := context.Background()
|
|
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil {
|
|
|
|
udpCtx = session.ContextWithInbound(udpCtx, inbound)
|
|
|
|
}
|
2019-02-22 10:58:16 -05:00
|
|
|
udpCtx = session.ContextWithContent(udpCtx, &session.Content{
|
|
|
|
Protocol: "dns",
|
|
|
|
})
|
2019-01-16 14:32:41 -05:00
|
|
|
s.udpServer.Dispatch(udpCtx, s.address, b)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
func (s *ClassicNameServer) findIPsForDomain(domain string, option IPOption) ([]net.IP, error) {
|
2018-06-27 03:12:55 -04:00
|
|
|
s.RLock()
|
2019-02-21 07:43:48 -05:00
|
|
|
record, found := s.ips[domain]
|
2018-06-27 03:12:55 -04:00
|
|
|
s.RUnlock()
|
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
if !found {
|
|
|
|
return nil, errRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var ips []net.Address
|
|
|
|
var lastErr error
|
|
|
|
if option.IPv4Enable {
|
|
|
|
a, err := record.A.getIPs()
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
ips = append(ips, a...)
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
2019-02-21 07:43:48 -05:00
|
|
|
|
|
|
|
if option.IPv6Enable {
|
|
|
|
aaaa, err := record.AAAA.getIPs()
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
}
|
|
|
|
ips = append(ips, aaaa...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ips) > 0 {
|
|
|
|
return toNetIP(ips), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastErr != nil {
|
|
|
|
return nil, lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, dns_feature.ErrEmptyResponse
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:13:02 -05:00
|
|
|
func Fqdn(domain string) string {
|
|
|
|
if len(domain) > 0 && domain[len(domain)-1] == '.' {
|
|
|
|
return domain
|
|
|
|
}
|
|
|
|
return domain + "."
|
|
|
|
}
|
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
|
2018-11-19 07:13:02 -05:00
|
|
|
fqdn := Fqdn(domain)
|
2018-06-26 09:04:47 -04:00
|
|
|
|
2019-02-21 07:43:48 -05:00
|
|
|
ips, err := s.findIPsForDomain(fqdn, option)
|
|
|
|
if err != errRecordNotFound {
|
|
|
|
return ips, err
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
2018-07-01 06:38:40 -04:00
|
|
|
sub := s.pub.Subscribe(fqdn)
|
|
|
|
defer sub.Close()
|
|
|
|
|
2018-11-19 14:42:02 -05:00
|
|
|
s.sendQuery(ctx, fqdn, option)
|
2018-06-26 09:04:47 -04:00
|
|
|
|
|
|
|
for {
|
2019-02-21 07:43:48 -05:00
|
|
|
ips, err := s.findIPsForDomain(fqdn, option)
|
|
|
|
if err != errRecordNotFound {
|
|
|
|
return ips, err
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2018-07-01 06:38:40 -04:00
|
|
|
case <-sub.Wait():
|
2018-06-26 09:04:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|