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"
15 type webseedPeer struct {
17 requests map[Request]webseed.Request
21 var _ peerImpl = (*webseedPeer)(nil)
23 func (me *webseedPeer) connStatusString() string {
27 func (ws *webseedPeer) String() string {
28 return fmt.Sprintf("webseed peer for %q", ws.client.Url)
31 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
32 ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
36 func (ws *webseedPeer) _postCancel(r Request) {
40 func (ws *webseedPeer) writeInterested(interested bool) bool {
44 func (ws *webseedPeer) cancel(r Request) bool {
45 ws.requests[r].Cancel()
49 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
50 return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
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)
60 func (ws *webseedPeer) connectionFlags() string {
64 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
65 // return bool if this is even possible, and if it isn't, skip to the next drop candidate.
66 func (ws *webseedPeer) drop() {}
68 func (ws *webseedPeer) updateRequests() {
69 ws.peer.doRequestState()
72 func (ws *webseedPeer) onClose() {}
74 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
75 result := <-webseedRequest.Result
77 defer ws.peer.t.cl.unlock()
78 if result.Err != nil {
79 ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
80 // Always close for now. We need to filter out temporary errors, but this is a nightmare in
81 // Go. Currently a bad webseed URL can starve out the good ones due to the chunk selection
83 const closeOnAllErrors = false
84 if closeOnAllErrors || strings.Contains(errors.Cause(result.Err).Error(), "unsupported protocol scheme") {
87 ws.peer.remoteRejectedRequest(r)
90 err := ws.peer.receiveChunk(&pp.Message{