-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrowser.go
543 lines (450 loc) · 12 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package browser
import (
"strings"
"github.com/dineshgowda24/browser/matchers"
)
const (
userAgentSizeLimit = 2048 // 2KiB
)
// Matcher is an interface for user agent matchers.
// A matcher is used to detect a match from the user agent string.
type Matcher interface {
Match() bool
Name() string
}
// BrowserMatcher is an interface for browser matchers.
type BrowserMatcher interface {
Matcher // BrowserMatcher implements Matcher interface.
Version() string // Version returns the full version of the browser.
}
// Browser is a struct that contains information about the user agent's browser.
// It contains information about the browser's name, version, platform and device.
type Browser struct {
userAgent string // the user agent string
platform *Platform // detected platform
device *Device // detected device
bot *Bot // detected bot
matcher BrowserMatcher // detected browser matcher
registered bool // whether the matcher has been registered
}
// NewBrowser creates a new browser based on the user agent string.
// It builds the browser by matching the user agent string with the known browsers.
//
// UserAgentSizeError is returned if the user agent string is larger than the limit.
func NewBrowser(userAgent string) (*Browser, error) {
// if user agent is larger than the limit, return error
if len(userAgent) > userAgentSizeLimit {
return nil, ErrUserAgentSizeExceeded
}
p, _ := NewPlatform(userAgent)
d, _ := NewDevice(userAgent)
bot, err := NewBot(userAgent)
if err != nil {
return nil, err
}
b := &Browser{
userAgent: userAgent,
platform: p,
device: d,
bot: bot,
}
b.register()
return b, nil
}
// register registers the browser matcher detected from the user agent string.
func (b *Browser) register() {
// add all your browser matchers here, order matters
parser := matchers.NewUAParser(b.userAgent)
matchers := []BrowserMatcher{
matchers.NewAlipay(parser),
matchers.NewNokia(parser),
matchers.NewUCBrowser(parser),
matchers.NewBlackBerry(parser),
matchers.NewBrave(parser),
matchers.NewOpera(parser),
matchers.NewOtter(parser),
matchers.NewInstagram(parser),
matchers.NewSnapchat(parser),
matchers.NewWeibo(parser),
matchers.NewMicroMessenger(parser),
matchers.NewQQ(parser),
matchers.NewElectron(parser),
matchers.NewDuckDuckGo(parser),
matchers.NewGoogleSearchApp(parser),
matchers.NewHuaweiBrowser(parser),
matchers.NewKonqueror(parser),
matchers.NewMaxthon(parser),
matchers.NewMiuiBrowser(parser),
matchers.NewPaleMoon(parser),
matchers.NewPuffin(parser),
matchers.NewEdge(parser),
matchers.NewInternetExplorer(parser),
matchers.NewSamsungBrowser(parser),
matchers.NewSogouBrowser(parser),
matchers.NewVivaldi(parser),
matchers.NewVivoBrowser(parser),
matchers.NewSputnik(parser),
matchers.NewYaaniBrowser(parser),
matchers.NewYandex(parser),
matchers.NewFirefox(parser),
matchers.NewChrome(parser), // chrome should be before safari
matchers.NewSafari(parser), // chrome and safari must be at the end
matchers.NewUnknown(parser),
}
for _, matcher := range matchers {
if matcher.Match() {
b.matcher = matcher
break
}
}
b.registered = true
}
// getMatcher returns the browser matcher detected from the user agent string.
// If the browser matcher has not been registered, it will be registered.
func (b *Browser) getMatcher() BrowserMatcher {
if !b.registered {
b.register()
}
return b.matcher
}
// Name returns the name of the matched browser.
func (b *Browser) Name() string {
return b.getMatcher().Name()
}
// Version returns the full version of the matched browser.
// Examples:
// - 12.1.1
// - 04.34
func (b *Browser) Version() string {
return b.getMatcher().Version()
}
// ShortVersion returns the short version of the version of the matched browser.
// Below are some examples of how the short version is returned:
//
// 12.1.1 => 12
//
// 04.34 => 04
func (b *Browser) ShortVersion() string {
return strings.Split(b.Version(), ".")[0]
}
// Platform returns the platform of the browser.
// It can used to further query information about the platform.
func (b *Browser) Platform() *Platform {
return b.platform
}
// Device returns the device of the browser.
// It can used to further query information about the device.
func (b *Browser) Device() *Device {
return b.device
}
// Bot returns the bot of the browser.
func (b *Browser) Bot() *Bot {
return b.bot
}
// IsAliPay returns true if the browser is AliPay.
//
// https://www.alipay.com/
func (b *Browser) IsAlipay() bool {
if _, ok := b.getMatcher().(*matchers.Alipay); ok {
return true
}
return false
}
// IsNokia returns true if the browser is Nokia.
//
// https://www.nokia.com/
func (b *Browser) IsNokia() bool {
if _, ok := b.getMatcher().(*matchers.Nokia); ok {
return true
}
return false
}
// IsUCBrowser returns true if the browser is UCBrowser.
//
// https://www.ucweb.com/
func (b *Browser) IsUCBrowser() bool {
if _, ok := b.getMatcher().(*matchers.UCBrowser); ok {
return true
}
return false
}
// IsBlackBerry returns true if the browser is BlackBerry.
//
// https://www.blackberry.com/
func (b *Browser) IsBlackBerry() bool {
if _, ok := b.getMatcher().(*matchers.BlackBerry); ok {
return true
}
return false
}
// IsBrave returns true if the browser is Brave.
//
// https://brave.com/
func (b *Browser) IsBrave() bool {
if _, ok := b.getMatcher().(*matchers.Brave); ok {
return true
}
return false
}
// IsOpera returns true if the browser is Opera.
//
// https://www.opera.com/
func (b *Browser) IsOpera() bool {
if _, ok := b.getMatcher().(*matchers.Opera); ok {
return true
}
return false
}
// IsOtter returns true if the browser is Otter.
//
// https://otter-browser.org/
func (b *Browser) IsOtter() bool {
if _, ok := b.getMatcher().(*matchers.Otter); ok {
return true
}
return false
}
// IsInstagram returns true if the browser is Instagram.
//
// https://www.instagram.com/
func (b *Browser) IsInstagram() bool {
if _, ok := b.getMatcher().(*matchers.Instagram); ok {
return true
}
return false
}
// IsSnapchat returns true if the browser is Snapchat.
//
// https://www.snapchat.com/
func (b *Browser) IsSnapchat() bool {
if _, ok := b.getMatcher().(*matchers.Snapchat); ok {
return true
}
return false
}
// IsWeibo returns true if the browser is Weibo.
//
// https://weibo.com/
func (b *Browser) IsWeibo() bool {
if _, ok := b.getMatcher().(*matchers.Weibo); ok {
return true
}
return false
}
// IsWechat returns true if the browser is Wechat.
// Wechat is an alias for MicroMessenger.
func (b *Browser) IsWechat() bool {
return b.IsMicroMessenger()
}
// IsMicroMessenger returns true if the browser is MicroMessenger.
func (b *Browser) IsMicroMessenger() bool {
if _, ok := b.getMatcher().(*matchers.MicroMessenger); ok {
return true
}
return false
}
// IsQQ returns true if the browser is QQ.
//
// https://browser.qq.com/
func (b *Browser) IsQQ() bool {
if _, ok := b.getMatcher().(*matchers.QQ); ok {
return true
}
return false
}
// IsElectron returns true if the browser is Electron.
//
// https://www.electronjs.org/
func (b *Browser) IsElectron() bool {
if _, ok := b.getMatcher().(*matchers.Electron); ok {
return true
}
return false
}
// IsFirefox returns true if the browser is Firefox.
//
// https://www.mozilla.org/firefox/
func (b *Browser) IsFirefox() bool {
if _, ok := b.getMatcher().(*matchers.Firefox); ok {
return true
}
return false
}
// IsDuckDuckGo returns true if the browser is DuckDuckGo.
//
// https://duckduckgo.com/
func (b *Browser) IsDuckDuckGo() bool {
if _, ok := b.getMatcher().(*matchers.DuckDuckGo); ok {
return true
}
return false
}
// IsGoogleSearchApp returns true if the browser is GoogleSearchApp.
//
// https://www.google.com/search/about/
func (b *Browser) IsGoogleSearchApp() bool {
if _, ok := b.getMatcher().(*matchers.GoogleSearchApp); ok {
return true
}
return false
}
// IsHuaweiBrowser returns true if the browser is HuaweiBrowser.
//
// https://consumer.huawei.com/en/mobileservices/browser/
func (b *Browser) IsHuaweiBrowser() bool {
if _, ok := b.getMatcher().(*matchers.HuaweiBrowser); ok {
return true
}
return false
}
// IsKonqueror returns true if the browser is Konqueror.
//
// https://konqueror.org/
func (b *Browser) IsKonqueror() bool {
if _, ok := b.getMatcher().(*matchers.Konqueror); ok {
return true
}
return false
}
// IsMaxthon returns true if the browser is Maxthon.
//
// https://www.maxthon.com/
func (b *Browser) IsMaxthon() bool {
if _, ok := b.getMatcher().(*matchers.Maxthon); ok {
return true
}
return false
}
// IsMiuiBrowser returns true if the browser is MiuiBrowser.
func (b *Browser) IsMiuiBrowser() bool {
if _, ok := b.getMatcher().(*matchers.MiuiBrowser); ok {
return true
}
return false
}
// IsPaleMoon returns true if the browser is PaleMoon.
//
// https://www.palemoon.org/
func (b *Browser) IsPaleMoon() bool {
if _, ok := b.getMatcher().(*matchers.PaleMoon); ok {
return true
}
return false
}
// IsPuffin returns true if the browser is Puffin.
//
// https://www.puffin.com/
func (b *Browser) IsPuffin() bool {
if _, ok := b.getMatcher().(*matchers.Puffin); ok {
return true
}
return false
}
// IsEdge returns true if the browser is Edge.
func (b *Browser) IsEdge() bool {
if _, ok := b.getMatcher().(*matchers.Edge); ok {
return true
}
return false
}
// IsInternetExplorer returns true if the browser is Internet Explorer.
func (b *Browser) IsInternetExplorer() bool {
if _, ok := b.getMatcher().(*matchers.InternetExplorer); ok {
return true
}
return false
}
// IsSamsungBrowser returns true if the browser is SamsungBrowser.
func (b *Browser) IsSamsungBrowser() bool {
if _, ok := b.getMatcher().(*matchers.SamsungBrowser); ok {
return true
}
return false
}
// IsSougouBrowser returns true if the browser is SougouBrowser.
//
// https://www.sogou.com/
func (b *Browser) IsSougouBrowser() bool {
if _, ok := b.getMatcher().(*matchers.SogouBrowser); ok {
return true
}
return false
}
// IsVivaldi returns true if the browser is Vivaldi.
//
// https://vivaldi.com/
func (b *Browser) IsVivaldi() bool {
if _, ok := b.getMatcher().(*matchers.Vivaldi); ok {
return true
}
return false
}
// IsVivoBrowser returns true if the browser is VivoBrowser.
//
// https://www.vivo.com/
func (b *Browser) IsVivoBrowser() bool {
if _, ok := b.getMatcher().(*matchers.VivoBrowser); ok {
return true
}
return false
}
// IsSputnik returns true if the browser is Sputnik.
func (b *Browser) IsSputnik() bool {
if _, ok := b.getMatcher().(*matchers.Sputnik); ok {
return true
}
return false
}
// IsYaaniBrowser returns true if the browser is YaaniBrowser.
func (b *Browser) IsYaaniBrowser() bool {
if _, ok := b.getMatcher().(*matchers.YaaniBrowser); ok {
return true
}
return false
}
// IsYandex returns true if the browser is Yandex.
//
// https://yandex.com/
func (b *Browser) IsYandex() bool {
if _, ok := b.getMatcher().(*matchers.Yandex); ok {
return true
}
return false
}
// IsChrome returns true if the browser is Chrome.
//
// https://www.google.com/chrome/
func (b *Browser) IsChrome() bool {
if _, ok := b.getMatcher().(*matchers.Chrome); ok {
return true
}
return false
}
// IsSafari returns true if the browser is Safari.
//
// https://www.apple.com/safari/
func (b *Browser) IsSafari() bool {
if _, ok := b.getMatcher().(*matchers.Safari); ok {
return true
}
return false
}
// IsUnknown returns true if the browser is Unknown.
// Browsers are considered unknown when they do not match any of the known browsers.
func (b *Browser) IsUnknown() bool {
if _, ok := b.getMatcher().(*matchers.Unknown); ok {
return true
}
return false
}
// IsBrowserKnown returns true if a match was found for the browser.
func (b *Browser) IsBrowserKnown() bool {
return !b.IsUnknown()
}
// IsSafariWebappMode returns true if the browser is Safari Webapp Mode.
// It is true for iOS devices which have AppleWebKit in the user agent string.
func (b *Browser) IsSafariWebappMode() bool {
d := b.Device()
return (d.IsIPad() || d.IsIPhone()) &&
strings.Contains(b.userAgent, "AppleWebKit")
}