1Panel/agent/middleware/certificate.go

31 lines
876 B
Go
Raw Normal View History

2024-07-31 18:29:41 +08:00
package middleware
import (
"errors"
"fmt"
"github.com/1Panel-dev/1Panel/agent/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/agent/constant"
2024-08-05 18:22:25 +08:00
"github.com/1Panel-dev/1Panel/agent/global"
2024-07-31 18:29:41 +08:00
"github.com/gin-gonic/gin"
)
func Certificate() gin.HandlerFunc {
return func(c *gin.Context) {
2024-08-05 18:22:25 +08:00
if global.CurrentNode == "127.0.0.1" || len(global.CurrentNode) == 0 {
c.Next()
return
}
2024-07-31 18:29:41 +08:00
if !c.Request.TLS.HandshakeComplete || len(c.Request.TLS.PeerCertificates) == 0 {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("no such tls peer certificates"))
return
}
cert := c.Request.TLS.PeerCertificates[0]
if cert.Subject.CommonName != "panel_client" {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, fmt.Errorf("err certificate"))
return
}
c.Next()
}
}