增加ua信息获取

This commit is contained in:
白茶清欢 2023-04-20 16:36:09 +08:00
parent 0a75d06083
commit 0054da0620
3 changed files with 79 additions and 0 deletions

1
go.mod
View File

@ -16,6 +16,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mssola/user_agent v0.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect

2
go.sum
View File

@ -9,6 +9,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@ -10,6 +10,8 @@ package util
import (
"math/rand"
"time"
"github.com/mssola/user_agent"
)
var defaultUserAgentList = []string{
@ -98,3 +100,77 @@ func (ua *userAgent) GetRandomUA() string {
idx := rand.Intn(len(ua.list))
return ua.list[idx]
}
// Parse 解析UA
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:31 2023/4/20
func (ua *userAgent) Parse(inputUA string) *UAInfo {
uaInstance := user_agent.New(inputUA)
uaInfo := &UAInfo{
Mozilla: uaInstance.Mozilla(),
Platform: uaInstance.Platform(),
OS: uaInstance.OS(),
Localization: uaInstance.Localization(),
Model: uaInstance.Model(),
Browser: &BrowserInfo{
Engine: "",
EngineVersion: "",
Name: "",
Version: "",
},
OSInfo: &OSInfo{
FullName: uaInstance.OSInfo().FullName,
Name: uaInstance.OSInfo().Name,
Version: uaInstance.OSInfo().Version,
},
Bot: false,
Mobile: false,
Undecided: false,
}
uaInfo.Browser.Engine, uaInfo.Browser.EngineVersion = uaInstance.Engine()
uaInfo.Browser.Name, uaInfo.Browser.Version = uaInstance.Browser()
return uaInfo
}
// UAInfo ua解析结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:34 2023/4/20
type UAInfo struct {
Mozilla string `json:"mozilla"`
Platform string `json:"platform"`
OS string `json:"os"`
OSInfo *OSInfo `json:"os_info"`
Localization string `json:"localization"`
Model string `json:"model"`
Browser *BrowserInfo `json:"browser"`
Bot bool `json:"bot"`
Mobile bool `json:"mobile"`
Undecided bool `json:"undecided"`
}
// BrowserInfo 浏览器信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:34 2023/4/20
type BrowserInfo struct {
Engine string `json:"engine"` // 浏览器引擎
EngineVersion string `json:"engine_version"` // 浏览器引擎版本
Name string `json:"name"` // 浏览器名称
Version string `json:"version"` // 浏览器版本
}
// OSInfo 系统信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:34 2023/4/20
type OSInfo struct {
FullName string `json:"full_name"` // 操作系统全称
Name string `json:"name"` // 操作系统名称
Version string `json:"version"` // 操作系统版本
}