2015-01-01 18:02:25 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2013-06-22 03:43:11 +02:00
|
|
|
// 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 (
|
2013-11-24 06:49:20 +01:00
|
|
|
"encoding/json"
|
2014-07-15 02:36:49 +02:00
|
|
|
"errors"
|
2013-11-24 06:49:20 +01:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
2014-07-15 05:13:35 +02:00
|
|
|
// ErrMissingRequiredParam is used by drivers to indicate that an entry required
|
|
|
|
// to be within the DriverConfig.Params map is not present.
|
2014-07-15 02:36:49 +02:00
|
|
|
var ErrMissingRequiredParam = errors.New("A parameter that was required by a driver is not present")
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// Duration wraps a time.Duration and adds JSON marshalling.
|
2014-07-03 23:37:13 +02:00
|
|
|
type Duration struct{ time.Duration }
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// MarshalJSON transforms a duration into JSON.
|
2013-06-23 12:21:59 +02:00
|
|
|
func (d *Duration) MarshalJSON() ([]byte, error) {
|
2013-11-24 06:49:20 +01:00
|
|
|
return json.Marshal(d.String())
|
2013-06-23 12:21:59 +02:00
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// UnmarshalJSON transform JSON into a Duration.
|
2013-06-23 12:21:59 +02:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2013-11-24 06:49:20 +01:00
|
|
|
var str string
|
|
|
|
err := json.Unmarshal(b, &str)
|
|
|
|
d.Duration, err = time.ParseDuration(str)
|
|
|
|
return err
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 09:59:30 +02:00
|
|
|
// DriverConfig is the configuration used to connect to a tracker.Driver or
|
|
|
|
// a backend.Driver.
|
|
|
|
type DriverConfig struct {
|
2014-07-03 23:37:13 +02:00
|
|
|
Name string `json:"driver"`
|
|
|
|
Params map[string]string `json:"params,omitempty"`
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
// SubnetConfig is the configuration used to specify if local peers should be
|
|
|
|
// given a preference when responding to an announce.
|
2014-09-24 05:00:50 +02:00
|
|
|
type SubnetConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
PreferredSubnet bool `json:"preferredSubnet,omitempty"`
|
|
|
|
PreferredIPv4Subnet int `json:"preferredIPv4Subnet,omitempty"`
|
|
|
|
PreferredIPv6Subnet int `json:"preferredIPv6Subnet,omitempty"`
|
2014-09-24 05:00:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-23 20:00:17 +02:00
|
|
|
// NetConfig is the configuration used to tune networking behaviour.
|
|
|
|
type NetConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
AllowIPSpoofing bool `json:"allowIPSpoofing"`
|
|
|
|
DualStackedPeers bool `json:"dualStackedPeers"`
|
|
|
|
RealIPHeader string `json:"realIPHeader"`
|
|
|
|
RespectAF bool `json:"respectAF"`
|
2014-09-24 05:00:50 +02:00
|
|
|
SubnetConfig
|
2014-07-23 20:00:17 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
// StatsConfig is the configuration used to record runtime statistics.
|
2014-07-23 23:25:01 +02:00
|
|
|
type StatsConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
BufferSize int `json:"statsBufferSize"`
|
|
|
|
IncludeMem bool `json:"includeMemStats"`
|
|
|
|
VerboseMem bool `json:"verboseMemStats"`
|
|
|
|
MemUpdateInterval Duration `json:"memStatsInterval"`
|
2014-07-23 23:25:01 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
// WhitelistConfig is the configuration used enable and store a whitelist of
|
|
|
|
// acceptable torrent client peer ID prefixes.
|
|
|
|
type WhitelistConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
ClientWhitelistEnabled bool `json:"clientWhitelistEnabled"`
|
|
|
|
ClientWhitelist []string `json:"clientWhitelist,omitempty"`
|
2014-09-24 18:53:35 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
// TrackerConfig is the configuration for tracker functionality.
|
|
|
|
type TrackerConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
CreateOnAnnounce bool `json:"createOnAnnounce"`
|
|
|
|
PrivateEnabled bool `json:"privateEnabled"`
|
|
|
|
FreeleechEnabled bool `json:"freeleechEnabled"`
|
|
|
|
PurgeInactiveTorrents bool `json:"purgeInactiveTorrents"`
|
2014-11-02 01:12:40 +01:00
|
|
|
Announce Duration `json:"announce"`
|
2015-10-25 21:53:23 +01:00
|
|
|
MinAnnounce Duration `json:"minAnnounce"`
|
|
|
|
ReapInterval Duration `json:"reapInterval"`
|
|
|
|
ReapRatio float64 `json:"reapRatio"`
|
|
|
|
NumWantFallback int `json:"defaultNumWant"`
|
|
|
|
TorrentMapShards int `json:"torrentMapShards"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
NetConfig
|
|
|
|
WhitelistConfig
|
|
|
|
}
|
2014-07-17 01:38:51 +02:00
|
|
|
|
2015-10-11 08:32:24 +02:00
|
|
|
// APIConfig is the configuration for an HTTP JSON API server.
|
|
|
|
type APIConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
ListenAddr string `json:"apiListenAddr"`
|
|
|
|
RequestTimeout Duration `json:"apiRequestTimeout"`
|
|
|
|
ReadTimeout Duration `json:"apiReadTimeout"`
|
|
|
|
WriteTimeout Duration `json:"apiWriteTimeout"`
|
|
|
|
ListenLimit int `json:"apiListenLimit"`
|
2015-10-11 08:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPConfig is the configuration for the HTTP protocol.
|
2014-11-02 01:12:40 +01:00
|
|
|
type HTTPConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
ListenAddr string `json:"httpListenAddr"`
|
|
|
|
RequestTimeout Duration `json:"httpRequestTimeout"`
|
|
|
|
ReadTimeout Duration `json:"httpReadTimeout"`
|
|
|
|
WriteTimeout Duration `json:"httpWriteTimeout"`
|
|
|
|
ListenLimit int `json:"httpListenLimit"`
|
2015-02-20 05:46:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-11 08:32:24 +02:00
|
|
|
// UDPConfig is the configuration for the UDP protocol.
|
2015-02-20 05:46:28 +01:00
|
|
|
type UDPConfig struct {
|
2015-10-25 21:53:23 +01:00
|
|
|
ListenAddr string `json:"udpListenAddr"`
|
|
|
|
ReadBufferSize int `json:"udpReadBufferSize"`
|
2014-11-02 01:12:40 +01:00
|
|
|
}
|
2014-07-25 22:58:26 +02:00
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
// Config is the global configuration for an instance of Chihaya.
|
|
|
|
type Config struct {
|
|
|
|
TrackerConfig
|
2015-10-11 08:32:24 +02:00
|
|
|
APIConfig
|
2014-11-02 01:12:40 +01:00
|
|
|
HTTPConfig
|
2015-02-20 05:46:28 +01:00
|
|
|
UDPConfig
|
2014-11-02 01:12:40 +01:00
|
|
|
DriverConfig
|
2014-07-23 23:25:01 +02:00
|
|
|
StatsConfig
|
2014-06-24 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 05:13:35 +02:00
|
|
|
// DefaultConfig is a configuration that can be used as a fallback value.
|
2014-07-02 03:40:29 +02:00
|
|
|
var DefaultConfig = Config{
|
2014-11-02 01:12:40 +01:00
|
|
|
TrackerConfig: TrackerConfig{
|
2015-03-24 01:02:13 +01:00
|
|
|
CreateOnAnnounce: true,
|
2014-11-02 01:12:40 +01:00
|
|
|
PrivateEnabled: false,
|
|
|
|
FreeleechEnabled: false,
|
|
|
|
PurgeInactiveTorrents: true,
|
|
|
|
Announce: Duration{30 * time.Minute},
|
|
|
|
MinAnnounce: Duration{15 * time.Minute},
|
2015-09-18 06:42:14 +02:00
|
|
|
ReapInterval: Duration{60 * time.Second},
|
|
|
|
ReapRatio: 1.25,
|
2014-11-02 01:12:40 +01:00
|
|
|
NumWantFallback: 50,
|
|
|
|
TorrentMapShards: 1,
|
|
|
|
|
|
|
|
NetConfig: NetConfig{
|
|
|
|
AllowIPSpoofing: true,
|
|
|
|
DualStackedPeers: true,
|
|
|
|
RespectAF: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
WhitelistConfig: WhitelistConfig{
|
|
|
|
ClientWhitelistEnabled: false,
|
|
|
|
},
|
|
|
|
},
|
2014-07-17 01:38:51 +02:00
|
|
|
|
2015-10-11 08:32:24 +02:00
|
|
|
APIConfig: APIConfig{
|
2015-10-28 21:29:11 +01:00
|
|
|
ListenAddr: "localhost:6880",
|
2015-10-11 08:32:24 +02:00
|
|
|
RequestTimeout: Duration{10 * time.Second},
|
|
|
|
ReadTimeout: Duration{10 * time.Second},
|
|
|
|
WriteTimeout: Duration{10 * time.Second},
|
|
|
|
},
|
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
HTTPConfig: HTTPConfig{
|
2015-10-28 21:29:11 +01:00
|
|
|
ListenAddr: "localhost:6881",
|
2015-10-11 08:32:24 +02:00
|
|
|
RequestTimeout: Duration{10 * time.Second},
|
|
|
|
ReadTimeout: Duration{10 * time.Second},
|
|
|
|
WriteTimeout: Duration{10 * time.Second},
|
2015-02-20 05:46:28 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
UDPConfig: UDPConfig{
|
2015-10-28 21:29:11 +01:00
|
|
|
ListenAddr: "localhost:6882",
|
2014-07-02 03:40:29 +02:00
|
|
|
},
|
2014-07-17 01:38:51 +02:00
|
|
|
|
2014-11-02 01:12:40 +01:00
|
|
|
DriverConfig: DriverConfig{
|
2014-07-15 06:22:04 +02:00
|
|
|
Name: "noop",
|
2014-07-02 03:40:29 +02:00
|
|
|
},
|
2014-07-17 01:38:51 +02:00
|
|
|
|
2014-07-23 23:25:01 +02:00
|
|
|
StatsConfig: StatsConfig{
|
|
|
|
BufferSize: 0,
|
|
|
|
IncludeMem: true,
|
|
|
|
VerboseMem: false,
|
|
|
|
|
|
|
|
MemUpdateInterval: Duration{5 * time.Second},
|
|
|
|
},
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// Open is a shortcut to open a file, read it, and generate a Config.
|
2014-07-15 05:13:35 +02:00
|
|
|
// It supports relative and absolute paths. Given "", it returns DefaultConfig.
|
2013-07-05 12:50:52 +02:00
|
|
|
func Open(path string) (*Config, error) {
|
2014-06-24 09:59:30 +02:00
|
|
|
if path == "" {
|
2014-07-02 03:40:29 +02:00
|
|
|
return &DefaultConfig, nil
|
2014-06-24 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
f, err := os.Open(os.ExpandEnv(path))
|
2013-11-24 06:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2014-06-24 09:59:30 +02:00
|
|
|
conf, err := Decode(f)
|
2013-11-24 06:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conf, nil
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
2013-06-23 12:21:59 +02:00
|
|
|
|
2014-07-15 05:13:35 +02:00
|
|
|
// Decode casts an io.Reader into a JSONDecoder and decodes it into a *Config.
|
2014-06-24 09:59:30 +02:00
|
|
|
func Decode(r io.Reader) (*Config, error) {
|
2014-07-23 22:14:50 +02:00
|
|
|
conf := DefaultConfig
|
|
|
|
err := json.NewDecoder(r).Decode(&conf)
|
|
|
|
return &conf, err
|
2013-06-23 12:21:59 +02:00
|
|
|
}
|