-
Notifications
You must be signed in to change notification settings - Fork 14
/
rules.go
206 lines (173 loc) · 3.78 KB
/
rules.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
package goreferrer
import (
"encoding/json"
"io"
"net/url"
"path"
"strings"
)
type DomainRule struct {
Type ReferrerType
Label string
Domain string
Parameters []string
}
type UaRule struct {
Url string
Domain string
Tld string
}
func (u UaRule) RegisteredDomain() string {
if u.Domain == "" || u.Tld == "" {
return ""
}
return u.Domain + "." + u.Tld
}
type RuleSet struct {
DomainRules map[string]DomainRule
UaRules map[string]UaRule
}
func NewRuleSet() RuleSet {
return RuleSet{
DomainRules: make(map[string]DomainRule),
UaRules: make(map[string]UaRule),
}
}
func (r RuleSet) Merge(other RuleSet) {
for k, v := range other.DomainRules {
r.DomainRules[k] = v
}
for k, v := range other.UaRules {
r.UaRules[k] = v
}
}
func (r RuleSet) Parse(URL string) Referrer {
return r.ParseWith(URL, nil, "")
}
func (r RuleSet) ParseWith(URL string, domains []string, agent string) Referrer {
ref := Referrer{
Type: Indirect,
URL: strings.Trim(URL, " \t\r\n"),
}
uaRule := r.getUaRule(agent)
if ref.URL == "" {
ref.URL = uaRule.Url
}
if ref.URL == "" {
ref.Type = Direct
return ref
}
u, ok := parseRichUrl(ref.URL)
if !ok {
ref.Type = Invalid
return ref
}
ref.Subdomain = u.Subdomain
ref.Domain = u.Domain
ref.Tld = u.Tld
ref.Path = cleanPath(u.Path)
if ref.Domain == "" {
ref.Domain = uaRule.Domain
}
if ref.Tld == "" {
ref.Tld = uaRule.Tld
}
for _, domain := range domains {
if u.Host == domain {
ref.Type = Direct
return ref
}
}
variations := []string{
path.Join(u.Host, u.Path),
path.Join(u.RegisteredDomain(), u.Path),
u.Host,
u.RegisteredDomain(),
}
for _, host := range variations {
domainRule, exists := r.DomainRules[host]
if !exists {
continue
}
query := getQuery(u.Query(), domainRule.Parameters)
if query == "" {
values, err := url.ParseQuery(u.Fragment)
if err == nil {
query = getQuery(values, domainRule.Parameters)
}
}
ref.Type = domainRule.Type
ref.Label = domainRule.Label
ref.Query = query
ref.GoogleType = googleSearchType(ref)
return ref
}
ref.Label = strings.Title(u.Domain)
return ref
}
func (r *RuleSet) getUaRule(agent string) UaRule {
for pattern, rule := range r.UaRules {
if strings.Contains(agent, pattern) {
return rule
}
}
return UaRule{}
}
func getQuery(values url.Values, params []string) string {
for _, param := range params {
query := values.Get(param)
if query != "" {
return query
}
}
return ""
}
func googleSearchType(ref Referrer) GoogleSearchType {
if ref.Type != Search || !strings.Contains(ref.Label, "Google") {
return NotGoogleSearch
}
if strings.HasPrefix(ref.Path, "/aclk") || strings.HasPrefix(ref.Path, "/pagead/aclk") {
return Adwords
}
return OrganicSearch
}
func cleanPath(path string) string {
if i := strings.Index(path, ";"); i != -1 {
return path[:i]
}
return path
}
type jsonRule struct {
Domains []string
Parameters []string
}
type jsonRules struct {
Email map[string]jsonRule
Search map[string]jsonRule
Social map[string]jsonRule
}
func LoadJsonDomainRules(reader io.Reader) (map[string]DomainRule, error) {
var decoded jsonRules
if err := json.NewDecoder(reader).Decode(&decoded); err != nil {
return nil, err
}
rules := NewRuleSet()
rules.Merge(extractRules(decoded.Email, Email))
rules.Merge(extractRules(decoded.Search, Search))
rules.Merge(extractRules(decoded.Social, Social))
return rules.DomainRules, nil
}
func extractRules(ruleMap map[string]jsonRule, Type ReferrerType) RuleSet {
rules := NewRuleSet()
for label, jsonRule := range ruleMap {
for _, domain := range jsonRule.Domains {
rules.DomainRules[domain] = DomainRule{
Type: Type,
Label: label,
Domain: domain,
Parameters: jsonRule.Parameters,
}
}
}
return rules
}