]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Apply IP blocklist to trackers
authorMatt Joiner <anacrolix@gmail.com>
Tue, 10 Mar 2015 15:41:41 +0000 (02:41 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 10 Mar 2015 15:41:41 +0000 (02:41 +1100)
client.go

index 5701834e6de9927346c45b490be50f955572fb71..2f3a37f24488d8d3f169cbd001a2ca7f9210ba11 100644 (file)
--- a/client.go
+++ b/client.go
@@ -29,6 +29,7 @@ import (
        "math/big"
        mathRand "math/rand"
        "net"
+       "net/url"
        "os"
        "path/filepath"
        "sort"
@@ -2100,7 +2101,37 @@ func (cl *Client) announceTorrentDHT(t *torrent, impliedPort bool) {
        }
 }
 
+func (cl *Client) trackerBlockedUnlocked(tr tracker.Client) (blocked bool, err error) {
+       url_, err := url.Parse(tr.URL())
+       if err != nil {
+               return
+       }
+       host, _, err := net.SplitHostPort(url_.Host)
+       if err != nil {
+               host = url_.Host
+       }
+       addr, err := net.ResolveIPAddr("ip", host)
+       if err != nil {
+               return
+       }
+       cl.mu.Lock()
+       if cl.ipBlockList != nil {
+               if cl.ipBlockRange(addr.IP) != nil {
+                       blocked = true
+               }
+       }
+       cl.mu.Unlock()
+       return
+}
+
 func (cl *Client) announceTorrentSingleTracker(tr tracker.Client, req *tracker.AnnounceRequest, t *torrent) error {
+       blocked, err := cl.trackerBlockedUnlocked(tr)
+       if err != nil {
+               return fmt.Errorf("error determining if tracker blocked: %s", err)
+       }
+       if blocked {
+               return fmt.Errorf("tracker blocked: %s", tr)
+       }
        if err := tr.Connect(); err != nil {
                return fmt.Errorf("error connecting: %s", err)
        }