38 lines
971 B
Go
38 lines
971 B
Go
package decoder
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
base64DecodePaths = map[string]bool{}
|
|
)
|
|
|
|
func Base64Decoder(logger *zap.SugaredLogger) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
requestPath := c.Request.URL.Path
|
|
if _, ok := base64DecodePaths[requestPath]; ok {
|
|
body := c.Request.Body
|
|
if bodyByte, err := io.ReadAll(body); err == nil {
|
|
strBody := strings.Trim(string(bodyByte), "\"")
|
|
if decodedBodyByte, err := base64.StdEncoding.DecodeString(strBody); err == nil {
|
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(decodedBodyByte))
|
|
} else {
|
|
logger.Errorf("base64decoder uri:%s DecodeString request body err:%s", c.Request.RequestURI, err.Error())
|
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyByte))
|
|
}
|
|
} else {
|
|
logger.Errorf("base64decoder uri:%s read request body err:%s", c.Request.RequestURI, err.Error())
|
|
}
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|