-
Notifications
You must be signed in to change notification settings - Fork 10
/
iprepd.go
163 lines (150 loc) · 3.19 KB
/
iprepd.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
package iprepd
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
log "github.com/sirupsen/logrus"
"go.mozilla.org/mozlogrus"
yaml "gopkg.in/yaml.v2"
)
type serverRuntime struct {
cfg ServerCfg
redis redisLink
versionResponse []byte
exceptionsLoaded chan bool
statsd *statsdClient
}
type ServerCfg struct {
Listen string
Redis struct {
Addr string
Replicas []string
ReadTimeout int
WriteTimeout int
DialTimeout int
MaxPoolSize int
MinIdleConn int
}
Auth struct {
DisableAuth bool
Hawk map[string]string
APIKey map[string]string
ROHawk map[string]string
ROAPIKey map[string]string
}
IP6Prefix int
Violations []Violation
Decay struct {
Points int
Interval time.Duration
}
Exceptions struct {
File []string
AWS bool
}
VersionResponse string
Statsd struct {
Addr string
}
Sync struct {
MaxLimit int
MinimumReputation int
DeleteFile bool
GCS struct {
Filename string
Bucketname string
}
}
}
func (cfg *ServerCfg) validate() error {
if cfg.VersionResponse == "" {
cfg.VersionResponse = "./version.json"
}
if cfg.Redis.ReadTimeout == 0 {
cfg.Redis.ReadTimeout = 100
}
if cfg.Redis.WriteTimeout == 0 {
cfg.Redis.WriteTimeout = 100
}
if cfg.Redis.DialTimeout == 0 {
cfg.Redis.DialTimeout = 250
}
if cfg.Redis.MinIdleConn == 0 {
cfg.Redis.MinIdleConn = 20
}
if cfg.IP6Prefix == 0 {
cfg.IP6Prefix = 64
}
return nil
}
func (cfg *ServerCfg) getViolation(v string) *Violation {
for _, x := range cfg.Violations {
if x.Name == v {
return &x
}
}
return nil
}
var sruntime serverRuntime
func init() {
mozlogrus.Enable("iprepd")
rand.Seed(time.Now().Unix())
}
func LoadCfg(confpath string) (ret ServerCfg, err error) {
buf, err := ioutil.ReadFile(confpath)
if err != nil {
return
}
err = yaml.Unmarshal(buf, &ret)
if err != nil {
return
}
// prefer STATSD_HOST env var over config file (#13)
statsdHost := os.Getenv("STATSD_HOST")
if statsdHost != "" {
statsdPort := os.Getenv("STATSD_PORT")
if statsdPort == "" {
statsdPort = "8125"
}
ret.Statsd.Addr = fmt.Sprintf("%s:%s", statsdHost, statsdPort)
}
return ret, ret.validate()
}
func CreateServerRuntime(confpath string) {
var err error
sruntime.exceptionsLoaded = make(chan bool, 1)
sruntime.cfg, err = LoadCfg(confpath)
if err != nil {
log.Fatalf(err.Error())
}
sruntime.statsd, err = newStatsdClient(sruntime.cfg)
if err != nil {
log.Fatalf(err.Error())
}
sruntime.redis, err = newRedisLink(sruntime.cfg)
if err != nil {
log.Fatalf(err.Error())
}
sruntime.versionResponse, err = ioutil.ReadFile(sruntime.cfg.VersionResponse)
if err != nil {
log.Warnf(err.Error())
}
}
// StartDaemon starts a new instance of iprepd using configuration file confpath.
func StartDaemon(confpath string) {
log.Infof("starting daemon")
CreateServerRuntime(confpath)
go startExceptions()
select {
case <-sruntime.exceptionsLoaded:
log.Infof("initial exception load completed, starting API")
case <-time.After(5 * time.Second):
log.Fatalf("initial exception load timed out")
}
err := startAPI()
if err != nil {
log.Fatalf(err.Error())
}
}