-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_agent.go
109 lines (91 loc) · 2.7 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
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
package main
import (
_ "embed"
"strconv"
ua "github.com/mileusna/useragent"
"github.com/ua-parser/uap-go/uaparser"
)
// ineffassign: ignore
//
//go:embed embed/build/user_agents.yaml
var userAgents []byte
const (
uaTypeUnknown = 0
uaTypeMobile = 1
uaTypeDesktop = 2
uaTypeTablet = 3
uaTypeBot = 9
)
type userAgentParser struct {
parser *uaparser.Parser
}
type userAgentResult struct {
UaType uint8 `json:"t"`
UaFull string `json:"f"`
UaChecksum string `json:"c"`
UaBrowserName string `json:"b"`
UaBrowserVersionMajor uint64 `json:"bvm"`
UaBrowserVersion string `json:"bv"`
UaOSName string `json:"o"`
UaOSVersionMajor uint64 `json:"ovm"`
UaOSVersion string `json:"ov"`
UaDeviceBrand string `json:"db"`
UaDeviceFamily string `json:"df"`
UaDeviceModel string `json:"dm"`
}
func newUserAgentParser() *userAgentParser {
uaParser := userAgentParser{}
parser, err := uaparser.NewFromBytes(userAgents)
if err != nil {
panic(err)
}
uaParser.parser = parser
return &uaParser
}
func (uaParser *userAgentParser) parse(uaString string) userAgentResult {
client := uaParser.parser.Parse(uaString)
result := userAgentResult{
UaChecksum: checksum(uaString),
UaFull: sanitizeText(uaString),
UaType: uaTypeUnknown,
}
// process type
ua := ua.Parse(uaString)
if ua.Mobile {
result.UaType = uaTypeMobile
} else if ua.Bot {
result.UaType = uaTypeBot
} else if ua.Desktop {
result.UaType = uaTypeDesktop
} else if ua.Tablet {
result.UaType = uaTypeTablet
}
result.UaBrowserName = sanitizeTitle(client.UserAgent.Family)
result.UaOSName = sanitizeTitle(client.Os.Family)
result.UaDeviceBrand = sanitizeTitle(client.Device.Brand)
result.UaDeviceFamily = sanitizeTitle(client.Device.Family)
result.UaDeviceModel = sanitizeTitle(client.Device.Model)
result.UaBrowserVersion = client.UserAgent.Major
browserVersionMajor, browserVersionMajorErr := strconv.ParseUint(client.UserAgent.Major, 10, 64)
if browserVersionMajorErr == nil && browserVersionMajor > 0 {
result.UaBrowserVersionMajor = browserVersionMajor
}
if client.UserAgent.Minor != "" {
result.UaBrowserVersion += "." + client.UserAgent.Minor
}
if client.UserAgent.Patch != "" {
result.UaBrowserVersion += "." + client.UserAgent.Patch
}
result.UaOSVersion = client.Os.Major
osVersionMajor, osVersionMajorErr := strconv.ParseUint(client.Os.Major, 10, 64)
if osVersionMajorErr == nil && osVersionMajor > 0 {
result.UaOSVersionMajor = osVersionMajor
}
if client.Os.Minor != "" {
result.UaOSVersion += "." + client.Os.Minor
}
if client.Os.Patch != "" {
result.UaOSVersion += "." + client.Os.Patch
}
return result
}