Implement HTTP-01 challenge

This commit is contained in:
Moritz Marquardt 2021-11-20 21:10:46 +01:00
parent 73c21d0195
commit c99dbb34ce
No known key found for this signature in database
GPG key ID: D5788327BEE388B6
3 changed files with 40 additions and 2 deletions

21
main.go
View file

@ -22,6 +22,7 @@ import (
"fmt"
"log"
"net"
"net/http"
"os"
"time"
@ -103,6 +104,26 @@ func main() {
listener = tls.NewListener(listener, tlsConfig)
setupCertificates()
if os.Getenv("ENABLE_HTTP_SERVER") == "true" {
go (func() {
challengePath := []byte("/.well-known/acme-challenge/")
err := fasthttp.ListenAndServe("[::]:80", func(ctx *fasthttp.RequestCtx) {
if bytes.HasPrefix(ctx.Path(), challengePath) {
challenge, ok := challengeCache.Get(string(TrimHostPort(ctx.Host())) + "/" + string(bytes.TrimPrefix(ctx.Path(), challengePath)))
if !ok {
ctx.SetStatusCode(http.StatusNotFound)
ctx.SetBodyString("no challenge for this token")
}
ctx.SetBodyString(challenge.(string))
} else {
ctx.Redirect("https://" + string(ctx.Host()) + string(ctx.RequestURI()), http.StatusMovedPermanently)
}
})
if err != nil {
log.Fatalf("Couldn't start HTTP server: %s", err)
}
})()
}
// Start the web server
err = server.Serve(listener)