feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
Always-on Go service (stdlib only) that follows the Traefik JSON access log, the sshd log and the fail2ban log of a host, classifies attacks, scores sources, keeps bounded aggregates with snapshots, and serves an embedded dashboard with live updates, offline geolocation (DB-IP Lite), privacy-friendly traffic analytics, an exportable blocklist and token-authenticated federation between instances. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
b4a2c2c9ec
99 files changed
+15403
No files matched your search
@@ -0,0 +1,504 @@
|
||||
// Package ua interpreta User-Agents sem dependências: navegador, SO, tipo de
|
||||
// dispositivo, bots conhecidos (buscadores/IA) e ferramentas de varredura.
|
||||
package ua
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Info é o resultado de Parse.
|
||||
type Info struct {
|
||||
Browser string `json:"browser,omitempty"`
|
||||
BrowserMajor string `json:"browser_major,omitempty"`
|
||||
OS string `json:"os,omitempty"`
|
||||
Device string `json:"device"` // desktop | mobile | tablet | bot | tool | unknown
|
||||
Bot bool `json:"bot"`
|
||||
BotName string `json:"bot_name,omitempty"`
|
||||
Tool string `json:"tool,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
Desktop = "desktop"
|
||||
Mobile = "mobile"
|
||||
Tablet = "tablet"
|
||||
BotDev = "bot"
|
||||
ToolDev = "tool"
|
||||
Unknown = "unknown"
|
||||
)
|
||||
|
||||
const cacheMax = 4096
|
||||
|
||||
var (
|
||||
cacheMu sync.RWMutex
|
||||
cache = make(map[string]Info, 256)
|
||||
)
|
||||
|
||||
// Parse é pura (sem estado observável) e segura para uso concorrente; usa um
|
||||
// cache pequeno e limitado.
|
||||
func Parse(ua string) Info {
|
||||
cacheMu.RLock()
|
||||
in, ok := cache[ua]
|
||||
cacheMu.RUnlock()
|
||||
if ok {
|
||||
return in
|
||||
}
|
||||
in = parse(ua)
|
||||
if len(ua) <= 512 {
|
||||
cacheMu.Lock()
|
||||
if len(cache) >= cacheMax {
|
||||
clear(cache)
|
||||
}
|
||||
cache[ua] = in
|
||||
cacheMu.Unlock()
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
||||
type needle struct {
|
||||
s string // minúsculo
|
||||
name string
|
||||
// prefix: só vale no início do UA
|
||||
prefix bool
|
||||
}
|
||||
|
||||
// Ferramentas de varredura, ataque, automação e monitoramento. Ordem importa
|
||||
// apenas entre entradas que se sobrepõem.
|
||||
var tools = []needle{
|
||||
{"zgrab", "zgrab", false},
|
||||
{"masscan", "masscan", false},
|
||||
{"nuclei", "Nuclei", false},
|
||||
{"sqlmap", "sqlmap", false},
|
||||
{"nmap", "Nmap", false},
|
||||
{"nikto", "Nikto", false},
|
||||
{"dirbuster", "DirBuster", false},
|
||||
{"gobuster", "gobuster", false},
|
||||
{"ffuf", "ffuf", false},
|
||||
{"wfuzz", "wfuzz", false},
|
||||
{"feroxbuster", "feroxbuster", false},
|
||||
{"wpscan", "WPScan", false},
|
||||
{"acunetix", "Acunetix", false},
|
||||
{"nessus", "Nessus", false},
|
||||
{"openvas", "OpenVAS", false},
|
||||
{"qualys", "Qualys", false},
|
||||
{"netsparker", "Netsparker", false},
|
||||
{"burpcollaborator", "Burp", false},
|
||||
{"zmeu", "ZmEu", false},
|
||||
{"l9explore", "l9explore", false},
|
||||
{"l9tcpid", "l9tcpid", false},
|
||||
{"libredtail", "libredtail-http", false},
|
||||
{"censysinspect", "CensysInspect", false},
|
||||
{"expanse", "Expanse", false},
|
||||
{"internet-measurement", "InternetMeasurement", false},
|
||||
{"netsystemsresearch", "NetSystemsResearch", false},
|
||||
{"hello, world", "Hello World", true},
|
||||
{"hello world", "Hello World", true},
|
||||
{"headlesschrome", "HeadlessChrome", false},
|
||||
{"phantomjs", "PhantomJS", false},
|
||||
{"selenium", "Selenium", false},
|
||||
{"puppeteer", "Puppeteer", false},
|
||||
{"playwright", "Playwright", false},
|
||||
{"chrome-lighthouse", "Lighthouse", false},
|
||||
{"curl/", "curl", false},
|
||||
{"wget", "Wget", false},
|
||||
{"httpie", "HTTPie", false},
|
||||
{"python-requests", "python-requests", false},
|
||||
{"python-urllib", "python-urllib", false},
|
||||
{"python-httpx", "python-httpx", false},
|
||||
{"aiohttp", "aiohttp", false},
|
||||
{"python/", "Python", true},
|
||||
{"go-http-client", "Go-http-client", false},
|
||||
{"fasthttp", "fasthttp", false},
|
||||
{"okhttp", "okhttp", false},
|
||||
{"java/", "Java", true},
|
||||
{"apache-httpclient", "Apache-HttpClient", false},
|
||||
{"jakarta commons", "Jakarta-HttpClient", false},
|
||||
{"libwww-perl", "libwww-perl", false},
|
||||
{"lwp::simple", "libwww-perl", false},
|
||||
{"ruby", "Ruby", true},
|
||||
{"faraday", "Faraday", false},
|
||||
{"guzzlehttp", "GuzzleHttp", false},
|
||||
{"php/", "PHP", true},
|
||||
{"node-fetch", "node-fetch", false},
|
||||
{"axios/", "axios", false},
|
||||
{"undici", "undici", false},
|
||||
{"deno/", "Deno", true},
|
||||
{"dart/", "Dart", true},
|
||||
{"reqwest", "reqwest", false},
|
||||
{"scrapy", "Scrapy", false},
|
||||
{"postmanruntime", "Postman", false},
|
||||
{"insomnia", "Insomnia", false},
|
||||
{"winhttp", "WinHTTP", false},
|
||||
{"wappalyzer", "Wappalyzer", false},
|
||||
{"apache/", "Apache", true},
|
||||
{"uptime-kuma", "Uptime-Kuma", false},
|
||||
{"uptimerobot", "UptimeRobot", false},
|
||||
{"pingdom", "Pingdom", false},
|
||||
{"statuscake", "StatusCake", false},
|
||||
{"site24x7", "Site24x7", false},
|
||||
{"zabbix", "Zabbix", false},
|
||||
{"prometheus", "Prometheus", false},
|
||||
{"kube-probe", "kube-probe", false},
|
||||
{"elb-healthchecker", "ELB-HealthChecker", false},
|
||||
{"amazon-route53-health-check", "Route53-HealthCheck", false},
|
||||
{"googlehc", "GoogleHC", false},
|
||||
{"check_http", "check_http", false},
|
||||
{"nagios", "Nagios", false},
|
||||
{"monitoring", "Monitoring", true},
|
||||
}
|
||||
|
||||
// Bots de buscadores, IA, redes sociais e SEO.
|
||||
var bots = []needle{
|
||||
{"googlebot", "Googlebot", false},
|
||||
{"adsbot-google", "AdsBot-Google", false},
|
||||
{"mediapartners-google", "Mediapartners-Google", false},
|
||||
{"google-inspectiontool", "Google-InspectionTool", false},
|
||||
{"google-extended", "Google-Extended", false},
|
||||
{"googleother", "GoogleOther", false},
|
||||
{"google-cloudvertexbot", "Google-CloudVertexBot", false},
|
||||
{"storebot-google", "Storebot-Google", false},
|
||||
{"apis-google", "APIs-Google", false},
|
||||
{"feedfetcher-google", "FeedFetcher-Google", false},
|
||||
{"google-read-aloud", "Google-Read-Aloud", false},
|
||||
{"google-site-verification", "Google-Site-Verification", false},
|
||||
{"google page speed", "Google-PageSpeed", false},
|
||||
{"bingbot", "Bingbot", false},
|
||||
{"bingpreview", "BingPreview", false},
|
||||
{"adidxbot", "adidxbot", false},
|
||||
{"msnbot", "MSNBot", false},
|
||||
{"yahoo! slurp", "Yahoo Slurp", false},
|
||||
{"slurp", "Yahoo Slurp", false},
|
||||
{"duckassistbot", "DuckAssistBot", false},
|
||||
{"duckduckbot", "DuckDuckBot", false},
|
||||
{"baiduspider", "Baiduspider", false},
|
||||
{"yandex", "YandexBot", false},
|
||||
{"sogou", "Sogou", false},
|
||||
{"exabot", "Exabot", false},
|
||||
{"seznambot", "SeznamBot", false},
|
||||
{"qwantify", "Qwantify", false},
|
||||
{"applebot", "Applebot", false},
|
||||
{"petalbot", "PetalBot", false},
|
||||
{"gptbot", "GPTBot", false},
|
||||
{"chatgpt-user", "ChatGPT-User", false},
|
||||
{"oai-searchbot", "OAI-SearchBot", false},
|
||||
{"claudebot", "ClaudeBot", false},
|
||||
{"claude-web", "Claude-Web", false},
|
||||
{"claude-user", "Claude-User", false},
|
||||
{"claude-searchbot", "Claude-SearchBot", false},
|
||||
{"anthropic-ai", "anthropic-ai", false},
|
||||
{"perplexitybot", "PerplexityBot", false},
|
||||
{"perplexity-user", "Perplexity-User", false},
|
||||
{"mistralai-user", "MistralAI-User", false},
|
||||
{"cohere-ai", "cohere-ai", false},
|
||||
{"bytespider", "Bytespider", false},
|
||||
{"ccbot", "CCBot", false},
|
||||
{"amazonbot", "Amazonbot", false},
|
||||
{"diffbot", "Diffbot", false},
|
||||
{"youbot", "YouBot", false},
|
||||
{"imagesiftbot", "ImagesiftBot", false},
|
||||
{"timpibot", "Timpibot", false},
|
||||
{"omgili", "omgili", false},
|
||||
{"meta-externalagent", "meta-externalagent", false},
|
||||
{"meta-externalfetcher", "meta-externalfetcher", false},
|
||||
{"facebookexternalhit", "facebookexternalhit", false},
|
||||
{"facebot", "Facebot", false},
|
||||
{"twitterbot", "Twitterbot", false},
|
||||
{"linkedinbot", "LinkedInBot", false},
|
||||
{"slackbot", "Slackbot", false},
|
||||
{"slack-imgproxy", "Slack-ImgProxy", false},
|
||||
{"discordbot", "Discordbot", false},
|
||||
{"telegrambot", "TelegramBot", false},
|
||||
{"whatsapp/", "WhatsApp", false},
|
||||
{"pinterestbot", "Pinterestbot", false},
|
||||
{"redditbot", "redditbot", false},
|
||||
{"skypeuripreview", "SkypeUriPreview", false},
|
||||
{"embedly", "Embedly", false},
|
||||
{"quora link preview", "Quora Link Preview", false},
|
||||
{"semrushbot", "SemrushBot", false},
|
||||
{"ahrefsbot", "AhrefsBot", false},
|
||||
{"ahrefssiteaudit", "AhrefsSiteAudit", false},
|
||||
{"mj12bot", "MJ12bot", false},
|
||||
{"dotbot", "DotBot", false},
|
||||
{"rogerbot", "rogerbot", false},
|
||||
{"blexbot", "BLEXBot", false},
|
||||
{"dataforseobot", "DataForSeoBot", false},
|
||||
{"barkrowler", "Barkrowler", false},
|
||||
{"megaindex", "MegaIndex", false},
|
||||
{"serpstatbot", "serpstatbot", false},
|
||||
{"screaming frog", "Screaming Frog", false},
|
||||
{"ia_archiver", "ia_archiver", false},
|
||||
{"archive.org_bot", "archive.org_bot", false},
|
||||
{"better uptime bot", "Better Uptime Bot", false},
|
||||
{"wp rocket", "WP Rocket", false},
|
||||
}
|
||||
|
||||
func lower(s string) string {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if c := s[i]; c >= 'A' && c <= 'Z' {
|
||||
return strings.ToLower(s)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func matchNeedle(list []needle, l string) string {
|
||||
for i := range list {
|
||||
n := &list[i]
|
||||
if n.prefix {
|
||||
if strings.HasPrefix(l, n.s) {
|
||||
return n.name
|
||||
}
|
||||
} else if strings.Contains(l, n.s) {
|
||||
return n.name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func parse(raw string) Info {
|
||||
s := strings.TrimSpace(raw)
|
||||
if s == "" || s == "-" {
|
||||
return Info{Device: Unknown}
|
||||
}
|
||||
l := lower(s)
|
||||
|
||||
if t := matchNeedle(tools, l); t != "" {
|
||||
in := Info{Device: ToolDev, Tool: t}
|
||||
if strings.HasPrefix(l, "mozilla/") {
|
||||
in.Browser, in.BrowserMajor = browser(s)
|
||||
in.OS = osName(s)
|
||||
}
|
||||
return in
|
||||
}
|
||||
if b := matchNeedle(bots, l); b != "" {
|
||||
return Info{Device: BotDev, Bot: true, BotName: b}
|
||||
}
|
||||
if b := genericBot(s); b != "" {
|
||||
return Info{Device: BotDev, Bot: true, BotName: b}
|
||||
}
|
||||
|
||||
in := Info{}
|
||||
in.Browser, in.BrowserMajor = browser(s)
|
||||
in.OS = osName(s)
|
||||
in.Device = device(s, l, &in)
|
||||
return in
|
||||
}
|
||||
|
||||
// genericBot reconhece bots não listados: tokens terminados em "bot",
|
||||
// "crawler"/"spider", ou "(compatible; Nome/1.0; +http://...)".
|
||||
func genericBot(s string) string {
|
||||
isSep := func(r byte) bool {
|
||||
return r == ' ' || r == ';' || r == '(' || r == ')' || r == '/' || r == ','
|
||||
}
|
||||
var firstCompat string
|
||||
for i := 0; i < len(s); {
|
||||
for i < len(s) && isSep(s[i]) {
|
||||
i++
|
||||
}
|
||||
j := i
|
||||
for j < len(s) && !isSep(s[j]) {
|
||||
j++
|
||||
}
|
||||
tok := s[i:j]
|
||||
i = j
|
||||
if len(tok) < 3 {
|
||||
continue
|
||||
}
|
||||
lt := lower(tok)
|
||||
if strings.EqualFold(tok, "compatible") {
|
||||
firstCompat = "?"
|
||||
continue
|
||||
}
|
||||
if firstCompat == "?" {
|
||||
firstCompat = tok
|
||||
}
|
||||
if lt == "cubot" {
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(lt, "bot") || strings.Contains(lt, "crawler") || strings.Contains(lt, "spider") {
|
||||
return tok
|
||||
}
|
||||
}
|
||||
// "(compatible; Foo/1.0; +http...)" sem navegador conhecido
|
||||
if firstCompat != "" && firstCompat != "?" && strings.Contains(s, "+http") &&
|
||||
!strings.HasPrefix(firstCompat, "MSIE") && !strings.HasPrefix(firstCompat, "Opera") {
|
||||
return firstCompat
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// major devolve os dígitos após tok em s ("Chrome/" -> "89").
|
||||
func major(s, tok string) (string, bool) {
|
||||
i := strings.Index(s, tok)
|
||||
if i < 0 {
|
||||
return "", false
|
||||
}
|
||||
i += len(tok)
|
||||
j := i
|
||||
for j < len(s) && s[j] >= '0' && s[j] <= '9' {
|
||||
j++
|
||||
}
|
||||
if j == i {
|
||||
return "", false
|
||||
}
|
||||
return s[i:j], true
|
||||
}
|
||||
|
||||
func browser(s string) (name, ver string) {
|
||||
type rule struct {
|
||||
tok, name string
|
||||
}
|
||||
// ordem: derivados de Chromium/WebKit antes de Chrome e Safari
|
||||
for _, r := range [...]rule{
|
||||
{"Edg/", "Edge"}, {"EdgA/", "Edge"}, {"EdgiOS/", "Edge"}, {"Edge/", "Edge"},
|
||||
{"OPR/", "Opera"}, {"OPT/", "Opera"}, {"OPiOS/", "Opera"},
|
||||
{"SamsungBrowser/", "Samsung Internet"},
|
||||
{"Vivaldi/", "Vivaldi"},
|
||||
{"YaBrowser/", "Yandex Browser"},
|
||||
{"UCBrowser/", "UC Browser"},
|
||||
{"DuckDuckGo/", "DuckDuckGo"},
|
||||
{"MiuiBrowser/", "MIUI Browser"},
|
||||
{"HuaweiBrowser/", "Huawei Browser"},
|
||||
{"Whale/", "Whale"},
|
||||
{"QQBrowser/", "QQ Browser"},
|
||||
{"Silk/", "Silk"},
|
||||
{"Electron/", "Electron"},
|
||||
{"MicroMessenger/", "WeChat"},
|
||||
{"FBAV/", "Facebook"},
|
||||
{"Instagram ", "Instagram"},
|
||||
{"Firefox/", "Firefox"},
|
||||
{"FxiOS/", "Firefox"},
|
||||
{"Chromium/", "Chromium"},
|
||||
{"CriOS/", "Chrome"},
|
||||
} {
|
||||
if v, ok := major(s, r.tok); ok {
|
||||
return r.name, v
|
||||
}
|
||||
}
|
||||
if strings.Contains(s, "Opera Mini/") {
|
||||
v, _ := major(s, "Opera Mini/")
|
||||
return "Opera Mini", v
|
||||
}
|
||||
if strings.Contains(s, "Opera") {
|
||||
if v, ok := major(s, "Version/"); ok {
|
||||
return "Opera", v
|
||||
}
|
||||
if v, ok := major(s, "Opera/"); ok {
|
||||
return "Opera", v
|
||||
}
|
||||
v, _ := major(s, "Opera ")
|
||||
return "Opera", v
|
||||
}
|
||||
if v, ok := major(s, "MSIE "); ok {
|
||||
return "Internet Explorer", v
|
||||
}
|
||||
if strings.Contains(s, "Trident/") {
|
||||
v, _ := major(s, "rv:")
|
||||
return "Internet Explorer", v
|
||||
}
|
||||
if v, ok := major(s, "Chrome/"); ok {
|
||||
if strings.Contains(s, "; wv)") {
|
||||
return "Android WebView", v
|
||||
}
|
||||
return "Chrome", v
|
||||
}
|
||||
if strings.Contains(s, "Safari/") || strings.Contains(s, "AppleWebKit/") {
|
||||
if v, ok := major(s, "Version/"); ok {
|
||||
return "Safari", v
|
||||
}
|
||||
if strings.Contains(s, "Safari/") {
|
||||
return "Safari", ""
|
||||
}
|
||||
return "WebKit", ""
|
||||
}
|
||||
if strings.HasPrefix(s, "Lynx/") {
|
||||
v, _ := major(s, "Lynx/")
|
||||
return "Lynx", v
|
||||
}
|
||||
if strings.HasPrefix(s, "w3m/") {
|
||||
return "w3m", ""
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func osName(s string) string {
|
||||
switch {
|
||||
case strings.Contains(s, "Windows Phone"):
|
||||
return "Windows Phone"
|
||||
case strings.Contains(s, "Windows"):
|
||||
return "Windows"
|
||||
case strings.Contains(s, "Android"):
|
||||
return "Android"
|
||||
case strings.Contains(s, "iPhone"), strings.Contains(s, "iPod"), strings.Contains(s, "iPad"):
|
||||
return "iOS"
|
||||
case strings.Contains(s, "CrOS"):
|
||||
return "ChromeOS"
|
||||
case strings.Contains(s, "Macintosh"):
|
||||
// iPadOS 13+ se apresenta como Mac, mas traz "Mobile/<build>"
|
||||
if strings.Contains(s, " Mobile/") {
|
||||
return "iOS"
|
||||
}
|
||||
return "macOS"
|
||||
case strings.Contains(s, "Mac OS X"):
|
||||
return "macOS"
|
||||
case strings.Contains(s, "KaiOS"):
|
||||
return "KaiOS"
|
||||
case strings.Contains(s, "Tizen"):
|
||||
return "Tizen"
|
||||
case strings.Contains(s, "Web0S"), strings.Contains(s, "WebOS"), strings.Contains(s, "webOS"):
|
||||
return "webOS"
|
||||
case strings.Contains(s, "FreeBSD"):
|
||||
return "FreeBSD"
|
||||
case strings.Contains(s, "OpenBSD"):
|
||||
return "OpenBSD"
|
||||
case strings.Contains(s, "NetBSD"):
|
||||
return "NetBSD"
|
||||
case strings.Contains(s, "BlackBerry"), strings.Contains(s, "BB10"):
|
||||
return "BlackBerry"
|
||||
case strings.Contains(s, "Linux"), strings.Contains(s, "X11"), strings.Contains(s, "Ubuntu"),
|
||||
strings.Contains(s, "Fedora"), strings.Contains(s, "Debian"):
|
||||
return "Linux"
|
||||
case strings.Contains(s, "Dalvik/"):
|
||||
return "Android"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func device(s, l string, in *Info) string {
|
||||
if strings.Contains(s, "iPad") ||
|
||||
(in.OS == "iOS" && strings.Contains(s, "Macintosh")) {
|
||||
return Tablet
|
||||
}
|
||||
if strings.Contains(l, "tablet") || strings.Contains(s, "Kindle") || strings.Contains(s, "PlayBook") ||
|
||||
(in.OS == "Android" && strings.Contains(s, "Silk/")) {
|
||||
return Tablet
|
||||
}
|
||||
switch in.OS {
|
||||
case "Android":
|
||||
if strings.Contains(s, "Mobile") || strings.Contains(s, "Dalvik/") || in.Browser == "Opera Mini" {
|
||||
return Mobile
|
||||
}
|
||||
if strings.Contains(s, "Android TV") || strings.Contains(s, "SMART-TV") {
|
||||
return Unknown
|
||||
}
|
||||
return Tablet
|
||||
case "iOS", "Windows Phone", "KaiOS", "BlackBerry":
|
||||
return Mobile
|
||||
case "Tizen", "webOS":
|
||||
return Unknown
|
||||
}
|
||||
if strings.Contains(s, "Mobile") || strings.Contains(s, "IEMobile") || in.Browser == "Opera Mini" {
|
||||
return Mobile
|
||||
}
|
||||
if strings.Contains(s, "SMART-TV") || strings.Contains(s, "HbbTV") || strings.Contains(s, "CrKey") || strings.Contains(s, "AppleTV") {
|
||||
return Unknown
|
||||
}
|
||||
switch in.OS {
|
||||
case "Windows", "macOS", "Linux", "ChromeOS", "FreeBSD", "OpenBSD", "NetBSD":
|
||||
return Desktop
|
||||
}
|
||||
if in.Browser != "" {
|
||||
return Desktop
|
||||
}
|
||||
return Unknown
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package ua
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type tc struct {
|
||||
ua string
|
||||
browser, major string
|
||||
os, device string
|
||||
botName, toolName string
|
||||
}
|
||||
|
||||
func TestParseTable(t *testing.T) {
|
||||
cases := []tc{
|
||||
// produção
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36", "Chrome", "89", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0", "", "", "", "unknown", "", ""},
|
||||
{"curl/8.14.1", "", "", "", "tool", "", "curl"},
|
||||
{"Uptime-Kuma/2.5.3", "", "", "", "tool", "", "Uptime-Kuma"},
|
||||
{"Apache/2.4.69 (Debian) PHP/8.3.36 (internal dummy connection)", "", "", "", "tool", "", "Apache"},
|
||||
// vazios e bizarros
|
||||
{"", "", "", "", "unknown", "", ""},
|
||||
{"-", "", "", "", "unknown", "", ""},
|
||||
{" ", "", "", "", "unknown", "", ""},
|
||||
{"asdfghjkl", "", "", "", "unknown", "", ""},
|
||||
{"Mozilla/5.0 (compatible)", "", "", "", "unknown", "", ""},
|
||||
// desktop
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0", "Firefox", "128", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0", "Firefox", "115", "Linux", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0", "Firefox", "121", "Linux", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15", "Safari", "17", "macOS", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Chrome", "120", "macOS", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.91", "Edge", "120", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582", "Edge", "18", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 OPR/105.0.0.0", "Opera", "105", "Windows", "desktop", "", ""},
|
||||
{"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.289 Version/12.02", "Opera", "12", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Vivaldi/6.4.3160.47", "Vivaldi", "6", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 YaBrowser/23.9.1.964 Yowser/2.5 Safari/537.36", "Yandex Browser", "23", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/80.0.3987.163 Chrome/80.0.3987.163 Safari/537.36", "Chromium", "80", "Linux", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36", "Chrome", "117", "ChromeOS", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36", "Chrome", "121", "Linux", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (X11; FreeBSD amd64; rv:109.0) Gecko/20100101 Firefox/115.0", "Firefox", "115", "FreeBSD", "desktop", "", ""},
|
||||
{"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2)", "Internet Explorer", "8", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko", "Internet Explorer", "11", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0", "Firefox", "52", "Windows", "desktop", "", ""},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.5414.74 Electron/23.1.1 Safari/537.36", "Electron", "23", "Windows", "desktop", "", ""},
|
||||
{"Lynx/2.9.0dev.10 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.7.8", "Lynx", "2", "", "desktop", "", ""},
|
||||
// mobile
|
||||
{"Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1", "Safari", "17", "iOS", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1", "Chrome", "120", "iOS", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/120.0 Mobile/15E148 Safari/605.1.15", "Firefox", "120", "iOS", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 EdgiOS/119.0.2151.64 Mobile/15E148 Safari/605.1.15", "Edge", "119", "iOS", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.144 Mobile Safari/537.36", "Chrome", "120", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 13; SAMSUNG SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/23.0 Chrome/115.0.0.0 Mobile Safari/537.36", "Samsung Internet", "23", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Android 13; Mobile; rv:120.0) Gecko/120.0 Firefox/120.0", "Firefox", "120", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 12; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36 EdgA/119.0.2151.78", "Edge", "119", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 OPR/76.2.4027", "Opera", "76", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 11; moto g(10)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36", "Chrome", "108", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 9; CUBOT_X30) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Mobile Safari/537.36", "Chrome", "99", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 12; SM-A525M Build/SP1A.210812.016; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/120.0.6099.43 Mobile Safari/537.36", "Android WebView", "120", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 8.1.0; Mi A2 Build/OPM1) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36 UCBrowser/13.4.0.1306", "UC Browser", "13", "Android", "mobile", "", ""},
|
||||
{"Opera/9.80 (Android; Opera Mini/36.2.2254/119.132; U; id) Presto/2.12.423 Version/12.16", "Opera Mini", "36", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Mobile Safari/537.36 Edge/15.15254", "Edge", "15", "Windows Phone", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 11; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 DuckDuckGo/5", "DuckDuckGo", "5", "Android", "mobile", "", ""},
|
||||
{"Dalvik/2.1.0 (Linux; U; Android 9; SM-J730G Build/PPR1.180610.011)", "", "", "Android", "mobile", "", ""},
|
||||
{"Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 305.0.0.20.116", "Instagram", "305", "iOS", "mobile", "", ""},
|
||||
// tablet
|
||||
{"Mozilla/5.0 (iPad; CPU OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1", "Safari", "17", "iOS", "tablet", "", ""},
|
||||
{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1", "Safari", "17", "iOS", "tablet", "", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 13; SM-X700) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Chrome", "120", "Android", "tablet", "", ""},
|
||||
{"Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; KFTT Build/IML74K) AppleWebKit/537.36 (KHTML, like Gecko) Silk/3.68 like Chrome/39.0.2171.93 Safari/537.36", "Silk", "3", "Android", "tablet", "", ""},
|
||||
// TV
|
||||
{"Mozilla/5.0 (SMART-TV; LINUX; Tizen 6.0) AppleWebKit/537.36 (KHTML, like Gecko) 76.0.3809.146/6.0 TV Safari/537.36", "Safari", "", "Tizen", "unknown", "", ""},
|
||||
// bots bons
|
||||
{"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "", "", "", "bot", "Googlebot", ""},
|
||||
{"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.109 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "", "", "", "bot", "Googlebot", ""},
|
||||
{"Googlebot-Image/1.0", "", "", "", "bot", "Googlebot", ""},
|
||||
{"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)", "", "", "", "bot", "Bingbot", ""},
|
||||
{"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.1; +https://openai.com/gptbot)", "", "", "", "bot", "GPTBot", ""},
|
||||
{"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)", "", "", "", "bot", "ClaudeBot", ""},
|
||||
{"Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)", "", "", "", "bot", "AhrefsBot", ""},
|
||||
{"Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://www.semrush.com/bot.html)", "", "", "", "bot", "SemrushBot", ""},
|
||||
{"Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)", "", "", "", "bot", "YandexBot", ""},
|
||||
{"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)", "", "", "", "bot", "Baiduspider", ""},
|
||||
{"DuckDuckBot/1.1; (+http://duckduckgo.com/duckduckbot.html)", "", "", "", "bot", "DuckDuckBot", ""},
|
||||
{"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)", "", "", "", "bot", "facebookexternalhit", ""},
|
||||
{"Twitterbot/1.0", "", "", "", "bot", "Twitterbot", ""},
|
||||
{"Mozilla/5.0 (compatible; PetalBot;+https://webmaster.petalsearch.com/site/petalbot)", "", "", "", "bot", "PetalBot", ""},
|
||||
{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15 (Applebot/0.1; +http://www.apple.com/go/applebot)", "", "", "", "bot", "Applebot", ""},
|
||||
{"Mozilla/5.0 (compatible; MJ12bot/v1.4.8; http://mj12bot.com/)", "", "", "", "bot", "MJ12bot", ""},
|
||||
{"Mozilla/5.0 (compatible; Bytespider; spider-feedback@bytedance.com) AppleWebKit/537.36", "", "", "", "bot", "Bytespider", ""},
|
||||
{"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; PerplexityBot/1.0; +https://perplexity.ai/perplexitybot", "", "", "", "bot", "PerplexityBot", ""},
|
||||
{"Mozilla/5.0 (compatible; Exampleinfobot/2.3; +https://example.org/bot)", "", "", "", "bot", "Exampleinfobot", ""},
|
||||
{"Mozilla/5.0 (compatible; NewSearchEngine/1.0; +https://newsearch.example/info)", "", "", "", "bot", "NewSearchEngine", ""},
|
||||
// ferramentas
|
||||
{"python-requests/2.31.0", "", "", "", "tool", "", "python-requests"},
|
||||
{"Go-http-client/1.1", "", "", "", "tool", "", "Go-http-client"},
|
||||
{"Go-http-client/2.0", "", "", "", "tool", "", "Go-http-client"},
|
||||
{"Wget/1.21.4", "", "", "", "tool", "", "Wget"},
|
||||
{"Mozilla/5.0 zgrab/0.x", "", "", "", "tool", "", "zgrab"},
|
||||
{"masscan/1.3 (https://github.com/robertdavidgraham/masscan)", "", "", "", "tool", "", "masscan"},
|
||||
{"Mozilla/5.0 (compatible; Nmap Scripting Engine; https://nmap.org/book/nse.html)", "", "", "", "tool", "", "Nmap"},
|
||||
{"Nuclei - Open-source project (github.com/projectdiscovery/nuclei)", "", "", "", "tool", "", "Nuclei"},
|
||||
{"sqlmap/1.7.2#stable (https://sqlmap.org)", "", "", "", "tool", "", "sqlmap"},
|
||||
{"Mozilla/5.00 (Nikto/2.1.6) (Evasions:None) (Test:Port Check)", "", "", "", "tool", "", "Nikto"},
|
||||
{"Mozilla/5.0 (compatible; CensysInspect/1.1; +https://about.censys.io/)", "", "", "", "tool", "", "CensysInspect"},
|
||||
{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/120.0.0.0 Safari/537.36", "Chrome", "120", "Windows", "tool", "", "HeadlessChrome"},
|
||||
{"Java/17.0.2", "", "", "", "tool", "", "Java"},
|
||||
{"okhttp/4.12.0", "", "", "", "tool", "", "okhttp"},
|
||||
{"libwww-perl/6.67", "", "", "", "tool", "", "libwww-perl"},
|
||||
{"PostmanRuntime/7.36.0", "", "", "", "tool", "", "Postman"},
|
||||
{"axios/1.6.2", "", "", "", "tool", "", "axios"},
|
||||
{"Hello World", "", "", "", "tool", "", "Hello World"},
|
||||
{"UptimeRobot/2.0; http://www.uptimerobot.com/", "", "", "", "tool", "", "UptimeRobot"},
|
||||
{"kube-probe/1.28", "", "", "", "tool", "", "kube-probe"},
|
||||
{"ELB-HealthChecker/2.0", "", "", "", "tool", "", "ELB-HealthChecker"},
|
||||
{"Python/3.11 aiohttp/3.9.1", "", "", "", "tool", "", "aiohttp"},
|
||||
{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Chrome-Lighthouse", "Chrome", "120", "Linux", "tool", "", "Lighthouse"},
|
||||
}
|
||||
if len(cases) < 60 {
|
||||
t.Fatalf("poucos casos: %d", len(cases))
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := Parse(c.ua)
|
||||
want := Info{Browser: c.browser, BrowserMajor: c.major, OS: c.os, Device: c.device,
|
||||
Bot: c.botName != "", BotName: c.botName, Tool: c.toolName}
|
||||
if got != want {
|
||||
t.Errorf("UA %q\n obtido: %+v\n esperado: %+v", c.ua, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseInvariants(t *testing.T) {
|
||||
for _, s := range []string{"", "x", "Mozilla/5.0", "\x00\xff", strings.Repeat("A", 100000), "Chrome/", "Version/", "MSIE "} {
|
||||
in := Parse(s)
|
||||
if in.Device == "" {
|
||||
t.Errorf("%q: device vazio", s)
|
||||
}
|
||||
if in.Bot != (in.BotName != "") {
|
||||
t.Errorf("%q: Bot e BotName inconsistentes", s)
|
||||
}
|
||||
if (in.Device == "tool") != (in.Tool != "") {
|
||||
t.Errorf("%q: Tool e Device inconsistentes", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheBoundedConcurrent(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
for g := 0; g < 8; g++ {
|
||||
wg.Add(1)
|
||||
go func(g int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 3000; i++ {
|
||||
ua := "Mozilla/5.0 (Windows NT 10.0) Chrome/" + strings.Repeat("1", 1+(i+g)%50) + " Safari/537.36"
|
||||
if Parse(ua).Browser != "Chrome" {
|
||||
t.Error("falhou")
|
||||
return
|
||||
}
|
||||
}
|
||||
}(g)
|
||||
}
|
||||
wg.Wait()
|
||||
cacheMu.RLock()
|
||||
n := len(cache)
|
||||
cacheMu.RUnlock()
|
||||
if n > cacheMax {
|
||||
t.Fatalf("cache cresceu além do limite: %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseCold(b *testing.B) {
|
||||
uas := []string{
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1",
|
||||
"curl/8.14.1",
|
||||
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
|
||||
}
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
parse(uas[i&3])
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseCached(b *testing.B) {
|
||||
ua := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
Parse(ua)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user