From 64d08ca16b89c2d5ab13c277945884114a4df40e Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Fri, 25 Jul 2014 01:48:30 -0400 Subject: [PATCH] add whitelist flag --- chihaya.go | 15 ++++++++++++--- example_config.json | 22 ++++++++++++++++------ example_whitelist.json | 14 ++++++++++++++ tracker/tracker.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 example_whitelist.json diff --git a/chihaya.go b/chihaya.go index 2d5dd2a..260b035 100644 --- a/chihaya.go +++ b/chihaya.go @@ -23,15 +23,17 @@ import ( ) var ( - maxProcs int - profile string - configPath string + 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 @@ -78,6 +80,13 @@ 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") } diff --git a/example_config.json b/example_config.json index 83e8912..f54e8a0 100644 --- a/example_config.json +++ b/example_config.json @@ -1,5 +1,4 @@ { - "network": "tcp", "addr": ":6881", "tracker": { @@ -11,14 +10,25 @@ }, "private_tracker": false, - "freeleech": false, - "whitelist": false, + "freelech": false, + "whitelist": true, "purge_inactive_torrents": true, - "allow_ip_spoofing": true, - "dual_stacked_peers": true, "announce": "30m", "min_announce": "15m", "request_timeout": "10s", - "default_num_want": 50 + "default_num_want": 50, + + "allow_ip_spoofing": true, + "dual_stacked_peers": true, + "real_ip_header": "", + "preferred_subnet": false, + "preferred_ipv4_subnet": 0, + "preferred_ipv6_subnet": 0, + + "stats_buffer_size": 0, + "include_mem_stats": true, + "verbose_mem_stats": true, + "mem_stats_interval": "30s" + } diff --git a/example_whitelist.json b/example_whitelist.json new file mode 100644 index 0000000..856a0c3 --- /dev/null +++ b/example_whitelist.json @@ -0,0 +1,14 @@ +[ + "UT340-", + "AZ2500", + "exbc0J", + "FUTB0L", + "XBT054", + "OP1011", + "ML2.7.", + "BOWA0C", + "Q1-0-0", + "Q1-10-", + "346---", + "QVOD00" +] diff --git a/tracker/tracker.go b/tracker/tracker.go index 57ba2a3..3716aac 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -7,6 +7,8 @@ package tracker import ( + "encoding/json" + "os" "time" "github.com/golang/glog" @@ -66,6 +68,40 @@ 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 + } + + conn, err := tkr.Pool.Get() + if err != nil { + return err + } + + for _, client := range clients { + err = conn.PutClient(client) + if err != nil { + return err + } + } + + return nil +} + // Writer serializes a tracker's responses, and is implemented for each // response transport used by the tracker. //