uautil
IsBot
检测 HTTP 请求是否来自机器人
IsBot
检测 HTTP 请求是否来自机器人。
函数签名
func IsBot(r *http.Request, allowLegitimate bool) bool参数
r(*http.Request): HTTP 请求对象allowLegitimate(bool): 是否允许合法的搜索引擎爬虫true: 允许合法搜索引擎(Google、Bing 等),仅拦截恶意机器人false: 拦截所有机器人,包括搜索引擎
返回值
bool: 如果是机器人返回true,否则返回false
使用示例
拦截所有机器人
package main
import (
"net/http"
"github.com/woodchen-ink/go-web-utils/uautil"
)
func handler(w http.ResponseWriter, r *http.Request) {
// 拦截所有机器人
if uautil.IsBot(r, false) {
http.Error(w, "Bot access denied", http.StatusForbidden)
return
}
w.Write([]byte("Welcome, human!"))
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}SEO 友好模式(允许搜索引擎)
func handler(w http.ResponseWriter, r *http.Request) {
// 允许合法搜索引擎,但拦截恶意爬虫
if uautil.IsBot(r, true) {
http.Error(w, "Malicious bot detected", http.StatusForbidden)
return
}
w.Write([]byte("Welcome!"))
}记录机器人访问
func handler(w http.ResponseWriter, r *http.Request) {
isBot := uautil.IsBot(r, true)
if isBot {
// 记录恶意机器人访问
log.Printf("Blocked bot: %s from %s",
r.UserAgent(),
r.RemoteAddr)
http.Error(w, "Access denied", http.StatusForbidden)
return
}
// 正常处理
w.Write([]byte("Success"))
}检测逻辑
- 空 User-Agent: 如果请求没有 User-Agent,视为机器人
- 合法爬虫检查 (当
allowLegitimate=true时):- 先检查是否匹配合法搜索引擎爬虫特征
- 如果是合法爬虫,返回
false(不拦截)
- 机器人特征匹配:
- 检查 User-Agent 是否包含常见机器人特征
- 匹配任一特征则返回
true
内置特征列表
恶意机器人特征
python-requests,python-urllibcurl,wgetjava/,okhttp,go-http-clientscrapy,selenium,phantomjsnmap,masscan,nikto,sqlmapbot,crawler,spider,scraper
合法搜索引擎特征
googlebot(Google)bingbot(Bing)baiduspider(百度)slurp(Yahoo)duckduckbot(DuckDuckGo)facebookexternalhit,twitterbot,linkedinbot
性能说明
- 时间复杂度: O(n),n 为特征列表长度
- 空间复杂度: O(1)
- 适用于高并发场景
另请参阅
- IsBotUserAgent - 直接检测 User-Agent 字符串
- BlockBotMiddleware - 中间件方式拦截机器人
- AddCustomBotPattern - 添加自定义机器人特征