Readding uasurfer library (#8420)

This commit is contained in:
Jesús Espino
2018-03-07 17:56:24 +00:00
committed by Corey Hulen
parent d448a6bef3
commit fd9ee780ed
12 changed files with 2371 additions and 2 deletions

56
vendor/github.com/avct/uasurfer/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,56 @@
# Compiled bin #
###################
# Compiled source #
###################
*.dll
*.exe
*.o
*.so
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
# Configuration Files #
#######################
*.cfg
# Logs and databases #
######################
*.log
*.sql
*.sqlite
logs
coverage.html
coverage.out
# Test Files #
#######################
*.test
# OS generated files #
######################
.DS_Store
.DS_Store?
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# go.rice generated files
*.rice-box.go
# Dev Tools #
######################
.vagrant

11
vendor/github.com/avct/uasurfer/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,11 @@
sudo: false
language: go
go:
- 1.9.x
- 1.8.x
- 1.7.x
script:
- go test

192
vendor/github.com/avct/uasurfer/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,192 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Copyright 2015 Avocet Systems Ltd.
http://avocet.io/opensource
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
Copyright 2015 Avocet Systems Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

169
vendor/github.com/avct/uasurfer/README.md generated vendored Normal file
View File

@@ -0,0 +1,169 @@
[![Build Status](https://travis-ci.org/avct/uasurfer.svg?branch=master)](https://travis-ci.org/avct/uasurfer) [![GoDoc](https://godoc.org/github.com/avct/uasurfer?status.svg)](https://godoc.org/github.com/avct/uasurfer) [![Go Report Card](https://goreportcard.com/badge/github.com/avct/uasurfer)](https://goreportcard.com/report/github.com/avct/uasurfer)
# uasurfer
![uasurfer-100px](https://cloud.githubusercontent.com/assets/597902/16172506/9debc136-357a-11e6-90fb-c7c46f50dff0.png)
**User Agent Surfer** (uasurfer) is a lightweight Golang package that parses and abstracts [HTTP User-Agent strings](https://en.wikipedia.org/wiki/User_agent) with particular attention to device type.
The following information is returned by uasurfer from a raw HTTP User-Agent string:
| Name | Example | Coverage in 192,792 parses |
|----------------|---------|--------------------------------|
| Browser name | `chrome` | 99.85% |
| Browser version | `53` | 99.17% |
| Platform | `ipad` | 99.97% |
| OS name | `ios` | 99.96% |
| OS version | `10` | 98.81% |
| Device type | `tablet` | 99.98% |
Layout engine, browser language, and other esoteric attributes are not parsed.
Coverage is estimated from a random sample of real UA strings collected across thousands of sources in US and EU mid-2016.
## Usage
### Parse(ua string) Function
The `Parse()` function accepts a user agent `string` and returns UserAgent struct with named constants and integers for versions (minor, major and patch separately), and the full UA string that was parsed (lowercase). A string can be retrieved by adding `.String()` to a variable, such as `uasurfer.BrowserName.String()`.
```
// Define a user agent string
myUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"
// Parse() returns all attributes, including returning the full UA string last
ua, uaString := uasurfer.Parse(myUA)
```
where example UserAgent is:
```
{
Browser {
BrowserName: BrowserChrome,
Version: {
Major: 45,
Minor: 0,
Patch: 2454,
},
},
OS {
Platform: PlatformMac,
Name: OSMacOSX,
Version: {
Major: 10,
Minor: 10,
Patch: 5,
},
},
DeviceType: DeviceComputer,
}
```
**Usage note:** There are some OSes that do not return a version, see docs below. Linux is typically not reported with a specific Linux distro name or version.
#### Browser Name
* `BrowserChrome` - Google [Chrome](https://en.wikipedia.org/wiki/Google_Chrome), [Chromium](https://en.wikipedia.org/wiki/Chromium_(web_browser))
* `BrowserSafari` - Apple [Safari](https://en.wikipedia.org/wiki/Safari_(web_browser)), Google Search ([GSA](https://itunes.apple.com/us/app/google/id284815942))
* `BrowserIE` - Microsoft [Internet Explorer](https://en.wikipedia.org/wiki/Internet_Explorer), [Edge](https://en.wikipedia.org/wiki/Microsoft_Edge)
* `BrowserFirefox` - Mozilla [Firefox](https://en.wikipedia.org/wiki/Firefox), GNU [IceCat](https://en.wikipedia.org/wiki/GNU_IceCat), [Iceweasel](https://en.wikipedia.org/wiki/Mozilla_Corporation_software_rebranded_by_the_Debian_project#Iceweasel), [Seamonkey](https://en.wikipedia.org/wiki/SeaMonkey)
* `BrowserAndroid` - Android [WebView](https://developer.chrome.com/multidevice/webview/overview) (Android OS <4.4 only)
* `BrowserOpera` - [Opera](https://en.wikipedia.org/wiki/Opera_(web_browser))
* `BrowserUCBrowser` - [UC Browser](https://en.wikipedia.org/wiki/UC_Browser)
* `BrowserSilk` - Amazon [Silk](https://en.wikipedia.org/wiki/Amazon_Silk)
* `BrowserSpotify` - [Spotify](https://en.wikipedia.org/wiki/Spotify#Clients) desktop client
* `BrowserBlackberry` - RIM [BlackBerry](https://en.wikipedia.org/wiki/BlackBerry)
* `BrowserUnknown` - Unknown
#### Browser Version
Browser version returns an `unint8` of the major version attribute of the User-Agent String. For example Chrome 45.0.23423 would return `45`. The intention is to support math operators with versions, such as "do XYZ for Chrome version >23".
Unknown version is returned as `0`.
#### Platform
* `PlatformWindows` - Microsoft Windows
* `PlatformMac` - Apple Macintosh
* `PlatformLinux` - Linux, including Android and other OSes
* `PlatformiPad` - Apple iPad
* `PlatformiPhone` - Apple iPhone
* `PlatformBlackberry` - RIM Blackberry
* `PlatformWindowsPhone` Microsoft Windows Phone & Mobile
* `PlatformKindle` - Amazon Kindle & Kindle Fire
* `PlatformPlaystation` - Sony Playstation, Vita, PSP
* `PlatformXbox` - Microsoft Xbox - `PlatformXbox`
* `PlatformNintendo` - Nintendo DS, Wii, etc.
* `PlatformUnknown` - Unknown
#### OS Name
* `OSWindows`
* `OSMacOSX` - includes "macOS Sierra"
* `OSiOS`
* `OSAndroid`
* `OSChromeOS`
* `OSWebOS`
* `OSLinux`
* `OSPlaystation`
* `OSXbox`
* `OSNintendo`
* `OSUnknown`
#### OS Version
OS X major version is alway 10 with consecutive minor versions indicating release releases (10 - Yosemite, 11 - El Capitain, 12 Sierra, etc). Windows version is NT version. `Version{0, 0, 0}` indicated version is unknown or not evaluated.
Versions can be compared using `Less` function: `if ver1.Less(ver2) {}`
Here are some examples across the platform, os.name, and os.version:
* For Windows XP (Windows NT 5.1), "`PlatformWindows`" is the platform, "`OSWindows`" is the name, and `{5, 1, 0}` the version.
* For OS X 10.5.1, "`PlatformMac`" is the platform, "`OSMacOSX`" the name, and `{10, 5, 1}` the version.
* For Android 5.1, "`PlatformLinux`" is the platform, "`OSAndroid`" is the name, and `{5, 1, 0}` the version.
* For iOS 5.1, "`PlatformiPhone`" or "`PlatformiPad`" is the platform, "`OSiOS`" is the name, and `{5, 1, 0}` the version.
###### Windows Version Guide
* Windows 10 - `{10, 0, 0}`
* Windows 8.1 - `{6, 3, 0}`
* Windows 8 - `{6, 2, 0}`
* Windows 7 - `{6, 1, 0}`
* Windows Vista - `{6, 0, 0}`
* Windows XP - `{5, 1, 0}` or `{5, 2, 0}`
* Windows 2000 - `{5, 0, 0}`
Windows 95, 98, and ME represent 0.01% of traffic worldwide and are not available through this package at this time.
#### DeviceType
DeviceType is typically quite accurate, though determining between phones and tablets on Android is not always possible due to how some vendors design their UA strings. A mobile Android device without tablet indicator defaults to being classified as a phone. DeviceTV supports major brands such as Philips, Sharp, Vizio and steaming boxes such as Apple, Google, Roku, Amazon.
* `DeviceComputer`
* `DevicePhone`
* `DeviceTablet`
* `DeviceTV`
* `DeviceConsole`
* `DeviceWearable`
* `DeviceUnknown`
## Example Combinations of Attributes
* Surface RT -> `OSWindows8`, `DeviceTablet`, OSVersion >= `6`
* Android Tablet -> `OSAndroid`, `DeviceTablet`
* Microsoft Edge -> `BrowserIE`, BrowserVersion >= `12.0.0`
## To do
* Remove compiled regexp in favor of string.Contains wherever possible (lowers mem/alloc)
* Better version support on Firefox derivatives (e.g. SeaMonkey)
* Potential additional browser support:
* "NetFront" (1% share in India)
* "QQ Browser" (6.5% share in China)
* "Sogou Explorer" (5% share in China)
* "Maxthon" (1.5% share in China)
* "Nokia"
* Potential additional OS support:
* "Nokia" (5% share in India)
* "Series 40" (5.5% share in India)
* Windows 2003 Server
* iOS safari browser identification based on iOS version
* Add android version to browser identification
* old Macs
* "opera/9.64 (macintosh; ppc mac os x; u; en) presto/2.1.1"
* old Windows
* "mozilla/5.0 (windows nt 4.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/37.0.2049.0 safari/537.36"

192
vendor/github.com/avct/uasurfer/browser.go generated vendored Normal file
View File

@@ -0,0 +1,192 @@
package uasurfer
import (
"strings"
)
// Browser struct contains the lowercase name of the browser, along
// with its browser version number. Browser are grouped together without
// consideration for device. For example, Chrome (Chrome/43.0) and Chrome for iOS
// (CriOS/43.0) would both return as "chrome" (name) and 43.0 (version). Similarly
// Internet Explorer 11 and Edge 12 would return as "ie" and "11" or "12", respectively.
// type Browser struct {
// Name BrowserName
// Version struct {
// Major int
// Minor int
// Patch int
// }
// }
// Retrieve browser name from UA strings
func (u *UserAgent) evalBrowserName(ua string) bool {
// Blackberry goes first because it reads as MSIE & Safari
if strings.Contains(ua, "blackberry") || strings.Contains(ua, "playbook") || strings.Contains(ua, "bb10") || strings.Contains(ua, "rim ") {
u.Browser.Name = BrowserBlackberry
return u.isBot()
}
if strings.Contains(ua, "applewebkit") {
switch {
case strings.Contains(ua, "opr/") || strings.Contains(ua, "opios/"):
u.Browser.Name = BrowserOpera
case strings.Contains(ua, "silk/"):
u.Browser.Name = BrowserSilk
case strings.Contains(ua, "edge/") || strings.Contains(ua, "iemobile/") || strings.Contains(ua, "msie "):
u.Browser.Name = BrowserIE
case strings.Contains(ua, "ucbrowser/") || strings.Contains(ua, "ucweb/"):
u.Browser.Name = BrowserUCBrowser
// Edge, Silk and other chrome-identifying browsers must evaluate before chrome, unless we want to add more overhead
case strings.Contains(ua, "chrome/") || strings.Contains(ua, "crios/") || strings.Contains(ua, "chromium/") || strings.Contains(ua, "crmo/"):
u.Browser.Name = BrowserChrome
case strings.Contains(ua, "android") && !strings.Contains(ua, "chrome/") && strings.Contains(ua, "version/") && !strings.Contains(ua, "like android"):
// Android WebView on Android >= 4.4 is purposefully being identified as Chrome above -- https://developer.chrome.com/multidevice/webview/overview
u.Browser.Name = BrowserAndroid
case strings.Contains(ua, "fxios"):
u.Browser.Name = BrowserFirefox
case strings.Contains(ua, " spotify/"):
u.Browser.Name = BrowserSpotify
// AppleBot uses webkit signature as well
case strings.Contains(ua, "applebot"):
u.Browser.Name = BrowserAppleBot
// presume it's safari unless an esoteric browser is being specified (webOSBrowser, SamsungBrowser, etc.)
case strings.Contains(ua, "like gecko") && strings.Contains(ua, "mozilla/") && strings.Contains(ua, "safari/") && !strings.Contains(ua, "linux") && !strings.Contains(ua, "android") && !strings.Contains(ua, "browser/") && !strings.Contains(ua, "os/"):
u.Browser.Name = BrowserSafari
// if we got this far and the device is iPhone or iPad, assume safari. Some agents don't actually contain the word "safari"
case strings.Contains(ua, "iphone") || strings.Contains(ua, "ipad"):
u.Browser.Name = BrowserSafari
// Google's search app on iPhone, leverages native Safari rather than Chrome
case strings.Contains(ua, " gsa/"):
u.Browser.Name = BrowserSafari
default:
goto notwebkit
}
return u.isBot()
}
notwebkit:
switch {
case strings.Contains(ua, "msie") || strings.Contains(ua, "trident"):
u.Browser.Name = BrowserIE
case strings.Contains(ua, "gecko") && (strings.Contains(ua, "firefox") || strings.Contains(ua, "iceweasel") || strings.Contains(ua, "seamonkey") || strings.Contains(ua, "icecat")):
u.Browser.Name = BrowserFirefox
case strings.Contains(ua, "presto") || strings.Contains(ua, "opera"):
u.Browser.Name = BrowserOpera
case strings.Contains(ua, "ucbrowser"):
u.Browser.Name = BrowserUCBrowser
case strings.Contains(ua, "applebot"):
u.Browser.Name = BrowserAppleBot
case strings.Contains(ua, "baiduspider"):
u.Browser.Name = BrowserBaiduBot
case strings.Contains(ua, "adidxbot") || strings.Contains(ua, "bingbot") || strings.Contains(ua, "bingpreview"):
u.Browser.Name = BrowserBingBot
case strings.Contains(ua, "duckduckbot"):
u.Browser.Name = BrowserDuckDuckGoBot
case strings.Contains(ua, "facebot") || strings.Contains(ua, "facebookexternalhit"):
u.Browser.Name = BrowserFacebookBot
case strings.Contains(ua, "googlebot"):
u.Browser.Name = BrowserGoogleBot
case strings.Contains(ua, "linkedinbot"):
u.Browser.Name = BrowserLinkedInBot
case strings.Contains(ua, "msnbot"):
u.Browser.Name = BrowserMsnBot
case strings.Contains(ua, "pingdom.com_bot"):
u.Browser.Name = BrowserPingdomBot
case strings.Contains(ua, "twitterbot"):
u.Browser.Name = BrowserTwitterBot
case strings.Contains(ua, "yandex") || strings.Contains(ua, "yadirectfetcher"):
u.Browser.Name = BrowserYandexBot
case strings.Contains(ua, "yahoo"):
u.Browser.Name = BrowserYahooBot
case strings.Contains(ua, "phantomjs"):
u.Browser.Name = BrowserBot
default:
u.Browser.Name = BrowserUnknown
}
return u.isBot()
}
// Retrieve browser version
// Methods used in order:
// 1st: look for generic version/#
// 2nd: look for browser-specific instructions (e.g. chrome/34)
// 3rd: infer from OS (iOS only)
func (u *UserAgent) evalBrowserVersion(ua string) {
// if there is a 'version/#' attribute with numeric version, use it -- except for Chrome since Android vendors sometimes hijack version/#
if u.Browser.Name != BrowserChrome && u.Browser.Version.findVersionNumber(ua, "version/") {
return
}
switch u.Browser.Name {
case BrowserChrome:
// match both chrome and crios
_ = u.Browser.Version.findVersionNumber(ua, "chrome/") || u.Browser.Version.findVersionNumber(ua, "crios/") || u.Browser.Version.findVersionNumber(ua, "crmo/")
case BrowserIE:
if u.Browser.Version.findVersionNumber(ua, "msie ") || u.Browser.Version.findVersionNumber(ua, "edge/") {
return
}
// get MSIE version from trident version https://en.wikipedia.org/wiki/Trident_(layout_engine)
if u.Browser.Version.findVersionNumber(ua, "trident/") {
// convert trident versions 3-7 to MSIE version
if (u.Browser.Version.Major >= 3) && (u.Browser.Version.Major <= 7) {
u.Browser.Version.Major += 4
}
}
case BrowserFirefox:
_ = u.Browser.Version.findVersionNumber(ua, "firefox/") || u.Browser.Version.findVersionNumber(ua, "fxios/")
case BrowserSafari: // executes typically if we're on iOS and not using a familiar browser
u.Browser.Version = u.OS.Version
// early Safari used a version number +1 to OS version
if (u.Browser.Version.Major <= 3) && (u.Browser.Version.Major >= 1) {
u.Browser.Version.Major++
}
case BrowserUCBrowser:
_ = u.Browser.Version.findVersionNumber(ua, "ucbrowser/")
case BrowserOpera:
_ = u.Browser.Version.findVersionNumber(ua, "opr/") || u.Browser.Version.findVersionNumber(ua, "opios/") || u.Browser.Version.findVersionNumber(ua, "opera/")
case BrowserSilk:
_ = u.Browser.Version.findVersionNumber(ua, "silk/")
case BrowserSpotify:
_ = u.Browser.Version.findVersionNumber(ua, "spotify/")
}
}

49
vendor/github.com/avct/uasurfer/const_string.go generated vendored Normal file
View File

@@ -0,0 +1,49 @@
// Code generated by "stringer -type=DeviceType,BrowserName,OSName,Platform -output=const_string.go"; DO NOT EDIT.
package uasurfer
import "fmt"
const _DeviceType_name = "DeviceUnknownDeviceComputerDeviceTabletDevicePhoneDeviceConsoleDeviceWearableDeviceTV"
var _DeviceType_index = [...]uint8{0, 13, 27, 39, 50, 63, 77, 85}
func (i DeviceType) String() string {
if i < 0 || i >= DeviceType(len(_DeviceType_index)-1) {
return fmt.Sprintf("DeviceType(%d)", i)
}
return _DeviceType_name[_DeviceType_index[i]:_DeviceType_index[i+1]]
}
const _BrowserName_name = "BrowserUnknownBrowserChromeBrowserIEBrowserSafariBrowserFirefoxBrowserAndroidBrowserOperaBrowserBlackberryBrowserUCBrowserBrowserSilkBrowserNokiaBrowserNetFrontBrowserQQBrowserMaxthonBrowserSogouExplorerBrowserSpotifyBrowserBotBrowserAppleBotBrowserBaiduBotBrowserBingBotBrowserDuckDuckGoBotBrowserFacebookBotBrowserGoogleBotBrowserLinkedInBotBrowserMsnBotBrowserPingdomBotBrowserTwitterBotBrowserYandexBotBrowserYahooBot"
var _BrowserName_index = [...]uint16{0, 14, 27, 36, 49, 63, 77, 89, 106, 122, 133, 145, 160, 169, 183, 203, 217, 227, 242, 257, 271, 291, 309, 325, 343, 356, 373, 390, 406, 421}
func (i BrowserName) String() string {
if i < 0 || i >= BrowserName(len(_BrowserName_index)-1) {
return fmt.Sprintf("BrowserName(%d)", i)
}
return _BrowserName_name[_BrowserName_index[i]:_BrowserName_index[i+1]]
}
const _OSName_name = "OSUnknownOSWindowsPhoneOSWindowsOSMacOSXOSiOSOSAndroidOSBlackberryOSChromeOSOSKindleOSWebOSOSLinuxOSPlaystationOSXboxOSNintendoOSBot"
var _OSName_index = [...]uint8{0, 9, 23, 32, 40, 45, 54, 66, 76, 84, 91, 98, 111, 117, 127, 132}
func (i OSName) String() string {
if i < 0 || i >= OSName(len(_OSName_index)-1) {
return fmt.Sprintf("OSName(%d)", i)
}
return _OSName_name[_OSName_index[i]:_OSName_index[i+1]]
}
const _Platform_name = "PlatformUnknownPlatformWindowsPlatformMacPlatformLinuxPlatformiPadPlatformiPhonePlatformiPodPlatformBlackberryPlatformWindowsPhonePlatformPlaystationPlatformXboxPlatformNintendoPlatformBot"
var _Platform_index = [...]uint8{0, 15, 30, 41, 54, 66, 80, 92, 110, 130, 149, 161, 177, 188}
func (i Platform) String() string {
if i < 0 || i >= Platform(len(_Platform_index)-1) {
return fmt.Sprintf("Platform(%d)", i)
}
return _Platform_name[_Platform_index[i]:_Platform_index[i+1]]
}

60
vendor/github.com/avct/uasurfer/device.go generated vendored Normal file
View File

@@ -0,0 +1,60 @@
package uasurfer
import (
"strings"
)
func (u *UserAgent) evalDevice(ua string) {
switch {
case u.OS.Platform == PlatformWindows || u.OS.Platform == PlatformMac || u.OS.Name == OSChromeOS:
if strings.Contains(ua, "mobile") || strings.Contains(ua, "touch") {
u.DeviceType = DeviceTablet // windows rt, linux haxor tablets
return
}
u.DeviceType = DeviceComputer
case u.OS.Platform == PlatformiPad || u.OS.Platform == PlatformiPod || strings.Contains(ua, "tablet") || strings.Contains(ua, "kindle/") || strings.Contains(ua, "playbook"):
u.DeviceType = DeviceTablet
case u.OS.Platform == PlatformiPhone || u.OS.Platform == PlatformBlackberry || strings.Contains(ua, "phone"):
u.DeviceType = DevicePhone
// long list of smarttv and tv dongle identifiers
case strings.Contains(ua, "tv") || strings.Contains(ua, "crkey") || strings.Contains(ua, "googletv") || strings.Contains(ua, "aftb") || strings.Contains(ua, "adt-") || strings.Contains(ua, "roku") || strings.Contains(ua, "viera") || strings.Contains(ua, "aquos") || strings.Contains(ua, "dtv") || strings.Contains(ua, "appletv") || strings.Contains(ua, "smarttv") || strings.Contains(ua, "tuner") || strings.Contains(ua, "smart-tv") || strings.Contains(ua, "hbbtv") || strings.Contains(ua, "netcast") || strings.Contains(ua, "vizio"):
u.DeviceType = DeviceTV
case u.OS.Name == OSAndroid:
// android phones report as "mobile", android tablets should not but often do -- http://android-developers.blogspot.com/2010/12/android-browser-user-agent-issues.html
if strings.Contains(ua, "mobile") {
u.DeviceType = DevicePhone
return
}
if strings.Contains(ua, "tablet") || strings.Contains(ua, "nexus 7") || strings.Contains(ua, "nexus 9") || strings.Contains(ua, "nexus 10") || strings.Contains(ua, "xoom") {
u.DeviceType = DeviceTablet
return
}
u.DeviceType = DevicePhone // default to phone
case u.OS.Platform == PlatformPlaystation || u.OS.Platform == PlatformXbox || u.OS.Platform == PlatformNintendo:
u.DeviceType = DeviceConsole
case strings.Contains(ua, "glass") || strings.Contains(ua, "watch") || strings.Contains(ua, "sm-v"):
u.DeviceType = DeviceWearable
// specifically above "mobile" string check as Kindle Fire tablets report as "mobile"
case u.Browser.Name == BrowserSilk || u.OS.Name == OSKindle && !strings.Contains(ua, "sd4930ur"):
u.DeviceType = DeviceTablet
case strings.Contains(ua, "mobile") || strings.Contains(ua, "touch") || strings.Contains(ua, " mobi") || strings.Contains(ua, "webos"): //anything "mobile"/"touch" that didn't get captured as tablet, console or wearable is presumed a phone
u.DeviceType = DevicePhone
case u.OS.Name == OSLinux: // linux goes last since it's in so many other device types (tvs, wearables, android-based stuff)
u.DeviceType = DeviceComputer
default:
u.DeviceType = DeviceUnknown
}
}

336
vendor/github.com/avct/uasurfer/system.go generated vendored Normal file
View File

@@ -0,0 +1,336 @@
package uasurfer
import (
"regexp"
"strconv"
"strings"
)
var (
amazonFireFingerprint = regexp.MustCompile("\\s(k[a-z]{3,5}|sd\\d{4}ur)\\s") //tablet or phone
)
func (u *UserAgent) evalOS(ua string) bool {
s := strings.IndexRune(ua, '(')
e := strings.IndexRune(ua, ')')
if s > e {
s = 0
e = len(ua)
}
if e == -1 {
e = len(ua)
}
agentPlatform := ua[s+1 : e]
specsEnd := strings.Index(agentPlatform, ";")
var specs string
if specsEnd != -1 {
specs = agentPlatform[:specsEnd]
} else {
specs = agentPlatform
}
//strict OS & version identification
switch specs {
case "android":
u.evalLinux(ua, agentPlatform)
case "bb10", "playbook":
u.OS.Platform = PlatformBlackberry
u.OS.Name = OSBlackberry
case "x11", "linux":
u.evalLinux(ua, agentPlatform)
case "ipad", "iphone", "ipod touch", "ipod":
u.evaliOS(specs, agentPlatform)
case "macintosh":
u.evalMacintosh(ua)
default:
switch {
// Blackberry
case strings.Contains(ua, "blackberry") || strings.Contains(ua, "playbook"):
u.OS.Platform = PlatformBlackberry
u.OS.Name = OSBlackberry
// Windows Phone
case strings.Contains(agentPlatform, "windows phone "):
u.evalWindowsPhone(agentPlatform)
// Windows, Xbox
case strings.Contains(ua, "windows ") || strings.Contains(ua, "microsoft-cryptoapi"):
u.evalWindows(ua)
// Kindle
case strings.Contains(ua, "kindle/") || amazonFireFingerprint.MatchString(agentPlatform):
u.OS.Platform = PlatformLinux
u.OS.Name = OSKindle
// Linux (broader attempt)
case strings.Contains(ua, "linux"):
u.evalLinux(ua, agentPlatform)
// WebOS (non-linux flagged)
case strings.Contains(ua, "webos") || strings.Contains(ua, "hpwos"):
u.OS.Platform = PlatformLinux
u.OS.Name = OSWebOS
// Nintendo
case strings.Contains(ua, "nintendo"):
u.OS.Platform = PlatformNintendo
u.OS.Name = OSNintendo
// Playstation
case strings.Contains(ua, "playstation") || strings.Contains(ua, "vita") || strings.Contains(ua, "psp"):
u.OS.Platform = PlatformPlaystation
u.OS.Name = OSPlaystation
// Android
case strings.Contains(ua, "android"):
u.evalLinux(ua, agentPlatform)
// Apple CFNetwork
case strings.Contains(ua, "cfnetwork") && strings.Contains(ua, "darwin"):
u.evalMacintosh(ua)
default:
u.OS.Platform = PlatformUnknown
u.OS.Name = OSUnknown
}
}
return u.isBot()
}
func (u *UserAgent) isBot() bool {
if u.OS.Platform == PlatformBot || u.OS.Name == OSBot {
u.DeviceType = DeviceComputer
return true
}
if u.Browser.Name >= BrowserBot && u.Browser.Name <= BrowserYahooBot {
u.OS.Platform = PlatformBot
u.OS.Name = OSBot
u.DeviceType = DeviceComputer
return true
}
return false
}
// evalLinux returns the `Platform`, `OSName` and Version of UAs with
// 'linux' listed as their platform.
func (u *UserAgent) evalLinux(ua string, agentPlatform string) {
switch {
// Kindle Fire
case strings.Contains(ua, "kindle") || amazonFireFingerprint.MatchString(agentPlatform):
// get the version of Android if available, though we don't call this OSAndroid
u.OS.Platform = PlatformLinux
u.OS.Name = OSKindle
u.OS.Version.findVersionNumber(agentPlatform, "android ")
// Android, Kindle Fire
case strings.Contains(ua, "android") || strings.Contains(ua, "googletv"):
// Android
u.OS.Platform = PlatformLinux
u.OS.Name = OSAndroid
u.OS.Version.findVersionNumber(agentPlatform, "android ")
// ChromeOS
case strings.Contains(ua, "cros"):
u.OS.Platform = PlatformLinux
u.OS.Name = OSChromeOS
// WebOS
case strings.Contains(ua, "webos") || strings.Contains(ua, "hpwos"):
u.OS.Platform = PlatformLinux
u.OS.Name = OSWebOS
// Linux, "Linux-like"
case strings.Contains(ua, "x11") || strings.Contains(ua, "bsd") || strings.Contains(ua, "suse") || strings.Contains(ua, "debian") || strings.Contains(ua, "ubuntu"):
u.OS.Platform = PlatformLinux
u.OS.Name = OSLinux
default:
u.OS.Platform = PlatformLinux
u.OS.Name = OSLinux
}
}
// evaliOS returns the `Platform`, `OSName` and Version of UAs with
// 'ipad' or 'iphone' listed as their platform.
func (u *UserAgent) evaliOS(uaPlatform string, agentPlatform string) {
switch uaPlatform {
// iPhone
case "iphone":
u.OS.Platform = PlatformiPhone
u.OS.Name = OSiOS
u.OS.getiOSVersion(agentPlatform)
// iPad
case "ipad":
u.OS.Platform = PlatformiPad
u.OS.Name = OSiOS
u.OS.getiOSVersion(agentPlatform)
// iPod
case "ipod touch", "ipod":
u.OS.Platform = PlatformiPod
u.OS.Name = OSiOS
u.OS.getiOSVersion(agentPlatform)
default:
u.OS.Platform = PlatformiPad
u.OS.Name = OSUnknown
}
}
func (u *UserAgent) evalWindowsPhone(agentPlatform string) {
u.OS.Platform = PlatformWindowsPhone
if u.OS.Version.findVersionNumber(agentPlatform, "windows phone os ") || u.OS.Version.findVersionNumber(agentPlatform, "windows phone ") {
u.OS.Name = OSWindowsPhone
} else {
u.OS.Name = OSUnknown
}
}
func (u *UserAgent) evalWindows(ua string) {
switch {
//Xbox -- it reads just like Windows
case strings.Contains(ua, "xbox"):
u.OS.Platform = PlatformXbox
u.OS.Name = OSXbox
if !u.OS.Version.findVersionNumber(ua, "windows nt ") {
u.OS.Version.Major = 6
u.OS.Version.Minor = 0
u.OS.Version.Patch = 0
}
// No windows version
case !strings.Contains(ua, "windows "):
u.OS.Platform = PlatformWindows
u.OS.Name = OSUnknown
case strings.Contains(ua, "windows nt ") && u.OS.Version.findVersionNumber(ua, "windows nt "):
u.OS.Platform = PlatformWindows
u.OS.Name = OSWindows
case strings.Contains(ua, "windows xp"):
u.OS.Platform = PlatformWindows
u.OS.Name = OSWindows
u.OS.Version.Major = 5
u.OS.Version.Minor = 1
u.OS.Version.Patch = 0
default:
u.OS.Platform = PlatformWindows
u.OS.Name = OSUnknown
}
}
func (u *UserAgent) evalMacintosh(uaPlatformGroup string) {
u.OS.Platform = PlatformMac
if i := strings.Index(uaPlatformGroup, "os x 10"); i != -1 {
u.OS.Name = OSMacOSX
u.OS.Version.parse(uaPlatformGroup[i+5:])
return
}
u.OS.Name = OSUnknown
}
func (v *Version) findVersionNumber(s string, m string) bool {
if ind := strings.Index(s, m); ind != -1 {
return v.parse(s[ind+len(m):])
}
return false
}
// getiOSVersion accepts the platform portion of a UA string and returns
// a Version.
func (o *OS) getiOSVersion(uaPlatformGroup string) {
if i := strings.Index(uaPlatformGroup, "cpu iphone os "); i != -1 {
o.Version.parse(uaPlatformGroup[i+14:])
return
}
if i := strings.Index(uaPlatformGroup, "cpu os "); i != -1 {
o.Version.parse(uaPlatformGroup[i+7:])
return
}
o.Version.parse(uaPlatformGroup)
}
// strToInt simply accepts a string and returns a `int`,
// with '0' being default.
func strToInt(str string) int {
i, _ := strconv.Atoi(str)
return i
}
// strToVer accepts a string and returns a Version,
// with {0, 0, 0} being default.
func (v *Version) parse(str string) bool {
if len(str) == 0 || str[0] < '0' || str[0] > '9' {
return false
}
for i := 0; i < 3; i++ {
empty := true
val := 0
l := len(str) - 1
for k, c := range str {
if c >= '0' && c <= '9' {
if empty {
val = int(c) - 48
empty = false
if k == l {
str = str[:0]
}
continue
}
if val == 0 {
if c == '0' {
if k == l {
str = str[:0]
}
continue
}
str = str[k:]
break
}
val = 10*val + int(c) - 48
if k == l {
str = str[:0]
}
continue
}
str = str[k+1:]
break
}
switch i {
case 0:
v.Major = val
case 1:
v.Minor = val
case 2:
v.Patch = val
}
}
return true
}

227
vendor/github.com/avct/uasurfer/uasurfer.go generated vendored Normal file
View File

@@ -0,0 +1,227 @@
// Package uasurfer provides fast and reliable abstraction
// of HTTP User-Agent strings. The philosophy is to identify
// technologies that holds >1% market share, and to avoid
// expending resources and accuracy on guessing at esoteric UA
// strings.
package uasurfer
import "strings"
//go:generate stringer -type=DeviceType,BrowserName,OSName,Platform -output=const_string.go
// DeviceType (int) returns a constant.
type DeviceType int
// A complete list of supported devices in the
// form of constants.
const (
DeviceUnknown DeviceType = iota
DeviceComputer
DeviceTablet
DevicePhone
DeviceConsole
DeviceWearable
DeviceTV
)
// BrowserName (int) returns a constant.
type BrowserName int
// A complete list of supported web browsers in the
// form of constants.
const (
BrowserUnknown BrowserName = iota
BrowserChrome
BrowserIE
BrowserSafari
BrowserFirefox
BrowserAndroid
BrowserOpera
BrowserBlackberry
BrowserUCBrowser
BrowserSilk
BrowserNokia
BrowserNetFront
BrowserQQ
BrowserMaxthon
BrowserSogouExplorer
BrowserSpotify
BrowserBot // Bot list begins here
BrowserAppleBot
BrowserBaiduBot
BrowserBingBot
BrowserDuckDuckGoBot
BrowserFacebookBot
BrowserGoogleBot
BrowserLinkedInBot
BrowserMsnBot
BrowserPingdomBot
BrowserTwitterBot
BrowserYandexBot
BrowserYahooBot // Bot list ends here
)
// OSName (int) returns a constant.
type OSName int
// A complete list of supported OSes in the
// form of constants. For handling particular versions
// of operating systems (e.g. Windows 2000), see
// the README.md file.
const (
OSUnknown OSName = iota
OSWindowsPhone
OSWindows
OSMacOSX
OSiOS
OSAndroid
OSBlackberry
OSChromeOS
OSKindle
OSWebOS
OSLinux
OSPlaystation
OSXbox
OSNintendo
OSBot
)
// Platform (int) returns a constant.
type Platform int
// A complete list of supported platforms in the
// form of constants. Many OSes report their
// true platform, such as Android OS being Linux
// platform.
const (
PlatformUnknown Platform = iota
PlatformWindows
PlatformMac
PlatformLinux
PlatformiPad
PlatformiPhone
PlatformiPod
PlatformBlackberry
PlatformWindowsPhone
PlatformPlaystation
PlatformXbox
PlatformNintendo
PlatformBot
)
type Version struct {
Major int
Minor int
Patch int
}
func (v Version) Less(c Version) bool {
if v.Major < c.Major {
return true
}
if v.Major > c.Major {
return false
}
if v.Minor < c.Minor {
return true
}
if v.Minor > c.Minor {
return false
}
return v.Patch < c.Patch
}
type UserAgent struct {
Browser Browser
OS OS
DeviceType DeviceType
}
type Browser struct {
Name BrowserName
Version Version
}
type OS struct {
Platform Platform
Name OSName
Version Version
}
// Reset resets the UserAgent to it's zero value
func (ua *UserAgent) Reset() {
ua.Browser = Browser{}
ua.OS = OS{}
ua.DeviceType = DeviceUnknown
}
// Parse accepts a raw user agent (string) and returns the UserAgent.
func Parse(ua string) *UserAgent {
dest := new(UserAgent)
parse(ua, dest)
return dest
}
// ParseUserAgent is the same as Parse, but populates the supplied UserAgent.
// It is the caller's responsibility to call Reset() on the UserAgent before
// passing it to this function.
func ParseUserAgent(ua string, dest *UserAgent) {
parse(ua, dest)
}
func parse(ua string, dest *UserAgent) {
ua = normalise(ua)
switch {
case len(ua) == 0:
dest.OS.Platform = PlatformUnknown
dest.OS.Name = OSUnknown
dest.Browser.Name = BrowserUnknown
dest.DeviceType = DeviceUnknown
// stop on on first case returning true
case dest.evalOS(ua):
case dest.evalBrowserName(ua):
default:
dest.evalBrowserVersion(ua)
dest.evalDevice(ua)
}
}
// normalise normalises the user supplied agent string so that
// we can more easily parse it.
func normalise(ua string) string {
if len(ua) <= 1024 {
var buf [1024]byte
ascii := copyLower(buf[:len(ua)], ua)
if !ascii {
// Fall back for non ascii characters
return strings.ToLower(ua)
}
return string(buf[:len(ua)])
}
// Fallback for unusually long strings
return strings.ToLower(ua)
}
// copyLower copies a lowercase version of s to b. It assumes s contains only single byte characters
// and will panic if b is nil or is not long enough to contain all the bytes from s.
// It returns early with false if any characters were non ascii.
func copyLower(b []byte, s string) bool {
for j := 0; j < len(s); j++ {
c := s[j]
if c > 127 {
return false
}
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
}
b[j] = c
}
return true
}

1074
vendor/github.com/avct/uasurfer/uasurfer_test.go generated vendored Normal file

File diff suppressed because it is too large Load Diff