github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/app/user_agent.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/avct/uasurfer" 11 ) 12 13 var platformNames = map[uasurfer.Platform]string{ 14 uasurfer.PlatformUnknown: "Windows", 15 uasurfer.PlatformWindows: "Windows", 16 uasurfer.PlatformMac: "Macintosh", 17 uasurfer.PlatformLinux: "Linux", 18 uasurfer.PlatformiPad: "iPad", 19 uasurfer.PlatformiPhone: "iPhone", 20 uasurfer.PlatformiPod: "iPod", 21 uasurfer.PlatformBlackberry: "BlackBerry", 22 uasurfer.PlatformWindowsPhone: "Windows Phone", 23 } 24 25 func getPlatformName(ua *uasurfer.UserAgent) string { 26 platform := ua.OS.Platform 27 28 if name, ok := platformNames[platform]; !ok { 29 return platformNames[uasurfer.PlatformUnknown] 30 } else { 31 return name 32 } 33 } 34 35 var osNames = map[uasurfer.OSName]string{ 36 uasurfer.OSUnknown: "", 37 uasurfer.OSWindowsPhone: "Windows Phone", 38 uasurfer.OSWindows: "Windows", 39 uasurfer.OSMacOSX: "Mac OS", 40 uasurfer.OSiOS: "iOS", 41 uasurfer.OSAndroid: "Android", 42 uasurfer.OSBlackberry: "BlackBerry", 43 uasurfer.OSChromeOS: "Chrome OS", 44 uasurfer.OSKindle: "Kindle", 45 uasurfer.OSWebOS: "webOS", 46 uasurfer.OSLinux: "Linux", 47 } 48 49 func getOSName(ua *uasurfer.UserAgent) string { 50 os := ua.OS 51 52 if os.Name == uasurfer.OSWindows { 53 major := os.Version.Major 54 minor := os.Version.Minor 55 56 name := "Windows" 57 58 // Adapted from https://github.com/mssola/user_agent/blob/master/operating_systems.go#L26 59 if major == 5 { 60 if minor == 0 { 61 name = "Windows 2000" 62 } else if minor == 1 { 63 name = "Windows XP" 64 } else if minor == 2 { 65 name = "Windows XP x64 Edition" 66 } 67 } else if major == 6 { 68 if minor == 0 { 69 name = "Windows Vista" 70 } else if minor == 1 { 71 name = "Windows 7" 72 } else if minor == 2 { 73 name = "Windows 8" 74 } else if minor == 3 { 75 name = "Windows 8.1" 76 } 77 } else if major == 10 { 78 name = "Windows 10" 79 } 80 81 return name 82 } else if name, ok := osNames[os.Name]; ok { 83 return name 84 } else { 85 return osNames[uasurfer.OSUnknown] 86 } 87 } 88 89 func getBrowserVersion(ua *uasurfer.UserAgent, userAgentString string) string { 90 if index := strings.Index(userAgentString, "Mattermost/"); index != -1 { 91 afterVersion := userAgentString[index+len("Mattermost/"):] 92 return strings.Fields(afterVersion)[0] 93 } else if index := strings.Index(userAgentString, "Franz/"); index != -1 { 94 afterVersion := userAgentString[index+len("Franz/"):] 95 return strings.Fields(afterVersion)[0] 96 } else { 97 return getUAVersion(ua.Browser.Version) 98 } 99 } 100 101 func getUAVersion(version uasurfer.Version) string { 102 if version.Patch == 0 { 103 return fmt.Sprintf("%v.%v", version.Major, version.Minor) 104 } else { 105 return fmt.Sprintf("%v.%v.%v", version.Major, version.Minor, version.Patch) 106 } 107 } 108 109 var browserNames = map[uasurfer.BrowserName]string{ 110 uasurfer.BrowserUnknown: "Unknown", 111 uasurfer.BrowserChrome: "Chrome", 112 uasurfer.BrowserIE: "Internet Explorer", 113 uasurfer.BrowserSafari: "Safari", 114 uasurfer.BrowserFirefox: "Firefox", 115 uasurfer.BrowserAndroid: "Android", 116 uasurfer.BrowserOpera: "Opera", 117 uasurfer.BrowserBlackberry: "BlackBerry", 118 } 119 120 func getBrowserName(ua *uasurfer.UserAgent, userAgentString string) string { 121 browser := ua.Browser.Name 122 123 if strings.Contains(userAgentString, "Mattermost") { 124 return "Desktop App" 125 } else if browser == uasurfer.BrowserIE && ua.Browser.Version.Major > 11 { 126 return "Edge" 127 } else if name, ok := browserNames[browser]; ok { 128 return name 129 } else { 130 return browserNames[uasurfer.BrowserUnknown] 131 } 132 }