util/user_agent.go

177 lines
7.1 KiB
Go
Raw Normal View History

2023-03-31 18:58:14 +08:00
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-03-31 18:51
package util
import (
"math/rand"
"time"
2023-04-20 16:36:09 +08:00
"github.com/mssola/user_agent"
2023-03-31 18:58:14 +08:00
)
var defaultUserAgentList = []string{
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.3 Mobile/14E277 Safari/603.1.30",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1",
"(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11",
"(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6",
"(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6",
"(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1",
"(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5",
"(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5",
"(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3",
"(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24",
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24",
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
}
type userAgent struct {
list []string
}
// Set 设置ua池
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:55 2023/3/31
func (ua *userAgent) Set(agentList []string) {
if len(agentList) == 0 {
return
}
ua.list = agentList
}
// GetRandomUA 随机获取UA
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:56 2023/3/31
func (ua *userAgent) GetRandomUA() string {
rand.Seed(time.Now().UnixNano())
idx := rand.Intn(len(ua.list))
return ua.list[idx]
}
2023-04-20 16:36:09 +08:00
// 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"` // 操作系统版本
}