Skip to content

Commit

Permalink
make ip lookup cache global
Browse files Browse the repository at this point in the history
  • Loading branch information
mh0lt committed Aug 3, 2024
1 parent ba9b2b3 commit b09452a
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions tracker_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type trackerScraper struct {
mu sync.RWMutex
lastAnnounce trackerAnnounceResult
lookupTrackerIp func(*url.URL) ([]net.IP, error)
ips []net.IP
lastIpLookup time.Time
}

type torrentTrackerAnnouncer interface {
Expand Down Expand Up @@ -75,25 +73,50 @@ type trackerAnnounceResult struct {
Completed time.Time
}

type ilu struct {
ips []net.IP
lookupTime time.Time
}

var ipc = map[string]*ilu{}
var ipcmu sync.RWMutex

func (me *trackerScraper) getIp() (ip net.IP, err error) {
// cache the ip lookup for 15 mins, this avoids
// spamming DNS on os's that don't cache DNS lookups
// Cache TTL between 1 and 6 hours

if len(me.ips) == 0 || time.Since(me.lastIpLookup) > time.Hour+time.Duration(rand.Int63n(int64(5*time.Hour))) {
me.lastIpLookup = time.Now()
ipcmu.RLock()
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))) {
ips = lu.ips
}

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

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

ipcmu.Unlock()
}

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

me.ips = nil
err = errors.New("no acceptable ips")
return
}
Expand Down

0 comments on commit b09452a

Please sign in to comment.