]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix some stuff Rob Clifford broke through stress testing
authorMatt Joiner <anacrolix@gmail.com>
Fri, 6 Feb 2015 03:54:59 +0000 (14:54 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 6 Feb 2015 03:54:59 +0000 (14:54 +1100)
client.go
client_test.go
dht/dht.go
torrent.go

index 208d953993b6064927f2e04853aa9dd4dfd94224..e4549eeaf2b7ebd7759326c2c4ce19052a9bb187 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1505,7 +1505,12 @@ func (cl *Client) saveTorrentFile(t *torrent) error {
        if err != nil {
                return fmt.Errorf("error marshalling metainfo: %s", err)
        }
-       mi, _ := cl.torrentCacheMetaInfo(t.InfoHash)
+       mi, err := cl.torrentCacheMetaInfo(t.InfoHash)
+       if err != nil {
+               // For example, a script kiddy makes us load too many files, and we're
+               // able to save the torrent, but not load it again to check it.
+               return nil
+       }
        if !bytes.Equal(mi.Info.Hash, t.InfoHash[:]) {
                log.Fatalf("%x != %x", mi.Info.Hash, t.InfoHash[:])
        }
@@ -1517,6 +1522,17 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
        if err != nil {
                return
        }
+
+       if err := cl.saveTorrentFile(t); err != nil {
+               log.Printf("error saving torrent file for %s: %s", t, err)
+       }
+
+       if strings.Contains(strings.ToLower(md.Name), "porn") {
+               cl.dropTorrent(t.InfoHash)
+               err = errors.New("no porn plx")
+               return
+       }
+
        // If the client intends to upload, it needs to know what state pieces are
        // in.
        if !cl.noUpload {
@@ -1533,9 +1549,6 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
        }
 
        cl.downloadStrategy.TorrentStarted(t)
-       if err := cl.saveTorrentFile(t); err != nil {
-               log.Printf("error saving torrent file for %s: %s", t, err)
-       }
        close(t.gotMetainfo)
        return
 }
@@ -1619,6 +1632,10 @@ type Torrent struct {
        *torrent
 }
 
+func (t Torrent) Drop() {
+       t.cl.dropTorrent(t.InfoHash)
+}
+
 type File struct {
        t      Torrent
        path   string
@@ -1721,7 +1738,10 @@ func (cl *Client) AddMagnet(uri string) (T Torrent, err error) {
        if err != nil {
                log.Printf("error getting cached metainfo for %x: %s", m.InfoHash[:], err)
        } else if mi != nil {
-               cl.AddTorrent(mi)
+               _, err = cl.AddTorrent(mi)
+               if err != nil {
+                       return
+               }
        }
        cl.mu.Lock()
        defer cl.mu.Unlock()
@@ -1766,6 +1786,10 @@ func (cl *Client) connectionPruner(t *torrent) {
 func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
        me.mu.Lock()
        defer me.mu.Unlock()
+       return me.dropTorrent(infoHash)
+}
+
+func (me *Client) dropTorrent(infoHash InfoHash) (err error) {
        t, ok := me.torrents[infoHash]
        if !ok {
                err = fmt.Errorf("no such torrent")
index d29afc89afbde49550dd25021ff366db12fd2db4..5afcf2d38ce3019a815713c84a310b7655decda3 100644 (file)
@@ -25,6 +25,21 @@ func TestClientDefault(t *testing.T) {
        cl.Stop()
 }
 
+func TestAddDropTorrent(t *testing.T) {
+       cl, err := NewClient(nil)
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer cl.Stop()
+       dir, mi := testutil.GreetingTestTorrent()
+       defer os.RemoveAll(dir)
+       tt, err := cl.AddTorrent(mi)
+       if err != nil {
+               t.Fatal(err)
+       }
+       tt.Drop()
+}
+
 func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
        t.SkipNow()
 }
index ba792a11b08316748c6d318026467312b23a32bf..cac9fbd061ebc3c604023b5b5b1f6f81ca85d777 100644 (file)
@@ -262,6 +262,9 @@ func (m Msg) Nodes() (nodes []NodeInfo) {
                }()
                return m["r"].(map[string]interface{})["nodes"].(string)
        }()
+       if len(b)%26 != 0 {
+               return
+       }
        for i := 0; i < len(b); i += 26 {
                var n NodeInfo
                err := n.UnmarshalCompact([]byte(b[i : i+26]))
index 5e6d21e0b1b6cf357aa4903ee1bf6047e01777c4..41b97368e8f14be381523b05f83a81f42e153a71 100644 (file)
@@ -218,15 +218,21 @@ func (t *torrent) SetMetadataSize(bytes int64) {
        if t.MetaData != nil {
                return
        }
+       if bytes > 10000000 { // 10MB, pulled from my ass.
+               return
+       }
        t.MetaData = make([]byte, bytes)
        t.metadataHave = make([]bool, (bytes+(1<<14)-1)/(1<<14))
 }
 
 func (t *torrent) Name() string {
-       if !t.haveInfo() {
+       if t.haveInfo() {
+               return t.Info.Name
+       }
+       if t.DisplayName != "" {
                return t.DisplayName
        }
-       return t.Info.Name
+       return t.InfoHash.HexString()
 }
 
 func (t *torrent) pieceStatusChar(index int) byte {
@@ -446,11 +452,6 @@ func (t *torrent) NumPiecesCompleted() (num int) {
 }
 
 func (t *torrent) Length() int64 {
-       if t.Data == nil {
-               // Possibly the length might be available before the data is mmapped,
-               // I defer this decision to such a need arising.
-               panic("torrent length not known?")
-       }
        return t.length
 }