diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index 48a74b9..fbddd58 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Embedded challenge data in initial HTML response to improve performance - Whitelisted [DuckDuckBot](https://duckduckgo.com/duckduckgo-help-pages/results/duckduckbot/) in botPolicies - Improvements to build scripts to make them less independent of the build host - Improved the OpenGraph error logging diff --git a/go.mod b/go.mod index 3063368..d936bfb 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/TecharoHQ/anubis -go 1.24.2 +go 1.24 require ( github.com/a-h/templ v0.3.857 diff --git a/lib/anubis.go b/lib/anubis.go index 6fd18a5..353fe63 100644 --- a/lib/anubis.go +++ b/lib/anubis.go @@ -263,21 +263,21 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) { if err != nil { lg.Debug("cookie not found", "path", r.URL.Path) s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } if err := ckie.Valid(); err != nil { lg.Debug("cookie is invalid", "err", err) s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } if time.Now().After(ckie.Expires) && !ckie.Expires.IsZero() { lg.Debug("cookie expired", "path", r.URL.Path) s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } @@ -288,7 +288,7 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) { if err != nil || !token.Valid { lg.Debug("invalid token", "path", r.URL.Path, "err", err) s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } @@ -303,7 +303,7 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) { if !ok { lg.Debug("invalid token claims type", "path", r.URL.Path) s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } challenge := s.challengeFor(r, rule.Challenge.Difficulty) @@ -311,7 +311,7 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) { if claims["challenge"] != challenge { lg.Debug("invalid challenge", "path", r.URL.Path) s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } @@ -328,7 +328,7 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) { lg.Debug("invalid response", "path", r.URL.Path) failedValidations.Inc() s.ClearCookie(w) - s.RenderIndex(w, r) + s.RenderIndex(w, r, cr, rule) return } @@ -337,21 +337,36 @@ func (s *Server) MaybeReverseProxy(w http.ResponseWriter, r *http.Request) { s.next.ServeHTTP(w, r) } -func (s *Server) RenderIndex(w http.ResponseWriter, r *http.Request) { +func (s *Server) RenderIndex(w http.ResponseWriter, r *http.Request, cr CheckResult, rule *policy.Bot) { + lg := slog.With( + "user_agent", r.UserAgent(), + "accept_language", r.Header.Get("Accept-Language"), + "priority", r.Header.Get("Priority"), + "x-forwarded-for", + r.Header.Get("X-Forwarded-For"), + "x-real-ip", r.Header.Get("X-Real-Ip"), + ) + + challenge := s.challengeFor(r, rule.Challenge.Difficulty) + var ogTags map[string]string = nil if s.opts.OGPassthrough { var err error ogTags, err = s.OGTags.GetOGTags(r.URL) if err != nil { - slog.Error("failed to get OG tags", "err", err) + lg.Error("failed to get OG tags", "err", err) ogTags = nil } } - handler := internal.NoStoreCache( - templ.Handler( - web.BaseWithOGTags("Making sure you're not a bot!", web.Index(), ogTags), - ), - ) + + component, err := web.BaseWithChallengeAndOGTags("Making sure you're not a bot!", web.Index(), challenge, rule.Challenge, ogTags) + if err != nil { + lg.Error("render failed", "err", err) + templ.Handler(web.Base("Oh noes!", web.ErrorPage("Other internal server error (contact the admin)", s.opts.WebmasterEmail)), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r) + return + } + + handler := internal.NoStoreCache(templ.Handler(component)) handler.ServeHTTP(w, r) } diff --git a/web/index.go b/web/index.go index 5d2957b..ac4cbda 100644 --- a/web/index.go +++ b/web/index.go @@ -2,14 +2,22 @@ package web import ( "github.com/a-h/templ" + + "github.com/TecharoHQ/anubis/lib/policy/config" ) func Base(title string, body templ.Component) templ.Component { - return base(title, body, nil) + return base(title, body, nil, nil) } -func BaseWithOGTags(title string, body templ.Component, ogTags map[string]string) templ.Component { - return base(title, body, ogTags) +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: challenge, + Rules: rules, + }, ogTags), nil } func Index() templ.Component { diff --git a/web/index.templ b/web/index.templ index 01d12b6..4eb1d45 100644 --- a/web/index.templ +++ b/web/index.templ @@ -5,7 +5,7 @@ import ( "github.com/TecharoHQ/anubis/xess" ) -templ base(title string, body templ.Component, ogTags map[string]string) { +templ base(title string, body templ.Component, challenge any, ogTags map[string]string) {
@@ -56,6 +56,9 @@ templ base(title string, body templ.Component, ogTags map[string]string) { } @templ.JSONScript("anubis_version", anubis.Version) + if challenge != nil { + @templ.JSONScript("anubis_challenge", challenge) + } diff --git a/web/index_templ.go b/web/index_templ.go index a3e10fe..7f501e3 100644 --- a/web/index_templ.go +++ b/web/index_templ.go @@ -13,7 +13,7 @@ import ( "github.com/TecharoHQ/anubis/xess" ) -func base(title string, body templ.Component, ogTags map[string]string) templ.Component { +func base(title string, body templ.Component, challenge any, ogTags map[string]string) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -104,6 +104,12 @@ func base(title string, body templ.Component, ogTags map[string]string) templ.Co if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } + if challenge != nil { + templ_7745c5c3_Err = templ.JSONScript("anubis_challenge", challenge).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "