botdetector

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: MIT Imports: 3 Imported by: 0

README

BotDetector

Build Status Go Report Card codecov

BotDetector is a Go library that detects bots, spiders, and crawlers from user agents.

Installation

go get -u github.com/logocomune/botdetector/v2

Usage

Simple usage
   userAgent := req.Header.Get("User-Agent")

detector, _ := botdetector.New()
isBot := detector.IsBot(userAgnet)

if isBot {
log.Println("Bot, Spider or Crawler detected")
}

Adding Custom Rules

You can add custom detection rules with the WithRules method. For example:

userAgent := req.Header.Get("User-Agent")

detector, _ := botdetector.New(WithRules([]string{"my rule", "^test"}))
isBot := detector.IsBot(userAgent)

if isBot {
log.Println("Bot, Spider or Crawler detected")
}

Custom Rule Patterns:

pattern description
"..." Checks if the string contains the specified pattern.
"^..." Checks if the string starts with the specified pattern.
"...$" Checks if the string ends with the specified pattern.
"^...$" Checks if the string strictly matches the entire pattern.

In this example, the custom rules "my rule" and "^test" are added to the existing detection rules.

Adding Cache

You can add a lru cache rules with the WithCache method. For example:

userAgent := req.Header.Get("User-Agent")

detector, _ := botdetector.New(WithCache(1000))
isBot := detector.IsBot(userAgent)

if isBot {
log.Println("Bot, Spider or Crawler detected")
}

Example

Simple example

Inspiration

BotSeeker is inspired by CrawlerDetect, an awesome PHP project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BotDetector

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

func New

func New(opt ...Option) (*BotDetector, error)

New creates a new instance of BotDetector using predefined rules.

func NewWithRules

func NewWithRules(rules []string) *BotDetector

NewWithRules initializes a new instance of BotDetector with provided rules.

func (*BotDetector) IsBot

func (b *BotDetector) IsBot(ua string) bool

IsBot tests whether the useragent is a bot, crawler or a spider.

type Option

type Option func(*BotDetector) (*BotDetector, error)

func WithCache

func WithCache(size int) Option

func WithRules

func WithRules(r []string) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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