Skip to main content

Overview

The bot middleware detects bot traffic based on user-agent patterns, behavior analysis, and known bot signatures. Use it when you need:
  • Bot traffic identification
  • Search engine crawler handling
  • Automated traffic filtering

Installation

Quick Start

Configuration

Options

Examples

Detect Bots

Block Bad Bots

Custom Patterns

Bot-Specific Routes

API Reference

Functions

BotInfo

Known Good Bots

  • Googlebot
  • Bingbot
  • Slurp (Yahoo)
  • DuckDuckBot
  • Baiduspider

Technical Details

User-Agent Pattern Matching

The middleware uses compiled regular expressions for efficient pattern matching:
  • Default Patterns: Includes search engines (Googlebot, Bingbot), social bots (Facebook, Twitter), SEO tools (Semrush, Ahrefs), and HTTP clients (curl, wget)
  • Pattern Compilation: All patterns are compiled into a single regex at initialization for O(n) matching
  • Case Insensitive: Matching is case-insensitive to handle various User-Agent formats

Bot Categories

Detected bots are categorized:
  • search: Googlebot, Bingbot, Yandexbot, Baiduspider, DuckDuckBot
  • social: Facebook, Twitter, LinkedIn, Pinterest
  • seo: Semrush, Ahrefs, Majestic, MJ12bot
  • tool: curl, wget, Python requests, Go HTTP client
  • crawler: Generic crawlers, spiders, scrapers

Context Storage

Bot information is stored in the request context using context.WithValue() for thread-safe access throughout the request lifecycle.

Allowlist/Blocklist Logic

When BlockBots is enabled:
  1. If AllowedBots is set, only listed bots are allowed (allowlist mode)
  2. If BlockedBots is set, only listed bots are blocked (blocklist mode)
  3. If neither is set, all detected bots are blocked

Best Practices

  • Allow search engine bots for SEO
  • Serve simplified content to bots
  • Log bot traffic for analysis
  • Rate limit suspicious bots

Testing

The middleware includes comprehensive test coverage. Key test cases: