user_agent

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: MIT Imports: 3 Imported by: 1

README

Build Status for the default branch go.dev page MIT


UserAgent is a Go library that parses HTTP User Agents. As an example:

package main

import (
    "fmt"

    "github.com/23233/user_agent"
)

func main() {
    // The "New" function will create a new UserAgent object and it will parse
    // the given string. If you need to parse more strings, you can re-use
    // this object and call: ua.Parse("another string")
    ua := user_agent.New("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")

    fmt.Printf("%v\n", ua.Mobile())   // => true
    fmt.Printf("%v\n", ua.Bot())      // => false
    fmt.Printf("%v\n", ua.Mozilla())  // => "5.0"
    fmt.Printf("%v\n", ua.Model())    // => "Nexus One"

    fmt.Printf("%v\n", ua.Platform()) // => "Linux"
    fmt.Printf("%v\n", ua.OS())       // => "Android 2.3.7"

    name, version := ua.Engine()
    fmt.Printf("%v\n", name)          // => "AppleWebKit"
    fmt.Printf("%v\n", version)       // => "533.1"

    name, version = ua.Browser()
    fmt.Printf("%v\n", name)          // => "Android"
    fmt.Printf("%v\n", version)       // => "4.0"

    // Let's see an example with a bot.

    ua.Parse("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")

    fmt.Printf("%v\n", ua.Bot())      // => true

    name, version = ua.Browser()
    fmt.Printf("%v\n", name)          // => Googlebot
    fmt.Printf("%v\n", version)       // => 2.1
}

If you want to read the full API documentation simply check godoc.

Installation

go get -u github.com/23233/user_agent

Contributing

Do you want to contribute with code, or to report an issue you are facing? Read the CONTRIBUTING.md file.

Changelog

Read the CHANGELOG.md file.

License

Copyright (c) 2012-2021 Miquel Sabaté Solà

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package user_agent implements an HTTP User Agent string parser. It defines the type UserAgent that contains all the information from the parsed string. It also implements the Parse function and getters for all the relevant information that has been extracted from a parsed User Agent string.

Index

Constants

View Source
const (
	QqBrowserId = "MQQBrowser"
	QqUniqueId  = "QQ"
	QqMiniId    = "miniProgram"
)
View Source
const (
	WechatUniqueId     = "MicroMessenger"
	WechatBrowserId    = "micromessenger"
	WechatWorkUniqueId = "wxwork"
	WechatNetTypeId    = "NetType"
	WechatMiniId       = "miniProgram"
)

Variables

View Source
var (
	CommentSplit = []string{"; ", "/"}
)

Functions

func GetEnvKey

func GetEnvKey(ua, referrer string) string

Types

type Browser

type Browser struct {
	// The name of the browser's engine.
	Engine string

	// The version of the browser's engine.
	EngineVersion string

	// The name of the browser.
	Name string

	// The version of the browser.
	Version string
}

Browser is a struct containing all the information that we might be interested from the browser.

type Env

type Env struct {
	// contains filtered or unexported fields
}

func NewEnv

func NewEnv(ua, referrer string) *Env

func (*Env) GetKey

func (c *Env) GetKey() string

type OSInfo

type OSInfo struct {
	// Full name of the operating system. This is identical to the output of ua.OS()
	FullName string

	// Name of the operating system. This is sometimes a shorter version of the
	// operating system name, e.g. "Mac OS X" instead of "Intel Mac OS X"
	Name string

	// Operating system version, e.g. 7 for Windows 7 or 10.8 for Max OS X Mountain Lion
	Version string
}

OSInfo represents full information on the operating system extracted from the user agent.

type Qq

type Qq struct {
	// contains filtered or unexported fields
}

type Referrer

type Referrer struct {
	// contains filtered or unexported fields
}

func NewReferrer

func NewReferrer(ref string) *Referrer

func (*Referrer) GetAliMiniInfo

func (r *Referrer) GetAliMiniInfo() *ReferrerMiniInfo

func (*Referrer) GetBaiduMiniInfo

func (r *Referrer) GetBaiduMiniInfo() *ReferrerMiniInfo

func (*Referrer) GetBytedanceMiniInfo

func (r *Referrer) GetBytedanceMiniInfo() *ReferrerMiniInfo

func (*Referrer) GetQqMiniInfo

func (r *Referrer) GetQqMiniInfo() *ReferrerMiniInfo

func (*Referrer) GetWechatMiniInfo

func (r *Referrer) GetWechatMiniInfo() *ReferrerMiniInfo

func (*Referrer) IsAliMini

