Skip to content

Commit d3c9b47

Browse files
committed
finish config refactor
1 parent 097e003 commit d3c9b47

File tree

9 files changed

+155
-218
lines changed

9 files changed

+155
-218
lines changed

cmd/main_test.go

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,61 @@ import (
1212
func TestMain(m *testing.M) {
1313
conf := &config.Configuration{
1414
LogLevel: "debug",
15-
Debug: struct{ Pprof int }{
16-
Pprof: 0,
17-
},
15+
Debug: struct{ Pprof int }{},
1816
Stats: struct {
1917
General bool
2018
IP bool
2119
Interval time.Duration
22-
}{
23-
General: false,
24-
IP: false,
25-
Interval: 0,
26-
},
20+
}{},
2721
UDP: struct {
28-
Enabled bool
29-
IP string
30-
Port int
31-
Threads int
32-
ConnDB struct {
22+
Port int
23+
IP string
24+
Routines int
25+
Connections struct {
3326
Validate bool
34-
Size uint64
35-
Trim time.Duration
27+
GC time.Duration
3628
Expiry time.Duration
3729
}
3830
}{
39-
ConnDB: struct {
31+
Connections: struct {
4032
Validate bool
41-
Size uint64
42-
Trim time.Duration
33+
GC time.Duration
4334
Expiry time.Duration
4435
}{
4536
Validate: true,
46-
Trim: 1 * time.Hour,
37+
GC: 1 * time.Hour,
4738
Expiry: 1 * time.Hour,
4839
},
49-
Enabled: true,
50-
Port: 1337,
51-
Threads: 1,
40+
Port: 1337,
41+
Routines: 1,
5242
},
5343
Announce: struct {
5444
Base time.Duration
5545
Fuzz time.Duration
5646
}{
57-
Base: 0,
5847
Fuzz: 1 * time.Second,
5948
},
6049
HTTP: struct {
61-
Mode string
62-
IP string
63-
Port int
64-
Timeout struct {
50+
Tracker bool
51+
Port int
52+
IP string
53+
Routines int
54+
Timeout struct {
6555
Read time.Duration
6656
Write time.Duration
6757
}
68-
Threads int
69-
ServePath string
58+
Serve string
7059
}{
71-
Mode: "enabled",
72-
Port: 1337,
60+
Tracker: true,
61+
Port: 1337,
62+
Routines: 1,
7363
Timeout: struct {
7464
Read time.Duration
7565
Write time.Duration
7666
}{
7767
Read: 2 * time.Second,
7868
Write: 2 * time.Second,
7969
},
80-
Threads: 1,
81-
ServePath: "",
8270
},
8371
Numwant: struct {
8472
Default uint
@@ -88,26 +76,15 @@ func TestMain(m *testing.M) {
8876
Limit: 100,
8977
},
9078
DB: struct {
91-
Type string
79+
GC time.Duration
80+
Expiry time.Duration
9281
Backup struct {
93-
Frequency time.Duration
94-
Type string
95-
Path string
82+
Interval time.Duration
83+
Path string
9684
}
97-
Trim time.Duration
98-
Expiry time.Duration
9985
}{
100-
Type: "gomap",
101-
Trim: 1 * time.Hour,
86+
GC: 1 * time.Hour,
10287
Expiry: 1 * time.Hour,
103-
Backup: struct {
104-
Frequency time.Duration
105-
Type string
106-
Path string
107-
}{
108-
Type: "none",
109-
Frequency: 0,
110-
},
11188
},
11289
}
11390

cmd/run.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,14 @@ func Run(conf *config.Configuration) {
3333

3434
zap.L().Debug("Starting Trakx")
3535

36-
if conf.Stats.General {
37-
if conf.Stats.Interval <= 0 {
38-
zap.L().Fatal("Invalid configuration: Stats.Interval must be greater than 0 if Stats.General is enabled")
39-
}
40-
41-
zap.L().Info("Collecting statistics", zap.Bool("general", conf.Stats.General), zap.Bool("ip", conf.Stats.IP), zap.Duration("interval", conf.Stats.Interval))
42-
43-
if conf.HTTP.Mode == config.TrackerModeDisabled {
44-
zap.L().Warn("Statistics collection enabled but no HTTP server is enabled to publish them")
45-
}
46-
}
47-
4836
// TODO: cache the prev IP collector map size :D
4937
collector := stats.NewCollectors(conf.Stats.General, conf.Stats.IP, 0)
5038

5139
db, err := inmemory.NewInMemory(inmemory.Config{
5240
InitalSize: 0, // TODO: cache this on exit and load on startup
5341
Persistance: &inmemory.FilePersistance{},
5442
PersistanceAddress: conf.DB.Backup.Path,
55-
EvictionFrequency: conf.DB.Trim,
43+
EvictionFrequency: conf.DB.GC,
5644
ExpirationTime: conf.DB.Expiry,
5745
Collector: collector,
5846
})
@@ -63,13 +51,13 @@ func Run(conf *config.Configuration) {
6351
zap.L().Info("Initialized database", zap.Int("torrents", db.Torrents()))
6452
}
6553

66-
if conf.UDP.Enabled {
54+
if conf.UDP.Port != 0 {
6755
zap.L().Info("UDP tracker enabled", zap.String("ip", conf.UDP.IP), zap.Int("port", conf.UDP.Port))
6856

69-
connectionsDB = connections.NewConnections(0, conf.UDP.ConnDB.Expiry, conf.UDP.ConnDB.Trim)
57+
connectionsDB = connections.NewConnections(0, conf.UDP.Connections.Expiry, conf.UDP.Connections.GC)
7058

7159
trackers = append(trackers, udp.NewTracker(db, connectionsDB, collector, tracker.TrackerConfig{
72-
Validate: conf.UDP.ConnDB.Validate,
60+
Validate: conf.UDP.Connections.Validate,
7361
DefaultNumwant: conf.Numwant.Default,
7462
MaximumNumwant: conf.Numwant.Limit,
7563
Interval: uint(conf.Announce.Base),
@@ -84,19 +72,15 @@ func Run(conf *config.Configuration) {
8472
serveConfigs = append(serveConfigs, serveConfig{
8573
ip: ip,
8674
port: conf.UDP.Port,
87-
routines: conf.UDP.Threads,
75+
routines: conf.UDP.Routines,
8876
})
8977
}
9078

91-
// TODO: make http tracker enable based on port with -1 for disabled
92-
// info mode will run if stats are enabled and/or fileserver mode and tracker mode is disabled
93-
// ah but then how to set port -.- xd
94-
if conf.HTTP.Mode == config.TrackerModeEnabled {
79+
if conf.HTTP.Tracker {
9580
zap.L().Info("HTTP tracker enabled", zap.String("ip", conf.HTTP.IP), zap.Int("port", conf.HTTP.Port))
9681

9782
// TODO: HTTP serve path in config, also validate it here (ie. dir exists and can access)
98-
// docs = leave serve path blank to disable serving files
99-
trackers = append(trackers, http.NewTracker(db, "/TODO/", collector, tracker.TrackerConfig{
83+
trackers = append(trackers, http.NewTracker(db, conf.HTTP.Serve, collector, tracker.TrackerConfig{
10084
DefaultNumwant: conf.Numwant.Default,
10185
MaximumNumwant: conf.Numwant.Limit,
10286
Interval: uint(conf.Announce.Base),
@@ -113,9 +97,9 @@ func Run(conf *config.Configuration) {
11397
serveConfigs = append(serveConfigs, serveConfig{
11498
ip: ip,
11599
port: conf.HTTP.Port,
116-
routines: conf.HTTP.Threads,
100+
routines: conf.HTTP.Routines,
117101
})
118-
} else if conf.HTTP.Mode == config.TrackerModeInfo {
102+
} else if conf.HTTP.Port != 0 {
119103
mux := gohttp.NewServeMux()
120104
if conf.Stats.General {
121105
mux.HandleFunc("/stats", func(w gohttp.ResponseWriter, r *gohttp.Request) {

config/config.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import (
99
"go.uber.org/zap"
1010
)
1111

12+
type LogLevel string
13+
14+
const (
15+
DebugLevel = "debug"
16+
InfoLevel = "info"
17+
WarnLevel = "warn"
18+
ErrorLevel = "error"
19+
)
20+
1221
type Configuration struct {
1322
LogLevel LogLevel
1423
Cache string
@@ -25,25 +34,23 @@ type Configuration struct {
2534
Fuzz time.Duration
2635
}
2736
HTTP struct {
28-
Mode string
29-
IP string
30-
Port int
31-
Timeout struct {
37+
Tracker bool
38+
Port int
39+
IP string
40+
Routines int
41+
Timeout struct {
3242
Read time.Duration
3343
Write time.Duration
3444
}
35-
Threads int
36-
ServePath string
45+
Serve string
3746
}
3847
UDP struct {
39-
Enabled bool
40-
IP string
41-
Port int
42-
Threads int
43-
ConnDB struct {
48+
Port int
49+
IP string
50+
Routines int
51+
Connections struct {
4452
Validate bool
45-
Size uint64
46-
Trim time.Duration
53+
GC time.Duration
4754
Expiry time.Duration
4855
}
4956
}
@@ -52,14 +59,12 @@ type Configuration struct {
5259
Limit uint
5360
}
5461
DB struct {
55-
Type string
62+
GC time.Duration
63+
Expiry time.Duration
5664
Backup struct {
57-
Frequency time.Duration
58-
Type string
59-
Path string
65+
Interval time.Duration
66+
Path string
6067
}
61-
Trim time.Duration
62-
Expiry time.Duration
6368
}
6469
}
6570

@@ -78,14 +83,24 @@ func (conf *Configuration) validate() error {
7883
return errors.Errorf("cache '%s' is not a directory", conf.Cache)
7984
}
8085

81-
// potential misconfigurations warnings
82-
if !conf.UDP.ConnDB.Validate {
86+
if !conf.UDP.Connections.Validate {
8387
zap.L().Warn("Configuration warning: UDP connection validation is disabled. Do not expose this service to untrusted networks; it could be abused in UDP based amplification attacks.")
8488
}
89+
8590
if conf.DB.Expiry < conf.Announce.Base+conf.Announce.Fuzz {
8691
zap.L().Warn("Configuration warning: Peer expiry time < announce base + fuzz. Peers will expire from the database between announces.")
8792
}
8893

94+
if conf.Stats.General {
95+
if conf.Stats.Interval <= 0 {
96+
zap.L().Fatal("Invalid configuration: Stats.Interval must be greater than 0 if Stats.General is enabled")
97+
}
98+
99+
if conf.HTTP.Port == 0 {
100+
zap.L().Warn("Configuration warning: Statistics collection enabled but no HTTP server is running to publish them")
101+
}
102+
}
103+
89104
return nil
90105
}
91106

0 commit comments

Comments
 (0)