-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
172 lines (145 loc) · 3.78 KB
/
util.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
package main
import (
"fmt"
"net"
"path"
"strings"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/alienth/go-fastly"
)
// The edge ACL which we will be interacting with in fastly.
const aclName = "ratelimit"
type appConfig struct {
Main struct {
LogFormat LogFormat
} `toml:"main"`
LogFormatOptions map[string]interface{}
HookService hookService
Lists IPLists
logParser logParser
}
func readConfig(filename string) (appConfig, error) {
config := appConfig{}
if _, err := toml.DecodeFile(filename, &config); err != nil {
return config, fmt.Errorf("toml parsing error: %s", err)
}
for name, list := range config.Lists {
list.init(name)
}
config.logParser = config.Main.LogFormat.parser()
if err := config.logParser.readOptions(config.LogFormatOptions); err != nil {
return config, err
}
if len(config.Lists) < 1 {
return config, fmt.Errorf("No IP lists defined in config file.")
}
if _, ok := config.Lists["_default_"]; !ok {
return config, fmt.Errorf("No _default_ IP list defined in config file.")
}
return config, nil
}
type ServiceDomains map[*fastly.Service][]fastly.Domain
var aclByService = make(map[*fastly.Service]*fastly.ACL)
// This also happens to populate the aclByService variable which is used in
// several functions.
func getServiceDomains() (ServiceDomains, error) {
serviceDomains := make(ServiceDomains)
services, _, err := client.Service.List()
if err != nil {
return nil, err
}
for _, s := range services {
domains, _, err := client.Domain.List(s.ID, s.Version)
if err != nil {
return nil, err
}
acls, _, err := client.ACL.List(s.ID, s.Version)
if err != nil {
return nil, err
}
var found bool
for _, acl := range acls {
if acl.Name == aclName {
found = true
aclByService[s] = acl
break
}
}
if found {
for _, d := range domains {
serviceDomains[s] = append(serviceDomains[s], *d)
}
}
}
return serviceDomains, nil
}
// getServiceByHost takes in a hostname and returns the faslty service
// associated with that hostname.
func (services ServiceDomains) getServiceByHost(hostname string) (*fastly.Service, error) {
for s, domains := range services {
for _, d := range domains {
if d.Name == hostname {
return s, nil
}
}
for _, d := range domains {
// The fastly hostname can contain wildcard records such as
// *.stackoverflow.com. We use path.Match() to match on those.
// A specific domain will override a wildcard, which is why
// we're doing this in a second loop.
found, err := path.Match(d.Name, strings.ToLower(hostname))
if err != nil {
// only possible error here would be a malformed pattern
return nil, err
}
if found {
return s, nil
}
}
}
return nil, nil
}
type duration struct {
time.Duration
}
type ipNet struct {
net.IPNet
}
func (d *duration) UnmarshalText(b []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(b))
return err
}
// multiply takes in a factor and returns a new duration multiplied by that factor.
func (d duration) multiply(factor float64) duration {
var newDuration duration
newDuration.Duration = time.Duration(int(float64(d.Seconds())*factor)) * time.Second
return newDuration
}
func (n *ipNet) UnmarshalText(b []byte) error {
_, network, err := net.ParseCIDR(string(b))
n.IPNet = *network
return err
}
type rwMutex struct {
sync.RWMutex
}
// A lock which prints a log warning each second we wait for the lock.
func Lock(m *rwMutex) {
done := make(chan struct{})
ticker := time.NewTicker(time.Duration(1) * time.Second)
start := time.Now()
go func() {
m.RWMutex.Lock()
close(done)
}()
select {
case <-done:
break
case <-ticker.C:
d := start.Sub(time.Now())
logger.Printf("Warning: Blocked for %d seconds waiting for lock\n", int(d.Seconds()))
}
}