detector

package
v0.0.0-...-f3202c1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: LGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package detector parses User-Agent strings and browser Client Hints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BotInfo

type BotInfo struct {
	Name     string `json:"name"`
	Category string `json:"category"`
}

type ClientHints

type ClientHints struct {
	Brands   string
	Platform string
	Mobile   *bool
	Model    string
}

ClientHints contains the high-entropy browser hints useful for UA-reduced Chromium requests. Values are the decoded header values, without quotes.

func ClientHintsFromHeader

func ClientHintsFromHeader(h http.Header) ClientHints

ClientHintsFromHeader extracts the request hints needed by Parse.

type ClientInfo

type ClientInfo struct {
	Type    string `json:"type"`
	Name    string `json:"name"`
	Version string `json:"version,omitempty"`
	Engine  string `json:"engine,omitempty"`
}

type Detector

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

Detector is safe for concurrent use after construction.

func New

func New() *Detector

New creates a parser with common browser, OS, device and bot rules. It is a deliberately small baseline; production parity is obtained by loading a maintained Matomo-compatible rule set on top of this API.

func (*Detector) LoadEmbeddedMatomoRules

func (d *Detector) LoadEmbeddedMatomoRules() (RuleReport, error)

LoadEmbeddedMatomoRules loads the rule corpus bundled in this Go module. Call it once during application startup; no runtime rule files are needed.

func (*Detector) LoadMatomoRules

func (d *Detector) LoadMatomoRules(regexesDir string) (RuleReport, error)

LoadMatomoRules loads a checkout's regexes directory into d. The expected layout is the upstream `regexes` directory (not the repository root). Existing built-in fallback rules remain available for any unmatched UA.

func (*Detector) Middleware

func (d *Detector) Middleware(next http.Handler) http.Handler

Middleware parses the request once and adds its Result to the request context.

func (*Detector) Parse

func (d *Detector) Parse(userAgent string, hints ClientHints) Result

Parse analyzes one user agent and optionally overlays Client Hints.

type DeviceInfo

type DeviceInfo struct {
	Type  string `json:"type"`
	Brand string `json:"brand,omitempty"`
	Model string `json:"model,omitempty"`
}

type OSInfo

type OSInfo struct {
	Name    string `json:"name"`
	Version string `json:"version,omitempty"`
}

type Result

type Result struct {
	Bot    *BotInfo   `json:"bot,omitempty"`
	Client ClientInfo `json:"client"`
	OS     OSInfo     `json:"os"`
	Device DeviceInfo `json:"device"`
}

Result is the normalized result of parsing a request's user agent.

func FromContext

func FromContext(ctx context.Context) (Result, bool)

FromContext retrieves a result inserted by Middleware.

type RuleReport

type RuleReport struct {
	Loaded       int
	Skipped      int
	PCREFallback []UnsupportedRule
}

RuleReport describes a Matomo rule import.

type UnsupportedRule

type UnsupportedRule struct {
	File    string
	Pattern string
	Reason  string
}

UnsupportedRule identifies an upstream expression that Go's RE2 engine cannot compile. The pattern is retained verbatim for migration work.

Jump to

Keyboard shortcuts

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