Skip to content

Commit

Permalink
fix: include browser engines in user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Nov 21, 2024
1 parent db453de commit a19d63a
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 21 deletions.
29 changes: 27 additions & 2 deletions src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,29 @@ function getBotType(userAgent) {
return type ? `:${type[0].toLowerCase()}` : '';
}

function getBrowserEngine(userAgent) {
const engines = [
{
name: 'edgehtml',
pattern: /windows.+ edge/,
}, {
name: 'blink',
pattern: /webkit.+chrome/,
}, {
name: 'presto',
pattern: /presto/,
}, {
name: 'webkit',
pattern: /(webkit)\/([\w.]+)/,
}, {
name: 'gecko',
pattern: /rv:([\w.]{1,9})\b.+(gecko)/,
}];

const engine = engines.find(({ pattern }) => pattern.test(userAgent));
return engine ? `:${engine.name}` : '';
}

export function getMaskedUserAgent(headers) {
if (!headers) {
return 'undefined';
Expand Down Expand Up @@ -216,10 +239,12 @@ export function getMaskedUserAgent(headers) {
if (lcUA.includes('mobile')
|| lcUA.includes('android')
|| lcUA.includes('opera mini')) {
return `mobile${getMobileOS(lcUA)}`;
const mobileOS = getMobileOS(lcUA);
return `mobile${mobileOS}${mobileOS ? getBrowserEngine(lcUA) : ''}`;
}

return `desktop${getDesktopOS(lcUA)}`;
const desktopOS = getDesktopOS(lcUA);
return `desktop${desktopOS}${desktopOS ? getBrowserEngine(lcUA) : ''}`;
}

function cleanJWT(str) {
Expand Down
317 changes: 317 additions & 0 deletions test/fixtures/user-agents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
[
{
"desc": "Baiduspider",
"ua": "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
"expected": "bot"
},
{
"desc": "HubSpot Crawler",
"ua": "Mozilla/5.0 (compatible; HubSpot Crawler; +https://www.hubspot.com)",
"expected": "bot"
},
{
"desc": "PingdomPageSpeed",
"ua": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 PingdomPageSpeed/1.0 (pingbot/2.0; +http://www.pingdom.com/)",
"expected": "bot:monitoring"
},
{
"desc": "AHC bot",
"ua": "AHC/2.1",
"expected": "bot"
},
{
"desc": "DataDog Synthetics",
"ua": "mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, like gecko) chrome/123.0.6312.122 safari/537.36 datadogsynthetics",
"expected": "bot:monitoring"
},
{
"desc": "Googlebot",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"expected": "bot:search"
},
{
"desc": "Googlebot Deepcrawl",
"ua": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) https://deepcrawl.com/bot",
"expected": "bot:seo"
},
{
"desc": "Android Browser on Galaxy Nexus",
"ua": "Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"expected": "mobile:android:webkit"
},
{
"desc": "Android Browser on Galaxy S3",
"ua": "Mozilla/5.0 (Linux; Android 4.4.4; en-us; SAMSUNG GT-I9300I Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Android Browser on Huawei P8 (H891L)",
"ua": "Mozilla/5.0 (Linux; Android 4.4.4; HUAWEI H891L Build/HuaweiH891L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Android Browser on Samsung S6 (SM-G925F)",
"ua": "Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-G925F Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.0 Chrome/38.0.2125.102 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "baidu app on iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16C101 main%2F1.0 baiduboxapp/11.12.0.18 (Baidu; P2 12.1.2)",
"expected": "mobile:ios:webkit"
},
{
"desc": "baidu app on Android",
"ua": "Mozilla/5.0 (Linux; Android 8.1.0; BKK-AL10 Build/HONORBKK-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/63.0.3239.83 Mobile Safari/537.36 T7/11.11 baiduboxapp/11.11.0.0 (Baidu; P1 8.1.0)",
"expected": "mobile:android:blink"
},
{
"desc": "Chrome Generic",
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4758.102 Safari/537.36",
"expected": "desktop:mac:blink"
},
{
"desc": "Chrome 112.0 on Win10",
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
"expected": "desktop:windows:blink"
},
{
"desc": "Chrome 112.0 on macOS",
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
"expected": "desktop:mac:blink"
},
{
"desc": "Chrome 111.0 on Linux",
"ua": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
"expected": "desktop:linux:blink"
},
{
"desc": "Chrome 111.0 on ChromeOS",
"ua": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
"expected": "desktop:chromeos:blink"
},
{
"desc": "Chrome WebView",
"ua": "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Chrome on iOS",
"ua": "Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3",
"expected": "mobile:ios:webkit"
},
{
"desc": "Chrome on Android",
"ua": "Mozilla/5.0 (Linux; U; Android-4.0.3; en-us; Galaxy Nexus Build/IML74K) AppleWebKit/535.7 (KHTML, like Gecko) CrMo/16.0.912.75 Mobile Safari/535.7",
"expected": "mobile:android:webkit"
},
{
"desc": "Facebook in-App Browser for Android with version",
"ua": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/35.0.0.48.273;]",
"expected": "mobile:android:blink"
},
{
"desc": "Facebook in-App Browser for iOS with version",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304 [FBAN/FBIOS;FBAV/91.0.0.41.73;FBBV/57050710;FBDV/iPhone8,1;FBMD/iPhone;FBSN/iOS;FBSV/10.3.1;FBSS/2;FBCR/Telekom.de;FBID/phone;FBLC/de_DE;FBOP/5;FBRV/0])",
"expected": "mobile:ios:webkit"
},
{
"desc": "Facebook in-App Browser for iOS without version",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 [FBAN/FBIOS;FBDV/iPhone10,2;FBMD/iPhone;FBSN/iOS;FBSV/13.3.1;FBSS/3;FBID/phone;FBLC/en_US;FBOP/5;FBCR/]",
"expected": "mobile:ios:webkit"
},
{
"desc": "Instagram in-App Browser for iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 142.0.0.22.109 (iPhone12,5; iOS 14_1; en_US; en-US; scale=3.00; 1242x2688; 214888322) NW/1",
"expected": "mobile:ios:webkit"
},
{
"desc": "Firefox Generic",
"ua": "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2",
"expected": "desktop:windows:gecko"
},
{
"desc": "Firefox Mac",
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:100.0) Gecko/20100101 Firefox/100.0",
"expected": "desktop:mac:gecko"
},
{
"desc": "Firefox Reality",
"ua": "Mozilla/5.0 (Android 7.1.2; Mobile VR; rv:65.0) Gecko/65.0 Firefox/65.0",
"expected": "mobile:android:gecko"
},
{
"desc": "Firefox-based browser",
"ua": "Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0 AppName/1.0",
"expected": "desktop:linux:gecko"
},
{
"desc": "HuaweiBrowser 1",
"ua": "Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;HMSCore/4.0.0 GMS/10.4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "HuaweiBrowser 2",
"ua": "Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;HMSCore/4.0.0 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "HuaweiBrowser 3",
"ua": "Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00;GMS/10.4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "HuaweiBrowser 4",
"ua": "Mozilla/5.0 (Linux; Android 6.0.1; LYA-AL00 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.102 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "LINE on iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_6 like Mac OS X) AppleWebKit/604.5.6 (KHTML, like Gecko) Mobile/15D100 Safari Line/8.4.1",
"expected": "mobile:ios:webkit"
},
{
"desc": "Mobile Safari",
"ua": "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",
"expected": "mobile:ios:webkit"
},
{
"desc": "Mobile Safari",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Safari/604.1",
"expected": "mobile:ios:webkit"
},
{
"desc": "Opera Mobile",
"ua": "Opera/9.80 (Android 2.3.5; Linux; Opera Mobi/ADR-1111101157; U; de) Presto/2.9.201 Version/11.50",
"expected": "mobile:android:presto"
},
{
"desc": "Opera Mini 8 above on iPhone",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) OPiOS/12.1.1.98980 Mobile/13C75 Safari/9537.53",
"expected": "mobile:ios:webkit"
},
{
"desc": "Opera GX on Android",
"ua": "Mozilla/5.0 (Linux; Android 10; Redmi Note 8 Pro Build/QP1A.190711.020) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.5790.168 Mobile Safari/537.36 OPX/2",
"expected": "mobile:android:blink"
},
{
"desc": "Opera GX on Windows",
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.50747 OPRGX/60.0.3255.50747",
"expected": "desktop:windows:blink"
},
{
"desc": "Safari",
"ua": "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8",
"expected": "desktop:windows:webkit"
},
{
"desc": "Samsung Internet for Android",
"ua": "Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG-SM-G925A Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Samsung Internet for Gear VR",
"ua": "Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-G925K Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile VR Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Samsung Internet in Redmi 8A",
"ua": "Mozilla/5.0 (Linux; Android 10; Redmi 8A) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/23.0 Chrome/115.0.0.0 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Microsoft Edge on iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 EdgiOS/42.1.1.0 Mobile/15F79 Safari/605.1.15",
"expected": "mobile:ios:webkit"
},
{
"desc": "Microsoft Edge on Android",
"ua": "Mozilla/5.0 (Linux; Android 8.0.0; G8441 Build/47.1.A.12.270) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.123 Mobile Safari/537.36 EdgA/42.0.0.2529",
"expected": "mobile:android:blink"
},
{
"desc": "Microsoft Edge Chromium",
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.48 Safari/537.36 Edg/74.1.96.24",
"expected": "desktop:windows:blink"
},
{
"desc": "Firefox iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) FxiOS/1.1 Mobile/13B143 Safari/601.1.46",
"expected": "mobile:ios:webkit"
},
{
"desc": "Firefox on iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/112.0 Mobile/15E148 Safari/605.1.15",
"expected": "mobile:ios:webkit"
},
{
"desc": "Firefox iOS using iPad",
"ua": "Mozilla/5.0 (iPad; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4",
"expected": "mobile:ipados:webkit"
},
{
"desc": "Electron",
"ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Atom/1.41.0 Chrome/69.0.3497.128 Electron/4.2.7 Safari/537.36",
"expected": "desktop:windows:blink"
},
{
"desc": "LinkedIn",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 [LinkedInApp]",
"expected": "mobile:ios:webkit"
},
{
"desc": "Safari including comma in minor version number",
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6,2 Safari/605.1.15",
"expected": "desktop:mac:webkit"
},
{
"desc": "Mobile Safari including comma in minor version number",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6,2 Mobile/15E148 Safari/604.1",
"expected": "mobile:ios:webkit"
},
{
"desc": "TikTok on Android",
"ua": "Mozilla/5.0 (Linux; Android 11; 21061119AG Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/92.0.4515.131 Mobile Safari/537.36 trill_2022109040 JsSdk/1.0 NetType/MOBILE Channel/googleplay AppName/musical_ly app_version/21.9.4 ByteLocale/ru-RU ByteFullLocale/ru-RU Region/KG BytedanceWebview/d8a21c6",
"expected": "mobile:android:blink"
},
{
"desc": "TikTok on iOS",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_8 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 musical_ly_21.1.0 JsSdk/2.0 NetType/4G Channel/App Store ByteLocale/ru Region/RU ByteFullLocale/ru-RU isDarkMode/1 WKWebView/1 BytedanceWebview/d8a21c6",
"expected": "mobile:ios:webkit"
},
{
"desc": "TikTok Huawei on Android",
"ua": "Mozilla/5.0 (Linux; Android 10; STK-LX1 Build/HONORSTK-LX1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/110.0.5481.153 Mobile Safari/537.36 musical_ly_2022803040 JsSdk/1.0 NetType/WIFI Channel/huaweiadsglobal_int AppName/musical_ly app_version/28.3.4 ByteLocale/en ByteFullLocale/en Region/IQ Spark/1.2.7-alpha.8 AppVersion/28.3.4 PIA/1.5.11 BytedanceWebview/d8a21c6",
"expected": "mobile:android:blink"
},
{
"desc": "Chrome Mobile",
"ua": "Mozilla/5.0 (Linux; Android 7.1.2; Nexus 5X Build/N2G47W) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36",
"expected": "mobile:android:blink"
},
{
"desc": "Firefox Mobile Nexus 5",
"ua": "Mozilla/5.0 (Linux; Android 7.1.2; Nexus 5X Build/N2G47W) AppleWebKit/537.36 (KHTML, like Gecko) FxiOS/7.5b3349 Mobile/14F89 Safari/603.2.4",
"expected": "mobile:android:webkit"
},
{
"desc": "Firefox Mobile Generic",
"ua": "Mozilla/5.0 (Android 5.0; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0",
"expected": "mobile:android:gecko"
},
{
"desc": "Snapchat",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Snapchat/12.33.0.36 (like Safari/8614.1.25.0.31, panda)",
"expected": "mobile:ios:webkit"
},
{
"desc": "Twitter on Android",
"ua": "Mozilla/5.0 (Linux; Android 13; CPH2531 Build/SP1A.210812.016; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/123.0.6312.120 Mobile Safari/537.36 TwitterAndroid",
"expected": "mobile:android:blink"
},
{
"desc": "Twitter",
"ua": "Mozilla/5.0 (iPad; CPU OS 15_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19H12 Twitter for iPhone/10.34",
"expected": "mobile:ipados:webkit"
}
]
Loading

0 comments on commit a19d63a

Please sign in to comment.