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"
18 type webseedPeer struct {
20 activeRequests map[Request]webseed.Request
21 requesterCond sync.Cond
25 var _ peerImpl = (*webseedPeer)(nil)
27 func (me *webseedPeer) writeBufferFull() bool {
31 func (me *webseedPeer) connStatusString() string {
35 func (ws *webseedPeer) String() string {
36 return fmt.Sprintf("webseed peer for %q", ws.client.Url)
39 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
40 ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
44 func (ws *webseedPeer) writeInterested(interested bool) bool {
48 func (ws *webseedPeer) _cancel(r Request) bool {
49 active, ok := ws.activeRequests[r]
56 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
57 return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
60 func (ws *webseedPeer) _request(r Request) bool {
61 ws.requesterCond.Signal()
65 func (ws *webseedPeer) doRequest(r Request) {
66 webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
67 ws.activeRequests[r] = webseedRequest
69 ws.requesterCond.L.Unlock()
70 defer ws.requesterCond.L.Lock()
71 ws.requestResultHandler(r, webseedRequest)
73 delete(ws.activeRequests, r)
76 func (ws *webseedPeer) requester() {
77 ws.requesterCond.L.Lock()
78 defer ws.requesterCond.L.Unlock()
80 for !ws.peer.closed.IsSet() {
81 for r := range ws.peer.actualRequestState.Requests {
82 if _, ok := ws.activeRequests[r]; ok {
88 ws.requesterCond.Wait()
92 func (ws *webseedPeer) connectionFlags() string {
96 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
97 // return bool if this is even possible, and if it isn't, skip to the next drop candidate.
98 func (ws *webseedPeer) drop() {}
100 func (ws *webseedPeer) updateRequests() {
103 func (ws *webseedPeer) onClose() {
104 ws.peer.logger.Print("closing")
105 for _, r := range ws.activeRequests {
108 ws.requesterCond.Broadcast()
111 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
112 result := <-webseedRequest.Result
113 // We do this here rather than inside receiveChunk, since we want to count errors too. I'm not
114 // sure if we can divine which errors indicate cancellation on our end without hitting the
116 ws.peer.doChunkReadStats(int64(len(result.Bytes)))
118 defer ws.peer.t.cl.unlock()
119 if result.Err != nil {
120 if !errors.Is(result.Err, context.Canceled) {
121 ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
123 // We need to filter out temporary errors, but this is a nightmare in Go. Currently a bad
124 // webseed URL can starve out the good ones due to the chunk selection algorithm.
125 const closeOnAllErrors = false
126 if closeOnAllErrors ||
127 strings.Contains(result.Err.Error(), "unsupported protocol scheme") ||
129 var err webseed.ErrBadResponse
130 if !errors.As(result.Err, &err) {
133 return err.Response.StatusCode == http.StatusNotFound
137 ws.peer.remoteRejectedRequest(r)
140 err := ws.peer.receiveChunk(&pp.Message{
152 func (me *webseedPeer) onNextRequestStateChanged() {
153 me.peer.applyNextRequestState()