forked from arlolra/check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.go
278 lines (241 loc) · 5.17 KB
/
datastore.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"encoding/json"
"io"
"log"
"net"
"os"
"os/signal"
"sort"
"syscall"
"time"
)
type Rule struct {
IsAccept bool
IsAddressWildcard bool
Address string
Mask string
IP net.IP
IPNet *net.IPNet
MinPort int
MaxPort int
}
func (r Rule) IsMatch(address net.IP, port int) bool {
if !r.IsAddressWildcard {
if r.IPNet != nil {
if !r.IPNet.Contains(address) {
return false
}
} else {
if !r.IP.Equal(address) {
return false
}
}
}
if port < r.MinPort || port > r.MaxPort {
return false
}
return true
}
func ValidPort(port int) bool {
return port >= 0 && port < 65536
}
type AddressPort struct {
Address string
Port int
}
type CanExitCache struct {
ap AddressPort
can bool
}
type Policy struct {
Fingerprint string
Address []string
Rules []Rule
IsAllowedDefault bool
Tminus int
CacheLast CanExitCache
}
func (p Policy) CanExit(ap AddressPort) (can bool) {
if p.CacheLast.ap == ap {
can = p.CacheLast.can
return
}
// update the cache *after* we return
defer func() {
p.CacheLast = CanExitCache{ap, can}
}()
addr := net.ParseIP(ap.Address)
if addr != nil && ValidPort(ap.Port) {
for _, rule := range p.Rules {
if rule.IsMatch(addr, ap.Port) {
can = rule.IsAccept
return
}
}
}
can = p.IsAllowedDefault
return
}
type PolicyAddress struct {
Policy Policy
Address string
}
type PolicyList []PolicyAddress
func (p PolicyList) Less(i, j int) bool {
return p[i].Address < p[j].Address
}
func (p PolicyList) Len() int {
return len(p)
}
func (p PolicyList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
type ExitInfo struct {
Address string
Fingerprint string
}
func (e ExitInfo) toJSON() (b []byte) {
j, err := json.MarshalIndent(e, "", " ")
if err != nil {
return
}
return j
}
type Exits struct {
List PolicyList
UpdateTime time.Time
ReloadChan chan os.Signal
IsTorLookup map[string]string
}
func (e *Exits) Dump(w io.Writer, tminus int, ip string, port int) {
ap := AddressPort{ip, port}
var last string
e.GetAllExits(ap, tminus, func(exit string, _ string, _ int) {
if exit != last {
w.Write([]byte(exit + "\n"))
last = exit
}
})
}
func (e *Exits) DumpJSON(w io.Writer, tminus int, ip string, port int) {
ap := AddressPort{ip, port}
Prefix := []byte(",\n")
w.Write([]byte("["))
e.GetAllExits(ap, tminus, func(address string, fingerprint string, ind int) {
if ind > 0 {
w.Write(Prefix)
}
w.Write(ExitInfo{address, fingerprint}.toJSON())
})
w.Write([]byte("]"))
}
func (e *Exits) GetAllExits(ap AddressPort, tminus int, fn func(string, string, int)) {
ind := 0
for _, val := range e.List {
if val.Policy.Tminus <= tminus && val.Policy.CanExit(ap) {
fn(val.Address, val.Policy.Fingerprint, ind)
ind += 1
}
}
}
var DefaultTarget = AddressPort{"38.229.72.22", 443}
func (e *Exits) PreComputeTorList() {
newmap := make(map[string]string)
e.GetAllExits(DefaultTarget, 16, func(ip string, fingerprint string, _ int) {
newmap[ip] = fingerprint
})
e.IsTorLookup = newmap
}
func (e *Exits) IsTor(remoteAddr string) (fingerprint string, ok bool) {
fingerprint, ok = e.IsTorLookup[remoteAddr]
return
}
func InsertUnique(arr *[]string, a string) {
for _, b := range *arr {
if a == b {
return
}
}
*arr = append(*arr, a)
}
func (e *Exits) Update(exits []Policy, update bool) {
m := make(map[string]Policy)
// bump entries by an hour that aren't in the new exit list
if update {
for _, p := range e.List {
if _, ok := m[p.Policy.Fingerprint]; !ok {
p.Policy.Tminus = p.Policy.Tminus + 1
m[p.Policy.Fingerprint] = p.Policy
}
}
}
// keep all unique ips we've seen
for _, p := range exits {
if q, ok := m[p.Fingerprint]; ok {
for _, a := range q.Address {
InsertUnique(&p.Address, a)
}
}
m[p.Fingerprint] = p
}
var pl PolicyList
for _, p := range m {
for _, a := range p.Address {
pl = append(pl, PolicyAddress{p, a})
}
}
// sort -n
sort.Sort(pl)
e.List = pl
}
func (e *Exits) Load(source io.Reader, update bool) error {
var exits []Policy
dec := json.NewDecoder(source)
for {
var p Policy
if err := dec.Decode(&p); err == io.EOF {
break
} else if err != nil {
return err
}
for i := range p.Rules {
r := &p.Rules[i]
if !r.IsAddressWildcard {
r.IP = net.ParseIP(r.Address)
if mask := net.ParseIP(r.Mask); r.IP != nil && mask != nil {
m := make(net.IPMask, len(mask))
copy(m, mask)
r.IPNet = &net.IPNet{IP: r.IP.Mask(m), Mask: m}
}
}
}
exits = append(exits, p)
}
e.Update(exits, update)
e.UpdateTime = time.Now()
e.PreComputeTorList()
return nil
}
func (e *Exits) LoadFromFile(filePath string, update bool) {
file, err := os.Open(os.ExpandEnv(filePath))
defer file.Close()
if err != nil {
log.Fatal(err)
}
if err = e.Load(file, update); err != nil {
log.Fatal(err)
}
}
func (e *Exits) Run(filePath string) {
e.ReloadChan = make(chan os.Signal, 1)
signal.Notify(e.ReloadChan, syscall.SIGUSR2)
go func() {
for {
<-e.ReloadChan
e.LoadFromFile(filePath, true)
log.Println("Exit list updated.")
}
}()
e.LoadFromFile(filePath, false)
}