-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
71 lines (59 loc) · 1.13 KB
/
filter.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
package filter
import (
"net/url"
"strings"
"github.com/wrfly/yasuser/config"
)
type Filter interface {
OK(*url.URL) error
}
type list struct {
blacklist map[string]bool
whitelist map[string]bool
}
type urlFilter struct {
domain list
keyword list
}
func (f *urlFilter) OK(u *url.URL) error {
// bypass domain
if f.domain.whitelist[u.Hostname()] {
return nil
}
// bypass keyword
for x := range f.keyword.whitelist {
if strings.Contains(u.Path, x) {
return nil
}
}
// bad domain
if f.domain.blacklist[u.Hostname()] {
return ErrBadDomain
}
// bad keyword
for x := range f.keyword.blacklist {
if strings.Contains(u.Path, x) {
return ErrBadKeyword
}
}
return nil
}
func makeList(slice []string) map[string]bool {
x := make(map[string]bool, len(slice))
for _, s := range slice {
x[s] = true
}
return x
}
func New(conf config.Filter) Filter {
return &urlFilter{
domain: list{
whitelist: makeList(conf.Domain.WhiteList),
blacklist: makeList(conf.Domain.BlackList),
},
keyword: list{
whitelist: makeList(conf.Keyword.WhiteList),
blacklist: makeList(conf.Keyword.BlackList),
},
}
}