代码拉取完成,页面将自动刷新
package useragent
import (
"fmt"
"regexp"
"strings"
)
/**
* @Description: Get WinNT name at Window NT
* @param sName
* @return string
*/
func getWinOsNameWithWinNT(sName string) string {
osName := "Windows"
types := map[string]string{
"Windows NT 10": "Windows 10",
"Windows NT 6.3": "Windows 8",
"Windows NT 6.2": "Windows 8",
"Windows NT 6.1": "Windows 7",
"Windows NT 6.0": "Windows Vista/Server 2008",
"Windows NT 5.2": "Windows Server 2003",
"Windows NT 5.1": "Windows XP",
"Windows NT 5": "Windows 2000",
"Windows NT 4": "Windows NT4",
}
for keyWord, name := range types {
if strings.Contains(sName, keyWord) {
osName = name
break
}
}
return osName
}
//GetOsName 获取OS名称
func GetOsName(userAgent string) string {
osName := "Unknown"
if userAgent == "" {
return osName
}
strRe, _ := regexp.Compile("(?i:\\((.*?)\\))")
userAgent = strRe.FindString(userAgent)
levelNames := ":micromessenger:dart:Windows NT:Windows Mobile:Windows Phone:Windows Phone OS:Macintosh|Macintosh:Mac OS:CrOS|CrOS:iPhone OS:iPad|iPad:OS:Android:Linux:blackberry:hpwOS:Series:Symbian:PalmOS:SymbianOS:J2ME:Sailfish:Bada:MeeGo:webOS|hpwOS:Maemo:"
var regStrArr []string
namesArr := strings.Split(strings.Trim(levelNames, ":"), ":")
for _, name := range namesArr {
regStrArr = append(regStrArr, fmt.Sprintf("(%s[\\s?\\/XxSs0-9_.]+)", name))
}
regexpStr := fmt.Sprintf("(?i:%s)", strings.Join(regStrArr, "|"))
nameRe, _ := regexp.Compile(regexpStr)
names := nameRe.FindAllString(userAgent, -1)
name := ""
for _, s := range names {
if name == "" {
name = strings.TrimSpace(s)
} else if len(name) > 0 {
if strings.Contains(name, "Macintosh") && s != "" {
name = strings.TrimSpace(s)
} else if strings.Contains(name, s) {
name = strings.TrimSpace(s)
} else if !strings.Contains(s, name) {
if strings.Contains(name, "iPhone") ||
strings.Contains(name, "iPad") {
s = strings.Trim(s, "Mac OS X")
}
if s != "" {
name += " " + strings.TrimSpace(s)
}
}
break
}
if strings.Contains(name, "Windows NT") {
name = getWinOsNameWithWinNT(name)
break
}
}
if name != "" {
osName = name
}
return osName
}
//GetBrowserName 获取浏览器名称
func GetBrowserName(userAgent string) string {
deviceName := "Unknown"
levelNames := ":VivoBrowser:QQDownload:QQBrowser:QQ:MQQBrowser:MicroMessenger:TencentTraveler:LBBROWSER:TaoBrowser:BrowserNG:UCWEB:TwonkyBeamBrowser:NokiaBrowser:OviBrowser:NF-Browser:OneBrowser:Obigo:DiigoBrowser:baidubrowser:baiduboxapp:xiaomi:Redmi:MI:Lumia:Micromax:MSIEMobile:IEMobile:EdgiOS:Yandex:Mercury:Openwave:TouchPad:UBrowser:Presto:Maxthon:MetaSr:Trident:Opera:IEMobile:Edge:Chrome:Chromium:OPR:CriOS:Firefox:FxiOS:fennec:CrMo:Safari:Nexus One:Nexus S:Nexus:Blazer:teashark:bolt:HTC:Dell:Motorola:Samsung:LG:Sony:SonyST:SonyLT:SonyEricsson:Asus:Palm:Vertu:Pantech:Fly:Wiko:i-mobile:Alcatel:Nintendo:Amoi:INQ:ONEPLUS:Tapatalk:PDA:Novarra-Vision:NetFront:Minimo:FlyFlow:Dolfin:Nokia:Series:AppleWebKit:Mobile:Mozilla:Version:"
var regStrArr []string
namesArr := strings.Split(strings.Trim(levelNames, ":"), ":")
for _, name := range namesArr {
regStrArr = append(regStrArr, fmt.Sprintf("(%s[\\s?\\/0-9.]+)", name))
}
regexpStr := fmt.Sprintf("(?i:%s)", strings.Join(regStrArr, "|"))
nameRe, _ := regexp.Compile(regexpStr)
names := nameRe.FindAllString(userAgent, -1)
level := 0
for _, name := range names {
replaceRe, _ := regexp.Compile("(?i:[\\s?\\/0-9.]+)")
n := replaceRe.ReplaceAllString(name, "")
l := strings.Index(levelNames, fmt.Sprintf(":%s:", n))
if level == 0 {
deviceName = strings.TrimSpace(name)
}
if l >= 0 && (level == 0 || level > l) {
level = l
deviceName = strings.TrimSpace(name)
}
}
return deviceName
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。