func (r *Referrer) IsAliMini() bool

IsAliMini 是否为阿里小程序 https://opendocs.alipay.com/mini/api/owycmh https://{appid}.hybrid.alipay-eco.com/{appid}/{version}/index.html#{page}

func (*Referrer) IsBaiduMini

func (r *Referrer) IsBaiduMini() bool

IsBaiduMini 是否为百度小程序 https://smartprogram.baidu.com/docs/develop/api/net/net_rule/ https://{域名}/{appKey}/{version}/page-frame.html https://smartapp.baidu.com/{appKey}/{version}/page-frame.html https://smartapps.cn/{appKey}/{version}/page-frame.html 自基础库版本 V3.170.0 起,其中域名由原来的 https://smartapp.baidu.com 更改为 https://smartapps.cn

func (*Referrer) IsMini

func (r *Referrer) IsMini() bool

func (*Referrer) MiniKey

func (r *Referrer) MiniKey() (string, string)

type ReferrerMiniInfo

type ReferrerMiniInfo struct {
	Appid   string `json:"appid,omitempty"`
	Version string `json:"version,omitempty"`
}

type UserAgent

type UserAgent struct {
	// contains filtered or unexported fields
}

The UserAgent struct contains all the info that can be extracted from the User-Agent string.

func New

func New(ua string) *UserAgent

New parses the given User-Agent string and get the resulting UserAgent object.

Returns an UserAgent object that has been initialized after parsing the given User-Agent string.

func (*UserAgent) Bot

func (p *UserAgent) Bot() bool

Bot returns true if it's a bot, false otherwise.

func (*UserAgent) Browser

func (p *UserAgent) Browser() (string, string)

Browser returns two strings. The first string is the name of the browser and the second one is the version of the browser.

func (*UserAgent) Engine

func (p *UserAgent) Engine() (string, string)

Engine returns two strings. The first string is the name of the engine and the second one is the version of the engine.

func (*UserAgent) InQq

func (p *UserAgent) InQq() bool

func (*UserAgent) InQqBrowser

func (p *UserAgent) InQqBrowser() bool

func (*UserAgent) InQqBrowserAndroid

func (p *UserAgent) InQqBrowserAndroid() bool

func (*UserAgent) InQqBrowserIos

func (p *UserAgent) InQqBrowserIos() bool

func (*UserAgent) InQqMini

func (p *UserAgent) InQqMini() bool

func (*UserAgent) InWechat

func (p *UserAgent) InWechat() bool

func (*UserAgent) InWechatAndroid

func (p *UserAgent) InWechatAndroid() bool

func (*UserAgent) InWechatBrowser

func (p *UserAgent) InWechatBrowser() bool

func (*UserAgent) InWechatIphone

func (p *UserAgent) InWechatIphone() bool

func (*UserAgent) InWechatMac

func (p *UserAgent) InWechatMac() bool

func (*UserAgent) InWechatMini

func (p *UserAgent) InWechatMini() bool

func (*UserAgent) InWechatWindow

func (p *UserAgent) InWechatWindow() bool

func (*UserAgent) InWechatWork

func (p *UserAgent) InWechatWork() bool

func (*UserAgent) Localization

func (p *UserAgent) Localization() string

Localization returns a string containing the localization.

func (*UserAgent) Mobile

func (p *UserAgent) Mobile() bool

Mobile returns true if it's a mobile device, false otherwise.

func (*UserAgent) Model

func (p *UserAgent) Model() string

Model returns a string containing the Phone Model like "Nexus 5X"

func (*UserAgent) Mozilla

func (p *UserAgent) Mozilla() string

Mozilla returns the mozilla version (it's how the User Agent string begins: "Mozilla/5.0 ...", unless we're dealing with Opera, of course).

func (*UserAgent) OS

func (p *UserAgent) OS() string

OS returns a string containing the name of the Operating System.

func (*UserAgent) OSInfo

func (p *UserAgent) OSInfo() OSInfo

OSInfo returns combined information for the operating system.

func (*UserAgent) Parse

func (p *UserAgent) Parse(ua string)

Parse the given User-Agent string. After calling this function, the receiver will be setted up with all the information that we've extracted.

func (*UserAgent) Platform

func (p *UserAgent) Platform() string

Platform returns a string containing the platform..

func (*UserAgent) QqKey

func (p *UserAgent) QqKey() string

func (*UserAgent) UA

func (p *UserAgent) UA() string

UA returns the original given user agent.

func (*UserAgent) WechatKey

func (p *UserAgent) WechatKey() string

type Wechat

type Wechat struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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