Repository navigation
Expand file tree
/
Copy pathaction.go
More file actions
92 lines (79 loc) · 2.37 KB
/
Copy pathaction.go
File metadata and controls
92 lines (79 loc) · 2.37 KB
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
package filter
import (
"regexp"
)
// ActionType represents the action taken upon domains, expressions, or lists
type ActionType int
const (
// ActionTypeAllow represents a domain, expression, or list that will not
// be filtered, regardless if the domain or expression is set to be blocked
ActionTypeAllow ActionType = iota
// ActionTypeBlock represents a domain, expression, or list that will be
// filtered
ActionTypeBlock
)
// String returns the action type
func (a ActionType) String() string {
actions := map[ActionType]string{
ActionTypeAllow: "allow",
ActionTypeBlock: "block",
}
return actions[a]
}
// ActionList is a list of URLs and the functions required to load them
type ActionList map[string]ListLoader
// ActionConfig contains the raw domains, expressions, and lists that are
// compiled and used by the Filter
type ActionConfig struct {
configType ActionType
domains map[string]bool
regex map[string]*regexp.Regexp
wildcards map[string]bool
domainLists ActionList
hostsLists ActionList
regexLists ActionList
wildcardLists ActionList
FileLoader FileListLoader
HTTPLoader HTTPListLoader
}
// DNSNameRegexp matches valid domain names.
// Sourced from https://github.com/asaskevich/govalidator; const DNSName
//
// MIT Licensed, Copyright (c) 2014-2020 Alex Saskevich
var DNSNameRegexp = regexp.MustCompile(
`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`,
)
// HostsRegexp matches multiple spaces or tab spaces for cleaning up each entry
var HostsRegexp = regexp.MustCompile(`\s+|\t+`)
// NewActionConfig returns an action ready to accept configurations
func NewActionConfig(action ActionType) ActionConfig {
return ActionConfig{
configType: action,
domains: make(map[string]bool),
regex: make(map[string]*regexp.Regexp),
wildcards: make(map[string]bool),
domainLists: make(ActionList),
hostsLists: make(ActionList),
regexLists: make(ActionList),
wildcardLists: make(ActionList),
FileLoader: FileListLoader{},
HTTPLoader: HTTPListLoader{},
}
}
func (a ActionConfig) shouldSkip(line []byte) bool {
if len(line) == 0 {
// skip empty lines
return true
}
switch line[0] {
case '!', '#', ';':
// skip commented lines
return true
}
switch string(line) {
case "[Adblock Plus]":
// skip adblock plus file headers
return true
}
return false
}