uasurfer Documentation

repository·master·Indexed 18 days ago

https://github.com/lumenresearch/uasurfer

A lightweight Go package for parsing and abstracting HTTP User-Agent strings. It identifies browser names, operating system versions, and device types (mobile, tablet, desktop, TV, console, wearable). The library includes specialized logic for bot and crawler detection, high-performance parsing for request loops via ParseUserAgent, and a CLI tool called uastats for generating User-Agent statistics.

Tokens
6.5K
Snippets
17
Records
39
Agent score
62%

What's inside uasurfer

  1. Identify Device Types and Android caveats

    master

    The DeviceType field identifies the hardware category (e.g., DeviceComputer, DeviceTablet, DeviceTV).

    Android Phone vs. Tablet Caveat: It is not always possible to distinguish between an Android phone and a tablet because some vendors ship tablets that announce Mobile in their User-Agent. In these cases, an Android agent with no specific tablet indicator defaults to being classified as a phone.

  2. Distinguish between Fire TV and Fire Tablets

    master
    Amazon Fire TV models are recognized as a family; new models released after the current library version will still report DeviceTV. However, Fire tablets are categorized separately and will report DeviceTablet.
  3. Compare Browser and OS versions

    master

    Versions in uasurfer are represented by a Version struct with Major, Minor, and Patch fields.

    To check if a browser meets a minimum requirement, compare the Major field directly or use the Less method for more complex comparisons.

    Example: Checking Chrome version

    if ua.Browser.Version.Major > 23 {
        // Handle modern Chrome
    }

    Example: Using Less()

    if ver1.Less(ver2) {
        // ver1 is older than ver2
    }
  4. Identify Connected TV devices using DeviceTV

    master
    When parsing User Agent strings for televisions, sticks, and set-top boxes, the library reports DeviceTV. Because many TV platforms are based on Linux but do not explicitly state a version, you should use DeviceTV to identify the device type and then rely on the reported browser and its version to determine rendering capabilities.
  5. How bot detection works in uasurfer

    master

    Instead of relying solely on a static list of names, uasurfer uses convention-based detection to identify bots. It catches:

    • Keyword patterns: Anything containing …bot, …spider, or …crawler.
    • Contact URLs: Agents that publish a contact URL starting with +http….
    • HTTP Client Libraries: Common libraries like curl, python-requests, Go-http-client, and Scrapy.
    • Automation Tools: Headless browsers, automation browsers, and link preview fetchers.

    Named Browser Bots

    High-volume crawlers are identified by specific constants. If a crawler is not one of these specific named constants, it is reported as BrowserBot.

    Specific constants include:

    • BrowserGoogleBot (covers AdsBot, Mediapartners, GoogleOther, Google-InspectionTool, and Googlebot)
    • BrowserBingBot
    • BrowserOpenAIBot
    • BrowserAnthropicBot
    • BrowserPerplexityBot
    • BrowserAmazonBot
    • BrowserBytedanceBot
    • BrowserAhrefsBot
    • BrowserSemrushBot

    Limitations and False Negatives

    uasurfer does not attempt to maintain an exhaustive list of every crawler in existence. Consequently, it may not catch crawlers that:

    • Use a complete browser agent string.
    • Use unguessable names.
    • Do not provide a contact URL or generic tokens (e.g., Datanyze, Rigor, Scope3/2.0, binlar).

    Detection accuracy is approximately 83% based on measured crawler agents, with zero false positives reported against real device agents.

  6. How to add new User Agents to uasurfer

    master

    To extend the library with support for new devices, systems, or browsers, follow these steps:

    1. Identify the target: Find source User-Agent strings that identify the device/system/browser you want to add.
    2. Find a unique identifier: Identify a unique substring within those User-Agent strings.
    3. Modify logic: Add a condition to the appropriate switch statement in browser.go, device.go, or system.go (e.g., using strings.Contains(ua, "identifier")).
    4. Update tests: Add new rows to the fixture sets in testdata/ that currently fail without your changes.
    5. Verify: Run gofmt, go vet, golangci-lint, and go test. For parsing logic, run benchmarks using go test -run=XXX -bench=Parse -benchmem -count=6 to ensure no performance regressions.
  7. Optimize User-Agent parsing in request loops

    master

    If you are parsing User-Agents within a high-frequency request loop, use ParseUserAgent to avoid repeated allocations. This function fills a UserAgent struct that you own.

    Important: You must call .Reset() on your UserAgent instance before reusing it for the next string to ensure data from the previous parse does not persist.

    // Assuming 'ua' is a pre-allocated *uasurfer.UserAgent
    ua.Reset()
    uasurfer.ParseUserAgent(rawUAString, ua)
  8. Identify Amazon Fire devices

    master

    The library includes specialized logic to detect Amazon Fire tablets and phones. It looks for specific model tokens within the User Agent string, specifically:

    • Tokens starting with 'k' followed by 3 to 5 lowercase letters (e.g., kabcde).
    • Tokens matching the pattern sd<4 digits>ur (e.g., sd1234ur).

    When detected, these are typically categorized under the Linux platform with the OSKindle name.

  9. Classify device types from User Agent strings

    master

    The uasurfer package classifies a User Agent string into a specific DeviceType based on OS platform, browser, and specific hardware markers. The classification logic follows a priority hierarchy to ensure accuracy (e.g., checking for TV markers before defaulting to mobile/phone).

    Supported DeviceType categories include:

    • DeviceComputer: Windows, Mac, or Linux (if not otherwise classified).
    • DeviceTV: Identified via specific markers like roku, chromecast, stb, or Amazon Fire TV (aft).
    • DeviceTablet: Identified via iPad, kindle/, playbook, or specific Android tablet identifiers (e.g., nexus 7, sm-t).
    • DevicePhone: Identified via iPhone, Blackberry, mobile, or mobi strings.
    • DeviceConsole: Identified via Playstation, Xbox, or Nintendo platforms.
    • DeviceWearable: Identified via glass, watch, or sm-v markers.
    • DeviceUnknown: The fallback when no patterns match.

    Note: The classification logic is internal to the Parse process and updates the DeviceType field on the UserAgent struct.

  10. How platform group parsing works

    master

    The platformGroup function extracts the substring located between the first opening parenthesis ( and the first closing parenthesis ).

    If the closing parenthesis is missing or appears before the opening one, the function returns everything from the opening parenthesis to the end of the User Agent string. This extracted group is often used as the primary source for identifying the OS and version before falling back to a full-string search.

  11. Identify Amazon Fire TV devices

    master
    The parser identifies the Amazon Fire TV family by looking for the aft prefix within the platform group of the User Agent. To avoid false positives (like the word "after"), the parser ensures aft is at a field edge (preceded by a space, ;, or ,) and is followed by alphanumeric characters representing the specific model (e.g., aftb, aftmm).