diff --git a/drivers/tracker/mock/conn.go b/drivers/tracker/mock/conn.go index 9fefd2f..8fb3b6b 100644 --- a/drivers/tracker/mock/conn.go +++ b/drivers/tracker/mock/conn.go @@ -36,7 +36,7 @@ func (c *Conn) FindTorrent(infohash string) (*models.Torrent, error) { return &*torrent, nil } -func (c *Conn) ClientWhitelisted(peerID string) error { +func (c *Conn) FindClient(peerID string) error { c.whitelistM.RLock() defer c.whitelistM.RUnlock() @@ -191,11 +191,11 @@ func (c *Conn) AddTorrent(t *models.Torrent) error { return nil } -func (c *Conn) RemoveTorrent(t *models.Torrent) error { +func (c *Conn) RemoveTorrent(infohash string) error { c.torrentsM.Lock() defer c.torrentsM.Unlock() - delete(c.torrents, t.Infohash) + delete(c.torrents, infohash) return nil } @@ -210,16 +210,16 @@ func (c *Conn) AddUser(u *models.User) error { return nil } -func (c *Conn) RemoveUser(u *models.User) error { +func (c *Conn) RemoveUser(passkey string) error { c.usersM.Lock() defer c.usersM.Unlock() - delete(c.users, u.Passkey) + delete(c.users, passkey) return nil } -func (c *Conn) WhitelistClient(peerID string) error { +func (c *Conn) AddClient(peerID string) error { c.whitelistM.Lock() defer c.whitelistM.Unlock() @@ -228,7 +228,7 @@ func (c *Conn) WhitelistClient(peerID string) error { return nil } -func (c *Conn) UnWhitelistClient(peerID string) error { +func (c *Conn) RemoveClient(peerID string) error { c.whitelistM.Lock() defer c.whitelistM.Unlock() diff --git a/drivers/tracker/tracker.go b/drivers/tracker/tracker.go index 65a3db4..af0385a 100644 --- a/drivers/tracker/tracker.go +++ b/drivers/tracker/tracker.go @@ -69,28 +69,30 @@ type Pool interface { // Conn represents a connection to the data store that can be used // to make reads/writes. type Conn interface { - // Reads - FindUser(passkey string) (*models.User, error) + // Torrent interactions FindTorrent(infohash string) (*models.Torrent, error) - ClientWhitelisted(clientID string) error - - // Writes + AddTorrent(t *models.Torrent) error + RemoveTorrent(infohash string) error IncrementSnatches(t *models.Torrent) error MarkActive(t *models.Torrent) error - AddLeecher(t *models.Torrent, p *models.Peer) error - AddSeeder(t *models.Torrent, p *models.Peer) error - RemoveLeecher(t *models.Torrent, p *models.Peer) error - RemoveSeeder(t *models.Torrent, p *models.Peer) error - SetLeecher(t *models.Torrent, p *models.Peer) error - SetSeeder(t *models.Torrent, p *models.Peer) error - // Priming / Testing - AddTorrent(t *models.Torrent) error - RemoveTorrent(t *models.Torrent) error + AddLeecher(t *models.Torrent, p *models.Peer) error + SetLeecher(t *models.Torrent, p *models.Peer) error + RemoveLeecher(t *models.Torrent, p *models.Peer) error + + AddSeeder(t *models.Torrent, p *models.Peer) error + SetSeeder(t *models.Torrent, p *models.Peer) error + RemoveSeeder(t *models.Torrent, p *models.Peer) error + + // User interactions + FindUser(passkey string) (*models.User, error) AddUser(u *models.User) error - RemoveUser(u *models.User) error - WhitelistClient(clientID string) error - UnWhitelistClient(clientID string) error + RemoveUser(passkey string) error + + // Whitelist interactions + FindClient(clientID string) error + AddClient(clientID string) error + RemoveClient(clientID string) error } // LeecherFinished moves a peer from the leeching pool to the seeder pool. diff --git a/http/announce.go b/http/announce.go index 501776c..68c34b8 100644 --- a/http/announce.go +++ b/http/announce.go @@ -31,7 +31,7 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro } if t.cfg.Whitelist { - err = conn.ClientWhitelisted(ann.ClientID()) + err = conn.FindClient(ann.ClientID()) if err == tracker.ErrClientUnapproved { fail(w, r, err) return http.StatusOK diff --git a/http/announce_test.go b/http/announce_test.go index bb9befd..8628db3 100644 --- a/http/announce_test.go +++ b/http/announce_test.go @@ -54,7 +54,7 @@ func loadTestData(tkr *Tracker) (err error) { return } - err = conn.WhitelistClient("TR2820") + err = conn.AddClient("TR2820") if err != nil { return } diff --git a/http/api.go b/http/api.go index 892071f..5317fd0 100644 --- a/http/api.go +++ b/http/api.go @@ -40,6 +40,18 @@ func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httproute } func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + conn, err := t.tp.Get() + if err != nil { + return http.StatusInternalServerError + } + + err = conn.RemoveTorrent(p.ByName("infohash")) + if err == tracker.ErrTorrentDNE { + return http.StatusNotFound + } else if err != nil { + return http.StatusInternalServerError + } + return http.StatusOK } @@ -70,6 +82,18 @@ func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.P } func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + conn, err := t.tp.Get() + if err != nil { + return http.StatusInternalServerError + } + + err = conn.RemoveUser(p.ByName("passkey")) + if err == tracker.ErrUserDNE { + return http.StatusNotFound + } else if err != nil { + return http.StatusInternalServerError + } + return http.StatusOK } @@ -79,7 +103,7 @@ func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter return http.StatusInternalServerError } - err = conn.WhitelistClient(p.ByName("clientID")) + err = conn.FindClient(p.ByName("clientID")) if err != nil { return http.StatusInternalServerError } @@ -93,7 +117,7 @@ func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter return http.StatusInternalServerError } - err = conn.UnWhitelistClient(p.ByName("clientID")) + err = conn.RemoveClient(p.ByName("clientID")) if err != nil { return http.StatusInternalServerError }