/
match.go
291 lines (261 loc) · 7.26 KB
/
match.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
package useragent
import (
"sort"
str "github.com/boyter/go-string"
)
const (
// These are enum constants for the match type.
BrowserMatch = 1
OSMatch = 2
TypeMatch = 3
VersionMatch = 4
UnknownMatch = 0
// The following constants are used to determine agents.
// Browsers.
Chrome = "Chrome"
Edge = "Edge"
Firefox = "Firefox"
IE = "IE"
Opera = "Opera"
OperaMini = "Mini"
Safari = "Safari"
Vivaldi = "Vivaldi"
Samsung = "SamsungBrowser"
Nintendo = "NintendoBrowser"
// Operating Systems.
Android = "Android"
ChromeOS = "ChromeOS"
IOS = "iOS"
Linux = "Linux"
MacOS = "MacOS"
Windows = "Windows"
// Types.
Desktop = "Desktop"
Mobile = "Mobile"
// We need a separate type for mobile devices since some user agents use "Mobile/"
// appended with a device ID. We need to handle these separately to strip those IDs
// out.
MobileDevice = "MobileDevice"
Tablet = "Tablet"
TV = "TV"
Bot = "Bot"
// Version.
Version = "Version"
)
// matchMap is a map of user agent types to their matching strings.
// These are the tokens saved into the trie when populating it.
//
// Matching tokens are ordered by precedence. The first match is the
// most important match.
var matchMap = map[string][]string{
// Browsers
Chrome: {"CriOS", Chrome},
Edge: {"EdgiOS", Edge, "Edg"},
Firefox: {"FxiOS", Firefox},
IE: {"MSIE", "Trident"},
Opera: {"OPiOS", "OPR", Opera},
OperaMini: {OperaMini},
Safari: {Safari},
Vivaldi: {Vivaldi},
Samsung: {Samsung},
Nintendo: {Nintendo},
// Operating Systems
Android: {Android},
ChromeOS: {"CrOS"},
IOS: {"iPhone", "iPad", "iPod"},
Linux: {Linux},
MacOS: {"Macintosh"},
Windows: {"Windows NT", "WindowsNT"},
// Types
Desktop: {Desktop, "Ubuntu", "Fedora"},
Mobile: {Mobile},
MobileDevice: {"ONEPLUS", "Huawei", "HTC", "Galaxy", "iPhone", "iPod", "Windows Phone", "WindowsPhone", "LG"},
Tablet: {Tablet, "Touch", "iPad", "Nintendo Switch", "NintendoSwitch", "Kindle"},
TV: {TV, "Large Screen", "LargeScreen", "Smart Display", "SmartDisplay", "PLAYSTATION", "PlayStation", "ADT-2", "ADT-1", "CrKey", "Roku", "AFT", "Web0S", "Nexus Player", "Xbox", "XBOX", "Nintendo WiiU", "NintendoWiiU"},
Bot: {Bot, "bot", "Slurp", "LinkCheck", "QuickLook", "Haosou", "Yahoo Ad", "YahooAd", "GoogleProber", "GoogleProducer", "Mediapartners", "Headless", "facebookexternalhit", "facebookcatalog"},
// Version
Version: {Version},
}
// matchPrecedenceMap is a map of user agent types to their importance
// in determining what is the actual browser/device/OS being used.
//
// For example, Chrome user agents also contain the string "Safari" at
// the end of the user agent. This means that if we only check for
// "Safari" first, we will incorrectly determine the browser to be Safari
// instead of Chrome.
//
// By setting a precedence, we can determine which match is more important
// and use that as the final result.
var matchPrecedenceMap = map[string]uint8{
// Browsers
Safari: 1, // Is always at the end of a Chrome user agent.
Chrome: 2,
Firefox: 3,
IE: 4,
Opera: 5,
OperaMini: 6,
Edge: 7,
Vivaldi: 8,
Samsung: 9,
Nintendo: 10,
// Operating Systems
Linux: 1,
Android: 2,
IOS: 3,
ChromeOS: 4,
MacOS: 5,
Windows: 6,
// Types
Desktop: 1,
Mobile: 2,
MobileDevice: 3,
Tablet: 4,
TV: 5,
Bot: 6,
}
// MatchResults contains the information from MatchTokenIndexes.
type MatchResults struct {
EndIndex int
Match string
// 0: Unknown, 1: Browser, 2: OS, 3: Type
MatchType uint8
// Precedence value for each result type to determine which result should be overwritten.
// Higher valeus are overwritten by lower values.
Precedence uint8
}
// GetMatchType returns the match type of a match result using the MatchPrecedenceMap.
func GetMatchType(match string) uint8 {
switch match {
case Chrome, Edge, Firefox, IE, Opera, OperaMini, Safari, Vivaldi, Samsung, Nintendo:
return BrowserMatch
case Android, ChromeOS, IOS, Linux, MacOS, Windows:
return OSMatch
case Desktop, Mobile, MobileDevice, Tablet, Bot, TV:
return TypeMatch
case Version:
return VersionMatch
default:
return UnknownMatch
}
}
// MatchTokenIndexes finds the start and end indexes of necessary tokens
// that match a known browser, device, or OS. This is used to determine
// when to insert a result value into the trie.
func MatchTokenIndexes(ua string) []MatchResults {
var results []MatchResults
exists := make(map[string]bool)
for key, match := range matchMap {
for _, m := range match {
// Check if key match doesn't already exist in results.
// This is to prevent duplicate matches in the trie.
if exists[key] {
continue
}
indexes := str.IndexAll(ua, m, -1)
// Return the last match.
if len(indexes) == 0 {
continue
}
lastIndex := indexes[len(indexes)-1]
// Add the match to the results.
matchType := GetMatchType(key)
results = append(results, MatchResults{EndIndex: lastIndex[1], Match: key, MatchType: matchType, Precedence: matchPrecedenceMap[key]})
exists[key] = true
}
}
// Sort the results by EndIndex in descending order.
// This allows us to determine the first matching token in the user agent
// when we iterate over it when populating the trie.
//
// Some tokens may have the same EndIndex, so we need to sort by Match key
// to make it deterministic.
sort.Slice(results, func(i, j int) bool {
if results[i].EndIndex == results[j].EndIndex {
return results[i].Match < results[j].Match
}
return results[i].EndIndex > results[j].EndIndex
})
return results
}
// This adds a matching constant to a user agent struct.
func (ua *UserAgent) addMatch(result Result) bool {
// Browsers
if result.Type == BrowserMatch && result.Precedence > ua.precedence.Browser {
switch result.Match {
case Chrome:
ua.Browser = Chrome
case Edge:
ua.Browser = Edge
case Firefox:
ua.Browser = Firefox
case IE:
ua.Browser = IE
case Opera:
ua.Browser = Opera
case OperaMini:
ua.Browser = OperaMini
ua.Mobile = true
case Safari:
ua.Browser = Safari
case Vivaldi:
ua.Browser = Vivaldi
case Samsung:
ua.Browser = Samsung
case Nintendo:
ua.Browser = Nintendo
}
ua.precedence.Browser = result.Precedence
return true
}
// Operating Systems
if result.Type == OSMatch && result.Precedence > ua.precedence.OS {
switch result.Match {
case Android:
ua.OS = Android
case ChromeOS:
ua.OS = ChromeOS
ua.Desktop = true
case IOS:
ua.OS = IOS
if !ua.Tablet {
ua.Mobile = true
}
case Linux:
ua.OS = Linux
if !ua.Tablet && !ua.TV {
ua.Desktop = true
}
case MacOS:
ua.OS = MacOS
ua.Desktop = true
case Windows:
ua.OS = Windows
ua.Desktop = true
}
ua.precedence.OS = result.Precedence
return true
}
// Types
if result.Type == TypeMatch && result.Precedence > ua.precedence.Type {
switch result.Match {
case Desktop:
ua.Desktop = true
case Tablet:
if ua.Mobile {
ua.Mobile = false
}
ua.Tablet = true
case Mobile, MobileDevice:
if !ua.Tablet {
ua.Mobile = true
}
case TV:
ua.TV = true
case Bot:
ua.Bot = true
}
ua.precedence.Type = result.Precedence
return true
}
return false
}