goclienthints

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 5 Imported by: 1

README

Go Reference Go

Go Client Hints

Install

go get -u github.com/cateiru/go-client-hints/v2

Parse Client Hints

import (
	"fmt"
	"net/http"

	goclienthints "github.com/cateiru/go-client-hints/v2"
)

func Handler(w http.ResponseWriter, r *http.Request) {
	clientHints, err := goclienthints.Parse(&r.Header)
	if err != nil {
		return
	}

	// Sec-CH-UA field
	fmt.Println("Brand: ", clientHints.Brand.Brand)
	fmt.Println("Brand Version: ", clientHints.BrandVersion)
	fmt.Println("Brands: ", clientHints.Brands)

	// Sec-Ch-Ua-Platform filed
	fmt.Println("Platform: ", clientHints.Platform)

	// Sec-CH-UA-Platform-Version filed
	fmt.Println("Platform Version: ", clientHints.PlatformVersion)

	// Sec-Ch-Ua-Mobile filed
	fmt.Println("IsMobile: ", clientHints.IsMobile)

	// Sec-CH-UA-Arch filed
	fmt.Println("Arch: ", clientHints.Architecture)

	// Sec-CH-UA-Bitness filed
	fmt.Println("Bitness: ", clientHints.Bitness)

	// Sec-CH-UA-Model filed
	fmt.Println("Model: ", clientHints.Model)

	// Sec-Ch-Ua-Full-Version filed
	fmt.Println("Full Version: ", clientHints.FullVersion)
}

Check Support Client Hints

import (
	"net/http"

	clienthint "github.com/cateiru/go-client-hints/v2"
)

func Handler2(w http.ResponseWriter, r *http.Request) {
	isSupport := clienthint.IsSupportClientHints(&r.Header)

	if isSupport {
		// ...do something
	}
}

LICENSE

MIT

Documentation

Index

Constants

View Source
const (
	HeaderSecChUa                = "Sec-Ch-Ua"
	HeaderSecChUaArch            = "Sec-Ch-Ua-Arch"
	HeaderSecChUaBitness         = "Sec-Ch-Ua-Bitness"
	HeaderSecChUaFullVersion     = "Sec-Ch-Ua-Full-Version"
	HeaderSecChUaFullVersionList = "Sec-Ch-Ua-Full-Version-List"
	HeaderSecChUaMobile          = "Sec-Ch-Ua-Mobile"
	HeaderSecChUaModel           = "Sec-Ch-Ua-Model"
	HeaderSecChUaPlatform        = "Sec-Ch-Ua-Platform"
	HeaderSecChUaPlatformVersion = "Sec-Ch-Ua-Platform-Version"
)

User-Agent-Client-Hints Headers

Variables

View Source
var PrimaryBrands = []string{
	"Google Chrome",
	"Chrome",
	"Microsoft Edge",
	"Edge",
	"Brave Browser",
	"Brave",
	"Yandex Browser",
	"Yandex",
	"CocCoc",
	"Opera",
	"Vivaldi",
	"SRWare Iron",
	"Epic Privacy Browser",
	"Comodo Dragon",
	"Torch Browser",
	"Samsung",
}

Brand array A array of brands that Sec-Ch-Ua prefers to compare.

View Source
var SecondaryBrands = []string{
	"Chromium",
}

This array will be used to try to get the brand name if it is not in the PrimaryBrands array.

Functions

func IsSupportClientHints

func IsSupportClientHints(headers *http.Header) bool

Determines if ClientHint is supported.

It is determined by the presence or absence of the `Sec-Ch-Ua` header.

func ParseBool

func ParseBool(h string) (bool, error)

func ParseItem

func ParseItem(h string) (string, error)

Types

type Brand

type Brand struct {
	Brand        string            `json:"brand,omitempty"`
	BrandVersion string            `json:"brand_version,omitempty"`
	Brands       map[string]string `json:"brands,omitempty"`
}

Sec-CH-UA field

func ParseSecChUa

func ParseSecChUa(h string) (*Brand, error)

Parse the `Sec-Ch-Ua` header Create an array of brand names and their versions, plus determine which one of the brands you are using.

type ClientHints

type ClientHints struct {
	Brand

	// Sec-Ch-Ua-Platform filed
	Platform Platform `json:"platform"`

	// Sec-CH-UA-Platform-Version filed
	PlatformVersion string `json:"platform_version,omitempty"`

	// Sec-Ch-Ua-Mobile filed
	IsMobile bool `json:"is_mobile"`

	// Sec-CH-UA-Arch filed
	Architecture string `json:"architecture,omitempty"`

	// Sec-CH-UA-Bitness filed
	Bitness int `json:"bitness"`

	// Sec-CH-UA-Model filed
	Model string `json:"model,omitempty"`

	// Sec-Ch-Ua-Full-Version filed
	FullVersion string `json:"full_version,omitempty"`
}

func Parse

func Parse(headers *http.Header) (*ClientHints, error)

Parse User-Agent Client Hints.

Parse the header below.

  • Sec-Ch-Ua
  • Sec-Ch-Ua-Arch
  • Sec-Ch-Ua-Bitness
  • Sec-Ch-Ua-Full-Version
  • Sec-Ch-Ua-Full-Version-List
  • Sec-Ch-Ua-Mobile
  • Sec-Ch-Ua-Model
  • Sec-Ch-Ua-Platform
  • Sec-Ch-Ua-Platform-Version

If the header does not exist, its value will be the empty string, the number 0, or false.

type Platform

type Platform string
const (
	Android    Platform = "Android"
	ChromeOS   Platform = "Chrome OS"
	ChromiumOS Platform = "Chromium OS"
	IOS        Platform = "iOS"
	Linux      Platform = "Linux"
	MacOS      Platform = "macOS"
	Windows    Platform = "Windows"
	Unknown    Platform = "Unknown"
)

Platforms You get `Sec-Ch-Ua-Platform` field

func ParsePlatform

func ParsePlatform(h string) (Platform, error)

Prase the `Sec-CH-UA-Platform` header

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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