-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_agent.go
executable file
·60 lines (54 loc) · 1.9 KB
/
user_agent.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
package ghttp
import (
"github.com/avct/uasurfer" // 准确性很高
"github.com/davidforest123/goutil/basic/gerrors"
"github.com/davidforest123/goutil/container/gconv"
"github.com/mssola/user_agent" // 实测准确性较差
"strings"
)
type UserAgnetInfo struct {
Platform string
OsName string
OsVersion string
DeviceType string
IsMobile bool
IsBot bool
EngineName string
EngineVersion string
BrowserName string
BrowserVersion string
}
func verToString(ver uasurfer.Version) (result string) {
if ver.Major >= 0 {
result += gconv.NumToString(ver.Major)
if ver.Minor >= 0 {
result += "." + gconv.NumToString(ver.Minor)
if ver.Patch >= 0 {
result += "." + gconv.NumToString(ver.Patch)
}
}
}
return result
}
func ParseUserAgent(uaString string) (*UserAgnetInfo, error) {
if len(uaString) == 0 {
return nil, gerrors.New("Nil uaString")
}
var uai UserAgnetInfo
ua := user_agent.New(uaString)
ua2 := uasurfer.Parse(uaString)
uai.Platform = ua2.OS.Platform.String()
uai.Platform = strings.Replace(uai.Platform, "Platform", "", 1) // "PaltformMac" -> "Mac"
uai.OsName = ua2.OS.Name.String()
uai.OsName = strings.Replace(uai.OsName, "OS", "", 1) // "OSMacOSX" -> "MacOSX"
uai.OsVersion = verToString(ua2.OS.Version)
uai.DeviceType = ua2.DeviceType.String()
uai.DeviceType = strings.Replace(uai.DeviceType, "Device", "", 1) // "DeviceComputer" -> "Computer"
uai.IsMobile = uai.DeviceType == "Phone" || uai.DeviceType == "Tablet" || uai.DeviceType == "Wearable"
uai.IsBot = ua.Bot() // uasurfer库de代码中出现了Bot,但README中没有提及,所以还是用的ua.Bot()接口
uai.EngineName, uai.EngineVersion = ua.Engine()
uai.BrowserName = ua2.Browser.Name.String()
uai.BrowserName = strings.Replace(uai.BrowserName, "Browser", "", 1) // "BrowserChrome" -> "Chrome"
uai.BrowserVersion = verToString(ua2.Browser.Version)
return &uai, nil
}