mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-08-03 17:59:24 -04:00
Merge 2e7f37215c912bf0448fd8a45973683a37a4f96b into bca2e87e80bc7ef976fea9dc16e18ea7a69b9933
This commit is contained in:
commit
106f178e35
@ -29,7 +29,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RobotsRule struct {
|
type RobotsRule struct {
|
||||||
UserAgent string
|
UserAgents []string
|
||||||
Disallows []string
|
Disallows []string
|
||||||
Allows []string
|
Allows []string
|
||||||
CrawlDelay int
|
CrawlDelay int
|
||||||
@ -133,7 +133,10 @@ func main() {
|
|||||||
func parseRobotsTxt(input io.Reader) ([]RobotsRule, error) {
|
func parseRobotsTxt(input io.Reader) ([]RobotsRule, error) {
|
||||||
scanner := bufio.NewScanner(input)
|
scanner := bufio.NewScanner(input)
|
||||||
var rules []RobotsRule
|
var rules []RobotsRule
|
||||||
var currentRule *RobotsRule
|
var currentUserAgents []string
|
||||||
|
var currentDisallows []string
|
||||||
|
var currentAllows []string
|
||||||
|
var currentCrawlDelay int
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
@ -154,38 +157,58 @@ func parseRobotsTxt(input io.Reader) ([]RobotsRule, error) {
|
|||||||
|
|
||||||
switch directive {
|
switch directive {
|
||||||
case "user-agent":
|
case "user-agent":
|
||||||
// Start a new rule section
|
// If we have accumulated rules with directives and encounter a new user-agent,
|
||||||
if currentRule != nil {
|
// flush the current rules
|
||||||
rules = append(rules, *currentRule)
|
if len(currentUserAgents) > 0 && (len(currentDisallows) > 0 || len(currentAllows) > 0 || currentCrawlDelay > 0) {
|
||||||
|
rule := RobotsRule{
|
||||||
|
UserAgents: make([]string, len(currentUserAgents)),
|
||||||
|
Disallows: make([]string, len(currentDisallows)),
|
||||||
|
Allows: make([]string, len(currentAllows)),
|
||||||
|
CrawlDelay: currentCrawlDelay,
|
||||||
}
|
}
|
||||||
currentRule = &RobotsRule{
|
copy(rule.UserAgents, currentUserAgents)
|
||||||
UserAgent: value,
|
copy(rule.Disallows, currentDisallows)
|
||||||
Disallows: make([]string, 0),
|
copy(rule.Allows, currentAllows)
|
||||||
Allows: make([]string, 0),
|
rules = append(rules, rule)
|
||||||
|
// Reset for next group
|
||||||
|
currentUserAgents = nil
|
||||||
|
currentDisallows = nil
|
||||||
|
currentAllows = nil
|
||||||
|
currentCrawlDelay = 0
|
||||||
}
|
}
|
||||||
|
currentUserAgents = append(currentUserAgents, value)
|
||||||
|
|
||||||
case "disallow":
|
case "disallow":
|
||||||
if currentRule != nil && value != "" {
|
if len(currentUserAgents) > 0 && value != "" {
|
||||||
currentRule.Disallows = append(currentRule.Disallows, value)
|
currentDisallows = append(currentDisallows, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "allow":
|
case "allow":
|
||||||
if currentRule != nil && value != "" {
|
if len(currentUserAgents) > 0 && value != "" {
|
||||||
currentRule.Allows = append(currentRule.Allows, value)
|
currentAllows = append(currentAllows, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "crawl-delay":
|
case "crawl-delay":
|
||||||
if currentRule != nil {
|
if len(currentUserAgents) > 0 {
|
||||||
if delay, err := parseIntSafe(value); err == nil {
|
if delay, err := parseIntSafe(value); err == nil {
|
||||||
currentRule.CrawlDelay = delay
|
currentCrawlDelay = delay
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't forget the last rule
|
// Don't forget the last group of rules
|
||||||
if currentRule != nil {
|
if len(currentUserAgents) > 0 {
|
||||||
rules = append(rules, *currentRule)
|
rule := RobotsRule{
|
||||||
|
UserAgents: make([]string, len(currentUserAgents)),
|
||||||
|
Disallows: make([]string, len(currentDisallows)),
|
||||||
|
Allows: make([]string, len(currentAllows)),
|
||||||
|
CrawlDelay: currentCrawlDelay,
|
||||||
|
}
|
||||||
|
copy(rule.UserAgents, currentUserAgents)
|
||||||
|
copy(rule.Disallows, currentDisallows)
|
||||||
|
copy(rule.Allows, currentAllows)
|
||||||
|
rules = append(rules, rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark blacklisted user agents (those with "Disallow: /")
|
// Mark blacklisted user agents (those with "Disallow: /")
|
||||||
@ -211,10 +234,11 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule {
|
|||||||
var anubisRules []AnubisRule
|
var anubisRules []AnubisRule
|
||||||
ruleCounter := 0
|
ruleCounter := 0
|
||||||
|
|
||||||
|
// Process each robots rule individually
|
||||||
for _, robotsRule := range robotsRules {
|
for _, robotsRule := range robotsRules {
|
||||||
userAgent := robotsRule.UserAgent
|
userAgents := robotsRule.UserAgents
|
||||||
|
|
||||||
// Handle crawl delay as weight adjustment (do this first before any continues)
|
// Handle crawl delay
|
||||||
if robotsRule.CrawlDelay > 0 && *crawlDelay > 0 {
|
if robotsRule.CrawlDelay > 0 && *crawlDelay > 0 {
|
||||||
ruleCounter++
|
ruleCounter++
|
||||||
rule := AnubisRule{
|
rule := AnubisRule{
|
||||||
@ -223,20 +247,32 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule {
|
|||||||
Weight: &config.Weight{Adjust: *crawlDelay},
|
Weight: &config.Weight{Adjust: *crawlDelay},
|
||||||
}
|
}
|
||||||
|
|
||||||
if userAgent == "*" {
|
if len(userAgents) == 1 && userAgents[0] == "*" {
|
||||||
rule.Expression = &config.ExpressionOrList{
|
rule.Expression = &config.ExpressionOrList{
|
||||||
All: []string{"true"}, // Always applies
|
All: []string{"true"}, // Always applies
|
||||||
}
|
}
|
||||||
} else {
|
} else if len(userAgents) == 1 {
|
||||||
rule.Expression = &config.ExpressionOrList{
|
rule.Expression = &config.ExpressionOrList{
|
||||||
All: []string{fmt.Sprintf("userAgent.contains(%q)", userAgent)},
|
All: []string{fmt.Sprintf("userAgent.contains(%q)", userAgents[0])},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Multiple user agents - use any block
|
||||||
|
var expressions []string
|
||||||
|
for _, ua := range userAgents {
|
||||||
|
if ua == "*" {
|
||||||
|
expressions = append(expressions, "true")
|
||||||
|
} else {
|
||||||
|
expressions = append(expressions, fmt.Sprintf("userAgent.contains(%q)", ua))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rule.Expression = &config.ExpressionOrList{
|
||||||
|
Any: expressions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
anubisRules = append(anubisRules, rule)
|
anubisRules = append(anubisRules, rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle blacklisted user agents (complete deny/challenge)
|
// Handle blacklisted user agents
|
||||||
if robotsRule.IsBlacklist {
|
if robotsRule.IsBlacklist {
|
||||||
ruleCounter++
|
ruleCounter++
|
||||||
rule := AnubisRule{
|
rule := AnubisRule{
|
||||||
@ -244,6 +280,8 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule {
|
|||||||
Action: *userAgentDeny,
|
Action: *userAgentDeny,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(userAgents) == 1 {
|
||||||
|
userAgent := userAgents[0]
|
||||||
if userAgent == "*" {
|
if userAgent == "*" {
|
||||||
// This would block everything - convert to a weight adjustment instead
|
// This would block everything - convert to a weight adjustment instead
|
||||||
rule.Name = fmt.Sprintf("%s-global-restriction-%d", *policyName, ruleCounter)
|
rule.Name = fmt.Sprintf("%s-global-restriction-%d", *policyName, ruleCounter)
|
||||||
@ -257,8 +295,21 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule {
|
|||||||
All: []string{fmt.Sprintf("userAgent.contains(%q)", userAgent)},
|
All: []string{fmt.Sprintf("userAgent.contains(%q)", userAgent)},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Multiple user agents - use any block
|
||||||
|
var expressions []string
|
||||||
|
for _, ua := range userAgents {
|
||||||
|
if ua == "*" {
|
||||||
|
expressions = append(expressions, "true")
|
||||||
|
} else {
|
||||||
|
expressions = append(expressions, fmt.Sprintf("userAgent.contains(%q)", ua))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rule.Expression = &config.ExpressionOrList{
|
||||||
|
Any: expressions,
|
||||||
|
}
|
||||||
|
}
|
||||||
anubisRules = append(anubisRules, rule)
|
anubisRules = append(anubisRules, rule)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle specific disallow rules
|
// Handle specific disallow rules
|
||||||
@ -276,9 +327,33 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule {
|
|||||||
// Build CEL expression
|
// Build CEL expression
|
||||||
var conditions []string
|
var conditions []string
|
||||||
|
|
||||||
// Add user agent condition if not wildcard
|
// Add user agent conditions
|
||||||
if userAgent != "*" {
|
if len(userAgents) == 1 && userAgents[0] == "*" {
|
||||||
conditions = append(conditions, fmt.Sprintf("userAgent.contains(%q)", userAgent))
|
// Wildcard user agent - no user agent condition needed
|
||||||
|
} else if len(userAgents) == 1 {
|
||||||
|
conditions = append(conditions, fmt.Sprintf("userAgent.contains(%q)", userAgents[0]))
|
||||||
|
} else {
|
||||||
|
// For multiple user agents, we need to use a more complex expression
|
||||||
|
// This is a limitation - we can't easily combine any for user agents with all for path
|
||||||
|
// So we'll create separate rules for each user agent
|
||||||
|
for _, ua := range userAgents {
|
||||||
|
if ua == "*" {
|
||||||
|
continue // Skip wildcard as it's handled separately
|
||||||
|
}
|
||||||
|
ruleCounter++
|
||||||
|
subRule := AnubisRule{
|
||||||
|
Name: fmt.Sprintf("%s-disallow-%d", *policyName, ruleCounter),
|
||||||
|
Action: *baseAction,
|
||||||
|
Expression: &config.ExpressionOrList{
|
||||||
|
All: []string{
|
||||||
|
fmt.Sprintf("userAgent.contains(%q)", ua),
|
||||||
|
buildPathCondition(disallow),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
anubisRules = append(anubisRules, subRule)
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add path condition
|
// Add path condition
|
||||||
@ -291,7 +366,6 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule {
|
|||||||
|
|
||||||
anubisRules = append(anubisRules, rule)
|
anubisRules = append(anubisRules, rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return anubisRules
|
return anubisRules
|
||||||
|
@ -78,6 +78,12 @@ func TestDataFileConversion(t *testing.T) {
|
|||||||
expectedFile: "complex.yaml",
|
expectedFile: "complex.yaml",
|
||||||
options: TestOptions{format: "yaml", crawlDelayWeight: 5},
|
options: TestOptions{format: "yaml", crawlDelayWeight: 5},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "consecutive_user_agents",
|
||||||
|
robotsFile: "consecutive.robots.txt",
|
||||||
|
expectedFile: "consecutive.yaml",
|
||||||
|
options: TestOptions{format: "yaml", crawlDelayWeight: 3},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
25
cmd/robots2policy/testdata/consecutive.robots.txt
vendored
Normal file
25
cmd/robots2policy/testdata/consecutive.robots.txt
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# Test consecutive user agents that should be grouped into any: blocks
|
||||||
|
User-agent: *
|
||||||
|
Disallow: /admin
|
||||||
|
Crawl-delay: 10
|
||||||
|
|
||||||
|
# Multiple consecutive user agents - should be grouped
|
||||||
|
User-agent: BadBot
|
||||||
|
User-agent: SpamBot
|
||||||
|
User-agent: EvilBot
|
||||||
|
Disallow: /
|
||||||
|
|
||||||
|
# Single user agent - should be separate
|
||||||
|
User-agent: GoodBot
|
||||||
|
Disallow: /private
|
||||||
|
|
||||||
|
# Multiple consecutive user agents with crawl delay
|
||||||
|
User-agent: SlowBot1
|
||||||
|
User-agent: SlowBot2
|
||||||
|
Crawl-delay: 5
|
||||||
|
|
||||||
|
# Multiple consecutive user agents with specific path
|
||||||
|
User-agent: SearchBot1
|
||||||
|
User-agent: SearchBot2
|
||||||
|
User-agent: SearchBot3
|
||||||
|
Disallow: /search
|
47
cmd/robots2policy/testdata/consecutive.yaml
vendored
Normal file
47
cmd/robots2policy/testdata/consecutive.yaml
vendored
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
- action: WEIGH
|
||||||
|
expression: "true"
|
||||||
|
name: robots-txt-policy-crawl-delay-1
|
||||||
|
weight:
|
||||||
|
adjust: 3
|
||||||
|
- action: CHALLENGE
|
||||||
|
expression: path.startsWith("/admin")
|
||||||
|
name: robots-txt-policy-disallow-2
|
||||||
|
- action: DENY
|
||||||
|
expression:
|
||||||
|
any:
|
||||||
|
- userAgent.contains("BadBot")
|
||||||
|
- userAgent.contains("SpamBot")
|
||||||
|
- userAgent.contains("EvilBot")
|
||||||
|
name: robots-txt-policy-blacklist-3
|
||||||
|
- action: CHALLENGE
|
||||||
|
expression:
|
||||||
|
all:
|
||||||
|
- userAgent.contains("GoodBot")
|
||||||
|
- path.startsWith("/private")
|
||||||
|
name: robots-txt-policy-disallow-4
|
||||||
|
- action: WEIGH
|
||||||
|
expression:
|
||||||
|
any:
|
||||||
|
- userAgent.contains("SlowBot1")
|
||||||
|
- userAgent.contains("SlowBot2")
|
||||||
|
name: robots-txt-policy-crawl-delay-5
|
||||||
|
weight:
|
||||||
|
adjust: 3
|
||||||
|
- action: CHALLENGE
|
||||||
|
expression:
|
||||||
|
all:
|
||||||
|
- userAgent.contains("SearchBot1")
|
||||||
|
- path.startsWith("/search")
|
||||||
|
name: robots-txt-policy-disallow-7
|
||||||
|
- action: CHALLENGE
|
||||||
|
expression:
|
||||||
|
all:
|
||||||
|
- userAgent.contains("SearchBot2")
|
||||||
|
- path.startsWith("/search")
|
||||||
|
name: robots-txt-policy-disallow-8
|
||||||
|
- action: CHALLENGE
|
||||||
|
expression:
|
||||||
|
all:
|
||||||
|
- userAgent.contains("SearchBot3")
|
||||||
|
- path.startsWith("/search")
|
||||||
|
name: robots-txt-policy-disallow-9
|
8
cmd/robots2policy/testdata/simple.json
vendored
8
cmd/robots2policy/testdata/simple.json
vendored
@ -1,12 +1,12 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"action": "CHALLENGE",
|
|
||||||
"expression": "path.startsWith(\"/admin/\")",
|
"expression": "path.startsWith(\"/admin/\")",
|
||||||
"name": "robots-txt-policy-disallow-1"
|
"name": "robots-txt-policy-disallow-1",
|
||||||
|
"action": "CHALLENGE"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"action": "CHALLENGE",
|
|
||||||
"expression": "path.startsWith(\"/private\")",
|
"expression": "path.startsWith(\"/private\")",
|
||||||
"name": "robots-txt-policy-disallow-2"
|
"name": "robots-txt-policy-disallow-2",
|
||||||
|
"action": "CHALLENGE"
|
||||||
}
|
}
|
||||||
]
|
]
|
@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- The [Thoth client](https://anubis.techaro.lol/docs/admin/thoth) is now public in the repo instead of being an internal package.
|
- The [Thoth client](https://anubis.techaro.lol/docs/admin/thoth) is now public in the repo instead of being an internal package.
|
||||||
- [Custom-AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client)'s default User-Agent has an increased weight by default ([#852](https://github.com/TecharoHQ/anubis/issues/852)).
|
- [Custom-AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client)'s default User-Agent has an increased weight by default ([#852](https://github.com/TecharoHQ/anubis/issues/852)).
|
||||||
- The [`segments`](./admin/configuration/expressions.mdx#segments) function was added for splitting a path into its slash-separated segments.
|
- The [`segments`](./admin/configuration/expressions.mdx#segments) function was added for splitting a path into its slash-separated segments.
|
||||||
|
- Fixed `robots2policy` to properly group consecutive user agents into `any:` instead of only processing the last one
|
||||||
|
|
||||||
## v1.21.3: Minfilia Warde - Echo 3
|
## v1.21.3: Minfilia Warde - Echo 3
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user