2020-05-30 15:52:27 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2020-06-04 09:50:20 +08:00
|
|
|
"fmt"
|
2020-07-10 11:18:33 +08:00
|
|
|
"strings"
|
2020-06-01 16:25:45 +08:00
|
|
|
|
2020-06-02 14:18:25 +08:00
|
|
|
"github.com/anacrolix/torrent/common"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2020-06-01 16:41:21 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2020-06-01 16:25:45 +08:00
|
|
|
"github.com/anacrolix/torrent/segments"
|
|
|
|
"github.com/anacrolix/torrent/webseed"
|
2020-07-10 11:18:33 +08:00
|
|
|
"github.com/pkg/errors"
|
2020-05-30 15:52:27 +08:00
|
|
|
)
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
type webseedPeer struct {
|
2020-06-02 11:54:26 +08:00
|
|
|
client webseed.Client
|
|
|
|
requests map[request]webseed.Request
|
|
|
|
peer peer
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
var _ peerImpl = (*webseedPeer)(nil)
|
2020-06-01 16:25:45 +08:00
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) String() string {
|
2020-06-04 09:50:20 +08:00
|
|
|
return fmt.Sprintf("webseed peer for %q", ws.client.Url)
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
|
2020-06-02 14:18:25 +08:00
|
|
|
ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
|
|
|
|
ws.client.Info = info
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) _postCancel(r request) {
|
2020-06-02 15:41:59 +08:00
|
|
|
ws.cancel(r)
|
2020-05-30 15:52:27 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) writeInterested(interested bool) bool {
|
2020-05-30 15:52:27 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) cancel(r request) bool {
|
2020-06-02 11:54:26 +08:00
|
|
|
ws.requests[r].Cancel()
|
2020-06-01 16:25:45 +08:00
|
|
|
return true
|
2020-05-30 15:52:27 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) intoSpec(r request) webseed.RequestSpec {
|
2020-06-02 11:54:26 +08:00
|
|
|
return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) request(r request) bool {
|
2020-06-02 11:54:26 +08:00
|
|
|
webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
|
|
|
|
ws.requests[r] = webseedRequest
|
|
|
|
go ws.requestResultHandler(r, webseedRequest)
|
2020-06-01 16:25:45 +08:00
|
|
|
return true
|
2020-05-30 15:52:27 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) connectionFlags() string {
|
2020-05-30 15:52:27 +08:00
|
|
|
return "WS"
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
// TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too.
|
|
|
|
func (ws *webseedPeer) drop() {}
|
2020-05-30 15:52:27 +08:00
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) updateRequests() {
|
2020-05-30 15:52:27 +08:00
|
|
|
ws.peer.doRequestState()
|
|
|
|
}
|
2020-05-31 11:09:56 +08:00
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) _close() {}
|
2020-06-01 16:41:21 +08:00
|
|
|
|
2020-06-04 09:58:18 +08:00
|
|
|
func (ws *webseedPeer) requestResultHandler(r request, webseedRequest webseed.Request) {
|
2020-06-02 14:41:49 +08:00
|
|
|
result := <-webseedRequest.Result
|
2020-06-02 11:54:26 +08:00
|
|
|
ws.peer.t.cl.lock()
|
2020-06-02 14:41:49 +08:00
|
|
|
defer ws.peer.t.cl.unlock()
|
|
|
|
if result.Err != nil {
|
2020-06-04 09:50:20 +08:00
|
|
|
ws.peer.logger.Printf("request %v rejected: %v", r, result.Err)
|
2020-07-10 11:18:33 +08:00
|
|
|
if strings.Contains(errors.Cause(result.Err).Error(), "unsupported protocol scheme") {
|
|
|
|
ws.peer.close()
|
|
|
|
} else {
|
|
|
|
ws.peer.remoteRejectedRequest(r)
|
|
|
|
}
|
2020-06-02 14:41:49 +08: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 16:41:21 +08:00
|
|
|
}
|
|
|
|
}
|