From 529f65674e1a8c737c67802ac0e2ea3ca69fb807 Mon Sep 17 00:00:00 2001 From: Jason Cameron Date: Fri, 9 May 2025 12:54:15 -0400 Subject: [PATCH] style: apply structpack & goimport (#469) * refactor: reorder import statements in fetch.go and fetch_test.go Signed-off-by: Jason Cameron * fix: optimize struct field alignment to reduce memory usage Signed-off-by: Jason Cameron --------- Signed-off-by: Jason Cameron --- internal/ogtags/cache_test.go | 8 +++--- internal/ogtags/fetch.go | 3 ++- internal/ogtags/fetch_test.go | 3 ++- internal/ogtags/integration_test.go | 2 +- internal/ogtags/ogtags.go | 8 +++--- internal/ogtags/parse_test.go | 2 +- internal/test/playwright_test.go | 9 ++++--- internal/xff_test.go | 4 +-- lib/anubis.go | 10 ++++---- lib/config.go | 29 ++++++++++------------ lib/policy/bot.go | 4 +-- lib/policy/celchecker.go | 2 +- lib/policy/checker_test.go | 10 ++++---- lib/policy/config/config.go | 11 ++++---- lib/policy/config/config_test.go | 4 +-- lib/policy/config/expressionorlist_test.go | 4 +-- lib/policy/expressions/join_test.go | 6 ++--- web/index.go | 2 +- 18 files changed, 60 insertions(+), 61 deletions(-) diff --git a/internal/ogtags/cache_test.go b/internal/ogtags/cache_test.go index fbacf22..bce98d9 100644 --- a/internal/ogtags/cache_test.go +++ b/internal/ogtags/cache_test.go @@ -158,12 +158,12 @@ func TestGetOGTagsWithHostConsideration(t *testing.T) { } testCases := []struct { - name string - ogCacheConsiderHost bool - requests []struct { + name string + requests []struct { host string - expectedLoadCount int // Expected load count *after* this request + expectedLoadCount int } + ogCacheConsiderHost bool // Expected load count *after* this request }{ { name: "Host Not Considered - Same Host", diff --git a/internal/ogtags/fetch.go b/internal/ogtags/fetch.go index 312e040..bd8276e 100644 --- a/internal/ogtags/fetch.go +++ b/internal/ogtags/fetch.go @@ -4,12 +4,13 @@ import ( "context" "errors" "fmt" - "golang.org/x/net/html" "io" "log/slog" "mime" "net" "net/http" + + "golang.org/x/net/html" ) var ( diff --git a/internal/ogtags/fetch_test.go b/internal/ogtags/fetch_test.go index 7462287..7a73697 100644 --- a/internal/ogtags/fetch_test.go +++ b/internal/ogtags/fetch_test.go @@ -2,7 +2,6 @@ package ogtags import ( "fmt" - "golang.org/x/net/html" "io" "net/http" "net/http/httptest" @@ -10,6 +9,8 @@ import ( "strings" "testing" "time" + + "golang.org/x/net/html" ) func TestFetchHTMLDocument(t *testing.T) { diff --git a/internal/ogtags/integration_test.go b/internal/ogtags/integration_test.go index 42bf97a..ab9f74a 100644 --- a/internal/ogtags/integration_test.go +++ b/internal/ogtags/integration_test.go @@ -56,10 +56,10 @@ func TestIntegrationGetOGTags(t *testing.T) { // Test with different configurations testCases := []struct { + expectedTags map[string]string name string path string query string - expectedTags map[string]string expectError bool }{ { diff --git a/internal/ogtags/ogtags.go b/internal/ogtags/ogtags.go index c88d280..0301742 100644 --- a/internal/ogtags/ogtags.go +++ b/internal/ogtags/ogtags.go @@ -20,12 +20,12 @@ const ( type OGTagCache struct { cache *decaymap.Impl[string, map[string]string] targetURL *url.URL - ogCacheConsiderHost bool - ogPassthrough bool - ogTimeToLive time.Duration + client *http.Client approvedTags []string approvedPrefixes []string - client *http.Client + ogTimeToLive time.Duration + ogCacheConsiderHost bool + ogPassthrough bool } func NewOGTagCache(target string, ogPassthrough bool, ogTimeToLive time.Duration, ogTagsConsiderHost bool) *OGTagCache { diff --git a/internal/ogtags/parse_test.go b/internal/ogtags/parse_test.go index e25a211..ce8c700 100644 --- a/internal/ogtags/parse_test.go +++ b/internal/ogtags/parse_test.go @@ -18,9 +18,9 @@ func TestExtractOGTags(t *testing.T) { testCache.approvedPrefixes = []string{"og:"} tests := []struct { + expected map[string]string name string htmlStr string - expected map[string]string }{ { name: "Basic OG tags", // Includes standard 'description' meta tag diff --git a/internal/test/playwright_test.go b/internal/test/playwright_test.go index ce94c7b..916a363 100644 --- a/internal/test/playwright_test.go +++ b/internal/test/playwright_test.go @@ -104,10 +104,11 @@ const ( type action string type testCase struct { - name string - action action - isHard bool - realIP, userAgent string + name string + action action + realIP string + userAgent string + isHard bool } func doesNPXExist(t *testing.T) { diff --git a/internal/xff_test.go b/internal/xff_test.go index 3a890ab..8eeea8e 100644 --- a/internal/xff_test.go +++ b/internal/xff_test.go @@ -65,12 +65,12 @@ func TestXForwardedForUpdateAddToChain(t *testing.T) { func TestComputeXFFHeader(t *testing.T) { for _, tt := range []struct { + err error name string remoteAddr string origXFFHeader string - pref XFFComputePreferences result string - err error + pref XFFComputePreferences }{ { name: "StripPrivate", diff --git a/lib/anubis.go b/lib/anubis.go index 9f177b2..039cf7d 100644 --- a/lib/anubis.go +++ b/lib/anubis.go @@ -58,14 +58,14 @@ var ( ) type Server struct { - mux *http.ServeMux next http.Handler - priv ed25519.PrivateKey - pub ed25519.PublicKey + mux *http.ServeMux policy *policy.ParsedConfig - opts Options DNSBLCache *decaymap.Impl[string, dnsbl.DroneBLResponse] OGTags *ogtags.OGTagCache + priv ed25519.PrivateKey + pub ed25519.PublicKey + opts Options } func (s *Server) challengeFor(r *http.Request, difficulty int) string { @@ -232,8 +232,8 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) { challenge := s.challengeFor(r, rule.Challenge.Difficulty) err = encoder.Encode(struct { - Challenge string `json:"challenge"` Rules *config.ChallengeRules `json:"rules"` + Challenge string `json:"challenge"` }{ Challenge: challenge, Rules: rule.Challenge, diff --git a/lib/config.go b/lib/config.go index 739e718..a07f603 100644 --- a/lib/config.go +++ b/lib/config.go @@ -23,24 +23,21 @@ import ( ) type Options struct { - Next http.Handler - Policy *policy.ParsedConfig - RedirectDomains []string - ServeRobotsTXT bool - PrivateKey ed25519.PrivateKey - - CookieExpiration time.Duration - CookieDomain string - CookieName string - CookiePartitioned bool - - OGPassthrough bool + Next http.Handler + Policy *policy.ParsedConfig + Target string + CookieDomain string + CookieName string + BasePrefix string + WebmasterEmail string + RedirectDomains []string + PrivateKey ed25519.PrivateKey + CookieExpiration time.Duration OGTimeToLive time.Duration OGCacheConsidersHost bool - Target string - - WebmasterEmail string - BasePrefix string + OGPassthrough bool + CookiePartitioned bool + ServeRobotsTXT bool } func LoadPoliciesOrDefault(fname string, defaultDifficulty int) (*policy.ParsedConfig, error) { diff --git a/lib/policy/bot.go b/lib/policy/bot.go index 3a43655..3e7a63a 100644 --- a/lib/policy/bot.go +++ b/lib/policy/bot.go @@ -8,10 +8,10 @@ import ( ) type Bot struct { + Rules Checker + Challenge *config.ChallengeRules Name string Action config.Rule - Challenge *config.ChallengeRules - Rules Checker } func (b Bot) Hash() string { diff --git a/lib/policy/celchecker.go b/lib/policy/celchecker.go index 82b71c6..1269fca 100644 --- a/lib/policy/celchecker.go +++ b/lib/policy/celchecker.go @@ -12,8 +12,8 @@ import ( ) type CELChecker struct { - src string program cel.Program + src string } func NewCELChecker(cfg *config.ExpressionOrList) (*CELChecker, error) { diff --git a/lib/policy/checker_test.go b/lib/policy/checker_test.go index 6739509..6109bab 100644 --- a/lib/policy/checker_test.go +++ b/lib/policy/checker_test.go @@ -8,11 +8,11 @@ import ( func TestRemoteAddrChecker(t *testing.T) { for _, tt := range []struct { - name string - cidrs []string - ip string - ok bool err error + name string + ip string + cidrs []string + ok bool }{ { name: "match_ipv4", @@ -86,13 +86,13 @@ func TestRemoteAddrChecker(t *testing.T) { func TestHeaderMatchesChecker(t *testing.T) { for _, tt := range []struct { + err error name string header string rexStr string reqHeaderKey string reqHeaderValue string ok bool - err error }{ { name: "match", diff --git a/lib/policy/config/config.go b/lib/policy/config/config.go index 8a49667..4b6f643 100644 --- a/lib/policy/config/config.go +++ b/lib/policy/config/config.go @@ -51,15 +51,14 @@ const ( ) type BotConfig struct { - Name string `json:"name"` UserAgentRegex *string `json:"user_agent_regex"` PathRegex *string `json:"path_regex"` HeadersRegex map[string]string `json:"headers_regex"` - RemoteAddr []string `json:"remote_addresses"` Expression *ExpressionOrList `json:"expression"` - - Action Rule `json:"action"` - Challenge *ChallengeRules `json:"challenge,omitempty"` + Challenge *ChallengeRules `json:"challenge,omitempty"` + Name string `json:"name"` + Action Rule `json:"action"` + RemoteAddr []string `json:"remote_addresses"` } func (b BotConfig) Zero() bool { @@ -171,9 +170,9 @@ func (b BotConfig) Valid() error { } type ChallengeRules struct { + Algorithm Algorithm `json:"algorithm"` Difficulty int `json:"difficulty"` ReportAs int `json:"report_as"` - Algorithm Algorithm `json:"algorithm"` } var ( diff --git a/lib/policy/config/config_test.go b/lib/policy/config/config_test.go index 86c490e..afc1ab8 100644 --- a/lib/policy/config/config_test.go +++ b/lib/policy/config/config_test.go @@ -15,9 +15,9 @@ func p[V any](v V) *V { return &v } func TestBotValid(t *testing.T) { var tests = []struct { + err error name string bot BotConfig - err error }{ { name: "simple user agent", @@ -239,9 +239,9 @@ func TestConfigValidKnownGood(t *testing.T) { func TestImportStatement(t *testing.T) { type testCase struct { + err error name string importPath string - err error } var tests []testCase diff --git a/lib/policy/config/expressionorlist_test.go b/lib/policy/config/expressionorlist_test.go index ea4319e..880effe 100644 --- a/lib/policy/config/expressionorlist_test.go +++ b/lib/policy/config/expressionorlist_test.go @@ -8,11 +8,11 @@ import ( func TestExpressionOrListUnmarshal(t *testing.T) { for _, tt := range []struct { - name string - inp string err error validErr error result *ExpressionOrList + name string + inp string }{ { name: "simple", diff --git a/lib/policy/expressions/join_test.go b/lib/policy/expressions/join_test.go index 5eff9bd..be393c6 100644 --- a/lib/policy/expressions/join_test.go +++ b/lib/policy/expressions/join_test.go @@ -14,11 +14,11 @@ func TestJoin(t *testing.T) { } for _, tt := range []struct { - name string - clauses []string - op JoinOperator err error + name string + op JoinOperator resultStr string + clauses []string }{ { name: "no-clauses", diff --git a/web/index.go b/web/index.go index ac4cbda..409dc59 100644 --- a/web/index.go +++ b/web/index.go @@ -12,8 +12,8 @@ func Base(title string, body templ.Component) templ.Component { func BaseWithChallengeAndOGTags(title string, body templ.Component, challenge string, rules *config.ChallengeRules, ogTags map[string]string) (templ.Component, error) { return base(title, body, struct { - Challenge string `json:"challenge"` Rules *config.ChallengeRules `json:"rules"` + Challenge string `json:"challenge"` }{ Challenge: challenge, Rules: rules,