2014-07-06 23:56:54 +02:00
|
|
|
// Copyright 2014 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"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
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-06-24 09:59:30 +02:00
|
|
|
// Config is a configuration for a Server.
|
2013-06-23 12:21:59 +02:00
|
|
|
type Config struct {
|
2014-06-24 09:59:30 +02:00
|
|
|
Addr string `json:"addr"`
|
|
|
|
Tracker DriverConfig `json:"tracker"`
|
|
|
|
Backend DriverConfig `json:"backend"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-11-24 06:49:20 +01:00
|
|
|
Private bool `json:"private"`
|
|
|
|
Freeleech bool `json:"freeleech"`
|
2014-07-02 03:40:29 +02:00
|
|
|
Whitelist bool `json:"whitelist"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2014-06-24 09:59:30 +02:00
|
|
|
Announce Duration `json:"announce"`
|
|
|
|
MinAnnounce Duration `json:"min_announce"`
|
2014-07-02 03:40:29 +02:00
|
|
|
RequestTimeout Duration `json:"request_timeout"`
|
2014-06-24 09:59:30 +02:00
|
|
|
NumWantFallback int `json:"default_num_want"`
|
|
|
|
}
|
|
|
|
|
2014-07-02 03:40:29 +02:00
|
|
|
var DefaultConfig = Config{
|
2014-07-10 03:26:44 +02:00
|
|
|
Addr: "127.0.0.1:6881",
|
2014-07-02 03:40:29 +02:00
|
|
|
Tracker: DriverConfig{
|
2014-07-03 23:37:13 +02:00
|
|
|
Name: "mock",
|
2014-07-02 03:40:29 +02:00
|
|
|
},
|
|
|
|
Backend: DriverConfig{
|
2014-07-03 23:37:13 +02:00
|
|
|
Name: "mock",
|
2014-07-02 03:40:29 +02:00
|
|
|
},
|
|
|
|
Private: false,
|
|
|
|
Freeleech: false,
|
|
|
|
Whitelist: false,
|
|
|
|
Announce: Duration{30 * time.Minute},
|
|
|
|
MinAnnounce: Duration{15 * time.Minute},
|
|
|
|
RequestTimeout: Duration{10 * time.Second},
|
|
|
|
NumWantFallback: 50,
|
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-06-24 09:59:30 +02:00
|
|
|
// It supports relative and absolute paths. Given "", it returns the result of
|
|
|
|
// New.
|
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-06-24 09:59:30 +02:00
|
|
|
// Decode attempts to decode a JSON encoded reader into a *Config.
|
|
|
|
func Decode(r io.Reader) (*Config, error) {
|
2013-11-24 06:49:20 +01:00
|
|
|
conf := &Config{}
|
2014-06-24 09:59:30 +02:00
|
|
|
err := json.NewDecoder(r).Decode(conf)
|
2013-11-24 06:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conf, nil
|
2013-06-23 12:21:59 +02:00
|
|
|
}
|