forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitestat.go
428 lines (372 loc) · 9.95 KB
/
sitestat.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"strings"
"sync"
"time"
)
func init() {
rand.Seed(time.Now().Unix())
}
// VisitCnt and SiteStat are used to track how many times a site is visited.
// With this information: COW knows which sites are frequently visited, and
// judging whether a site is blocked or not is more reliable.
const (
directDelta = 30
blockedDelta = 20
maxCnt = 100 // no protect to update visit cnt, smaller value is unlikely to overflow
userCnt = -1 // this represents user specified host or domain
)
type siteVisitMethod int
type vcntint int8
type Date time.Time
const dateLayout = "2006-01-02"
func (d Date) MarshalJSON() ([]byte, error) {
return []byte("\"" + time.Time(d).Format(dateLayout) + "\""), nil
}
func (d *Date) UnmarshalJSON(input []byte) error {
if len(input) != len(dateLayout)+2 {
return errors.New(fmt.Sprintf("unmarshaling date: invalid input %s", string(input)))
}
input = input[1 : len(dateLayout)+1]
t, err := time.Parse(dateLayout, string(input))
*d = Date(t)
return err
}
// COW don't need very accurate visit count, so update to visit count value is
// not protected.
type VisitCnt struct {
Direct vcntint `json:"direct"`
Blocked vcntint `json:"block"`
Recent Date `json:"recent"`
rUpdated bool // whether Recent is updated, we only need date precision
blockedOn time.Time // when is the site last blocked
}
func newVisitCnt(direct, blocked vcntint) *VisitCnt {
return &VisitCnt{direct, blocked, Date(time.Now()), true, zeroTime}
}
func newVisitCntWithTime(direct, blocked vcntint, t time.Time) *VisitCnt {
return &VisitCnt{direct, blocked, Date(t), true, zeroTime}
}
func (vc *VisitCnt) userSpecified() bool {
return vc.Blocked == userCnt || vc.Direct == userCnt
}
const siteStaleThreshold = 15 * 24 * time.Hour
// shouldDrop returns true if the a VisitCnt is not visited for a long time
// (several days) or is specified by user.
func (vc *VisitCnt) shouldDrop() bool {
return vc.userSpecified() || time.Now().Sub(time.Time(vc.Recent)) > siteStaleThreshold ||
(vc.Blocked == 0 && vc.Direct == 0)
}
const tmpBlockedTimeout = 2 * time.Minute
func (vc *VisitCnt) AsTempBlocked() bool {
return time.Now().Sub(vc.blockedOn) < tmpBlockedTimeout
}
func (vc *VisitCnt) AsDirect() bool {
return (vc.Direct == userCnt) || (vc.Direct-vc.Blocked >= directDelta)
}
func (vc *VisitCnt) AsBlocked() bool {
if vc.Blocked == userCnt || vc.AsTempBlocked() {
return true
}
// add some randomness to fix mistake
delta := vc.Blocked - vc.Direct
return delta >= blockedDelta && rand.Intn(int(delta)) != 0
}
func (vc *VisitCnt) AlwaysDirect() bool {
return vc.Direct == userCnt
}
func (vc *VisitCnt) AlwaysBlocked() bool {
return vc.Blocked == userCnt
}
func (vc *VisitCnt) OnceBlocked() bool {
return vc.Blocked > 0 || vc.AlwaysBlocked() || vc.AsTempBlocked()
}
func (vc *VisitCnt) tempBlocked() {
vc.BlockedVisit() // first blocked visit, then set it as temp blocked
vc.blockedOn = time.Now()
}
// time.Time is composed of 3 fields, so need lock to protect update. As
// update of last visit is not frequent (at most once for each domain), use a
// global lock to avoid associating a lock to each VisitCnt.
var visitLock sync.Mutex
// visit updates visit cnt
func (vc *VisitCnt) visit(inc *vcntint) {
if *inc < maxCnt {
*inc++
}
// Because of concurrent update, possible for *inc to overflow and become
// negative, but very unlikely.
if *inc > maxCnt || *inc < 0 {
*inc = maxCnt
}
if !vc.rUpdated {
vc.rUpdated = true
visitLock.Lock()
vc.Recent = Date(time.Now())
visitLock.Unlock()
}
}
func (vc *VisitCnt) DirectVisit() {
if vc.userSpecified() {
return
}
vc.visit(&vc.Direct)
// one successful direct visit probably means the site is not actually
// blocked
vc.Blocked = 0
}
func (vc *VisitCnt) BlockedVisit() {
if vc.userSpecified() || vc.AsTempBlocked() {
return
}
vc.visit(&vc.Blocked)
// blockage maybe caused by bad network connection
vc.Direct = vc.Direct - 5
if vc.Direct < 0 {
vc.Direct = 0
}
}
type SiteStat struct {
Update Date `json:"update"`
Vcnt map[string]*VisitCnt `json:"site_info"` // Vcnt uses host as key
vcLock sync.RWMutex
// Whether a domain has blocked host. Used to avoid considering a domain as
// direct though it has blocked hosts.
hasBlockedHost map[string]bool
hbhLock sync.RWMutex
}
func newSiteStat() *SiteStat {
return &SiteStat{
Vcnt: map[string]*VisitCnt{},
hasBlockedHost: map[string]bool{},
}
}
func (ss *SiteStat) get(s string) *VisitCnt {
ss.vcLock.RLock()
Vcnt, ok := ss.Vcnt[s]
ss.vcLock.RUnlock()
if ok {
return Vcnt
}
return nil
}
func (ss *SiteStat) create(s string) (vcnt *VisitCnt) {
vcnt = newVisitCnt(0, 0)
ss.vcLock.Lock()
ss.Vcnt[s] = vcnt
ss.vcLock.Unlock()
return
}
// Caller should guarantee that always direct url does not attempt
// blocked visit.
func (ss *SiteStat) TempBlocked(url *URL) {
debug.Printf("%s temp blocked\n", url.Host)
vcnt := ss.get(url.Host)
if vcnt == nil {
panic("TempBlocked should always get existing visitCnt")
}
vcnt.tempBlocked()
// Mistakenly consider a partial blocked domain as direct will make that
// domain into PAC and never have a chance to correct the error.
// Once using blocked visit, a host is considered to maybe blocked even if
// it's block visit count decrease to 0. As hasBlockedHost is not saved,
// upon next start up of COW, the information will reflect the current
// status of that host.
ss.hbhLock.RLock()
t := ss.hasBlockedHost[url.Domain]
ss.hbhLock.RUnlock()
if !t {
ss.hbhLock.Lock()
ss.hasBlockedHost[url.Domain] = true
ss.hbhLock.Unlock()
}
}
var alwaysDirectVisitCnt = newVisitCnt(userCnt, 0)
func (ss *SiteStat) GetVisitCnt(url *URL) (vcnt *VisitCnt) {
if url.Domain == "" { // simple host or ip
return alwaysDirectVisitCnt
}
if vcnt = ss.get(url.Host); vcnt != nil {
return
}
if len(url.Domain) != len(url.Host) {
if vcnt = ss.get(url.Domain); vcnt != nil && vcnt.userSpecified() {
// if the domain is not specified by user, should create a new host
// visitCnt
return vcnt
}
}
return ss.create(url.Host)
}
func (ss *SiteStat) store(file string) (err error) {
if err = mkConfigDir(); err != nil {
return
}
now := time.Now()
var s *SiteStat
if ss.Update == Date(zeroTime) {
ss.Update = Date(time.Now())
}
if now.Sub(time.Time(ss.Update)) > siteStaleThreshold {
// Not updated for a long time, don't drop any record
s = ss
// Changing update time too fast will also drop useful record
s.Update = Date(time.Time(ss.Update).Add(siteStaleThreshold / 5))
if time.Time(s.Update).Sub(now) > 0 {
s.Update = Date(now)
}
} else {
s = newSiteStat()
s.Update = Date(now)
ss.vcLock.RLock()
for site, vcnt := range ss.Vcnt {
// user specified sites may change, always filter them out
dmcnt := ss.get(host2Domain(site))
if (dmcnt != nil && dmcnt.userSpecified()) || vcnt.shouldDrop() {
continue
}
s.Vcnt[site] = vcnt
}
ss.vcLock.RUnlock()
}
b, err := json.MarshalIndent(s, "", "\t")
if err != nil {
errl.Println("Error marshalling site stat:", err)
panic("internal error: error marshalling site")
}
f, err := os.Create(file)
if err != nil {
errl.Println("Can't create stat file:", err)
return
}
defer f.Close()
if _, err = f.Write(b); err != nil {
errl.Println("Error writing stat file:", err)
return
}
return
}
func (ss *SiteStat) loadList(lst []string, direct, blocked vcntint) {
for _, d := range lst {
ss.Vcnt[d] = newVisitCntWithTime(direct, blocked, zeroTime)
}
}
func (ss *SiteStat) loadBuiltinList() {
ss.loadList(blockedDomainList, 0, userCnt)
ss.loadList(directDomainList, userCnt, 0)
}
func (ss *SiteStat) load(file string) (err error) {
var exists bool
if exists, err = isFileExists(file); err != nil {
fmt.Println("Error loading stat:", err)
return
}
if !exists {
return
}
var f *os.File
if f, err = os.Open(file); err != nil {
fmt.Printf("Error opening site stat %s: %v\n", file, err)
return
}
b, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println("Error reading site stat:", err)
return
}
if err = json.Unmarshal(b, ss); err != nil {
fmt.Println("Error decoding site stat:", err)
return
}
ss.loadBuiltinList()
// load user specified sites at last to override previous values
if directList, err := loadSiteList(dsFile.alwaysDirect); err == nil {
ss.loadList(directList, userCnt, 0)
}
if blockedList, err := loadSiteList(dsFile.alwaysBlocked); err == nil {
ss.loadList(blockedList, 0, userCnt)
}
for k, v := range ss.Vcnt {
if v.Blocked > 0 {
ss.hasBlockedHost[k] = true
}
}
return
}
func (ss *SiteStat) GetDirectList() []string {
lst := make([]string, 0)
// anyway to do more fine grained locking?
ss.vcLock.RLock()
for site, vc := range ss.Vcnt {
if ss.hasBlockedHost[host2Domain(site)] {
continue
}
if vc.AsDirect() {
lst = append(lst, site)
}
}
ss.vcLock.RUnlock()
return lst
}
var siteStat = newSiteStat()
func initSiteStat() {
loadSiteStat()
if isWindows() {
// TODO How to detect program exit on Windows? This
// is just a workaround.
go func() {
for {
time.Sleep(time.Hour)
storeSiteStat()
}
}()
}
}
func loadSiteStat() {
if siteStat.load(dsFile.stat) != nil {
os.Exit(1)
}
}
func storeSiteStat() {
siteStat.store(dsFile.stat)
}
func loadSiteList(fpath string) (lst []string, err error) {
var exists bool
if exists, err = isFileExists(fpath); err != nil {
errl.Printf("Error loading domaint list: %v\n", err)
}
if !exists {
return
}
f, err := os.Open(fpath)
if err != nil {
errl.Printf("Error opening domain list %s: %v\n", fpath)
return
}
defer f.Close()
fr := bufio.NewReader(f)
lst = make([]string, 0)
var site string
for {
site, err = ReadLine(fr)
if err == io.EOF {
return lst, nil
} else if err != nil {
errl.Printf("Error reading domain list %s: %v\n", fpath, err)
return
}
if site == "" {
continue
}
lst = append(lst, strings.TrimSpace(site))
}
return
}