|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "path/filepath" |
5 | 6 | "time" |
6 | 7 |
|
| 8 | + "github.com/pkg/errors" |
7 | 9 | "go.uber.org/zap" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | type Configuration struct { |
11 | | - LogLevel LogLevel |
12 | | - CachePath string |
13 | | - Stats struct { |
| 13 | + LogLevel LogLevel |
| 14 | + Cache string |
| 15 | + Stats struct { |
14 | 16 | General bool |
15 | 17 | IP bool |
16 | 18 | Interval time.Duration |
@@ -61,56 +63,38 @@ type Configuration struct { |
61 | 63 | } |
62 | 64 | } |
63 | 65 |
|
64 | | -type Warnings int |
65 | | - |
66 | | -const ( |
67 | | - WarningNone Warnings = 1 << iota |
68 | | - WarningUDPValidation |
69 | | - WarningPeerExpiry |
70 | | -) |
71 | | - |
72 | | -// Validate ensures that configuration values are sane and returns warnings for potential misconfigurations or security issues |
73 | | -func (conf *Configuration) Validate() Warnings { |
74 | | - warnings := WarningNone |
| 66 | +// validate checks that configuration values are sane and returns warnings for potential misconfigurations or security issues |
| 67 | +func (conf *Configuration) validate() error { |
| 68 | + // check cache directory exists and is a directory |
| 69 | + stat, err := os.Stat(conf.Cache) |
| 70 | + if os.IsNotExist(err) { |
| 71 | + zap.L().Debug("cache directory does not exist, creating it", zap.String("cache", conf.Cache)) |
| 72 | + if err = os.MkdirAll(conf.Cache, defaultFolderPermission); err != nil { |
| 73 | + return errors.Wrapf(err, "failed to create cache directory '%s'", conf.Cache) |
| 74 | + } |
| 75 | + } else if err != nil { |
| 76 | + return errors.Wrapf(err, "failed to stat cache '%s'", conf.Cache) |
| 77 | + } else if !stat.IsDir() { |
| 78 | + return errors.Errorf("cache '%s' is not a directory", conf.Cache) |
| 79 | + } |
75 | 80 |
|
| 81 | + // potential misconfigurations warnings |
76 | 82 | if !conf.UDP.ConnDB.Validate { |
77 | | - warnings |= WarningUDPValidation |
| 83 | + 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.") |
78 | 84 | } |
79 | 85 | if conf.DB.Expiry < conf.Announce.Base+conf.Announce.Fuzz { |
80 | | - warnings |= WarningPeerExpiry |
| 86 | + zap.L().Warn("Configuration warning: Peer expiry time < announce base + fuzz. Peers will expire from the database between announces.") |
81 | 87 | } |
82 | 88 |
|
83 | | - return warnings |
| 89 | + return nil |
84 | 90 | } |
85 | 91 |
|
86 | 92 | // LogPath returns the log path as defined by the configuration and current time |
87 | 93 | func (conf *Configuration) LogPath() string { |
88 | | - return filepath.Join(conf.CachePath, "trakx_"+time.Now().Format("06-01-02-15-04-05")+".log") |
| 94 | + return filepath.Join(conf.Cache, "trakx_"+time.Now().Format("06-01-02-15-04-05")+".log") |
89 | 95 | } |
90 | 96 |
|
91 | 97 | // PIDPath retuirns the pid file path |
92 | 98 | func (conf *Configuration) PIDPath() string { |
93 | | - return filepath.Join(conf.CachePath, "trakx.pid") |
94 | | -} |
95 | | - |
96 | | -// setLogLevel sets the desired loglevel in the in memory configuration and logger |
97 | | -func (conf *Configuration) setLogLevel(level LogLevel) { |
98 | | - conf.LogLevel = level |
99 | | - |
100 | | - switch level { |
101 | | - case "debug": |
102 | | - loggerAtom.SetLevel(zap.DebugLevel) |
103 | | - zap.L().Debug("Debug loglevel set, debug panics enabled") |
104 | | - case "info": |
105 | | - loggerAtom.SetLevel(zap.InfoLevel) |
106 | | - case "warn": |
107 | | - loggerAtom.SetLevel(zap.WarnLevel) |
108 | | - case "error": |
109 | | - loggerAtom.SetLevel(zap.ErrorLevel) |
110 | | - case "fatal": |
111 | | - loggerAtom.SetLevel(zap.FatalLevel) |
112 | | - default: |
113 | | - zap.L().Warn("Invalid log level was specified, defaulting to warn") |
114 | | - loggerAtom.SetLevel(zap.WarnLevel) |
115 | | - } |
| 99 | + return filepath.Join(conf.Cache, "trakx.pid") |
116 | 100 | } |
0 commit comments