Separate Conn and PrivateConn tracker interfaces

This commit is contained in:
Justin Li 2014-07-16 12:33:16 -04:00
parent 8ef67ff99f
commit 3ceaf72034
5 changed files with 39 additions and 13 deletions

View file

@ -69,7 +69,6 @@ type Pool interface {
// Conn represents a connection to the data store that can be used
// to make reads/writes.
type Conn interface {
// Torrent interactions
FindTorrent(infohash string) (*models.Torrent, error)
PutTorrent(t *models.Torrent) error
DeleteTorrent(infohash string) error
@ -79,13 +78,14 @@ type Conn interface {
DeleteLeecher(infohash, peerkey string) error
PutSeeder(infohash string, p *models.Peer) error
DeleteSeeder(infohash, peerkey string) error
}
// User interactions
// PrivateConn represents a connection that can service queries for private trackers.
type PrivateConn interface {
FindUser(passkey string) (*models.User, error)
PutUser(u *models.User) error
DeleteUser(passkey string) error
// Whitelist interactions
FindClient(clientID string) error
PutClient(clientID string) error
DeleteClient(clientID string) error

View file

@ -30,7 +30,7 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro
}
if t.cfg.Whitelist {
err = conn.FindClient(ann.ClientID())
err = conn.(tracker.PrivateConn).FindClient(ann.ClientID())
if err == tracker.ErrClientUnapproved {
fail(w, r, err)
return http.StatusOK, nil
@ -41,7 +41,7 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro
var user *models.User
if t.cfg.Private {
user, err = conn.FindUser(ann.Passkey)
user, err = conn.(tracker.PrivateConn).FindUser(ann.Passkey)
if err == tracker.ErrUserDNE {
fail(w, r, err)
return http.StatusOK, nil

View file

@ -9,6 +9,7 @@ import (
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/drivers/tracker"
"github.com/chihaya/chihaya/models"
)
@ -75,7 +76,7 @@ func loadTestData(tkr *Tracker) error {
}
for i, passkey := range users {
err = conn.PutUser(&models.User{
err = conn.(tracker.PrivateConn).PutUser(&models.User{
ID: uint64(i + 1),
Passkey: passkey,
})
@ -85,7 +86,7 @@ func loadTestData(tkr *Tracker) error {
}
}
err = conn.PutClient("TR2820")
err = conn.(tracker.PrivateConn).PutClient("TR2820")
if err != nil {
return err
}

View file

@ -102,11 +102,16 @@ func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httproute
}
func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.pool.Get()
base, err := t.pool.Get()
if err != nil {
return http.StatusInternalServerError, err
}
conn, private := base.(tracker.PrivateConn)
if !private {
return http.StatusNotFound, nil
}
user, err := conn.FindUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
return http.StatusNotFound, err
@ -136,11 +141,16 @@ func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.P
return http.StatusBadRequest, err
}
conn, err := t.pool.Get()
base, err := t.pool.Get()
if err != nil {
return http.StatusInternalServerError, err
}
conn, private := base.(tracker.PrivateConn)
if !private {
return http.StatusNotFound, nil
}
err = conn.PutUser(&user)
if err != nil {
return http.StatusInternalServerError, err
@ -150,11 +160,16 @@ 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, error) {
conn, err := t.pool.Get()
base, err := t.pool.Get()
if err != nil {
return http.StatusInternalServerError, err
}
conn, private := base.(tracker.PrivateConn)
if !private {
return http.StatusNotFound, nil
}
err = conn.DeleteUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
return http.StatusNotFound, err
@ -166,11 +181,16 @@ func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.P
}
func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.pool.Get()
base, err := t.pool.Get()
if err != nil {
return http.StatusInternalServerError, err
}
conn, private := base.(tracker.PrivateConn)
if !private {
return http.StatusNotFound, nil
}
err = conn.PutClient(p.ByName("clientID"))
if err != nil {
return http.StatusInternalServerError, err
@ -180,11 +200,16 @@ func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter
}
func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := t.pool.Get()
base, err := t.pool.Get()
if err != nil {
return http.StatusInternalServerError, err
}
conn, private := base.(tracker.PrivateConn)
if !private {
return http.StatusNotFound, nil
}
err = conn.DeleteClient(p.ByName("clientID"))
if err != nil {
return http.StatusInternalServerError, err

View file

@ -29,7 +29,7 @@ func (t *Tracker) ServeScrape(w http.ResponseWriter, r *http.Request, p httprout
}
if t.cfg.Private {
_, err = conn.FindUser(scrape.Passkey)
_, err = conn.(tracker.PrivateConn).FindUser(scrape.Passkey)
if err == tracker.ErrUserDNE {
fail(w, r, err)
return http.StatusOK, nil