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
profile string
configPath string
whitelistPath string
)
func init() {
flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
flag.StringVar(&profile, "profile", "", "if non-empty, path to write profiling data")
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
@ -80,13 +78,6 @@ func Boot() {
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)
glog.Info("Gracefully shut down")
}

View file

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

View file

@ -9,9 +9,8 @@
"driver": "noop"
},
"private_tracker": false,
"freeleech": false,
"whitelist": false,
"private_enabled": false,
"freeleech_enabled": false,
"purge_inactive_torrents": true,
"announce": "30m",
@ -29,6 +28,21 @@
"stats_buffer_size": 0,
"include_mem_stats": true,
"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) {
cfg := config.DefaultConfig
cfg.Private = true
cfg.PrivateEnabled = true
tkr, err := tracker.New(&cfg)
if err != nil {

View file

@ -66,7 +66,7 @@ func makeHandler(handler ResponseHandler) httprouter.Handle {
func newRouter(s *Server) *httprouter.Router {
r := httprouter.New()
if s.config.Private {
if s.config.PrivateEnabled {
r.GET("/users/:passkey/announce", makeHandler(s.serveAnnounce))
r.GET("/users/:passkey/scrape", makeHandler(s.serveScrape))
@ -77,7 +77,7 @@ func newRouter(s *Server) *httprouter.Router {
r.GET("/scrape", makeHandler(s.serveScrape))
}
if s.config.Whitelist {
if s.config.ClientWhitelistEnabled {
r.PUT("/clients/:clientID", makeHandler(s.putClient))
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()
if tkr.cfg.Whitelist {
if tkr.cfg.ClientWhitelistEnabled {
if err = conn.FindClient(ann.ClientID()); err != nil {
return err
}
}
var user *models.User
if tkr.cfg.Private {
if tkr.cfg.PrivateEnabled {
if user, err = conn.FindUser(ann.Passkey); err != nil {
return err
}
@ -37,7 +37,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
var torrent *models.Torrent
torrent, err = conn.FindTorrent(ann.Infohash)
switch {
case !tkr.cfg.Private && err == models.ErrTorrentDNE:
case !tkr.cfg.PrivateEnabled && err == models.ErrTorrentDNE:
torrent = &models.Torrent{
Infohash: ann.Infohash,
Seeders: models.PeerMap{},
@ -66,7 +66,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
return err
}
if tkr.cfg.Private {
if tkr.cfg.PrivateEnabled {
delta := models.NewAnnounceDelta(ann, peer, user, torrent, created, snatched)
err = tkr.backend.RecordAnnounce(delta)
if err != nil {
@ -160,7 +160,7 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
}
t.Snatches++
if ann.Config.Private {
if ann.Config.PrivateEnabled {
err = c.IncrementUserSnatches(u.Passkey)
if err != nil {
return

View file

@ -214,7 +214,7 @@ func NewAnnounceDelta(a *Announce, p *Peer, u *User, t *Torrent, created, snatch
rawDeltaDown uint64
)
if !a.Config.Freeleech {
if !a.Config.FreeleechEnabled {
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()
if tkr.cfg.Private {
if tkr.cfg.PrivateEnabled {
if _, err = conn.FindUser(scrape.Passkey); err != nil {
return err
}

View file

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