move whitelist into cfg

This commit is contained in:
Jimmy Zelinskie 2014-07-25 16:58:26 -04:00
parent cde8251bd3
commit ffb3c57181
10 changed files with 50 additions and 69 deletions

View file

@ -26,14 +26,12 @@ var (
maxProcs int maxProcs int
profile string profile string
configPath string configPath string
whitelistPath string
) )
func init() { func init() {
flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads") flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
flag.StringVar(&profile, "profile", "", "if non-empty, path to write profiling data") flag.StringVar(&profile, "profile", "", "if non-empty, path to write profiling data")
flag.StringVar(&configPath, "config", "", "path to the configuration file") flag.StringVar(&configPath, "config", "", "path to the configuration file")
flag.StringVar(&whitelistPath, "whitelist", "", "path to a client whitelist")
} }
// Boot starts Chihaya. By exporting this function, anyone can import their own // Boot starts Chihaya. By exporting this function, anyone can import their own
@ -80,13 +78,6 @@ func Boot() {
glog.Fatal("New: ", err) glog.Fatal("New: ", err)
} }
if whitelistPath != "" {
err = tkr.LoadApprovedClients(whitelistPath)
if err != nil {
glog.Fatal("Failed to load whitelist: ", err.Error())
}
}
http.Serve(cfg, tkr) http.Serve(cfg, tkr)
glog.Info("Gracefully shut down") glog.Info("Gracefully shut down")
} }

View file

