From: Matt Joiner Date: Fri, 6 Feb 2015 03:54:59 +0000 (+1100) Subject: Fix some stuff Rob Clifford broke through stress testing X-Git-Tag: v1.0.0~1348 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=4ca6b4e2c7eafaa359159c1f61d042139f7b5565;p=btrtrc.git Fix some stuff Rob Clifford broke through stress testing --- diff --git a/client.go b/client.go index 208d9539..e4549eea 100644 --- 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") diff --git a/client_test.go b/client_test.go index d29afc89..5afcf2d3 100644 --- a/client_test.go +++ b/client_test.go @@ -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() } diff --git a/dht/dht.go b/dht/dht.go index ba792a11..cac9fbd0 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -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])) diff --git a/torrent.go b/torrent.go index 5e6d21e0..41b97368 100644 --- a/torrent.go +++ b/torrent.go @@ -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 }