-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos.go
177 lines (166 loc) · 4.27 KB
/
os.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
package useragent
import (
"fmt"
"strings"
)
type Os struct {
Name string
Version string
}
// DeviceConstants
var possibleOs = map[string]string{
"microsoft": "Windows (iTunes)",
"windows nt": "Windows RT",
"windows phone": "Windows Phone",
"bb": "Blackberry 10",
"blackberry": "Blackberry",
"tizen": "Tizen",
"kaios": "KaiOS",
// android|webos|palm\sos|qnx|bada|rim\stablet\sos|meego|sailfish|contiki)
"webos": "WebOS",
"Palm": "PalmOS",
"qnx": "QNX",
"bada": "BadaOS",
"rim": "RIM",
"meego": "MeeGo",
"sailfish": "SailfishOS",
"contiki": "ContikiOS",
"symbian": "SymbianOS",
"symbos": "SymbianOS",
"s60": "SymbianOS",
"series40": "Series 40 OS",
"nintendo": "Nintendo",
"playstation": "Playstation",
"mint": "Mint",
"mageia": "MageiaOS",
"vectorlinux": "VectorLinux",
"hurd": "Hurd",
"linux": "Linux",
"gnu": "GNU",
"cros": "ChromiumOS",
"sunos": "SolarisOS",
"haiku": "HaikuOS",
"joli": "Jolie",
"ubuntu": "Ubuntu",
"debian": "Debian",
"suse": "SUSE",
"opensuse": "SUSE",
"gentoo": "Gentoo",
"arch": "Arch",
"slackware": "Slackware",
"fedora": "Fedora",
"mandriva": "Mandriva",
"centos": "CentOS",
"pclinuxos": "PCLinuxOS",
"redhat": "RedHat",
"zenwalk": "ZenWalk",
"linpus": "Linpus",
"dragonfly": "DragonFly",
"freebsd": "FreeBSD",
"netbsd": "NetBSD",
"openbsd": "OpenBSD",
"pc-bsd": "PC-BSD",
"cfnetwork": "iOS",
"ip": "iOS",
"mac": "MacOS",
"solaris": "Solaris",
"opensolaris": "Solaris",
"aix": "AIX",
"plan": "Plan9",
"minix": "Minix",
"beos": "BeOS",
"os2": "OS2",
"amigaos": "AmigaOS",
"morphos": "MorphOS",
"riscos": "RISCOS",
"openvms": "OpenVMS",
"fuchsia": "Fuchsia",
"unix": "Unix",
"windows 98": "Windows 98",
"windows 95": "Windows 95",
"windows me": "Windows ME",
"windows xp": "Windows XP",
"windows vista": "Windows Vista",
"windows 7": "Windows 7",
"windows 8": "Windows 8",
"windows 8.1": "Windows 8.1",
"windows 10": "Windows 10",
"windows rt": "Windows RT",
"windows 2000": "Windows 2000",
"windows": "Windows",
"android": "Android",
}
func (ua *UserAgent) guessOS() {
os := Os{}
osToken := ""
for token, _ := range ua.clients {
if val, ok := possibleOs[strings.ToLower(token)]; ok {
os.Name = val
os.Version = ua.clients[token]
osToken = token
break
}
}
// if ua.exists(osToken) {
// version := ua.clients[osToken]
// os.Version = nVersion
// }
if osToken == "" {
for token := range ua.clients {
switch {
case strings.HasPrefix(token, "CPU"):
os.Name, os.Version = parseIOS(token)
case strings.HasPrefix(token, "PPC"):
os.Name, os.Version = parseMacOS(token)
case ua.exists("windows"):
os.Name, os.Version = parseWindows(ua)
}
}
}
// parse windows
if osToken == "windows" || osToken == "windows nt" {
os.Name, os.Version = parseWindows(ua)
}
ua.OS = os
}
func parseIOS(version string) (string, string) {
// iPhone OS 5_1_1 like Mac OS X
name := "MacOS"
versionSlice := strings.Split(version, " ")
if versionSlice[1] == "iPhone" || versionSlice[1] == "iPad" {
name = "iOS"
}
newVersion := strings.ReplaceAll(versionSlice[3], "_", ".")
return name, newVersion
}
func parseMacOS(version string) (string, string) {
// PPC Mac OS X 10.5 name := "MacOS"
name := "MacOS"
versionSlice := strings.Split(version, " ")
return name, versionSlice[4]
}
func parseWindows(ua *UserAgent) (string, string) {
name := "Windows"
version := ""
windowsMapping := map[string]string{
"4.90": "ME",
"NT3.51": "NT 3.11",
"NT4.0": "NT 4.0",
"NT5.0": "2000",
"NT5.1": "XP",
"NT5.2": "XP",
"NT6.1": "7",
"NT6.2": "8",
"NT6.3": "8.1",
"NT6.4": "10",
"NT10.0": "10",
"ARM": "RT",
}
if ua.exists("windows nt") {
ntString := fmt.Sprintf("NT%s", ua.getValue("windows nt"))
version = windowsMapping[ntString]
} else {
version = "RT"
}
return name, version
}