Skip to content

Commit 25a9134

Browse files
committed
update: dns query lock
1 parent 9162404 commit 25a9134

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

pkg/query_utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"regexp"
88
"strings"
9+
"sync"
910
"time"
1011

1112
log "github.com/sirupsen/logrus"
@@ -25,6 +26,8 @@ type SpiderResolver struct {
2526
r *net.Resolver
2627
filter []*regexp.Regexp
2728
contains []string
29+
30+
lock *sync.Mutex
2831
}
2932

3033
func DefaultResolver() *SpiderResolver {
@@ -103,6 +106,8 @@ func (s *SpiderResolver) CurrentDNS() string {
103106
}
104107

105108
func (s *SpiderResolver) PTRRecord(ip net.IP) []string {
109+
s.lock.Lock()
110+
defer s.lock.Unlock()
106111
names, err := s.r.LookupAddr(s.ctx, ip.String())
107112
if err != nil {
108113
log.Debugf("LookupAddr failed: %v", err)
@@ -113,6 +118,8 @@ func (s *SpiderResolver) PTRRecord(ip net.IP) []string {
113118
}
114119

115120
func (s *SpiderResolver) SRVRecord(svcDomain string) (string, []*net.SRV, error) {
121+
s.lock.Lock()
122+
defer s.lock.Unlock()
116123
cname, srvs, err := s.r.LookupSRV(s.ctx, "", "", svcDomain)
117124
var finalsrv []*net.SRV
118125
for _, srv := range srvs {
@@ -126,17 +133,23 @@ func (s *SpiderResolver) SRVRecord(svcDomain string) (string, []*net.SRV, error)
126133
}
127134

128135
func (s *SpiderResolver) CustomSRVRecord(svcDomain string, service, proto string) (string, []*net.SRV, error) {
136+
s.lock.Lock()
137+
defer s.lock.Unlock()
129138
cname, srvs, err := s.r.LookupSRV(s.ctx, service, proto, svcDomain)
130139
time.Sleep(time.Duration(Latency) * time.Millisecond)
131140
return cname, srvs, err
132141
}
133142

134143
func (s *SpiderResolver) ARecord(domain string) ([]net.IP, error) {
144+
s.lock.Lock()
145+
defer s.lock.Unlock()
135146
time.Sleep(time.Duration(Latency) * time.Millisecond)
136147
return s.r.LookupIP(s.ctx, "ip", domain)
137148
}
138149

139150
func (s *SpiderResolver) TXTRecord(domain string) ([]string, error) {
151+
s.lock.Lock()
152+
defer s.lock.Unlock()
140153
time.Sleep(time.Duration(Latency) * time.Millisecond)
141154
return s.r.LookupTXT(s.ctx, domain)
142155
}

0 commit comments

Comments
 (0)