-
Notifications
You must be signed in to change notification settings - Fork 1
/
referer.go
123 lines (98 loc) · 2.57 KB
/
referer.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
package main
import (
_ "embed"
"net/url"
"gopkg.in/yaml.v2"
)
//go:embed embed/referer.yml
var referer []byte
const (
refererTypeNone uint8 = 0
refererTypeOther uint8 = 1
refererTypeSearchEngine uint8 = 2
refererTypeSocial uint8 = 3
refererTypeMailProvider uint8 = 4
)
type refererParser struct {
domainMap domainMap
}
type refererData struct {
RefExist bool `json:"e"`
RefURL string `json:"u"`
RefName string `json:"n"`
RefExternalHost string `json:"h"`
RefExternalDomain string `json:"d"`
RefScheme string `json:"s"`
RefType uint8 `json:"t"`
}
type domainMap map[string]*refererData
type refererYAML struct {
SearchEngine map[string][]string `yaml:"search_engine"`
SocialMedia map[string][]string `yaml:"social_media"`
MailProvider map[string][]string `yaml:"mail_provider"`
}
func getRefererData(name string, refererType uint8) *refererData {
i := refererData{
RefName: name,
RefType: refererType,
}
return &i
}
func newRefererParser() *refererParser {
domainMap := domainMap{}
refererYAML := refererYAML{}
err := yaml.Unmarshal(referer, &refererYAML)
if err != nil {
panic(err)
}
// SearchEngine
for name, domains := range refererYAML.SearchEngine {
for _, domain := range domains {
domainMap[domain] = getRefererData(name, refererTypeSearchEngine)
}
}
// SocialMedia
for name, domains := range refererYAML.SocialMedia {
for _, domain := range domains {
domainMap[domain] = getRefererData(name, refererTypeSocial)
}
}
// MailProvider
for name, domains := range refererYAML.MailProvider {
for _, domain := range domains {
domainMap[domain] = getRefererData(name, refererTypeMailProvider)
}
}
result := refererParser{}
result.domainMap = domainMap
return &result
}
func (rp *refererParser) parse(currentURL *url.URL, refererURL *url.URL) refererData {
result := refererData{
RefType: refererTypeNone,
}
if refererURL == nil || currentURL == nil {
return result
}
result.RefURL = refererURL.String()
result.RefScheme = refererURL.Scheme
rHost := getDomain(refererURL)
cHost := getDomain(currentURL)
if cHost != rHost {
result.RefExist = true
rDomain := getSecondDomainLevel(refererURL)
cDomain := getSecondDomainLevel(currentURL)
if rDomain != cDomain {
result.RefExternalDomain = rDomain
}
result.RefExternalHost = rHost
result.RefType = refererTypeOther
result.RefName = rDomain
if val, ok := rp.domainMap[rHost]; ok {
result.RefName = val.RefName
result.RefType = val.RefType
return result
}
}
return result
}