2
0

Merge branch 'master' into pr/ielevenliu/43

This commit is contained in:
Miloš Mileusnić
2024-09-12 20:15:36 +02:00
+103 -96
View File
@@ -25,16 +25,19 @@ type UserAgent struct {
// Constants for browsers and operating systems for easier comparison
const (
Windows = "Windows"
WindowsPhone = "Windows Phone"
Android = "Android"
MacOS = "macOS"
IOS = "iOS"
Linux = "Linux"
FreeBSD = "FreeBSD"
ChromeOS = "ChromeOS"
BlackBerry = "BlackBerry"
Harmony = "Harmony"
Windows = "Windows"
WindowsPhone = "Windows Phone"
WindowsNT = "Windows NT"
WindowsPhoneOS = "Windows Phone OS"
Android = "Android"
MacOS = "macOS"
IOS = "iOS"
Linux = "Linux"
FreeBSD = "FreeBSD"
ChromeOS = "ChromeOS"
BlackBerry = "BlackBerry"
CrOS = "CrOS"
Harmony = "Harmony"
Opera = "Opera"
OperaMini = "Opera Mini"
@@ -46,6 +49,10 @@ const (
Safari = "Safari"
Edge = "Edge"
Vivaldi = "Vivaldi"
MobileSafari = "Mobile Safari"
NetFront = "NetFront"
Mozilla = "Mozilla"
Msie = "MSIE"
GoogleAdsBot = "Google Ads Bot"
Googlebot = "Googlebot"
@@ -53,10 +60,16 @@ const (
FacebookExternalHit = "facebookexternalhit"
Applebot = "Applebot"
Bingbot = "Bingbot"
YandexBot = "YandexBot"
YandexAdNet = "YandexAdNet"
FacebookApp = "Facebook App"
InstagramApp = "Instagram App"
TiktokApp = "TikTok App"
Version = "Version"
Mobile = "Mobile"
Tablet = "Tablet"
)
// Parse user agent string returning UserAgent struct
@@ -76,15 +89,13 @@ func Parse(userAgent string) UserAgent {
}
}
//fmt.Printf("%+v\n", tokens)
// OS lookup
switch {
case tokens.exists("Android"):
case tokens.exists(Android):
ua.OS = Android
var osIndex int
osIndex, ua.OSVersion = tokens.getIndexValue(Android)
ua.Tablet = strings.Contains(strings.ToLower(ua.String), "tablet")
ua.Tablet = strings.Contains(strings.ToLower(ua.String), strings.ToLower(Tablet))
ua.Device = tokens.findAndroidDevice(osIndex)
case tokens.exists("iPhone"):
@@ -99,14 +110,14 @@ func Parse(userAgent string) UserAgent {
ua.Device = "iPad"
ua.Tablet = true
case tokens.exists("Windows NT"):
case tokens.exists(WindowsNT):
ua.OS = Windows
ua.OSVersion = tokens.get("Windows NT")
ua.OSVersion = tokens.get(WindowsNT)
ua.Desktop = true
case tokens.exists("Windows Phone OS"):
case tokens.exists(WindowsPhoneOS):
ua.OS = WindowsPhone
ua.OSVersion = tokens.get("Windows Phone OS")
ua.OSVersion = tokens.get(WindowsPhoneOS)
ua.Mobile = true
case tokens.exists("Macintosh"):
@@ -114,24 +125,24 @@ func Parse(userAgent string) UserAgent {
ua.OSVersion = tokens.findMacOSVersion()
ua.Desktop = true
case tokens.exists("Linux"):
case tokens.exists(Linux):
ua.OS = Linux
ua.OSVersion = tokens.get(Linux)
ua.Desktop = true
case tokens.exists("FreeBSD"):
case tokens.exists(FreeBSD):
ua.OS = FreeBSD
ua.OSVersion = tokens.get(FreeBSD)
ua.Desktop = true
case tokens.exists("CrOS"):
case tokens.exists(CrOS):
ua.OS = ChromeOS
ua.OSVersion = tokens.get("CrOS")
ua.OSVersion = tokens.get(CrOS)
ua.Desktop = true
case tokens.exists("BlackBerry"):
case tokens.exists(BlackBerry):
ua.OS = BlackBerry
ua.OSVersion = tokens.get("BlackBerry")
ua.OSVersion = tokens.get(BlackBerry)
ua.Mobile = true
case tokens.exists("OpenHarmony"):
@@ -141,11 +152,11 @@ func Parse(userAgent string) UserAgent {
}
switch {
case tokens.exists("Googlebot"):
case tokens.exists(Googlebot):
ua.Name = Googlebot
ua.Version = tokens.get(Googlebot)
ua.Bot = true
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.existsAny("GoogleProber", "GoogleProducer"):
if name := tokens.findBestMatch(false); name != "" {
@@ -153,14 +164,14 @@ func Parse(userAgent string) UserAgent {
}
ua.Bot = true
case tokens.exists("Applebot"):
case tokens.exists(Applebot):
ua.Name = Applebot
ua.Version = tokens.get(Applebot)
ua.Bot = true
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
ua.OS = ""
case tokens.get("Opera Mini") != "":
case tokens.get(OperaMini) != "":
ua.Name = OperaMini
ua.Version = tokens.get(OperaMini)
ua.Mobile = true
@@ -168,84 +179,91 @@ func Parse(userAgent string) UserAgent {
case tokens.get("OPR") != "":
ua.Name = Opera
ua.Version = tokens.get("OPR")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("OPT") != "":
ua.Name = OperaTouch
ua.Version = tokens.get("OPT")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
// Opera on iOS
case tokens.get("OPiOS") != "":
ua.Name = Opera
ua.Version = tokens.get("OPiOS")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
// Chrome on iOS
case tokens.get("CriOS") != "":
ua.Name = Chrome
ua.Version = tokens.get("CriOS")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
// Firefox on iOS
case tokens.get("FxiOS") != "":
ua.Name = Firefox
ua.Version = tokens.get("FxiOS")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("Firefox") != "":
case tokens.get(Firefox) != "":
ua.Name = Firefox
ua.Version = tokens.get(Firefox)
ua.Mobile = tokens.exists("Mobile")
ua.Tablet = tokens.exists("Tablet")
ua.Mobile = tokens.exists(Mobile)
ua.Tablet = tokens.exists(Tablet)
case tokens.get("Vivaldi") != "":
case tokens.get(Vivaldi) != "":
ua.Name = Vivaldi
ua.Version = tokens.get(Vivaldi)
case tokens.exists("MSIE"):
case tokens.exists(Msie):
ua.Name = InternetExplorer
ua.Version = tokens.get("MSIE")
ua.Version = tokens.get(Msie)
case tokens.get("EdgiOS") != "":
ua.Name = Edge
ua.Version = tokens.get("EdgiOS")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("Edge") != "":
case tokens.get(Edge) != "":
ua.Name = Edge
ua.Version = tokens.get("Edge")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Version = tokens.get(Edge)
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("Edg") != "":
ua.Name = Edge
ua.Version = tokens.get("Edg")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("EdgA") != "":
ua.Name = Edge
ua.Version = tokens.get("EdgA")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("bingbot") != "":
ua.Name = Bingbot
ua.Version = tokens.get("bingbot")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("YandexBot") != "":
ua.Name = "YandexBot"
ua.Version = tokens.get("YandexBot")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
case tokens.get(YandexBot) != "":
ua.Name = YandexBot
ua.Version = tokens.get(YandexBot)
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
ua.Bot = true
case tokens.get(YandexAdNet) != "":
ua.Name = YandexAdNet
ua.Version = tokens.get(YandexAdNet)
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
ua.Bot = true
case tokens.get("SamsungBrowser") != "":
ua.Name = "Samsung Browser"
ua.Version = tokens.get("SamsungBrowser")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.get("HeadlessChrome") != "":
ua.Name = HeadlessChrome
ua.Version = tokens.get("HeadlessChrome")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
ua.Bot = true
case tokens.existsAny("AdsBot-Google-Mobile", "Mediapartners-Google", "AdsBot-Google"):
@@ -285,18 +303,18 @@ func Parse(userAgent string) UserAgent {
case tokens.get("HuaweiBrowser") != "":
ua.Name = "Huawei Browser"
ua.Version = tokens.get("HuaweiBrowser")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.exists("BlackBerry"):
ua.Name = "BlackBerry"
ua.Version = tokens.get("Version")
case tokens.exists(BlackBerry):
ua.Name = BlackBerry
ua.Version = tokens.get(Version)
case tokens.exists("NetFront"):
ua.Name = "NetFront"
ua.Version = tokens.get("NetFront")
case tokens.exists(NetFront):
ua.Name = NetFront
ua.Version = tokens.get(NetFront)
ua.Mobile = true
// if chrome and Safari defined, find any other token sent descr
// if Chrome and Safari defined, find any other token sent descr
case tokens.exists(Chrome) && tokens.exists(Safari):
name := tokens.findBestMatch(true)
if name != "" {
@@ -306,30 +324,30 @@ func Parse(userAgent string) UserAgent {
}
fallthrough
case tokens.exists("Chrome"):
case tokens.exists(Chrome):
ua.Name = Chrome
ua.Version = tokens.get("Chrome")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Version = tokens.get(Chrome)
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.exists("Brave Chrome"):
ua.Name = Chrome
ua.Version = tokens.get("Brave Chrome")
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
case tokens.exists("Safari"):
case tokens.exists(Safari):
ua.Name = Safari
v := tokens.get("Version")
v := tokens.get(Version)
if v != "" {
ua.Version = v
} else {
ua.Version = tokens.get("Safari")
ua.Version = tokens.get(Safari)
}
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
default:
if ua.OS == "Android" && tokens.get("Version") != "" {
if ua.IsAndroid() && tokens.get(Version) != "" {
ua.Name = "Android browser"
ua.Version = tokens.get("Version")
ua.Version = tokens.get(Version)
ua.Mobile = true
} else {
if name := tokens.findBestMatch(false); name != "" {
@@ -341,7 +359,7 @@ func Parse(userAgent string) UserAgent {
ua.Bot = strings.Contains(strings.ToLower(ua.Name), "bot")
// If mobile flag has already been set, don't override it.
if !ua.Mobile {
ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari")
ua.Mobile = tokens.existsAny(Mobile, MobileSafari)
}
}
}
@@ -355,7 +373,7 @@ func Parse(userAgent string) UserAgent {
ua.Mobile = false
}
// if not already bot, check some popular bots and wether URL is set
// if not already bot, check some popular bots and whether URL is set
if !ua.Bot {
ua.Bot = ua.URL != ""
}
@@ -390,7 +408,7 @@ func parse(userAgent string) properties {
if val.Len() == 0 { // only if value don't exists
var ver string
s, ver = checkVer(s) // determin version string and split
s, ver = checkVer(s) // determine version string and split
clients.add(s, ver)
} else {
clients.add(s, strings.TrimSpace(val.String()))
@@ -409,7 +427,6 @@ func parse(userAgent string) properties {
bua := []byte(userAgent)
for i, c := range bua {
//fmt.Println(string(c), c)
switch {
case c == 41: // )
addToken()
@@ -440,7 +457,7 @@ func parse(userAgent string) properties {
// If the following character is not a space, change to a space.
buff.WriteByte(' ')
}
// Otherwise don't write as its probably a badly formatted key value separator.
// Otherwise don't write as it's probably a badly formatted key value separator.
case slash && c == 32:
addToken()
@@ -475,10 +492,8 @@ func checkVer(s string) (name, v string) {
return s, ""
}
//v = s[i+1:]
switch s[:i] {
case "Linux", "Windows NT", "Windows Phone OS", "MSIE", "Android", "OpenHarmony":
case Linux, WindowsNT, WindowsPhoneOS, Msie, Android, "OpenHarmony":
return s[:i], s[i+1:]
case "CrOS x86_64", "CrOS aarch64", "CrOS armv7l":
j := strings.LastIndex(s[:i], " ")
@@ -486,20 +501,12 @@ func checkVer(s string) (name, v string) {
default:
return s, ""
}
// for _, c := range v {
// if (c >= 48 && c <= 57) || c == 46 {
// } else {
// return s, ""
// }
// }
// return s[:i], s[i+1:]
}
// ignore retursn true if token should be ignored
// ignore returns true if token should be ignored
func ignore(s string) bool {
switch s {
case "KHTML, like Gecko", "U", "compatible", "Mozilla", "WOW64", "en", "en-us", "en-gb", "ru-ru", "Browser":
case "KHTML, like Gecko", "U", "compatible", Mozilla, "WOW64", "en", "en-us", "en-gb", "ru-ru", "Browser":
return true
default:
return false
@@ -613,9 +620,9 @@ func (p properties) findBestMatch(withVerOnly bool) string {
for i := 0; i < n; i++ {
for _, prop := range p.list {
switch prop.Key {
case Chrome, Firefox, Safari, "Version", "Mobile", "Mobile Safari", "Mozilla", "AppleWebKit", "Windows NT", "Windows Phone OS", Android, "Macintosh", Linux, "GSA", "CrOS", "Tablet", "OpenHarmony":
case Chrome, Firefox, Safari, Version, Mobile, MobileSafari, Mozilla, "AppleWebKit", WindowsNT, WindowsPhoneOS, Android, "Macintosh", Linux, "GSA", CrOS, Tablet, "OpenHarmony":
default:
// don' pick if starts with number
// don't pick if starts with number
if len(prop.Key) != 0 && prop.Key[0] >= 48 && prop.Key[0] <= 57 {
break
}
@@ -647,15 +654,15 @@ func (p *properties) findAndroidDevice(startIndex int) string {
if len(p.list) > i+1 {
dev := p.list[i+1].Key
if len(dev) == 2 || (len(dev) == 5 && dev[2] == '-') {
// probably langage tag (en-us etc..), ignore and continue loop
// probably language tag (en-us etc..), ignore and continue loop
continue
}
switch dev {
case Chrome, Firefox, Safari, "Opera Mini", "Presto", "Version", "Mobile", "Mobile Safari", "Mozilla", "AppleWebKit", "Windows NT", "Windows Phone OS", Android, "Macintosh", Linux, "CrOS":
// ignore this tokens, not device names
case Chrome, Firefox, Safari, OperaMini, "Presto", Version, Mobile, MobileSafari, Mozilla, "AppleWebKit", WindowsNT, WindowsPhoneOS, Android, "Macintosh", Linux, CrOS:
// ignore these tokens, not device names
default:
if strings.Contains(strings.ToLower(dev), "tablet") {
p.list[i+1].Key = "Tablet" // leave Tablet tag for later table detection
if strings.Contains(strings.ToLower(dev), strings.ToLower(Tablet)) {
p.list[i+1].Key = Tablet // leave Tablet tag for later table detection
} else {
p.list = append(p.list[:i+1], p.list[i+2:]...)
}