diff options
| author | Jason Cameron <git@jasoncameron.dev> | 2025-04-27 09:36:39 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-27 13:36:39 +0000 |
| commit | 301c7a42bde10cad814f9caa9f6320356734f499 (patch) | |
| tree | 5aaf8d8a79bc77662cf3abf6ad9a97e9f4bb5502 /lib/http.go | |
| parent | 755c18a9a76cf07e71f4c8e5f6cbc890411cf38f (diff) | |
| download | anubis-301c7a42bde10cad814f9caa9f6320356734f499.tar.xz anubis-301c7a42bde10cad814f9caa9f6320356734f499.zip | |
refactor(lib): Split up anubis.go into some smaller files. (#379)
* refactor(logging): centralize logger creation in GetLogger function
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
* refactor(logging): rename GetLogger to GetRequestLogger for clarity
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
* refactor: streamline error handling and response methods
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
* refactor(lib): Split anubis.go up into some smaller specialized methods
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
* refactor(http): simplify error response handling by using respondWithStatus
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
* chore(lib): run goimports
Signed-off-by: Xe Iaso <me@xeiaso.net>
---------
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
Signed-off-by: Xe Iaso <me@xeiaso.net>
Co-authored-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'lib/http.go')
| -rw-r--r-- | lib/http.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/lib/http.go b/lib/http.go index 2f32b6d..9e134b3 100644 --- a/lib/http.go +++ b/lib/http.go @@ -2,8 +2,14 @@ package lib import ( "net/http" + "slices" "time" + "github.com/TecharoHQ/anubis/internal" + "github.com/TecharoHQ/anubis/lib/policy" + "github.com/TecharoHQ/anubis/web" + "github.com/a-h/templ" + "github.com/TecharoHQ/anubis" ) @@ -33,3 +39,79 @@ func (t UnixRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { req.URL.Scheme = "http" // make http.Transport happy and avoid an infinite recursion return t.Transport.RoundTrip(req) } + +func (s *Server) RenderIndex(w http.ResponseWriter, r *http.Request, rule *policy.Bot, returnHTTPStatusOnly bool) { + if returnHTTPStatusOnly { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Authorization required")) + return + } + + lg := internal.GetRequestLogger(r) + + 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 { + lg.Error("failed to get OG tags", "err", err) + } + } + + component, err := web.BaseWithChallengeAndOGTags("Making sure you're not a bot!", web.Index(), challenge, rule.Challenge, ogTags) + if err != nil { + lg.Error("render failed, please open an issue", "err", err) // This is likely a bug in the template. Should never be triggered as CI tests for this. + s.respondWithError(w, r, "Internal Server Error: please contact the administrator and ask them to look for the logs around \"RenderIndex\"") + return + } + + handler := internal.NoStoreCache(templ.Handler(component)) + handler.ServeHTTP(w, r) +} + +func (s *Server) RenderBench(w http.ResponseWriter, r *http.Request) { + templ.Handler( + web.Base("Benchmarking Anubis!", web.Bench()), + ).ServeHTTP(w, r) +} + +func (s *Server) respondWithError(w http.ResponseWriter, r *http.Request, message string) { + s.respondWithStatus(w, r, message, http.StatusInternalServerError) +} + +func (s *Server) respondWithStatus(w http.ResponseWriter, r *http.Request, msg string, status int) { + templ.Handler(web.Base("Oh noes!", web.ErrorPage(msg, s.opts.WebmasterEmail)), templ.WithStatus(status)).ServeHTTP(w, r) +} + +func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + s.mux.ServeHTTP(w, r) +} + +func (s *Server) ServeHTTPNext(w http.ResponseWriter, r *http.Request) { + if s.next == nil { + redir := r.FormValue("redir") + urlParsed, err := r.URL.Parse(redir) + if err != nil { + s.respondWithStatus(w, r, "Redirect URL not parseable", http.StatusBadRequest) + return + } + + if (len(urlParsed.Host) > 0 && len(s.opts.RedirectDomains) != 0 && !slices.Contains(s.opts.RedirectDomains, urlParsed.Host)) || urlParsed.Host != r.URL.Host { + s.respondWithStatus(w, r, "Redirect domain not allowed", http.StatusBadRequest) + return + } + + if redir != "" { + http.Redirect(w, r, redir, http.StatusFound) + return + } + + templ.Handler( + web.Base("You are not a bot!", web.StaticHappy()), + ).ServeHTTP(w, r) + } else { + s.next.ServeHTTP(w, r) + } +} |
