diff --git a/tracker/announce.go b/tracker/announce.go index 6734a15..d8d3d61 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -148,13 +148,22 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t } case ann.Event == "completed": - err = c.IncrementSnatches(t.Infohash) + snatched = true + + err = c.IncrementTorrentSnatches(t.Infohash) if err != nil { return } - snatched = true t.Snatches++ + if ann.Config.Private { + err = c.IncrementUserSnatches(u.Passkey) + if err != nil { + return + } + u.Snatches++ + } + if t.InLeecherPool(p) { err = leecherFinished(c, t.Infohash, p) if err != nil { diff --git a/tracker/conn.go b/tracker/conn.go index 971f90f..5fe6c6e 100644 --- a/tracker/conn.go +++ b/tracker/conn.go @@ -71,15 +71,17 @@ type Conn interface { Close() error // Torrent interactions + TouchTorrent(infohash string) error FindTorrent(infohash string) (*models.Torrent, error) PutTorrent(t *models.Torrent) error DeleteTorrent(infohash string) error - IncrementSnatches(infohash string) error - TouchTorrent(infohash string) error + IncrementTorrentSnatches(infohash string) error + PutLeecher(infohash string, p *models.Peer) error - DeleteLeecher(infohash, peerkey string) error + DeleteLeecher(infohash, peerID string) error + PutSeeder(infohash string, p *models.Peer) error - DeleteSeeder(infohash, peerkey string) error + DeleteSeeder(infohash, peerID string) error PurgeInactiveTorrent(infohash string) error PurgeInactivePeers(purgeEmptyTorrents bool, before time.Time) error @@ -88,6 +90,7 @@ type Conn interface { FindUser(passkey string) (*models.User, error) PutUser(u *models.User) error DeleteUser(passkey string) error + IncrementUserSnatches(passkey string) error // Whitelist interactions FindClient(clientID string) error diff --git a/tracker/memory/conn.go b/tracker/memory/conn.go index 694a5e3..1a7caba 100644 --- a/tracker/memory/conn.go +++ b/tracker/memory/conn.go @@ -54,12 +54,12 @@ func (c *Conn) FindClient(peerID string) error { return nil } -func (c *Conn) IncrementSnatches(infohash string) error { +func (c *Conn) IncrementTorrentSnatches(infohash string) error { c.torrentsM.Lock() defer c.torrentsM.Unlock() - t, ok := c.torrents[infohash] - if !ok { + t, exists := c.torrents[infohash] + if !exists { return tracker.ErrTorrentDNE } t.Snatches++ @@ -67,6 +67,19 @@ func (c *Conn) IncrementSnatches(infohash string) error { return nil } +func (c *Conn) IncrementUserSnatches(userID string) error { + c.usersM.Lock() + defer c.usersM.Unlock() + + u, exists := c.users[userID] + if !exists { + return tracker.ErrUserDNE + } + u.Snatches++ + + return nil +} + func (c *Conn) TouchTorrent(infohash string) error { c.torrentsM.Lock() defer c.torrentsM.Unlock()