tracker/config/config.go

147 lines
3.9 KiB
Go
Raw Normal View History

// Copyright 2014 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.
// 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"
"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.
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.
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
}
// DriverConfig is the configuration used to connect to a tracker.Driver or
// a backend.Driver.
type DriverConfig struct {
Name string `json:"driver"`
Params map[string]string `json:"params,omitempty"`
2013-06-22 01:31:32 +02:00
}
// NetConfig is the configuration used to tune networking behaviour.
type NetConfig struct {
AllowIPSpoofing bool `json:"allow_ip_spoofing"`
DualStackedPeers bool `json:"dual_stacked_peers"`
RealIPHeader string `json:"real_ip_header"`
PreferredSubnet bool `json:"preferred_subnet,omitempty"`
PreferredIPv4Subnet int `json:"preferred_ipv4_subnet,omitempty"`
PreferredIPv6Subnet int `json:"preferred_ipv6_subnet,omitempty"`
}
2014-07-23 23:25:01 +02:00
type StatsConfig struct {
BufferSize int `json:"stats_buffer_size"`
IncludeMem bool `json:"include_mem_stats"`
VerboseMem bool `json:"verbose_mem_stats"`
MemUpdateInterval Duration `json:"mem_stats_interval"`
}
// Config is a configuration for a Server.
2013-06-23 12:21:59 +02:00
type Config struct {
Addr string `json:"addr"`
Tracker DriverConfig `json:"tracker"`
Backend DriverConfig `json:"backend"`
2013-06-22 01:31:32 +02:00
2014-07-25 22:58:26 +02:00
PrivateEnabled bool `json:"private_enabled"`
FreeleechEnabled bool `json:"freeleech_enabled"`
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
Announce Duration `json:"announce"`
MinAnnounce Duration `json:"min_announce"`
2014-07-02 03:40:29 +02:00
RequestTimeout Duration `json:"request_timeout"`
NumWantFallback int `json:"default_num_want"`
2014-07-25 22:58:26 +02:00
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`
ClientWhitelist []string `json:"client_whitelist,omitempty"`
2014-07-23 23:25:01 +02:00
StatsConfig
NetConfig
}
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{
Addr: ":6881",
2014-07-02 03:40:29 +02:00
Tracker: DriverConfig{
Name: "memory",
2014-07-02 03:40:29 +02:00
},
2014-07-02 03:40:29 +02:00
Backend: DriverConfig{
Name: "noop",
2014-07-02 03:40:29 +02:00
},
2014-07-25 22:58:26 +02:00
PrivateEnabled: false,
FreeleechEnabled: false,
PurgeInactiveTorrents: true,
2014-07-02 03:40:29 +02:00
Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute},
RequestTimeout: Duration{10 * time.Second},
NumWantFallback: 50,
2014-07-23 23:25:01 +02:00
StatsConfig: StatsConfig{
BufferSize: 0,
IncludeMem: true,
VerboseMem: false,
MemUpdateInterval: Duration{5 * time.Second},
},
NetConfig: NetConfig{
AllowIPSpoofing: true,
DualStackedPeers: true,
},
2014-07-25 22:58:26 +02:00
ClientWhitelistEnabled: false,
2013-06-22 01:31:32 +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.
func Open(path string) (*Config, error) {
if path == "" {
2014-07-02 03:40:29 +02:00
return &DefaultConfig, nil
}
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
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.
func Decode(r io.Reader) (*Config, error) {
conf := DefaultConfig
err := json.NewDecoder(r).Decode(&conf)
return &conf, err
2013-06-23 12:21:59 +02:00
}