-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbrowser.go
380 lines (342 loc) · 7.58 KB
/
browser.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Written by https://xojoc.pw. GPLv3 or later.
package useragent
import (
"net/url"
"github.com/blang/semver"
)
// Keep them sorted
var browsers = map[string]*url.URL{
"Chrome": u("http://www.chromium.org/"),
"Dillo": u("http://www.dillo.org/"),
"Edge": u("https://www.microsoft.com/en-us/windows/microsoft-edge"),
"Firefox": u("https://www.mozilla.org/en-US/firefox"),
"IceCat": u("https://www.gnu.org/software/gnuzilla/"),
"Iceweasel": u("https://wiki.debian.org/Iceweasel"),
"NetSurf": u("http://www.netsurf-browser.org/"),
"Opera": u("http://www.opera.com/"),
"PhantomJS": u("http://phantomjs.org/"),
"Silk": u("http://aws.amazon.com/documentation/silk/"),
"WebView": u("http://developer.android.com/guide/webapps/webview.html"),
}
const (
OSAndroid = "Android"
OSMacOS = "Mac OS X"
OSiOS = "iOS"
OSLinux = "GNU/Linux"
OSWindows = "Windows"
)
func parseBrowser(l *lex) *UserAgent {
for _, f := range []parseFn{parseGecko, parseChromeSafari, parseIE1, parseIE2, parseOperaClassic} {
if ua := f(newLex(l.s)); ua != nil {
return ua
}
}
return nil
}
func parseSecurity(l *lex) Security {
switch {
case l.match("U"):
if l.matchFirst("; ", ";") || l.matchNoConsume(")") {
return SecurityStrong
}
case l.match("I"):
if l.matchFirst("; ", ";") || l.matchNoConsume(")") {
return SecurityWeak
}
case l.match("N"):
if l.matchFirst("; ", ";") || l.matchNoConsume(")") {
return SecurityNone
}
}
return SecurityUnknown
}
func parseMozillaLike(l *lex, ua *UserAgent) bool {
ua.Type = Browser
if !l.match("Mozilla/5.0 (") {
return false
}
switch {
case l.match("X11; "):
ua.Security = parseSecurity(l)
parseUnixLike(l, ua)
case l.match("Android"):
ua.Security = parseSecurity(l)
ua.OS = OSAndroid
if l.match("; Mobile") {
ua.Mobile = true
} else if l.match("; Tablet") {
ua.Tablet = true
}
case l.match("Linux; "):
ua.Security = parseSecurity(l)
if l.match("Android") {
ua.OS = OSAndroid
} else {
return false
}
case l.match("Windows"):
ua.OS = OSWindows
// Windows has version before security
_ = parseOSVersion(l, ua)
l.span("; ")
ua.Security = parseSecurity(l)
case l.match("Macintosh"):
l.span("; ")
ua.Security = parseSecurity(l)
ua.OS = OSMacOS
case l.match("Mobile; "):
ua.Security = parseSecurity(l)
ua.OS = "Firefox OS"
ua.Mobile = true
case l.match("Tablet; "):
ua.Security = parseSecurity(l)
ua.OS = "Firefox OS"
ua.Tablet = true
case l.match("iPad; "):
ua.Security = parseSecurity(l)
ua.OS = OSiOS
ua.Tablet = true
case l.match("iPhone; ") || l.match("iPod; ") || l.match("iPod touch; "):
ua.Security = parseSecurity(l)
ua.OS = OSiOS
ua.Mobile = true
case l.match("Unknown; "):
ua.Security = parseSecurity(l)
parseUnixLike(l, ua)
default:
return false
}
// OS Version is not required, and may be set above
if ua.OSVersion.Equals(semver.Version{}) {
_ = parseOSVersion(l, ua)
}
if _, ok := l.span(") "); !ok {
return false
}
return true
}
// Parse *nix variants (eg inside of a MozillaLike)
func parseUnixLike(l *lex, ua *UserAgent) bool {
switch {
case l.match("Linux") || l.match("Ubuntu"):
ua.OS = OSLinux
case l.match("FreeBSD"):
ua.OS = "FreeBSD"
case l.match("OpenBSD"):
ua.OS = "OpenBSD"
case l.match("NetBSD"):
ua.OS = "NetBSD"
case l.match("Maemo"):
// FIXME: should it be GNU/Linux?
ua.OS = "Maemo"
case l.match("CrOS"):
ua.OS = "CrOS"
default:
// Various distros use "... Distro; Linux x86_64) "
if _, ok := l.spanBefore("Linux", ") "); ok {
ua.OS = OSLinux
return true
}
return false
}
return true
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
func parseGecko(l *lex) *UserAgent {
ua := new()
if !parseMozillaLike(l, ua) {
return nil
}
if !l.match("Gecko/") {
return nil
}
if _, ok := l.span(" "); !ok {
return nil
}
if !parseNameVersion(l, ua) {
return nil
}
if _, ok := l.span("Opera "); ok {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "Opera"
}
return ua
}
// Includes WebKit-based Firefox for iOS
func parseChromeSafari(l *lex) *UserAgent {
ua := new()
if !parseMozillaLike(l, ua) {
return nil
}
if !l.match("AppleWebKit/") {
return nil
}
if _, ok := l.span(" "); !ok {
return nil
}
if !l.match("(KHTML, like Gecko) ") {
return nil
}
if !parseNameVersion(l, ua) {
return nil
}
if ua.Name == "CriOS" {
ua.Name = "Chrome"
} else if ua.Name == "FxiOS" {
ua.Name = "Firefox"
} else if ua.Name == "Version" {
if l.match("Chrome/") {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "WebView"
ua.Type = Library
} else {
if l.match("Mobile/") {
if _, ok := l.span(" "); !ok {
return nil
}
}
if !l.match("Safari/") {
return nil
}
ua.Name = "Safari"
}
} else if ua.Name == "Silk" {
if l.match("like Chrome/") {
if _, ok := l.span(" "); !ok {
return nil
}
} else {
return nil
}
}
if l.match("Mobile") && !ua.Tablet {
ua.Mobile = true
}
if ua.OS == OSAndroid && !ua.Mobile {
ua.Tablet = true
}
// Identify non-Chrome browsers with Chromelike UAs:
if _, ok := l.span("OPR/"); ok {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "Opera"
}
if _, ok := l.span("Edge/"); ok {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "Edge"
}
return ua
}
// pre IE11 uas
func parseIE1(l *lex) *UserAgent {
ua := new()
ua.Type = Browser
if !l.match("Mozilla") {
return nil
}
if _, ok := l.span(" ("); !ok {
return nil
}
l.match("compatible; ")
l.match("Compatible; ")
if !l.match("MSIE ") {
return nil
}
ua.Name = "MSIE"
if !parseVersion(l, ua, ";") {
return nil
}
if !l.match(" Windows NT") {
return nil
}
ua.OS = OSWindows
// swallow the error to preserve backwards compatibility
_ = parseOSVersion(l, ua)
if _, ok := l.span("Opera "); ok {
if !parseVersion(l, ua, " ") {
return nil
}
ua.Name = "Opera"
}
return ua
}
// IE11 changed its uas http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx
func parseIE2(l *lex) *UserAgent {
ua := new()
ua.Type = Browser
if !l.match("Mozilla") {
return nil
}
if _, ok := l.span("(Windows NT"); !ok {
return nil
}
ua.OS = OSWindows
// swallow the error to preserve backwards compatibility
_ = parseOSVersion(l, ua)
if _, ok := l.span("Trident/"); !ok {
return nil
}
if _, ok := l.span("rv:"); !ok {
return nil
}
ua.Name = "MSIE"
if !parseVersion(l, ua, ")") {
return nil
}
return ua
}
// Non-Mozilla Opera UAs
func parseOperaClassic(l *lex) *UserAgent {
ua := new()
if !l.match("Opera/") {
return nil
}
ua.Type = Browser
ua.Name = "Opera"
// Start with the Opera version (versions 9.80+ will overwrite later)
if !parseVersion(l, ua, " ") {
return nil
}
if _, ok := l.span("("); !ok {
return nil
}
switch {
case l.match("Windows"):
ua.OS = OSWindows
case l.match("Macintosh"):
ua.OS = OSMacOS
l.spanBefore("OS X", ")")
default:
if !parseUnixLike(l, ua) {
return nil
}
}
// swallow the error to preserve backwards compatibility
_ = parseOSVersion(l, ua)
// Get security if present, then get to the end of the parens block
if _, ok := l.spanBefore("; ", ")"); ok {
ua.Security = parseSecurity(l)
}
if _, ok := l.span(")"); !ok {
return nil
} else {
// Opera occasionally uses nested parens; for simplicity, skip over all instead of matching
for ok {
_, ok = l.span(")")
}
}
if l.match(" Presto/") {
l.span(" ")
}
if l.match("Version/") && !parseVersion(l, ua, " ") {
return nil
}
return ua
}