| 193 | } |
| 194 | |
| 195 | func (s *Server) RenderIndex(w http.ResponseWriter, r *http.Request, cr policy.CheckResult, rule *policy.Bot, returnHTTPStatusOnly bool) { |
| 196 | localizer := localization.GetLocalizer(r) |
| 197 | |
| 198 | if returnHTTPStatusOnly { |
| 199 | if s.opts.PublicUrl == "" { |
| 200 | w.WriteHeader(http.StatusUnauthorized) |
| 201 | w.Write([]byte(localizer.T("authorization_required"))) |
| 202 | } else { |
| 203 | redirectURL, err := s.constructRedirectURL(r) |
| 204 | if err != nil { |
| 205 | s.respondWithStatus(w, r, err.Error(), "", http.StatusBadRequest) |
| 206 | return |
| 207 | } |
| 208 | http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect) |
| 209 | } |
| 210 | return |
| 211 | } |
| 212 | |
| 213 | lg := internal.GetRequestLogger(s.logger, r) |
| 214 | |
| 215 | if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && randomChance(64) { |
| 216 | lg.Error("client was given a challenge but does not in fact support gzip compression") |
| 217 | s.respondWithError(w, r, localizer.T("client_error_browser"), "") |
| 218 | return |
| 219 | } |
| 220 | |
| 221 | challengesIssued.WithLabelValues("embedded").Add(1) |
| 222 | chall, err := s.issueChallenge(r.Context(), r, lg, cr, rule) |
| 223 | if err != nil { |
| 224 | lg.Error("can't get challenge", "err", err) |
| 225 | s.ClearCookie(w, CookieOpts{Name: anubis.TestCookieName, Host: r.Host}) |
| 226 | s.respondWithError(w, r, fmt.Sprintf("%s: %s", localizer.T("internal_server_error"), rule.Challenge.Algorithm), makeCode(err)) |
| 227 | return |
| 228 | } |
| 229 | |
| 230 | lg = lg.With("challenge", chall.ID) |
| 231 | |
| 232 | var ogTags map[string]string = nil |
| 233 | if s.opts.OpenGraph.Enabled { |
| 234 | var err error |
| 235 | ogTags, err = s.OGTags.GetOGTags(r.Context(), r.URL, r.Host) |
| 236 | if err != nil { |
| 237 | lg.Error("failed to get OG tags", "err", err) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | s.SetCookie(w, CookieOpts{ |
| 242 | Value: chall.ID, |
| 243 | Host: r.Host, |
| 244 | Path: "/", |
| 245 | Name: anubis.TestCookieName, |
| 246 | Expiry: 30 * time.Minute, |
| 247 | }) |
| 248 | |
| 249 | impl, ok := challenge.Get(chall.Method) |
| 250 | if !ok { |
| 251 | lg.Error("check failed", "err", "can't get algorithm", "algorithm", rule.Challenge.Algorithm) |
| 252 | s.ClearCookie(w, CookieOpts{Name: anubis.TestCookieName, Host: r.Host}) |