diff --git a/client.go b/client.go index 5ca405a5..735d58ca 100644 --- a/client.go +++ b/client.go @@ -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() diff --git a/webtorrent/tracker_client.go b/webtorrent/tracker_client.go index c2052b99..5e56ca23 100644 --- a/webtorrent/tracker_client.go +++ b/webtorrent/tracker_client.go @@ -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. diff --git a/wstracker.go b/wstracker.go index 8c71e514..4e83ca5f 100644 --- a/wstracker.go +++ b/wstracker.go @@ -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