2013-06-22 03:43:11 +02:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2013-06-24 04:34:13 +02:00
|
|
|
// Package config implements the configuration for a BitTorrent tracker
|
2013-06-22 01:31:32 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
func (d *Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.String())
|
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
|
|
var str string
|
|
|
|
err := json.Unmarshal(b, &str)
|
|
|
|
d.Duration, err = time.ParseDuration(str)
|
|
|
|
return err
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 06:19:24 +02:00
|
|
|
type Client struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
PeerID string `json:"peer_id"`
|
|
|
|
}
|
|
|
|
|
2013-06-22 01:31:32 +02:00
|
|
|
type Storage struct {
|
|
|
|
Driver string `json:"driver"`
|
2013-06-24 20:18:32 +02:00
|
|
|
Network string `json:"network`
|
2013-06-22 01:31:32 +02:00
|
|
|
Addr string `json:"addr"`
|
|
|
|
Username string `json:"user"`
|
|
|
|
Password string `json:"pass"`
|
2013-06-24 20:18:32 +02:00
|
|
|
Schema string `json:"schema,omitempty"`
|
|
|
|
Encoding string `json:"encoding,omitempty"`
|
2013-06-26 03:58:06 +02:00
|
|
|
Prefix string `json:"prefix,omitempty"`
|
2013-06-24 20:18:32 +02:00
|
|
|
|
|
|
|
ConnectTimeout *Duration `json:"conn_timeout,omitempty"`
|
|
|
|
ReadTimeout *Duration `json:"read_timeout,omitempty"`
|
|
|
|
WriteTimeout *Duration `json:"write_timeout,omitempty"`
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
type Config struct {
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
Storage Storage `json:"storage"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
Private bool `json:"private"`
|
|
|
|
Freeleech bool `json:"freeleech"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
Announce Duration `json:"announce"`
|
|
|
|
MinAnnounce Duration `json:"min_announce"`
|
|
|
|
ReadTimeout Duration `json:"read_timeout"`
|
|
|
|
|
2013-06-24 06:19:24 +02:00
|
|
|
Whitelist []Client `json:"whitelist"`
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(path string) (*Config, error) {
|
|
|
|
expandedPath := os.ExpandEnv(path)
|
|
|
|
f, err := os.Open(expandedPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
conf := &Config{}
|
|
|
|
err = json.NewDecoder(f).Decode(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conf, nil
|
|
|
|
}
|
2013-06-23 12:21:59 +02:00
|
|
|
|
2013-06-24 06:19:24 +02:00
|
|
|
func (c *Config) Whitelisted(peerId string) (matched bool) {
|
|
|
|
for _, client := range c.Whitelist {
|
|
|
|
length := len(client.PeerID)
|
|
|
|
if length <= len(peerId) {
|
2013-06-23 12:21:59 +02:00
|
|
|
matched = true
|
2013-06-24 06:19:24 +02:00
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
if peerId[i] != client.PeerID[i] {
|
2013-06-23 12:21:59 +02:00
|
|
|
matched = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if matched {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|