Skip to content

Commit

Permalink
dns lookup cache error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mh0lt committed Aug 4, 2024
1 parent 092574b commit 6164078
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions tracker_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type trackerAnnounceResult struct {
}

type ilu struct {
err error
ips []net.IP
lookupTime time.Time
}
Expand All @@ -90,33 +91,35 @@ func (me *trackerScraper) getIp() (ip net.IP, err error) {
lu := ipc[me.u.String()]
ipcmu.RUnlock()

var ips []net.IP
if lu == nil ||
time.Since(lu.lookupTime) > time.Hour+time.Duration(rand.Int63n(int64(5*time.Hour))) ||
lu.err != nil && time.Since(lu.lookupTime) > 15*time.Minute {
var ips []net.IP

if lu != nil && time.Since(lu.lookupTime) > time.Hour+time.Duration(rand.Int63n(int64(5*time.Hour))) {
ips = lu.ips
}

if len(ips) == 0 {
if me.lookupTrackerIp != nil {
ips, err = me.lookupTrackerIp(&me.u)
} else {
// Do a regular dns lookup
fmt.Println("LU", me.u.Hostname())
ips, err = net.LookupIP(me.u.Hostname())
}
if err != nil {
return
}

ipcmu.Lock()
ipc[me.u.String()] = &ilu{
lu = &ilu{
err: err,
ips: ips,
lookupTime: time.Now(),
}

ipc[me.u.String()] = lu
ipcmu.Unlock()

}

if lu.err != nil {
return
}

if len(ips) == 0 {
if len(lu.ips) == 0 {
err = errors.New("no ips")
return
}
Expand All @@ -126,7 +129,7 @@ func (me *trackerScraper) getIp() (ip net.IP, err error) {
err = errors.New("client is closed")
return
}
for _, ip = range ips {
for _, ip = range lu.ips {
if me.t.cl.ipIsBlocked(ip) {
continue
}
Expand Down

0 comments on commit 6164078

Please sign in to comment.