2020-05-30 17:52:27 +10:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2021-02-09 19:21:54 +11:00
|
|
|
"context"
|
|
|
|
"errors"
|
2020-06-04 11:50:20 +10:00
|
|
|
"fmt"
|
2021-02-18 14:36:08 +11:00
|
|
|
"net/http"
|
2020-07-10 13:18:33 +10:00
|
|
|
"strings"
|
2021-01-29 16:01:35 +11:00
|
|
|
"sync"
|
2020-06-01 18:25:45 +10:00
|
|
|
|
2020-06-02 16:18:25 +10:00
|
|
|
"github.com/anacrolix/torrent/common"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2020-06-01 18:41:21 +10:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2020-06-01 18:25:45 +10:00
|
|
|
"github.com/anacrolix/torrent/segments"
|
|
|
|
"github.com/anacrolix/torrent/webseed"
|
2020-05-30 17:52:27 +10:00
|
|
|
)
|
|
|
|
|
2020-06-04 11:58:18 +10:00
|
|
|
type webseedPeer struct {
|
2021-01-29 16:01:35 +11:00
|
|
|
client webseed.Client
|
|
|
|
activeRequests map[Request]webseed.Request
|
|
|
|
requesterCond sync.Cond
|
|
|
|
peer Peer
|
2020-06-01 18:25:45 +10:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:58:18 +10:00
|
|
|
var _ peerImpl = (*webseedPeer)(nil)
|
2020-06-01 18:25:45 +10:00
|
|
|
|
2021-05-09 14:14:11 +10:00
|
|
|
func (me *webseedPeer) writeBufferFull() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:21:54 +10:00
|
|
|
func (me *webseedPeer) connStatusString() string {
|
|
|
|
return me.client.Url
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:58:18 +10:00
|
|
|
func (ws *webseedPeer) String() string {
|
2020-06-04 11:50:20 +10:00
|
|
|
return fmt.Sprintf("webseed peer for %q", ws.client.Url)
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:58:18 +10:00
|
|
|
func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
|
2020-06-02 16:18:25 +10:00
|
|
|
ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
|
|
|
|
ws.client.Info = info
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:58:18 +10:00
|
|
|
func (ws *webseedPeer) writeInterested(interested bool) bool {
|
2020-05-30 17:52:27 +10:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-13 10:16:56 +11:00
|
|
|
func (ws *webseedPeer) _cancel(r RequestIndex) bool {
|
|
|
|
active, ok := ws.activeRequests[ws.peer.t.requestIndexToRequest(r)]
|
2021-05-09 23:38:38 +10:00
|
|
|
if ok {
|
|
|
|
active.Cancel()
|
2021-10-13 10:16:56 +11:00
|
|
|
if !ws.peer.deleteRequest(r) {
|
|
|
|
panic("cancelled webseed request should exist")
|
|
|
|
}
|
2021-10-18 16:31:16 +11:00
|
|
|
if ws.peer.actualRequestState.Requests.IsEmpty() {
|
2021-10-13 10:16:56 +11:00
|
|
|
ws.peer.updateRequests("webseedPeer._cancel")
|
|
|
|
}
|
2021-01-29 16:01:35 +11:00
|
|
|
}
|
2021-05-20 20:23:45 +10:00
|
|
|
return true
|
2020-05-30 17:52:27 +10:00
|
|
|
}
|
|
|
|
|
2021-01-28 14:23:22 +11:00
|
|
|
func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
|
2020-06-02 13:54:26 +10:00
|
|
|
return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:23:45 +10:00
|
|
|
func (ws *webseedPeer) _request(r Request) bool {
|
2021-01-29 16:01:35 +11:00
|
|
|
ws.requesterCond.Signal()
|
2021-05-20 20:23:45 +10:00
|
|
|
return true
|
2020-05-30 17:52:27 +10:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:01:35 +11:00
|
|
|
func (ws *webseedPeer) doRequest(r Request) {
|
|
|
|
webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
|
|
|
|
ws.activeRequests[r] = webseedRequest
|
2021-02-09 19:21:54 +11:00
|
|
|
func() {
|
|
|
|
ws.requesterCond.L.Unlock()
|
|
|
|
defer ws.requesterCond.L.Lock()
|
|
|
|
ws.requestResultHandler(r, webseedRequest)
|
|
|
|
}()
|
2021-01-29 16:01:35 +11:00
|
|
|
delete(ws.activeRequests, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *webseedPeer) requester() {
|
|
|
|
ws.requesterCond.L.Lock()
|
|
|
|
defer ws.requesterCond.L.Unlock()
|
|
|
|
start:
|
|
|
|
for !ws.peer.closed.IsSet() {
|
2021-09-19 15:16:37 +10:00
|
|
|
restart := false
|
|
|
|
ws.peer.actualRequestState.Requests.Iterate(func(x uint32) bool {
|
|
|
|
r := ws.peer.t.requestIndexToRequest(x)
|
2021-01-29 16:01:35 +11:00
|
|
|
if _, ok := ws.activeRequests[r]; ok {
|
2021-09-19 15:16:37 +10:00
|
|
|
return true
|
2021-01-29 16:01:35 +11:00
|
|
|
}
|
|
|
|
ws.doRequest(r)
|
2021-09-19 15:16:37 +10:00
|
|
|
restart = true
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if restart {
|
2021-01-29 16:01:35 +11:00
|
|
|
goto start
|
|
|
|
}
|
|
|
|
ws.requesterCond.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:58:18 +10:00
|
|
|
func (ws *webseedPeer) connectionFlags() string {
|
2020-05-30 17:52:27 +10:00
|
|
|
return "WS"
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:51:23 +11:00
|
|
|
// TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
|
|
|
|
// return bool if this is even possible, and if it isn't, skip to the next drop candidate.
|
2020-06-04 11:58:18 +10:00
|
|
|
func (ws *webseedPeer) drop() {}
|
2020-05-30 17:52:27 +10:00
|
|
|
|
2021-10-08 13:53:36 +11:00
|
|
|
func (ws *webseedPeer) updateRequests(reason string) {
|
2020-05-30 17:52:27 +10:00
|
|
|
}
|
2020-05-31 13:09:56 +10:00
|
|
|
|
2021-01-29 16:01:35 +11:00
|
|
|
func (ws *webseedPeer) onClose() {
|
2021-02-09 19:21:54 +11:00
|
|
|
ws.peer.logger.Print("closing")
|
|
|
|
for _, r := range ws.activeRequests {
|
|
|
|
r.Cancel()
|
|
|
|
}
|
2021-01-29 16:01:35 +11:00
|
|
|
ws.requesterCond.Broadcast()
|
|
|
|
}
|
2020-06-01 18:41:21 +10:00
|
|
|
|
2021-01-28 14:23:22 +11:00
|
|
|
func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
|
2020-06-02 16:41:49 +10:00
|
|
|
result := <-webseedRequest.Result
|
2021-05-21 11:49:57 +10:00
|
|
|
// We do this here rather than inside receiveChunk, since we want to count errors too. I'm not
|
|
|
|
// sure if we can divine which errors indicate cancellation on our end without hitting the
|
|
|
|
// network though.
|
|
|
|
ws.peer.doChunkReadStats(int64(len(result.Bytes)))
|
2020-06-02 13:54:26 +10:00
|
|
|
ws.peer.t.cl.lock()
|
2020-06-02 16:41:49 +10:00
|
|
|
defer ws.peer.t.cl.unlock()
|
|
|
|
if result.Err != nil {
|
2021-02-09 19:21:54 +11:00
|
|
|
if !errors.Is(result.Err, context.Canceled) {
|
|
|
|
ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
|
|
|
|
}
|
2021-02-18 14:36:08 +11:00
|
|
|
// We need to filter out temporary errors, but this is a nightmare in Go. Currently a bad
|
|
|
|
// webseed URL can starve out the good ones due to the chunk selection algorithm.
|
2020-10-15 12:45:19 +11:00
|
|
|
const closeOnAllErrors = false
|
2021-02-18 14:36:08 +11:00
|
|
|
if closeOnAllErrors ||
|
|
|
|
strings.Contains(result.Err.Error(), "unsupported protocol scheme") ||
|
|
|
|
func() bool {
|
|
|
|
var err webseed.ErrBadResponse
|
|
|
|
if !errors.As(result.Err, &err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return err.Response.StatusCode == http.StatusNotFound
|
|
|
|
}() {
|
2020-07-10 13:18:33 +10:00
|
|
|
ws.peer.close()
|
|
|
|
} else {
|
2021-09-19 15:16:37 +10:00
|
|
|
ws.peer.remoteRejectedRequest(ws.peer.t.requestIndexFromRequest(r))
|
2020-07-10 13:18:33 +10:00
|
|
|
}
|
2020-06-02 16:41:49 +10:00
|
|
|
} else {
|
|
|
|
err := ws.peer.receiveChunk(&pp.Message{
|
|
|
|
Type: pp.Piece,
|
|
|
|
Index: r.Index,
|
|
|
|
Begin: r.Begin,
|
|
|
|
Piece: result.Bytes,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-01 18:41:21 +10:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 20:23:45 +10:00
|
|
|
|
|
|
|
func (me *webseedPeer) onNextRequestStateChanged() {
|
|
|
|
me.peer.applyNextRequestState()
|
|
|
|
}
|