diff --git a/README.md b/README.md index 7ba1eed..0e2179f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Go/Golang package for parsing user agent strings [![GoDoc](https://godoc.org/github.com/mileusna/useragent?status.svg)](https://godoc.org/github.com/mileusna/useragent) -Use `useragent.Parse(userAgent string)` function to parse browser's and bot's user agents strings and get: +Use `useragent.Parse(userAgent string)` function to parse browser's and bot's user agents strings and get: + User agent name and version (Chrome, Firefox, Googlebot, etc.) + Operating system name and version (Windows, Android, iOS etc.) + Device type (mobile, desktop, tablet, bot) diff --git a/ua.go b/ua.go index d643550..d0fcf3d 100644 --- a/ua.go +++ b/ua.go @@ -43,12 +43,14 @@ const ( OperaMini = "Opera Mini" OperaTouch = "Opera Touch" Chrome = "Chrome" + HeadlessChrome = "Headless Chrome" Firefox = "Firefox" InternetExplorer = "Internet Explorer" Safari = "Safari" Edge = "Edge" Vivaldi = "Vivaldi" + GoogleAdsBot = "Google Ads Bot" Googlebot = "Googlebot" Twitterbot = "Twitterbot" FacebookExternalHit = "facebookexternalhit" @@ -225,6 +227,25 @@ func Parse(userAgent string) UserAgent { ua.Version = tokens.get("SamsungBrowser") ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari") + case tokens.get("HeadlessChrome") != "": + ua.Name = HeadlessChrome + ua.Version = tokens.get("HeadlessChrome") + ua.Mobile = tokens.existsAny("Mobile", "Mobile Safari") + ua.Bot = true + + case tokens.exists("AdsBot-Google-Mobile") || tokens.exists("Mediapartners-Google") || tokens.exists("AdsBot-Google"): + ua.Name = GoogleAdsBot + ua.Bot = true + ua.Mobile = ua.IsAndroid() || ua.IsIOS() + + case tokens.exists("XiaoMi"): + miui := tokens.get("XiaoMi") + if strings.HasPrefix(miui, "MiuiBrowser") { + ua.Name = "Miui Browser" + ua.Version = strings.TrimPrefix(miui, "MiuiBrowser/") + ua.Mobile = true + } + case tokens.get("HuaweiBrowser") != "": ua.Name = "Huawei Browser" ua.Version = tokens.get("HuaweiBrowser") diff --git a/ua_test.go b/ua_test.go index e373db4..4a16ef1 100644 --- a/ua_test.go +++ b/ua_test.go @@ -46,6 +46,8 @@ var testTable = [][]string{ {"Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-A310F/A310FXXU2BQB1 Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/5.4 Chrome/51.0.2704.106 Mobile Safari/537.36", "Samsung Browser", "5.4", "mobile", "Android"}, {"Mozilla/5.0 (Linux; Android 9; LM-Q630) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36", ua.Chrome, "86.0.4240.198", "mobile", "Android"}, + {"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/79.0.3945.147 Safari/534.24 XiaoMi/MiuiBrowser/12.11.5-gn", "Miui Browser", "12.11.5-gn", "mobile", ua.Linux}, + {"Mozilla/5.0 (Linux; U; Android 11; ru-ru; Redmi Note 10S Build/RP1A.200720.011) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.116 Mobile Safari/537.36 XiaoMi/MiuiBrowser/12.13.2-gn", "Miui Browser", "12.13.2-gn", "mobile", ua.Android}, {"Mozilla/5.0 (Linux; Android 10; MED-LX9N; HMSCore 6.6.0.311) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.105 HuaweiBrowser/12.1.0.303 Mobile Safari/537.36", "Huawei Browser", "12.1.0.303", "mobile", "Android"}, @@ -77,10 +79,18 @@ var testTable = [][]string{ {"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/100.0.0.0 Safari/537.36", "Bingbot", "2.0", "bot", ""}, // new bingbot desktop {"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.1.0.0 Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)", "Bingbot", "2.0", "bot", ua.Android}, // new bingbot mobile + // Google ads bots + {"Mozilla/5.0 (Linux; Android 4.0.0; Galaxy Nexus Build/IMM76B) AppleWebKit/537.36 (KHTML, like Gecko; Mediapartners-Google) Chrome/104.0.0.0 Mobile Safari/537.36", ua.GoogleAdsBot, "", "bot", ua.Android}, + {"Mozilla/5.0 (Linux; Android 5.0; SM-G920A) AppleWebKit (KHTML, like Gecko) Chrome Mobile Safari (compatible; AdsBot-Google-Mobile; +http://www.google.com/mobile/adsbot.html)", ua.GoogleAdsBot, "", "bot", ua.Android}, + {"Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1 (compatible; AdsBot-Google-Mobile; +http://www.google.com/mobile/adsbot.html)", ua.GoogleAdsBot, "", "bot", ua.IOS}, + // Brave {"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Brave Chrome/87.0.4280.101 Safari/537.36", ua.Chrome, "87.0.4280.101", "desktop", ua.Linux}, {"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36", ua.Chrome, "87.0.4280.141", "desktop", ua.MacOS}, + // HeadlessChrome + {"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/98.0.4758.0 Safari/537.36", ua.HeadlessChrome, "98.0.4758.0", "desktop", ua.Linux}, + // other {"Mozilla/5.0 (X11; CrOS x86_64 14150.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.114 Safari/537.36", ua.Chrome, "94.0.4606.114", "desktop", ua.ChromeOS}, {"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 Google (+https://developers.google.com/+/web/snippet/)", ua.Chrome, "56.0.2924.87", "bot", ua.Linux}, // Google+ fetch