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
|
// Copyright (C) 2012-2017 Miquel Sabaté Solà <mikisabate@gmail.com>
// This file is licensed under the MIT license.
// See the LICENSE file.
package user_agent
import "strings"
// Represents full information on the operating system extracted from the user agent.
type OSInfo struct {
// Full name of the operating system. This is identical to the output of ua.OS()
FullName string
// Name of the operating system. This is sometimes a shorter version of the
// operating system name, e.g. "Mac OS X" instead of "Intel Mac OS X"
Name string
// Operating system version, e.g. 7 for Windows 7 or 10.8 for Max OS X Mountain Lion
Version string
}
// Normalize the name of the operating system. By now, this just
// affects to Windows NT.
//
// Returns a string containing the normalized name for the Operating System.
func normalizeOS(name string) string {
sp := strings.SplitN(name, " ", 3)
if len(sp) != 3 || sp[1] != "NT" {
return name
}
switch sp[2] {
case "5.0":
return "Windows 2000"
case "5.01":
return "Windows 2000, Service Pack 1 (SP1)"
case "5.1":
return "Windows XP"
case "5.2":
return "Windows XP x64 Edition"
case "6.0":
return "Windows Vista"
case "6.1":
return "Windows 7"
case "6.2":
return "Windows 8"
case "6.3":
return "Windows 8.1"
case "10.0":
return "Windows 10"
}
return name
}
// Guess the OS, the localization and if this is a mobile device for a
// Webkit-powered browser.
//
// The first argument p is a reference to the current UserAgent and the second
// argument is a slice of strings containing the comment.
func webkit(p *UserAgent, comment []string) {
if p.platform == "webOS" {
p.browser.Name = p.platform
p.os = "Palm"
if len(comment) > 2 {
p.localization = comment[2]
}
p.mobile = true
} else if p.platform == "Symbian" {
p.mobile = true
p.browser.Name = p.platform
p.os = comment[0]
} else if p.platform == "Linux" {
p.mobile = true
if p.browser.Name == "Safari" {
p.browser.Name = "Android"
}
if len(comment) > 1 {
if comment[1] == "U" {
if len(comment) > 2 {
p.os = comment[2]
} else {
p.mobile = false
p.os = comment[0]
}
} else {
p.os = comment[1]
}
}
if len(comment) > 3 {
p.localization = comment[3]
}
} else if len(comment) > 0 {
if len(comment) > 3 {
p.localization = comment[3]
}
if strings.HasPrefix(comment[0], "Windows NT") {
p.os = normalizeOS(comment[0])
} else if len(comment) < 2 {
p.localization = comment[0]
} else if len(comment) < 3 {
if !p.googleBot() {
p.os = normalizeOS(comment[1])
}
} else {
p.os = normalizeOS(comment[2])
}
if p.platform == "BlackBerry" {
p.browser.Name = p.platform
if p.os == "Touch" {
p.os = p.platform
}
}
}
}
// Guess the OS, the localization and if this is a mobile device
// for a Gecko-powered browser.
//
// The first argument p is a reference to the current UserAgent and the second
// argument is a slice of strings containing the comment.
func gecko(p *UserAgent, comment []string) {
if len(comment) > 1 {
if comment[1] == "U" {
if len(comment) > 2 {
p.os = normalizeOS(comment[2])
} else {
p.os = normalizeOS(comment[1])
}
} else {
if p.platform == "Android" {
p.mobile = true
p.platform, p.os = normalizeOS(comment[1]), p.platform
} else if comment[0] == "Mobile" || comment[0] == "Tablet" {
p.mobile = true
p.os = "FirefoxOS"
} else {
if p.os == "" {
p.os = normalizeOS(comment[1])
}
}
}
// Only parse 4th comment as localization if it doesn't start with rv:.
// For example Firefox on Ubuntu contains "rv:XX.X" in this field.
if len(comment) > 3 && !strings.HasPrefix(comment[3], "rv:") {
p.localization = comment[3]
}
}
}
// Guess the OS, the localization and if this is a mobile device
// for Internet Explorer.
//
// The first argument p is a reference to the current UserAgent and the second
// argument is a slice of strings containing the comment.
func trident(p *UserAgent, comment []string) {
// Internet Explorer only runs on Windows.
p.platform = "Windows"
// The OS can be set before to handle a new case in IE11.
if p.os == "" {
if len(comment) > 2 {
p.os = normalizeOS(comment[2])
} else {
p.os = "Windows NT 4.0"
}
}
// Last but not least, let's detect if it comes from a mobile device.
for _, v := range comment {
if strings.HasPrefix(v, "IEMobile") {
p.mobile = true
return
}
}
}
// Guess the OS, the localization and if this is a mobile device
// for Opera.
//
// The first argument p is a reference to the current UserAgent and the second
// argument is a slice of strings containing the comment.
func opera(p *UserAgent, comment []string) {
slen := len(comment)
if strings.HasPrefix(comment[0], "Windows") {
p.platform = "Windows"
p.os = normalizeOS(comment[0])
if slen > 2 {
if slen > 3 && strings.HasPrefix(comment[2], "MRA") {
p.localization = comment[3]
} else {
p.localization = comment[2]
}
}
} else {
if strings.HasPrefix(comment[0], "Android") {
p.mobile = true
}
p.platform = comment[0]
if slen > 1 {
p.os = comment[1]
if slen > 3 {
p.localization = comment[3]
}
} else {
p.os = comment[0]
}
}
}
// Guess the OS. Android browsers send Dalvik as the user agent in the
// request header.
//
// The first argument p is a reference to the current UserAgent and the second
// argument is a slice of strings containing the comment.
func dalvik(p *UserAgent, comment []string) {
slen := len(comment)
if strings.HasPrefix(comment[0], "Linux") {
p.platform = comment[0]
if slen > 2 {
p.os = comment[2]
}
p.mobile = true
}
}
// Given the comment of the first section of the UserAgent string,
// get the platform.
func getPlatform(comment []string) string {
if len(comment) > 0 {
if comment[0] != "compatible" {
if strings.HasPrefix(comment[0], "Windows") {
return "Windows"
} else if strings.HasPrefix(comment[0], "Symbian") {
return "Symbian"
} else if strings.HasPrefix(comment[0], "webOS") {
return "webOS"
} else if comment[0] == "BB10" {
return "BlackBerry"
}
return comment[0]
}
}
return ""
}
// Detect some properties of the OS from the given section.
func (p *UserAgent) detectOS(s section) {
if s.name == "Mozilla" {
// Get the platform here. Be aware that IE11 provides a new format
// that is not backwards-compatible with previous versions of IE.
p.platform = getPlatform(s.comment)
if p.platform == "Windows" && len(s.comment) > 0 {
p.os = normalizeOS(s.comment[0])
}
// And finally get the OS depending on the engine.
switch p.browser.Engine {
case "":
p.undecided = true
case "Gecko":
gecko(p, s.comment)
case "AppleWebKit":
webkit(p, s.comment)
case "Trident":
trident(p, s.comment)
}
} else if s.name == "Opera" {
if len(s.comment) > 0 {
opera(p, s.comment)
}
} else if s.name == "Dalvik" {
if len(s.comment) > 0 {
dalvik(p, s.comment)
}
} else {
// Check whether this is a bot or just a weird browser.
p.undecided = true
}
}
// Returns a string containing the platform..
func (p *UserAgent) Platform() string {
return p.platform
}
// Returns a string containing the name of the Operating System.
func (p *UserAgent) OS() string {
return p.os
}
// Returns a string containing the localization.
func (p *UserAgent) Localization() string {
return p.localization
}
// Return OS name and version from a slice of strings created from the full name of the OS.
func osName(osSplit []string) (name, version string) {
if len(osSplit) == 1 {
name = osSplit[0]
version = ""
} else {
// Assume version is stored in the last part of the array.
nameSplit := osSplit[:len(osSplit)-1]
version = osSplit[len(osSplit)-1]
// Nicer looking Mac OS X
if len(nameSplit) >= 2 && nameSplit[0] == "Intel" && nameSplit[1] == "Mac" {
nameSplit = nameSplit[1:]
}
name = strings.Join(nameSplit, " ")
if strings.Contains(version, "x86") || strings.Contains(version, "i686") {
// x86_64 and i868 are not Linux versions but architectures
version = ""
} else if version == "X" && name == "Mac OS" {
// X is not a version for Mac OS.
name = name + " " + version
version = ""
}
}
return name, version
}
// Returns combined information for the operating system.
func (p *UserAgent) OSInfo() OSInfo {
// Special case for iPhone weirdness
os := strings.Replace(p.os, "like Mac OS X", "", 1)
os = strings.Replace(os, "CPU", "", 1)
os = strings.Trim(os, " ")
osSplit := strings.Split(os, " ")
// Special case for x64 edition of Windows
if os == "Windows XP x64 Edition" {
osSplit = osSplit[:len(osSplit)-2]
}
name, version := osName(osSplit)
// Special case for names that contain a forward slash version separator.
if strings.Contains(name, "/") {
s := strings.Split(name, "/")
name = s[0]
version = s[1]
}
// Special case for versions that use underscores
version = strings.Replace(version, "_", ".", -1)
return OSInfo{
FullName: p.os,
Name: name,
Version: version,
}
}
|