-
Notifications
You must be signed in to change notification settings - Fork 22
/
device_detector.go
271 lines (228 loc) · 6.91 KB
/
device_detector.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
package devicedetector
import (
"path/filepath"
"strings"
regexp "github.com/dlclark/regexp2"
gover "github.com/mcuadros/go-version"
. "github.com/gamebtc/devicedetector/parser"
"github.com/gamebtc/devicedetector/parser/client"
"github.com/gamebtc/devicedetector/parser/device"
)
const UNKNOWN = "UNK"
const VERSION = `3.12.5`
var desktopOsArray = []string{
`AmigaOS`,
`IBM`,
`GNU/Linux`,
`Mac`,
`Unix`,
`Windows`,
`BeOS`,
`Chrome OS`,
}
var(
chrMobReg = regexp.MustCompile(fixUserAgentRegEx(`Chrome/[\.0-9]* Mobile`), regexp.IgnoreCase)
chrTabReg = regexp.MustCompile(fixUserAgentRegEx(`Chrome/[\.0-9]* (?!Mobile)`), regexp.IgnoreCase)
opaTabReg = regexp.MustCompile(fixUserAgentRegEx(`Opera Tablet`), regexp.IgnoreCase)
opaTvReg = regexp.MustCompile(fixUserAgentRegEx(`Opera TV Store`), regexp.IgnoreCase)
)
func fixUserAgentRegEx(regex string) string {
reg := strings.ReplaceAll(regex, `/`, `\/`)
reg = strings.ReplaceAll(reg, `++`, `+`)
return `(?:^|[^A-Z_-])(?:` + reg + `)`
}
type DeviceDetector struct {
deviceParsers []device.DeviceParser
clientParsers []client.ClientParser
botParsers []BotParser
osParsers []OsParser
vendorParser *VendorFragments
DiscardBotInformation bool
SkipBotDetection bool
}
func NewDeviceDetector(dir string) (*DeviceDetector, error) {
vp, err := NewVendor(filepath.Join(dir, FixtureFileVendor))
if err != nil {
return nil, err
}
osp, err := NewOss(filepath.Join(dir, FixtureFileOs))
if err != nil {
return nil, err
}
d := &DeviceDetector{
vendorParser: vp,
osParsers: []OsParser{osp},
}
clientDir := filepath.Join(dir, "client")
d.clientParsers = client.NewClientParsers(clientDir,
[]string{
client.ParserNameFeedReader,
client.ParserNameMobileApp,
client.ParserNameMediaPlayer,
client.ParserNamePim,
client.ParserNameBrowser,
client.ParserNameLibrary,
})
deviceDir := filepath.Join(dir, "device")
d.deviceParsers = device.NewDeviceParsers(deviceDir,
[]string{
device.ParserNameHbbTv,
device.ParserNameConsole,
device.ParserNameCar,
device.ParserNameCamera,
device.ParserNamePortableMediaPlayer,
device.ParserNameMobile,
})
d.botParsers = []BotParser{
NewBot(filepath.Join(dir, FixtureFileBot)),
}
return d, nil
}
func (d *DeviceDetector) AddClientParser(cp client.ClientParser) {
d.clientParsers = append(d.clientParsers, cp)
}
func (d *DeviceDetector) GetClientParser() []client.ClientParser {
return d.clientParsers
}
func (d *DeviceDetector) AddDeviceParser(dp device.DeviceParser) {
d.deviceParsers = append(d.deviceParsers, dp)
}
func (d *DeviceDetector) GetDeviceParsers() []device.DeviceParser {
return d.deviceParsers
}
func (d *DeviceDetector) AddBotParser(op BotParser) {
d.botParsers = append(d.botParsers, op)
}
func (d *DeviceDetector) GetBotParsers() []BotParser {
return d.botParsers
}
func (d *DeviceDetector) ParseBot(ua string) *BotMatchResult {
if !d.SkipBotDetection {
for _, parser := range d.botParsers {
parser.DiscardDetails(d.DiscardBotInformation)
if r := parser.Parse(ua); r != nil {
return r
}
}
}
return nil
}
func (d *DeviceDetector) ParseOs(ua string) *OsMatchResult {
for _, p := range d.osParsers {
if r := p.Parse(ua); r != nil {
return r
}
}
return nil
}
func (d *DeviceDetector) ParseClient(ua string) *client.ClientMatchResult {
for _, parser := range d.clientParsers {
if r := parser.Parse(ua); r != nil {
return r
}
}
return nil
}
func (d *DeviceDetector) ParseDevice(ua string) *device.DeviceMatchResult {
for _, parser := range d.deviceParsers {
if r := parser.Parse(ua); r != nil {
return r
}
}
return nil
}
func (d *DeviceDetector) parseInfo(info *DeviceInfo) {
ua := info.userAgent
if r := d.ParseDevice(ua); r != nil {
info.Type = r.Type
info.Model = r.Model
info.Brand = r.Brand
}
// If no brand has been assigned try to match by known vendor fragments
if info.Brand == "" && d.vendorParser != nil {
info.Brand = d.vendorParser.Parse(ua)
}
os := info.GetOs()
osShortName := os.ShortName
osFamily := GetOsFamily(osShortName)
osVersion := os.Version
cmr := info.GetClient()
if info.Brand == "" && (osShortName == `ATV` || osShortName == `IOS` || osShortName == `MAC`) {
info.Brand = `AP`
}
deviceType := GetDeviceType(info.Type)
// Chrome on Android passes the device type based on the keyword 'Mobile'
// If it is present the device should be a smartphone, otherwise it's a tablet
// See https://developer.chrome.com/multidevice/user-agent#chrome_for_android_user_agent
if deviceType == DEVICE_TYPE_INVALID && osFamily == `Android` {
if browserName,ok:=client.GetBrowserFamily(cmr.ShortName); ok&&browserName== `Chrome` {
if ok, _ := chrMobReg.MatchString(ua); ok {
deviceType = DEVICE_TYPE_SMARTPHONE
} else if ok, _ = chrTabReg.MatchString(ua); ok {
deviceType = DEVICE_TYPE_TABLET
}
}
}
if deviceType == DEVICE_TYPE_INVALID {
if info.HasAndroidMobileFragment() {
deviceType = DEVICE_TYPE_TABLET
} else if ok, _ := opaTabReg.MatchString(ua); ok {
deviceType = DEVICE_TYPE_TABLET
} else if info.HasAndroidMobileFragment() {
deviceType = DEVICE_TYPE_SMARTPHONE
} else if osShortName == "AND" && osVersion != "" {
if gover.CompareSimple(osVersion, `2.0`) == -1 {
deviceType = DEVICE_TYPE_SMARTPHONE
} else if gover.CompareSimple(osVersion, `3.0`) >= 0 &&
gover.CompareSimple(osVersion, `4.0`) == -1 {
deviceType = DEVICE_TYPE_TABLET
}
}
}
// All detected feature phones running android are more likely a smartphone
if deviceType == DEVICE_TYPE_FEATURE_PHONE && osFamily == `Android` {
deviceType = DEVICE_TYPE_SMARTPHONE
}
// According to http://msdn.microsoft.com/en-us/library/ie/hh920767(v=vs.85).aspx
if deviceType == DEVICE_TYPE_INVALID &&
(osShortName == `WRT` || (osShortName == `WIN` && gover.CompareSimple(osVersion, `8`) >= 0)) &&
info.IsTouchEnabled() {
deviceType = DEVICE_TYPE_TABLET
}
// All devices running Opera TV Store are assumed to be a tv
if ok, _ := opaTvReg.MatchString(ua); ok {
deviceType = DEVICE_TYPE_TV
}
// Devices running Kylo or Espital TV Browsers are assumed to be a TV
if deviceType == DEVICE_TYPE_INVALID {
if cmr.Name == `Kylo` || cmr.Name == `Espial TV Browser` {
deviceType = DEVICE_TYPE_TV
} else if info.IsDesktop() {
deviceType = DEVICE_TYPE_DESKTOP
}
}
if deviceType != DEVICE_TYPE_INVALID {
info.Type = GetDeviceName(deviceType)
}
return
}
func (d *DeviceDetector) Parse(ua string) *DeviceInfo {
// skip parsing for empty useragents or those not containing any letter
if !StringContainsLetter(ua) {
return nil
}
info := &DeviceInfo{
userAgent: ua,
}
info.bot = d.ParseBot(ua)
if info.IsBot() {
return info
}
info.os = d.ParseOs(ua)
// Parse Clients
// Clients might be browsers, Feed Readers, Mobile Apps, Media Players or
// any other application accessing with an parseable UA
info.client = d.ParseClient(ua)
d.parseInfo(info)
return info
}