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

import "github.com/go-mizu/mizu/middlewares/bot"

Quick Start

app := mizu.New()

app.Use(bot.New())

app.Get("/", func(c *mizu.Ctx) error {
    if bot.IsBot(c) {
        return c.Text(200, "Hello, bot!")
    }
    return c.HTML(200, fullPage)
})

Configuration

Options

OptionTypeDefaultDescription
BlockboolfalseBlock detected bots
AllowGoodbooltrueAllow known good bots
Patterns[]stringBuilt-inAdditional patterns

Examples

Detect Bots

app.Use(bot.New())

app.Get("/", func(c *mizu.Ctx) error {
    if bot.IsBot(c) {
        // Serve simplified content
        return c.Text(200, "Content")
    }
    return c.HTML(200, richContent)
})

Block Bad Bots

app.Use(bot.New(bot.Options{
    Block:     true,
    AllowGood: true, // Allow Google, Bing, etc.
}))

Custom Patterns

app.Use(bot.New(bot.Options{
    Patterns: []string{
        "MyCustomBot",
        "InternalCrawler",
    },
}))

Bot-Specific Routes

app.Get("/sitemap.xml", func(c *mizu.Ctx) error {
    return c.XML(200, sitemap)
})

app.Get("/", func(c *mizu.Ctx) error {
    info := bot.Info(c)
    if info.IsSearchEngine {
        // SEO-optimized content
    }
    return c.HTML(200, content)
})

API Reference

Functions

// New creates bot detection middleware
func New(opts ...Options) mizu.Middleware

// IsBot returns true if request is from bot
func IsBot(c *mizu.Ctx) bool

// Info returns detailed bot information
func Info(c *mizu.Ctx) *BotInfo

BotInfo

type BotInfo struct {
    IsBot          bool
    IsGoodBot      bool
    IsSearchEngine bool
    Name           string
    Category       string
}

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:
Test CaseDescriptionExpected Behavior
TestNew/detect_googlebotDetects Googlebot user agentIsBot=true, BotName=“googlebot”, Category=“search”
TestNew/detect_curlDetects curl HTTP clientIsBot=true, Category=“tool”
TestNew/normal_browserNormal browser user agentIsBot=false
TestWithOptions_BlockBots/block_botBlock detected botsReturns 403 Forbidden
TestWithOptions_BlockBots/allow_browserAllow normal browsersReturns 200 OK
TestWithOptions_AllowedBots/allowed_botAllowlist mode with GooglebotReturns 200 OK for allowed bot
TestWithOptions_AllowedBots/non-allowed_botAllowlist mode with curlReturns 403 Forbidden
TestWithOptions_BlockedBots/blocked_botBlocklist mode with SemrushReturns 403 Forbidden
TestWithOptions_CustomPatternsCustom bot pattern detectionDetects custom bot pattern
TestWithOptions_ErrorHandlerCustom error handlerReturns JSON response
TestIsBotHelper function testReturns true for bots
TestBotNameBot name extractionReturns detected bot name
TestBlockBlock all bots helperReturns 403 for any bot
TestAllowAllow specific bots helperAllows listed bots only
TestDenyDeny specific bots helperBlocks listed bots