36 lines
1.1 KiB
Go
36 lines
1.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// AcmeChallengeController 处理 Let's Encrypt 的 HTTP-01 验证回访。
|
|
//
|
|
// 路由:GET /.well-known/acme-challenge/:token
|
|
//
|
|
// 该路由在 routers/router.go 中无条件注册,不受 APP_MODE 影响——证书颁发机构
|
|
// 什么时候来验证与本服务以哪种模式启动无关。Nginx 扑底站点必须把这个路径按
|
|
// 明文 HTTP 反代过来,不能 301 跳转到 HTTPS(首次签发时还没有证书)。
|
|
type AcmeChallengeController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// Serve 返回 token 对应的 keyAuthorization(纯文本)
|
|
func (c *AcmeChallengeController) Serve() {
|
|
token := strings.TrimSpace(c.Ctx.Input.Param(":token"))
|
|
keyAuth, ok := services.GetHTTP01Challenge(token)
|
|
if !ok {
|
|
c.Ctx.Output.SetStatus(404)
|
|
_ = c.Ctx.Output.Body([]byte("not found"))
|
|
return
|
|
}
|
|
c.Ctx.Output.Header("Content-Type", "text/plain; charset=utf-8")
|
|
// challenge 是一次性的,不能被任何中间层缓存
|
|
c.Ctx.Output.Header("Cache-Control", "no-store")
|
|
_ = c.Ctx.Output.Body([]byte(keyAuth))
|
|
}
|