From b2cb7985cbcb54973190fd90742c56d6a97f6dc2 Mon Sep 17 00:00:00 2001 From: Antoine Goutagny Date: Fri, 17 Feb 2023 16:32:26 +0100 Subject: [PATCH] Change package name, more fb/insta detection --- go.mod | 2 +- ua.go | 34 +++++++++++++++++++++++++++++++++- ua_test.go | 6 +++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ab4182e..6f96180 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/mileusna/useragent +module github.com/swaven/useragent go 1.14 diff --git a/ua.go b/ua.go index 28b92a5..f823f9b 100644 --- a/ua.go +++ b/ua.go @@ -57,7 +57,8 @@ const ( Applebot = "Applebot" Bingbot = "Bingbot" - FacebookApp = "Facebook App" + FacebookApp = "Facebook App" + InstagramApp = "Instagram App" ) // Parse user agent string returning UserAgent struct @@ -250,6 +251,14 @@ func Parse(userAgent string) UserAgent { case tokens.exists("FBAN"): ua.Name = FacebookApp + ua.Version = tokens.get("FBAN") + case tokens.exists("FB_IAB"): + ua.Name = FacebookApp + ua.Version = tokens.get("FBAV") + + case tokens.startsWith("Instagram"): + ua.Name = InstagramApp + ua.Version = tokens.findInstagramVersion() case tokens.get("HuaweiBrowser") != "": ua.Name = "Huawei Browser" @@ -484,6 +493,29 @@ func (p properties) findMacOSVersion() string { return "" } +func (p properties) startsWith(value string) bool { + for _, prop := range p.list { + if strings.HasPrefix(prop.Key, value) { + return true + } + } + return false +} + +func (p properties) findInstagramVersion() string { + for _, token := range p.list { + if strings.HasPrefix(token.Key, "Instagram") { + if ver := findVersion(token.Value); ver != "" { + return ver + } else if ver = findVersion(token.Key); ver != "" { + return ver + } + } + + } + return "" +} + // findBestMatch from the rest of the bunch // in first cycle only return key with version value // if withVerValue is false, do another cycle and return any token diff --git a/ua_test.go b/ua_test.go index 6b991e3..d40aebd 100644 --- a/ua_test.go +++ b/ua_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - ua "github.com/mileusna/useragent" + ua "github.com/swaven/useragent" ) var testTable = [][]string{ @@ -93,6 +93,10 @@ var testTable = [][]string{ //FB App {"Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19E258 [FBAN/FBIOS;FBDV/iPhone8,2;FBMD/iPhone;FBSN/iOS;FBSV/15.4.1;FBSS/3;FBID/phone;FBLC/fr_FR;FBOP/5]", ua.FacebookApp, "FBIOS", "mobile", ua.IOS}, + {"Mozilla/5.0 (Linux; Android 13; SM-T220 Build/TP1A.220624.014; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/109.0.5414.117 Safari/537.36 [FB_IAB/FB4A;FBAV/400.0.0.37.76;]", ua.FacebookApp, "400.0.0.37.76", "mobile", ua.Android}, + + //Instagram + {"Mozilla/5.0 (iPhone; CPU iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 270.0.0.13.83 (iPhone13,2; iOS 16_3; es_ES; es-ES; scale=3.00; 1170x2532; 445843881) NW/1", ua.InstagramApp, "270.0.0.13.83", "mobile", ua.IOS}, // 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},