]> Sergey Matveev's repositories - btrtrc.git/blob - webseed-peer.go
Treat 404 responses from webseed peers as fatal
[btrtrc.git] / webseed-peer.go
1 package torrent
2
3 import (
4         "context"
5         "errors"
6         "fmt"
7         "net/http"
8         "strings"
9         "sync"
10
11         "github.com/anacrolix/torrent/common"
12         "github.com/anacrolix/torrent/metainfo"
13         pp "github.com/anacrolix/torrent/peer_protocol"
14         "github.com/anacrolix/torrent/segments"
15         "github.com/anacrolix/torrent/webseed"
16 )
17
18 type webseedPeer struct {
19         client         webseed.Client
20         activeRequests map[Request]webseed.Request
21         requesterCond  sync.Cond
22         peer           Peer
23 }
24
25 var _ peerImpl = (*webseedPeer)(nil)
26
27 func (me *webseedPeer) connStatusString() string {
28         return me.client.Url
29 }
30
31 func (ws *webseedPeer) String() string {
32         return fmt.Sprintf("webseed peer for %q", ws.client.Url)
33 }
34
35 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
36         ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
37         ws.client.Info = info
38 }
39
40 func (ws *webseedPeer) _postCancel(r Request) {
41         ws.cancel(r)
42 }
43
44 func (ws *webseedPeer) writeInterested(interested bool) bool {
45         return true
46 }
47
48 func (ws *webseedPeer) cancel(r Request) bool {
49         active, ok := ws.activeRequests[r]
50         if !ok {
51                 return false
52         }
53         active.Cancel()
54         return true
55 }
56
57 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
58         return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
59 }
60
61 func (ws *webseedPeer) request(r Request) bool {
62         ws.requesterCond.Signal()
63         return true
64 }
65
66 func (ws *webseedPeer) doRequest(r Request) {
67         webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
68         ws.activeRequests[r] = webseedRequest
69         func() {
70                 ws.requesterCond.L.Unlock()
71                 defer ws.requesterCond.L.Lock()
72                 ws.requestResultHandler(r, webseedRequest)
73         }()
74         delete(ws.activeRequests, r)
75 }
76
77 func (ws *webseedPeer) requester() {
78         ws.requesterCond.L.Lock()
79         defer ws.requesterCond.L.Unlock()
80 start:
81         for !ws.peer.closed.IsSet() {
82                 for r := range ws.peer.requests {
83                         if _, ok := ws.activeRequests[r]; ok {
84                                 continue
85                         }
86                         ws.doRequest(r)
87                         goto start
88                 }
89                 ws.requesterCond.Wait()
90         }
91 }
92
93 func (ws *webseedPeer) connectionFlags() string {
94         return "WS"
95 }
96
97 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
98 // return bool if this is even possible, and if it isn't, skip to the next drop candidate.
99 func (ws *webseedPeer) drop() {}
100
101 func (ws *webseedPeer) updateRequests() {
102         ws.peer.doRequestState()
103 }
104
105 func (ws *webseedPeer) onClose() {
106         ws.peer.logger.Print("closing")
107         for _, r := range ws.activeRequests {
108                 r.Cancel()
109         }
110         ws.requesterCond.Broadcast()
111 }
112
113 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
114         result := <-webseedRequest.Result
115         ws.peer.t.cl.lock()
116         defer ws.peer.t.cl.unlock()
117         if result.Err != nil {
118                 if !errors.Is(result.Err, context.Canceled) {
119                         ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
120                 }
121                 // We need to filter out temporary errors, but this is a nightmare in Go. Currently a bad
122                 // webseed URL can starve out the good ones due to the chunk selection algorithm.
123                 const closeOnAllErrors = false
124                 if closeOnAllErrors ||
125                         strings.Contains(result.Err.Error(), "unsupported protocol scheme") ||
126                         func() bool {
127                                 var err webseed.ErrBadResponse
128                                 if !errors.As(result.Err, &err) {
129                                         return false
130                                 }
131                                 return err.Response.StatusCode == http.StatusNotFound
132                         }() {
133                         ws.peer.close()
134                 } else {
135                         ws.peer.remoteRejectedRequest(r)
136                 }
137         } else {
138                 err := ws.peer.receiveChunk(&pp.Message{
139                         Type:  pp.Piece,
140                         Index: r.Index,
141                         Begin: r.Begin,
142                         Piece: result.Bytes,
143                 })
144                 if err != nil {
145                         panic(err)
146                 }
147         }
148 }