@ -65,9 +65,8 @@ type Config struct {
Tracker DriverConfig `json:"tracker"` Tracker DriverConfig `json:"tracker"`
Backend DriverConfig `json:"backend"` Backend DriverConfig `json:"backend"`
Private bool `json:"private_tracker"` PrivateEnabled bool `json:"private_enabled"`
Freeleech bool `json:"freeleech"` FreeleechEnabled bool `json:"freeleech_enabled"`
Whitelist bool `json:"whitelist"`
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"` PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
Announce Duration `json:"announce"` Announce Duration `json:"announce"`
@ -75,6 +74,9 @@ type Config struct {
RequestTimeout Duration `json:"request_timeout"` RequestTimeout Duration `json:"request_timeout"`
NumWantFallback int `json:"default_num_want"` NumWantFallback int `json:"default_num_want"`
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`
ClientWhitelist []string `json:"client_whitelist,omitempty"`
StatsConfig StatsConfig
NetConfig NetConfig
} }
@ -91,9 +93,8 @@ var DefaultConfig = Config{
Name: "noop", Name: "noop",
}, },
Private: false, PrivateEnabled: false,
Freeleech: false, FreeleechEnabled: false,
Whitelist: false,
PurgeInactiveTorrents: true, PurgeInactiveTorrents: true,
Announce: Duration{30 * time.Minute}, Announce: Duration{30 * time.Minute},
@ -113,6 +114,8 @@ var DefaultConfig = Config{
AllowIPSpoofing: true, AllowIPSpoofing: true,
DualStackedPeers: true, DualStackedPeers: true,
}, },
ClientWhitelistEnabled: false,
} }
// Open is a shortcut to open a file, read it, and generate a Config. // Open is a shortcut to open a file, read it, and generate a Config.

View file

@ -9,9 +9,8 @@
"driver": "noop" "driver": "noop"
}, },
"private_tracker": false, "private_enabled": false,
"freeleech": false, "freeleech_enabled": false,
"whitelist": false,
"purge_inactive_torrents": true, "purge_inactive_torrents": true,
"announce": "30m", "announce": "30m",
@ -29,6 +28,21 @@
"stats_buffer_size": 0, "stats_buffer_size": 0,
"include_mem_stats": true, "include_mem_stats": true,
"verbose_mem_stats": false, "verbose_mem_stats": false,
"mem_stats_interval": "5s" "mem_stats_interval": "5s",
"client_whitelist_enabled": false,
"client_whitelist": [
"UT340-",
"AZ2500",
"exbc0J",
"FUTB0L",
"XBT054",
"OP1011",
"ML2.7.",
"BOWA0C",
"Q1-0-0",
"Q1-10-",
"346---",
"QVOD00"
]
} }

View file

@ -1,14 +0,0 @@
[
"UT340-",
"AZ2500",
"exbc0J",
"FUTB0L",
"XBT054",
"OP1011",
"ML2.7.",
"BOWA0C",
"Q1-0-0",
"Q1-10-",
"346---",
"QVOD00"
]

View file

@ -124,7 +124,7 @@ func TestStalePeerPurging(t *testing.T) {
func TestPrivateAnnounce(t *testing.T) { func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig cfg := config.DefaultConfig
cfg.Private = true cfg.PrivateEnabled = true
tkr, err := tracker.New(&cfg) tkr, err := tracker.New(&cfg)
if err != nil { if err != nil {

View file

@ -66,7 +66,7 @@ func makeHandler(handler ResponseHandler) httprouter.Handle {
func newRouter(s *Server) *httprouter.Router { func newRouter(s *Server) *httprouter.Router {
r := httprouter.New() r := httprouter.New()
if s.config.Private { if s.config.PrivateEnabled {
r.GET("/users/:passkey/announce", makeHandler(s.serveAnnounce)) r.GET("/users/:passkey/announce", makeHandler(s.serveAnnounce))
r.GET("/users/:passkey/scrape", makeHandler(s.serveScrape)) r.GET("/users/:passkey/scrape", makeHandler(s.serveScrape))
@ -77,7 +77,7 @@ func newRouter(s *Server) *httprouter.Router {
r.GET("/scrape", makeHandler(s.serveScrape)) r.GET("/scrape", makeHandler(s.serveScrape))
} }
if s.config.Whitelist { if s.config.ClientWhitelistEnabled {
r.PUT("/clients/:clientID", makeHandler(s.putClient)) r.PUT("/clients/:clientID", makeHandler(s.putClient))
r.DELETE("/clients/:clientID", makeHandler(s.delClient)) r.DELETE("/clients/:clientID", makeHandler(s.delClient))
} }

View file

@ -21,14 +21,14 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
defer conn.Close() defer conn.Close()
if tkr.cfg.Whitelist { if tkr.cfg.ClientWhitelistEnabled {
if err = conn.FindClient(ann.ClientID()); err != nil { if err = conn.FindClient(ann.ClientID()); err != nil {
return err return err
} }
} }
var user *models.User var user *models.User
if tkr.cfg.Private { if tkr.cfg.PrivateEnabled {
if user, err = conn.FindUser(ann.Passkey); err != nil { if user, err = conn.FindUser(ann.Passkey); err != nil {
return err return err
} }
@ -37,7 +37,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
var torrent *models.Torrent var torrent *models.Torrent
torrent, err = conn.FindTorrent(ann.Infohash) torrent, err = conn.FindTorrent(ann.Infohash)
switch { switch {
case !tkr.cfg.Private && err == models.ErrTorrentDNE: case !tkr.cfg.PrivateEnabled && err == models.ErrTorrentDNE:
torrent = &models.Torrent{ torrent = &models.Torrent{
Infohash: ann.Infohash, Infohash: ann.Infohash,
Seeders: models.PeerMap{}, Seeders: models.PeerMap{},
@ -66,7 +66,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
return err return err
} }
if tkr.cfg.Private { if tkr.cfg.PrivateEnabled {
delta := models.NewAnnounceDelta(ann, peer, user, torrent, created, snatched) delta := models.NewAnnounceDelta(ann, peer, user, torrent, created, snatched)
err = tkr.backend.RecordAnnounce(delta) err = tkr.backend.RecordAnnounce(delta)
if err != nil { if err != nil {
@ -160,7 +160,7 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
} }
t.Snatches++ t.Snatches++
if ann.Config.Private { if ann.Config.PrivateEnabled {
err = c.IncrementUserSnatches(u.Passkey) err = c.IncrementUserSnatches(u.Passkey)
if err != nil { if err != nil {
return return

View file

@ -214,7 +214,7 @@ func NewAnnounceDelta(a *Announce, p *Peer, u *User, t *Torrent, created, snatch
rawDeltaDown uint64 rawDeltaDown uint64
) )
if !a.Config.Freeleech { if !a.Config.FreeleechEnabled {
rawDeltaDown = p.Downloaded - a.Downloaded rawDeltaDown = p.Downloaded - a.Downloaded
} }

View file

@ -16,7 +16,7 @@ func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) (err error) {
defer conn.Close() defer conn.Close()
if tkr.cfg.Private { if tkr.cfg.PrivateEnabled {
if _, err = conn.FindUser(scrape.Passkey); err != nil { if _, err = conn.FindUser(scrape.Passkey); err != nil {
return err return err
} }

View file

@ -7,8 +7,6 @@
package tracker package tracker
import ( import (
"encoding/json"
"os"
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
@ -46,11 +44,17 @@ func New(cfg *config.Config) (*Tracker, error) {
cfg.Announce.Duration, cfg.Announce.Duration,
) )
return &Tracker{ tkr := &Tracker{
cfg: cfg, cfg: cfg,
Pool: pool, Pool: pool,
backend: bc, backend: bc,
}, nil }
if cfg.ClientWhitelistEnabled {
tkr.LoadApprovedClients(cfg.ClientWhitelist)
}
return tkr, nil
} }
// Close gracefully shutdowns a Tracker by closing any database connections. // Close gracefully shutdowns a Tracker by closing any database connections.
@ -68,25 +72,8 @@ func (tkr *Tracker) Close() (err error) {
return return
} }
// LoadApprovedClients takes a path to a JSON file containing a list of clients // LoadApprovedClients loads a list of client IDs into the tracker's storage.
// and inserts them into the tracker's data store. func (tkr *Tracker) LoadApprovedClients(clients []string) error {
func (tkr *Tracker) LoadApprovedClients(path string) error {
if path == "" {
return nil
}
f, err := os.Open(os.ExpandEnv(path))
if err != nil {
return err
}
defer f.Close()
var clients []string
err = json.NewDecoder(f).Decode(&clients)
if err != nil {
return err
}
conn, err := tkr.Pool.Get() conn, err := tkr.Pool.Get()
if err != nil { if err != nil {
return err return err