From 2ed783c1a61158f4cc542acd0f972c5c02c71594 Mon Sep 17 00:00:00 2001 From: Jason Cameron Date: Fri, 13 Jun 2025 16:29:15 -0400 Subject: [PATCH] refactor: use preexisting structs in config --- cmd/robots2policy/main.go | 20 ++++++-------------- lib/policy/config/weight.go | 2 +- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/cmd/robots2policy/main.go b/cmd/robots2policy/main.go index 03e93263..3710cb3f 100644 --- a/cmd/robots2policy/main.go +++ b/cmd/robots2policy/main.go @@ -12,6 +12,8 @@ import ( "regexp" "strings" + "github.com/TecharoHQ/anubis/lib/policy/config" + "sigs.k8s.io/yaml" ) @@ -34,20 +36,10 @@ type RobotsRule struct { IsBlacklist bool // true if this is a specifically denied user agent } -type Weight struct { - Adjust int `yaml:"adjust" json:"adjust"` -} - -type Challenge struct { - Algorithm string `yaml:"algorithm,omitempty" json:"algorithm,omitempty"` - Difficulty int `yaml:"difficulty,omitempty" json:"difficulty,omitempty"` - ReportAs int `yaml:"report_as,omitempty" json:"report_as,omitempty"` -} - type AnubisRule struct { Expression map[string]interface{} `yaml:"expression,omitempty" json:"expression,omitempty"` - Challenge *Challenge `yaml:"challenge,omitempty" json:"challenge,omitempty"` - Weight *Weight `yaml:"weight,omitempty" json:"weight,omitempty"` + Challenge *config.ChallengeRules `yaml:"challenge,omitempty" json:"challenge,omitempty"` + Weight *config.Weight `yaml:"weight,omitempty" json:"weight,omitempty"` Name string `yaml:"name" json:"name"` Action string `yaml:"action" json:"action"` } @@ -249,7 +241,7 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule { rule := AnubisRule{ Name: fmt.Sprintf("%s-crawl-delay-%d", *policyName, ruleCounter), Action: "WEIGH", - Weight: &Weight{Adjust: *crawlDelay}, + Weight: &config.Weight{Adjust: *crawlDelay}, } if userAgent == "*" { @@ -277,7 +269,7 @@ func convertToAnubisRules(robotsRules []RobotsRule) []AnubisRule { // This would block everything - convert to a weight adjustment instead rule.Name = fmt.Sprintf("%s-global-restriction-%d", *policyName, ruleCounter) rule.Action = "WEIGH" - rule.Weight = &Weight{Adjust: 20} // Increase difficulty significantly + rule.Weight = &config.Weight{Adjust: 20} // Increase difficulty significantly rule.Expression = map[string]interface{}{ "single": "true", // Always applies } diff --git a/lib/policy/config/weight.go b/lib/policy/config/weight.go index f408111b..aa8348fa 100644 --- a/lib/policy/config/weight.go +++ b/lib/policy/config/weight.go @@ -1,5 +1,5 @@ package config type Weight struct { - Adjust int `json:"adjust"` + Adjust int `json:"adjust" yaml:"adjust"` }