From 79a6c79067a3f1af44b39c464c3edb1ea15ac1d0 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 24 Jun 2013 00:19:24 -0400 Subject: [PATCH] clients in whitelist --- config/config.go | 24 ++++++++++++------------ example/config.json | 30 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/config/config.go b/config/config.go index bbbb333..f10bf65 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,11 @@ func (d *Duration) UnmarshalJSON(b []byte) error { return err } +type Client struct { + Name string `json:"name"` + PeerID string `json:"peer_id"` +} + type Storage struct { Driver string `json:"driver"` Protocol string `json:"protocol"` @@ -47,7 +52,7 @@ type Config struct { MinAnnounce Duration `json:"min_announce"` ReadTimeout Duration `json:"read_timeout"` - Whitelist []string `json:"whitelist"` + Whitelist []Client `json:"whitelist"` } func New(path string) (*Config, error) { @@ -66,18 +71,13 @@ func New(path string) (*Config, error) { return conf, nil } -func (c *Config) Whitelisted(peerId string) bool { - var ( - widLen int - matched bool - ) - - for _, whitelistedId := range c.Whitelist { - widLen = len(whitelistedId) - if widLen <= len(peerId) { +func (c *Config) Whitelisted(peerId string) (matched bool) { + for _, client := range c.Whitelist { + length := len(client.PeerID) + if length <= len(peerId) { matched = true - for i := 0; i < widLen; i++ { - if peerId[i] != whitelistedId[i] { + for i := 0; i < length; i++ { + if peerId[i] != client.PeerID[i] { matched = false break } diff --git a/example/config.json b/example/config.json index 79d82d6..be6a111 100644 --- a/example/config.json +++ b/example/config.json @@ -1,20 +1,24 @@ { - "addr": ":34000", - "storage": { - "driver": "redis", - "addr": "127.0.0.1:6379", - "user": "root", - "pass": "", - }, + "addr": ":34000", + "storage": { + "driver": "redis", + "addr": "127.0.0.1:6379", + "user": "root", + "pass": "" + }, - "private": true, - "freeleech": false, + "private": true, + "freeleech": false, - "announce": "30m", - "min_announce": "15m", - "read_timeout": "20s", + "announce": "30m", + "min_announce": "15m", + "read_timeout": "20s", - "whitelist": [], + "whitelist": [ + { "name": "Azureus 2.5.x", "peer_id": "-AZ25" }, + { "name": "Azureus 3.0.x", "peer_id": "-AZ30" }, + { "name": "btgdaemon 0.9", "peer_id": "-BG09" } + ] }