/
os.go
258 lines (238 loc) · 6.09 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
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
package parser
import (
"strings"
)
const ParserNameOs = "os"
const FixtureFileOs = "oss.yml"
type OsReg struct {
Regular `yaml:",inline" json:",inline"`
Name string `yaml:"name" json:"name"`
Version string `yaml:"version" json:"version"`
}
// Known operating systems mapped to their internal short codes
var OperatingSystems = map[string]string{
`AIX`: `AIX`,
`AND`: `Android`,
`AMG`: `AmigaOS`,
`ATV`: `Apple TV`,
`ARL`: `Arch Linux`,
`BTR`: `BackTrack`,
`SBA`: `Bada`,
`BEO`: `BeOS`,
`BLB`: `BlackBerry OS`,
`QNX`: `BlackBerry Tablet OS`,
`BMP`: `Brew`,
`CES`: `CentOS`,
`COS`: `Chrome OS`,
`CYN`: `CyanogenMod`,
`DEB`: `Debian`,
`DFB`: `DragonFly`,
`FED`: `Fedora`,
`FOS`: `Firefox OS`,
`FIR`: `Fire OS`,
`BSD`: `FreeBSD`,
`GNT`: `Gentoo`,
`GTV`: `Google TV`,
`HPX`: `HP-UX`,
`HAI`: `Haiku OS`,
`IRI`: `IRIX`,
`INF`: `Inferno`,
`KOS`: `KaiOS`,
`KNO`: `Knoppix`,
`KBT`: `Kubuntu`,
`LIN`: `GNU/Linux`,
`LBT`: `Lubuntu`,
`VLN`: `VectorLinux`,
`MAC`: `Mac`,
`MAE`: `Maemo`,
`MDR`: `Mandriva`,
`SMG`: `MeeGo`,
`MCD`: `MocorDroid`,
`MIN`: `Mint`,
`MLD`: `MildWild`,
`MOR`: `MorphOS`,
`NBS`: `NetBSD`,
`MTK`: `MTK / Nucleus`,
`WII`: `Nintendo`,
`NDS`: `Nintendo Mobile`,
`OS2`: `OS/2`,
`T64`: `OSF1`,
`OBS`: `OpenBSD`,
`ORD`: `Ordissimo`,
`PSP`: `PlayStation Portable`,
`PS3`: `PlayStation`,
`RHT`: `Red Hat`,
`ROS`: `RISC OS`,
`REM`: `Remix OS`,
`RZD`: `RazoDroiD`,
`SAB`: `Sabayon`,
`SSE`: `SUSE`,
`SAF`: `Sailfish OS`,
`SLW`: `Slackware`,
`SOS`: `Solaris`,
`SYL`: `Syllable`,
`SYM`: `Symbian`,
`SYS`: `Symbian OS`,
`S40`: `Symbian OS Series 40`,
`S60`: `Symbian OS Series 60`,
`SY3`: `Symbian^3`,
`TDX`: `ThreadX`,
`TIZ`: `Tizen`,
`TOS`: `TmaxOS`,
`UBT`: `Ubuntu`,
`WTV`: `WebTV`,
`WIN`: `Windows`,
`WCE`: `Windows CE`,
`WIO`: `Windows IoT`,
`WMO`: `Windows Mobile`,
`WPH`: `Windows Phone`,
`WRT`: `Windows RT`,
`XBX`: `Xbox`,
`XBT`: `Xubuntu`,
`YNS`: `YunOs`,
`IOS`: `iOS`,
`POS`: `palmOS`,
`WOS`: `webOS`,
}
// Operating system families mapped to the short codes of the associated operating systems
var OsFamilies = map[string][]string{
`Android`: []string{`AND`, `CYN`, `FIR`, `REM`, `RZD`, `MLD`, `MCD`, `YNS`},
`AmigaOS`: []string{`AMG`, `MOR`},
`Apple TV`: []string{`ATV`},
`BlackBerry`: []string{`BLB`, `QNX`},
`Brew`: []string{`BMP`},
`BeOS`: []string{`BEO`, `HAI`},
`Chrome OS`: []string{`COS`},
`Firefox OS`: []string{`FOS`, `KOS`},
`Gaming Console`: []string{`WII`, `PS3`},
`Google TV`: []string{`GTV`},
`IBM`: []string{`OS2`},
`iOS`: []string{`IOS`},
`RISC OS`: []string{`ROS`},
`GNU/Linux`: []string{`LIN`, `ARL`, `DEB`, `KNO`, `MIN`, `UBT`, `KBT`, `XBT`, `LBT`, `FED`, `RHT`, `VLN`, `MDR`, `GNT`, `SAB`, `SLW`, `SSE`, `CES`, `BTR`, `SAF`, `ORD`, `TOS`},
`Mac`: []string{`MAC`},
`Mobile Gaming Console`: []string{`PSP`, `NDS`, `XBX`},
`Real-time OS`: []string{`MTK`, `TDX`},
`Other Mobile`: []string{`WOS`, `POS`, `SBA`, `TIZ`, `SMG`, `MAE`},
`Symbian`: []string{`SYM`, `SYS`, `SY3`, `S60`, `S40`},
`Unix`: []string{`SOS`, `AIX`, `HPX`, `BSD`, `NBS`, `OBS`, `DFB`, `SYL`, `IRI`, `T64`, `INF`},
`WebTV`: []string{`WTV`},
`Windows`: []string{`WIN`},
`Windows Mobile`: []string{`WPH`, `WMO`, `WCE`, `WRT`, `WIO`},
}
const (
PlatformTypeARM = "ARM"
PlatformTypeX64 = "x64"
PlatformTypeX86 = "x86"
PlatformTypeNONE = ""
)
type PlatformReg struct {
Name string
Regular
}
type OsMatchResult struct {
Name string `yaml:"name" json:"name"`
ShortName string `yaml:"short_name" json:"short_name"`
Version string `yaml:"version" json:"version"`
Platform string `yaml:"platform" json:"platform"`
}
type OsParser interface {
PreMatch(string) bool
Parse(string) *OsMatchResult
}
// Parses the useragent for operating system information
type Oss struct {
Regexes []*OsReg
platforms []*PlatformReg
overAllMatch Regular
}
func NewOss(file string) (*Oss, error) {
var v []*OsReg
err := ReadYamlFile(file, &v)
if err != nil {
return nil, err
}
ps := []*PlatformReg{
&PlatformReg{Name: PlatformTypeARM, Regular: Regular{Regex: "arm"}},
&PlatformReg{Name: PlatformTypeX64, Regular: Regular{Regex: "WOW64|x64|win64|amd64|x86_64"}},
&PlatformReg{Name: PlatformTypeX86, Regular: Regular{Regex: "i[0-9]86|i86pc"}},
}
for _, pp := range ps {
pp.Compile()
}
return &Oss{
Regexes: v,
platforms: ps,
}, nil
}
func (o *Oss) ParsePlatform(ua string) string {
for _, p := range o.platforms {
if p.IsMatchUserAgent(ua) {
return p.Name
}
}
return PlatformTypeNONE
}
func (o *Oss) PreMatch(ua string) bool {
if o.overAllMatch.Regexp == nil {
count := len(o.Regexes)
if count == 0 {
return false
}
sb := strings.Builder{}
sb.WriteString(o.Regexes[count-1].Regex)
for i := count - 2; i >= 0; i-- {
sb.WriteString("|")
sb.WriteString(o.Regexes[i].Regex)
}
o.overAllMatch.Regex = sb.String()
o.overAllMatch.Compile()
}
r := o.overAllMatch.IsMatchUserAgent(ua)
return r
}
func (o *Oss) Parse(ua string) *OsMatchResult {
var matches []string
var osRegex *OsReg
for _, osRegex = range o.Regexes {
matches = osRegex.MatchUserAgent(ua)
if len(matches) > 0 {
break
}
}
if len(matches) == 0 || osRegex == nil {
return nil
}
name := BuildByMatch(osRegex.Name, matches)
short := UnknownShort
for osShort, osName := range OperatingSystems {
if StringEqualIgnoreCase(name, osName) {
name = osName
short = osShort
break
}
}
result := &OsMatchResult{
Name: name,
ShortName: short,
Version: BuildVersion(osRegex.Version, matches),
Platform: o.ParsePlatform(ua),
}
return result
}
func GetOsFamily(osLabel string) string {
for k, vs := range OsFamilies {
for _, v := range vs {
if v == osLabel {
return k
}
}
}
return ""
}
func GetOsNameFromId(os, ver string) string {
if osFullName, ok := OperatingSystems[os]; ok {
return strings.TrimSpace(osFullName + " " + ver)
}
return ""
}