2021-12-05 14:09:21 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-12-05 14:45:22 +00:00
|
|
|
"net/http"
|
2022-11-12 19:37:20 +00:00
|
|
|
"strings"
|
2021-12-05 14:45:22 +00:00
|
|
|
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
2022-11-12 19:37:20 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/context"
|
2021-12-05 14:45:22 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/utils"
|
2021-12-05 14:09:21 +00:00
|
|
|
)
|
|
|
|
|
2022-11-12 19:37:20 +00:00
|
|
|
func SetupHTTPACMEChallengeServer(challengeCache cache.SetGetKey) http.HandlerFunc {
|
|
|
|
challengePath := "/.well-known/acme-challenge/"
|
2021-12-05 14:45:22 +00:00
|
|
|
|
2022-11-12 19:37:20 +00:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := context.New(w, req)
|
|
|
|
if strings.HasPrefix(ctx.Path(), challengePath) {
|
2022-11-15 15:15:11 +00:00
|
|
|
challenge, ok := challengeCache.Get(utils.TrimHostPort(ctx.Host()) + "/" + strings.TrimPrefix(ctx.Path(), challengePath))
|
2022-11-12 19:37:20 +00:00
|
|
|
if !ok || challenge == nil {
|
|
|
|
ctx.String("no challenge for this token", http.StatusNotFound)
|
2021-12-05 14:45:22 +00:00
|
|
|
}
|
2022-11-12 19:37:20 +00:00
|
|
|
ctx.String(challenge.(string))
|
|
|
|
} else {
|
2022-11-15 15:15:11 +00:00
|
|
|
ctx.Redirect("https://"+ctx.Host()+ctx.Path(), http.StatusMovedPermanently)
|
2022-11-12 19:37:20 +00:00
|
|
|
}
|
2021-12-05 14:45:22 +00:00
|
|
|
}
|
2021-12-05 14:09:21 +00:00
|
|
|
}
|