Skip to content

Commit

Permalink
update: locker modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Esonhugh committed Jan 19, 2025
1 parent fb11019 commit 5ac62b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ var RootCmd = &cobra.Command{

pkg.DnsTimeout = Opts.DnsTimeout
pkg.Latency = Opts.Latency
if Opts.Latency == 0 {
pkg.LockerMode = false
}
if Opts.DnsServer != "" {
pkg.NetResolver = pkg.WarpDnsServer(Opts.DnsServer)
} else {
Expand Down
68 changes: 41 additions & 27 deletions pkg/query_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ var (
NetResolver *SpiderResolver
Zone string // Zone is the domain name of the cluster

Latency = 0
Latency = 0
LockerMode = true // lockers if need locker to make sure only one dns record can make in same time
)

type SpiderResolver struct {
dns string
ctx context.Context
r *net.Resolver
filter []*regexp.Regexp
contains []string
timeout int
lock sync.Mutex
dns string
ctx context.Context
r *net.Resolver
filter []*regexp.Regexp
contains []string
timeout int
lock sync.Mutex
lockerMode bool
}

func DefaultResolver() *SpiderResolver {
return &SpiderResolver{
dns: "default-dns",
timeout: DnsTimeout,
r: net.DefaultResolver,
filter: []*regexp.Regexp{},
contains: []string{},
lock: sync.Mutex{},
dns: "default-dns",
timeout: DnsTimeout,
r: net.DefaultResolver,
filter: []*regexp.Regexp{},
contains: []string{},
lock: sync.Mutex{},
lockerMode: LockerMode,
}
}

Expand All @@ -52,9 +55,10 @@ func WarpDnsServer(dnsServer string) *SpiderResolver {
return d.DialContext(ctx, network, dnsServer)
},
},
filter: []*regexp.Regexp{},
contains: []string{},
lock: sync.Mutex{},
filter: []*regexp.Regexp{},
contains: []string{},
lock: sync.Mutex{},
lockerMode: LockerMode,
}
}

Expand Down Expand Up @@ -106,8 +110,10 @@ func (s *SpiderResolver) CurrentDNS() string {
}

func (s *SpiderResolver) PTRRecord(ip net.IP) []string {
s.lock.Lock()
defer s.lock.Unlock()
if s.lockerMode {
s.lock.Lock()
defer s.lock.Unlock()
}
ctx, _ := context.WithTimeout(context.Background(), time.Duration(s.timeout)*time.Second)
names, err := s.r.LookupAddr(ctx, ip.String())
if err != nil {
Expand All @@ -119,8 +125,10 @@ func (s *SpiderResolver) PTRRecord(ip net.IP) []string {
}

func (s *SpiderResolver) SRVRecord(svcDomain string) (string, []*net.SRV, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.lockerMode {
s.lock.Lock()
defer s.lock.Unlock()
}
ctx, _ := context.WithTimeout(context.Background(), time.Duration(s.timeout)*time.Second)
cname, srvs, err := s.r.LookupSRV(ctx, "", "", svcDomain)
var finalsrv []*net.SRV
Expand All @@ -135,25 +143,31 @@ func (s *SpiderResolver) SRVRecord(svcDomain string) (string, []*net.SRV, error)
}

func (s *SpiderResolver) CustomSRVRecord(svcDomain string, service, proto string) (string, []*net.SRV, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.lockerMode {
s.lock.Lock()
defer s.lock.Unlock()
}
ctx, _ := context.WithTimeout(context.Background(), time.Duration(s.timeout)*time.Second)
cname, srvs, err := s.r.LookupSRV(ctx, service, proto, svcDomain)
time.Sleep(time.Duration(Latency) * time.Millisecond)
return cname, srvs, err
}

func (s *SpiderResolver) ARecord(domain string) ([]net.IP, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.lockerMode {
s.lock.Lock()
defer s.lock.Unlock()
}
time.Sleep(time.Duration(Latency) * time.Millisecond)
ctx, _ := context.WithTimeout(context.Background(), time.Duration(s.timeout)*time.Second)
return s.r.LookupIP(ctx, "ip", domain)
}

func (s *SpiderResolver) TXTRecord(domain string) ([]string, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.lockerMode {
s.lock.Lock()
defer s.lock.Unlock()
}
time.Sleep(time.Duration(Latency) * time.Millisecond)
ctx, _ := context.WithTimeout(context.Background(), time.Duration(s.timeout)*time.Second)
return s.r.LookupTXT(ctx, domain)
Expand Down

0 comments on commit 5ac62b0

Please sign in to comment.