forked from chihaya/chihaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
193 lines (165 loc) · 5.59 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2015 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
package config
import (
"encoding/json"
"io"
"os"
"time"
)
// Duration wraps a time.Duration and adds JSON marshalling.
type Duration struct{ time.Duration }
// MarshalJSON transforms a duration into JSON.
func (d *Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON transform JSON into a Duration.
func (d *Duration) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
d.Duration, err = time.ParseDuration(str)
return err
}
// SubnetConfig is the configuration used to specify if local peers should be
// given a preference when responding to an announce.
type SubnetConfig struct {
PreferredSubnet bool `json:"preferredSubnet,omitempty"`
PreferredIPv4Subnet int `json:"preferredIPv4Subnet,omitempty"`
PreferredIPv6Subnet int `json:"preferredIPv6Subnet,omitempty"`
}
// NetConfig is the configuration used to tune networking behaviour.
type NetConfig struct {
AllowIPSpoofing bool `json:"allowIPSpoofing"`
DualStackedPeers bool `json:"dualStackedPeers"`
RealIPHeader string `json:"realIPHeader"`
RespectAF bool `json:"respectAF"`
SubnetConfig
}
// StatsConfig is the configuration used to record runtime statistics.
type StatsConfig struct {
BufferSize int `json:"statsBufferSize"`
IncludeMem bool `json:"includeMemStats"`
VerboseMem bool `json:"verboseMemStats"`
MemUpdateInterval Duration `json:"memStatsInterval"`
}
// WhitelistConfig is the configuration used enable and store a whitelist of
// acceptable torrent client peer ID prefixes.
type WhitelistConfig struct {
ClientWhitelistEnabled bool `json:"clientWhitelistEnabled"`
ClientWhitelist []string `json:"clientWhitelist,omitempty"`
}
// TrackerConfig is the configuration for tracker functionality.
type TrackerConfig struct {
CreateOnAnnounce bool `json:"createOnAnnounce"`
PurgeInactiveTorrents bool `json:"purgeInactiveTorrents"`
Announce Duration `json:"announce"`
MinAnnounce Duration `json:"minAnnounce"`
ReapInterval Duration `json:"reapInterval"`
ReapRatio float64 `json:"reapRatio"`
NumWantFallback int `json:"defaultNumWant"`
TorrentMapShards int `json:"torrentMapShards"`
JWKSetURI string `json:"jwkSetURI"`
JWKSetUpdateInterval Duration `json:"jwkSetUpdateInterval"`
JWTAudience string `json:"jwtAudience"`
NetConfig
WhitelistConfig
}
// APIConfig is the configuration for an HTTP JSON API server.
type APIConfig struct {
ListenAddr string `json:"apiListenAddr"`
RequestTimeout Duration `json:"apiRequestTimeout"`
ReadTimeout Duration `json:"apiReadTimeout"`
WriteTimeout Duration `json:"apiWriteTimeout"`
ListenLimit int `json:"apiListenLimit"`
}
// HTTPConfig is the configuration for the HTTP protocol.
type HTTPConfig struct {
ListenAddr string `json:"httpListenAddr"`
RequestTimeout Duration `json:"httpRequestTimeout"`
ReadTimeout Duration `json:"httpReadTimeout"`
WriteTimeout Duration `json:"httpWriteTimeout"`
ListenLimit int `json:"httpListenLimit"`
}
// UDPConfig is the configuration for the UDP protocol.
type UDPConfig struct {
ListenAddr string `json:"udpListenAddr"`
ReadBufferSize int `json:"udpReadBufferSize"`
}
// Config is the global configuration for an instance of Chihaya.
type Config struct {
TrackerConfig
APIConfig
HTTPConfig
UDPConfig
StatsConfig
}
// DefaultConfig is a configuration that can be used as a fallback value.
var DefaultConfig = Config{
TrackerConfig: TrackerConfig{
CreateOnAnnounce: true,
PurgeInactiveTorrents: true,
Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute},
ReapInterval: Duration{60 * time.Second},
ReapRatio: 1.25,
NumWantFallback: 50,
TorrentMapShards: 1,
JWKSetURI: "",
JWKSetUpdateInterval: Duration{5 * time.Minute},
JWTAudience: "",
NetConfig: NetConfig{
AllowIPSpoofing: true,
DualStackedPeers: true,
RespectAF: false,
},
WhitelistConfig: WhitelistConfig{
ClientWhitelistEnabled: false,
},
},
APIConfig: APIConfig{
ListenAddr: "localhost:6880",
RequestTimeout: Duration{10 * time.Second},
ReadTimeout: Duration{10 * time.Second},
WriteTimeout: Duration{10 * time.Second},
},
HTTPConfig: HTTPConfig{
ListenAddr: "localhost:6881",
RequestTimeout: Duration{10 * time.Second},
ReadTimeout: Duration{10 * time.Second},
WriteTimeout: Duration{10 * time.Second},
},
UDPConfig: UDPConfig{
ListenAddr: "localhost:6882",
},
StatsConfig: StatsConfig{
BufferSize: 0,
IncludeMem: true,
VerboseMem: false,
MemUpdateInterval: Duration{5 * time.Second},
},
}
// Open is a shortcut to open a file, read it, and generate a Config.
// It supports relative and absolute paths. Given "", it returns DefaultConfig.
func Open(path string) (*Config, error) {
if path == "" {
return &DefaultConfig, nil
}
f, err := os.Open(os.ExpandEnv(path))
if err != nil {
return nil, err
}
defer f.Close()
conf, err := Decode(f)
if err != nil {
return nil, err
}
return conf, nil
}
// 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
}