Add resource-load challenge

This commit is contained in:
WeebDataHoarder
2025-04-02 11:41:29 +02:00
parent c05202c2c5
commit b6537eaf50
4 changed files with 40 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/yl2chen/cidranger"
"html/template"
"io"
"io/fs"
"log/slog"
@@ -281,6 +282,27 @@ func NewState(p policy.Policy, settings StateSettings) (state *State, err error)
_ = state.challengePage(w, http.StatusTeapot, "", nil)
return ChallengeResultStop
}
case "resource-load":
c.Challenge = func(w http.ResponseWriter, r *http.Request, key []byte, expiry time.Time) ChallengeResult {
redirectUri := new(url.URL)
redirectUri.Path = c.Path + "/verify-challenge"
values := make(url.Values)
values.Set("result", hex.EncodeToString(key))
redirectUri.RawQuery = values.Encode()
// self redirect!
w.Header().Set("Refresh", "2; url="+r.URL.String())
_ = state.challengePage(w, http.StatusTeapot, "", map[string]any{
"Tags": []template.HTML{
template.HTML(fmt.Sprintf("<link href=\"%s\" rel=\"stylesheet\" crossorigin=\"use-credentials\">", redirectUri.String())),
},
})
return ChallengeResultStop
}
case "js":