]> Sergey Matveev's repositories - btrtrc.git/blob - webseed-peer.go
We might want to close webseed peers on response errors
[btrtrc.git] / webseed-peer.go
1 package torrent
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/anacrolix/torrent/common"
8         "github.com/anacrolix/torrent/metainfo"
9         pp "github.com/anacrolix/torrent/peer_protocol"
10         "github.com/anacrolix/torrent/segments"
11         "github.com/anacrolix/torrent/webseed"
12         "github.com/pkg/errors"
13 )
14
15 type webseedPeer struct {
16         client   webseed.Client
17         requests map[request]webseed.Request
18         peer     peer
19 }
20
21 var _ peerImpl = (*webseedPeer)(nil)
22
23 func (me *webseedPeer) connStatusString() string {
24         return me.client.Url
25 }
26
27 func (ws *webseedPeer) String() string {
28         return fmt.Sprintf("webseed peer for %q", ws.client.Url)
29 }
30
31 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
32         ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
33         ws.client.Info = info
34 }
35
36 func (ws *webseedPeer) _postCancel(r request) {
37         ws.cancel(r)
38 }
39
40 func (ws *webseedPeer) writeInterested(interested bool) bool {
41         return true
42 }
43
44 func (ws *webseedPeer) cancel(r request) bool {
45         ws.requests[r].Cancel()
46         return true
47 }
48
49 func (ws *webseedPeer) intoSpec(r request) webseed.RequestSpec {
50         return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
51 }
52
53 func (ws *webseedPeer) request(r request) bool {
54         webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
55         ws.requests[r] = webseedRequest
56         go ws.requestResultHandler(r, webseedRequest)
57         return true
58 }
59
60 func (ws *webseedPeer) connectionFlags() string {
61         return "WS"
62 }
63
64 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too.
65 func (ws *webseedPeer) drop() {}
66
67 func (ws *webseedPeer) updateRequests() {
68         ws.peer.doRequestState()
69 }
70
71 func (ws *webseedPeer) _close() {}
72
73 func (ws *webseedPeer) requestResultHandler(r request, webseedRequest webseed.Request) {
74         result := <-webseedRequest.Result
75         ws.peer.t.cl.lock()
76         defer ws.peer.t.cl.unlock()
77         if result.Err != nil {
78                 ws.peer.logger.Printf("request %v rejected: %v", r, result.Err)
79                 // Always close for now. We need to filter out temporary errors, but this is a nightmare in
80                 // Go. Currently a bad webseed URL can starve out the good ones due to the chunk selection
81                 // algorithm.
82                 const closeOnAllErrors = false
83                 if closeOnAllErrors || strings.Contains(errors.Cause(result.Err).Error(), "unsupported protocol scheme") {
84                         ws.peer.close()
85                 } else {
86                         ws.peer.remoteRejectedRequest(r)
87                 }
88         } else {
89                 err := ws.peer.receiveChunk(&pp.Message{
90                         Type:  pp.Piece,
91                         Index: r.Index,
92                         Begin: r.Begin,
93                         Piece: result.Bytes,
94                 })
95                 if err != nil {
96                         panic(err)
97                 }
98         }
99 }