simplify middleware

This commit is contained in:
w-mj 2023-03-14 19:49:24 +08:00
parent 5954615bea
commit 8ec0e3eb05
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
2 changed files with 54 additions and 53 deletions

103
main.go
View File

@ -2,12 +2,10 @@ package main
import ( import (
"crypto/aes" "crypto/aes"
"crypto/cipher"
"crypto/sha1" "crypto/sha1"
"embed" "embed"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -46,31 +44,32 @@ func main() {
r := gin.Default() r := gin.Default()
// r.Use(cors.Default()) // r.Use(cors.Default())
r.Use(Cors()) r.Use(Cors())
r.Use(m.JWTAuthMiddleware)
fp, _ := fs.Sub(f, "static") fp, _ := fs.Sub(f, "static")
r.StaticFS("/static", http.FS(fp)) r.StaticFS("/static", http.FS(fp))
r.GET("/index", m.JWTAuthMiddleware, func(c *gin.Context) { //r.GET("/index", func(c *gin.Context) {
c.FileFromFS("/template/app_view.html", http.FS(f)) // c.FileFromFS("/template/app_view.html", http.FS(f))
}) //})
r.GET("/apps", m.JWTAuthMiddleware, controller.GetAppsController) r.GET("/apps", controller.GetAppsController)
r.POST("/app", m.JWTAuthMiddleware, controller.CreateUpdateDeleteApp) r.POST("/app", controller.CreateUpdateDeleteApp)
r.PUT("/app", m.JWTAuthMiddleware, controller.CreateUpdateDeleteApp) r.PUT("/app", controller.CreateUpdateDeleteApp)
r.DELETE("/app", m.JWTAuthMiddleware, controller.CreateUpdateDeleteApp) r.DELETE("/app", controller.CreateUpdateDeleteApp)
r.POST("/dep", m.JWTAuthMiddleware, controller.CreateDeleteDeployment) r.POST("/dep", controller.CreateDeleteDeployment)
r.DELETE("/dep", m.JWTAuthMiddleware, controller.CreateDeleteDeployment) r.DELETE("/dep", controller.CreateDeleteDeployment)
r.GET("/pod", m.JWTAuthMiddleware, controller.GetPodStatus) r.GET("/pod", controller.GetPodStatus)
r.GET("/login", LoginToken) r.GET("/login", LoginToken)
r.Any("/test_token", TestToken) r.Any("/test_token", TestToken)
r.GET("/username", m.JWTAuthMiddleware, controller.GetUsername) r.GET("/username", controller.GetUsername)
r.GET("/files/:app", m.JWTAuthMiddleware, controller.GetFiles) r.GET("/files/:app", controller.GetFiles)
r.GET("/file/:app/:filename", m.JWTAuthMiddleware, controller.GetPostOrDeleteFile) r.GET("/file/:app/:filename", controller.GetPostOrDeleteFile)
r.POST("/file/:app/:filename", m.JWTAuthMiddleware, controller.GetPostOrDeleteFile) r.POST("/file/:app/:filename", controller.GetPostOrDeleteFile)
r.DELETE("/file/:app/:filename", m.JWTAuthMiddleware, controller.GetPostOrDeleteFile) r.DELETE("/file/:app/:filename", controller.GetPostOrDeleteFile)
err := r.Run(":8000") err := r.Run(":8000")
if err != nil { if err != nil {
@ -109,16 +108,17 @@ func ParseCGToken(token string) (string, error) {
return "", errors.New("jwt parse error") return "", errors.New("jwt parse error")
} }
func generateKey(key []byte) (genKey []byte) { //func generateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 16) // genKey = make([]byte, 16)
copy(genKey, key) // copy(genKey, key)
for i := 16; i < len(key); { // for i := 16; i < len(key); {
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 { // for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i] // genKey[j] ^= key[i]
} // }
} // }
return genKey // return genKey
} //}
func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) { func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
sh := sha1.New() sh := sha1.New()
sh.Write(key) sh.Write(key)
@ -141,34 +141,31 @@ func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
return decrypted[:trim] return decrypted[:trim]
} }
func AesDecrypt(data []byte, key []byte) ([]byte, error) { //func AesDecrypt(data []byte, key []byte) ([]byte, error) {
sh := sha1.New() // sh := sha1.New()
sh.Write(key) // sh.Write(key)
realKey := sh.Sum(nil) // realKey := sh.Sum(nil)
//
fmt.Println(string(realKey)) // fmt.Println(string(realKey))
block, err := aes.NewCipher(realKey[:16]) // block, err := aes.NewCipher(realKey[:16])
if err != nil { // if err != nil {
log.WithError(err).Error("AesDecrypt 1") // log.WithError(err).Error("AesDecrypt 1")
return nil, err // return nil, err
} // }
blockSize := block.BlockSize() // blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
crypted := make([]byte, len(data)) // crypted := make([]byte, len(data))
blockMode.CryptBlocks(crypted, data) // blockMode.CryptBlocks(crypted, data)
if err != nil { // if err != nil {
log.WithError(err).Error("AesDecrypt 2") // log.WithError(err).Error("AesDecrypt 2")
return nil, err // return nil, err
} // }
return crypted, nil // return crypted, nil
} //}
func LoginToken(c *gin.Context) { func LoginToken(c *gin.Context) {
username := c.Request.URL.Query().Get("username") //username := c.Request.URL.Query().Get("username")
var err error username, err := ParseCGToken(c.Request.URL.Query().Get("cgtoken"))
if username == "" {
username, err = ParseCGToken(c.Request.URL.Query().Get("cgtoken"))
}
if username == "" || err != nil { if username == "" || err != nil {
c.JSON(http.StatusNotFound, gin.H{ c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound, "code": http.StatusNotFound,

View File

@ -44,6 +44,10 @@ func ParseToken(tokenString string) (*MyClaims, error) {
} }
func JWTAuthMiddleware(c *gin.Context) { func JWTAuthMiddleware(c *gin.Context) {
if c.Request.URL.Query().Get("cgtoken") != "" {
// 登录过程,不验证身份
return
}
authHeader := c.Request.Header.Get("Authorization") authHeader := c.Request.Header.Get("Authorization")
if authHeader == "" { if authHeader == "" {
var err error var err error