useragent

package
v0.0.0-...-8ec3fd9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	Windows      = "Windows"
	WindowsPhone = "Windows Phone"
	Android      = "Android"
	MacOS        = "macOS"
	IOS          = "iOS"
	Linux        = "Linux"
	FreeBSD      = "FreeBSD"
	ChromeOS     = "ChromeOS"
	BlackBerry   = "BlackBerry"

	Opera            = "Opera"
	OperaMini        = "Opera Mini"
	OperaTouch       = "Opera Touch"
	Chrome           = "Chrome"
	HeadlessChrome   = "Headless Chrome"
	Firefox          = "Firefox"
	InternetExplorer = "Internet Explorer"
	Safari           = "Safari"
	Edge             = "Edge"
	Vivaldi          = "Vivaldi"
	Konqueror        = "Konqueror"
	Epiphany         = "Epiphany"

	GoogleAdsBot        = "Google Ads Bot"
	Googlebot           = "Googlebot"
	Twitterbot          = "Twitterbot"
	FacebookExternalHit = "facebookexternalhit"
	Applebot            = "Applebot"
	Bingbot             = "Bingbot"

	FacebookApp  = "Facebook App"
	InstagramApp = "Instagram App"
	TiktokApp    = "TikTok App"
)

Constants for browsers and operating systems for easier comparison

Variables

This section is empty.

Functions

This section is empty.

Types

type UserAgent

type UserAgent struct {
	VersionNo   VersionNo
	OSVersionNo VersionNo
	URL         string
	String      string
	Name        string
	Version     string
	OS          string
	OSVersion   string
	Device      string
	Mobile      bool
	Tablet      bool
	Desktop     bool
	Bot         bool
}

UserAgent struct containing all data extracted from parsed user-agent string

func Parse

func Parse(userAgent string) UserAgent

Parse user agent string returning UserAgent struct

Example
package main

import (
	"fmt"
	"strings"

	ua "github.com/pingooio/stdx/useragent"
)

func main() {
	userAgents := []string{
		// Mac
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 OPR/46.0.2597.57",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.91 Safari/537.36 Vivaldi/1.92.917.39",

		// Windows
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
		"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; GWX:RED)",
		"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) NS8/0.9.6",

		// iPhone
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) CriOS/60.0.3112.89 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) OPiOS/14.0.0.104835 Mobile/13E233 Safari/9537.53",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4",

		// iPad
		"Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/58.0.3029.113 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4",

		// Andorid
		"Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36",
		"Mozilla/5.0 (Android 4.3; Mobile; rv:54.0) Gecko/54.0 Firefox/54.0",
		"Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36 OPR/42.9.2246.119956",
		"Opera/9.80 (Android; Opera Mini/28.0.2254/66.318; U; en) Presto/2.12.423 Version/12.16",
		"Mozilla/5.0 (Linux; U; Android 4.3; en-us; GT-I9300 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",

		"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",
	}

	for _, s := range userAgents {
		ua := ua.Parse(s)
		fmt.Println()
		fmt.Println(ua.String)
		fmt.Println(strings.Repeat("=", len(ua.String)))
		fmt.Println("Name:", ua.Name, "v", ua.Version)
		fmt.Println("OS:", ua.OS, "v", ua.OSVersion)
		fmt.Println("Device:", ua.Device)
		if ua.Mobile {
			fmt.Println("(Mobile)")
		}
		if ua.Tablet {
			fmt.Println("(Tablet)")
		}
		if ua.Desktop {
			fmt.Println("(Desktop)")
		}
		if ua.Bot {
			fmt.Println("(Bot)")
		}
		if ua.URL != "" {
			fmt.Println(ua.URL)
		}
	}

}
Output:

func (UserAgent) IsAndroid

func (ua UserAgent) IsAndroid() bool

IsAndroid shorthand function to check if OS == Android

func (UserAgent) IsChrome

func (ua UserAgent) IsChrome() bool

IsChrome shorthand function to check if Name == Chrome

func (UserAgent) IsChromeOS

func (ua UserAgent) IsChromeOS() bool

IsChromeOS shorthand function to check if OS == CrOS

func (UserAgent) IsEdge

func (ua UserAgent) IsEdge() bool

IsEdge shorthand function to check if Name == Edge

func (UserAgent) IsFacebookbot

func (ua UserAgent) IsFacebookbot() bool

IsFacebookbot shorthand function to check if Name == FacebookExternalHit

func (UserAgent) IsFirefox

func (ua UserAgent) IsFirefox() bool

IsFirefox shorthand function to check if Name == Firefox

func (UserAgent) IsGooglebot

func (ua UserAgent) IsGooglebot() bool

IsGooglebot shorthand function to check if Name == Googlebot

func (UserAgent) IsIOS

func (ua UserAgent) IsIOS() bool

IsIOS shorthand function to check if OS == IOS

func (UserAgent) IsInternetExplorer

func (ua UserAgent) IsInternetExplorer() bool

IsInternetExplorer shorthand function to check if Name == Internet Explorer

func (UserAgent) IsLinux

func (ua UserAgent) IsLinux() bool

IsLinux shorthand function to check if OS == Linux

func (UserAgent) IsMacOS

func (ua UserAgent) IsMacOS() bool

IsMacOS shorthand function to check if OS == MacOS

func (UserAgent) IsOpera

func (ua UserAgent) IsOpera() bool

IsOpera shorthand function to check if Name == Opera

func (UserAgent) IsOperaMini

func (ua UserAgent) IsOperaMini() bool

IsOperaMini shorthand function to check if Name == Opera Mini

func (UserAgent) IsSafari

func (ua UserAgent) IsSafari() bool

IsSafari shorthand function to check if Name == Safari

func (UserAgent) IsTwitterbot

func (ua UserAgent) IsTwitterbot() bool

IsTwitterbot shorthand function to check if Name == Twitterbot

func (UserAgent) IsUnknown

func (ua UserAgent) IsUnknown() bool

IsUnknown returns true if the package can't determine the user agent reliably. Fields like Name, OS, etc. might still have values.

func (UserAgent) IsWindows

func (ua UserAgent) IsWindows() bool

IsWindows shorthand function to check if OS == Windows

func (UserAgent) OSVersionNoFull

func (ua UserAgent) OSVersionNoFull() string

OSVersionNoFull returns OS version string in format <Major>.<Minor>.<Patch>

func (UserAgent) OSVersionNoShort

func (ua UserAgent) OSVersionNoShort() string

OSVersionNoShort returns OS version string in format <Major>.<Minor>

func (UserAgent) VersionNoFull

func (ua UserAgent) VersionNoFull() string

VersionNoFull returns version string in format <Major>.<Minor>.<Patch>

func (UserAgent) VersionNoShort

func (ua UserAgent) VersionNoShort() string

VersionNoShort return version string in format <Major>.<Minor>

type VersionNo

type VersionNo struct {
	Major int
	Minor int
	Patch int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL