Fix crash when announcing unloaded torrent to websocket

This commit is contained in:
Matt Joiner 2020-05-02 18:36:32 +10:00
parent b4e10e00a9
commit f36fc10079
3 changed files with 12 additions and 5 deletions

View File

@ -247,10 +247,14 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
cl.websocketTrackers = websocketTrackers{
PeerId: cl.peerID,
Logger: cl.logger,
GetAnnounceRequest: func(event tracker.AnnounceEvent, infoHash [20]byte) tracker.AnnounceRequest {
GetAnnounceRequest: func(event tracker.AnnounceEvent, infoHash [20]byte) (tracker.AnnounceRequest, error) {
cl.lock()
defer cl.unlock()
return cl.torrents[infoHash].announceRequest(event)
t, ok := cl.torrents[infoHash]
if !ok {
return tracker.AnnounceRequest{}, errors.New("torrent not tracked by client")
}
return t.announceRequest(event), nil
},
OnConn: func(dc datachannel.ReadWriteCloser, dcc webtorrent.DataChannelContext) {
cl.lock()

View File

@ -24,7 +24,7 @@ type TrackerClientStats struct {
// Client represents the webtorrent client
type TrackerClient struct {
Url string
GetAnnounceRequest func(_ tracker.AnnounceEvent, infoHash [20]byte) tracker.AnnounceRequest
GetAnnounceRequest func(_ tracker.AnnounceEvent, infoHash [20]byte) (tracker.AnnounceRequest, error)
PeerId [20]byte
OnConn onDataChannelOpen
Logger log.Logger
@ -135,7 +135,10 @@ func (tc *TrackerClient) Announce(event tracker.AnnounceEvent, infoHash [20]byte
return fmt.Errorf("creating offer: %w", err)
}
request := tc.GetAnnounceRequest(event, infoHash)
request, err := tc.GetAnnounceRequest(event, infoHash)
if err != nil {
return fmt.Errorf("getting announce parameters: %w", err)
}
req := AnnounceRequest{
Numwant: 1, // If higher we need to create equal amount of offers.

View File

@ -33,7 +33,7 @@ type refCountedWebtorrentTrackerClient struct {
type websocketTrackers struct {
PeerId [20]byte
Logger log.Logger
GetAnnounceRequest func(event tracker.AnnounceEvent, infoHash [20]byte) tracker.AnnounceRequest
GetAnnounceRequest func(event tracker.AnnounceEvent, infoHash [20]byte) (tracker.AnnounceRequest, error)
OnConn func(datachannel.ReadWriteCloser, webtorrent.DataChannelContext)
mu sync.Mutex
clients map[string]*refCountedWebtorrentTrackerClient