2022-08-16 23:30:23 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-04-11 23:52:28 +08:00
|
|
|
"compress/gzip"
|
2022-08-23 15:21:08 +08:00
|
|
|
"encoding/json"
|
2022-12-13 18:54:28 +08:00
|
|
|
"fmt"
|
2023-04-07 11:30:10 +08:00
|
|
|
"io"
|
2022-08-16 23:30:23 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/service"
|
2022-12-13 18:54:28 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-01-05 11:57:03 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/copier"
|
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/docs"
|
2022-08-16 23:30:23 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2022-12-13 18:54:28 +08:00
|
|
|
func OperationLog() gin.HandlerFunc {
|
2022-08-16 23:30:23 +08:00
|
|
|
return func(c *gin.Context) {
|
2022-12-13 18:54:28 +08:00
|
|
|
if strings.Contains(c.Request.URL.Path, "search") || c.Request.Method == http.MethodGet {
|
2022-08-16 23:30:23 +08:00
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:48:32 +08:00
|
|
|
source := loadLogInfo(c.Request.URL.Path)
|
2022-08-16 23:30:23 +08:00
|
|
|
record := model.OperationLog{
|
2023-02-07 18:48:32 +08:00
|
|
|
Source: source,
|
2022-08-16 23:30:23 +08:00
|
|
|
IP: c.ClientIP(),
|
2023-01-05 11:57:03 +08:00
|
|
|
Method: strings.ToLower(c.Request.Method),
|
|
|
|
Path: strings.ReplaceAll(c.Request.URL.Path, "/api/v1", ""),
|
2022-08-16 23:30:23 +08:00
|
|
|
UserAgent: c.Request.UserAgent(),
|
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
var (
|
2023-01-05 11:57:03 +08:00
|
|
|
swagger swaggerJson
|
|
|
|
operationDic operationJson
|
2022-12-13 18:54:28 +08:00
|
|
|
)
|
2023-01-05 11:57:03 +08:00
|
|
|
if err := json.Unmarshal(docs.SwaggerJson, &swagger); err != nil {
|
2022-12-13 18:54:28 +08:00
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
2023-01-05 11:57:03 +08:00
|
|
|
path, hasPath := swagger.Paths[record.Path]
|
|
|
|
if !hasPath {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
methodMap, isMethodMap := path.(map[string]interface{})
|
|
|
|
if !isMethodMap {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataMap, hasPost := methodMap["post"]
|
|
|
|
if !hasPost {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, isDataMap := dataMap.(map[string]interface{})
|
|
|
|
if !isDataMap {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
xlog, hasXlog := data["x-panel-log"]
|
|
|
|
if !hasXlog {
|
|
|
|
c.Next()
|
|
|
|
return
|
2022-12-13 18:54:28 +08:00
|
|
|
}
|
2023-01-05 11:57:03 +08:00
|
|
|
if err := copier.Copy(&operationDic, xlog); err != nil {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(operationDic.FormatZH) == 0 {
|
2022-12-13 18:54:28 +08:00
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
formatMap := make(map[string]interface{})
|
|
|
|
if len(operationDic.BodyKeys) != 0 {
|
2023-04-07 11:30:10 +08:00
|
|
|
body, err := io.ReadAll(c.Request.Body)
|
2022-12-13 18:54:28 +08:00
|
|
|
if err == nil {
|
2023-04-07 11:30:10 +08:00
|
|
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
2022-12-13 18:54:28 +08:00
|
|
|
}
|
|
|
|
bodyMap := make(map[string]interface{})
|
|
|
|
_ = json.Unmarshal(body, &bodyMap)
|
|
|
|
for _, key := range operationDic.BodyKeys {
|
|
|
|
if _, ok := bodyMap[key]; ok {
|
|
|
|
formatMap[key] = bodyMap[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(operationDic.BeforeFuntions) != 0 {
|
|
|
|
for _, funcs := range operationDic.BeforeFuntions {
|
|
|
|
for key, value := range formatMap {
|
2022-12-26 14:47:08 +08:00
|
|
|
if funcs.InputValue == key {
|
2022-12-13 18:54:28 +08:00
|
|
|
var names []string
|
|
|
|
if funcs.IsList {
|
2023-05-30 15:30:57 +08:00
|
|
|
sql := fmt.Sprintf("SELECT %s FROM %s where %s in (?);", funcs.OutputColumn, funcs.DB, funcs.InputColumn)
|
2022-12-26 14:47:08 +08:00
|
|
|
_ = global.DB.Raw(sql, value).Scan(&names)
|
2022-12-13 18:54:28 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
_ = global.DB.Raw(fmt.Sprintf("select %s from %s where %s = ?;", funcs.OutputColumn, funcs.DB, funcs.InputColumn), value).Scan(&names)
|
2022-12-13 18:54:28 +08:00
|
|
|
}
|
2022-12-26 14:47:08 +08:00
|
|
|
formatMap[funcs.OutputValue] = strings.Join(names, ",")
|
2022-12-13 18:54:28 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for key, value := range formatMap {
|
2022-12-24 13:31:30 +08:00
|
|
|
if strings.Contains(operationDic.FormatEN, "["+key+"]") {
|
|
|
|
if arrys, ok := value.([]string); ok {
|
|
|
|
operationDic.FormatZH = strings.ReplaceAll(operationDic.FormatZH, "["+key+"]", fmt.Sprintf("[%v]", strings.Join(arrys, ",")))
|
|
|
|
operationDic.FormatEN = strings.ReplaceAll(operationDic.FormatEN, "["+key+"]", fmt.Sprintf("[%v]", strings.Join(arrys, ",")))
|
|
|
|
} else {
|
|
|
|
operationDic.FormatZH = strings.ReplaceAll(operationDic.FormatZH, "["+key+"]", fmt.Sprintf("[%v]", value))
|
|
|
|
operationDic.FormatEN = strings.ReplaceAll(operationDic.FormatEN, "["+key+"]", fmt.Sprintf("[%v]", value))
|
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-27 16:45:59 +08:00
|
|
|
record.DetailEN = strings.ReplaceAll(operationDic.FormatEN, "[]", "")
|
|
|
|
record.DetailZH = strings.ReplaceAll(operationDic.FormatZH, "[]", "")
|
2022-08-16 23:30:23 +08:00
|
|
|
|
|
|
|
writer := responseBodyWriter{
|
|
|
|
ResponseWriter: c.Writer,
|
|
|
|
body: &bytes.Buffer{},
|
|
|
|
}
|
|
|
|
c.Writer = writer
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
2023-06-05 18:35:25 +08:00
|
|
|
datas := writer.body.Bytes()
|
|
|
|
if c.Request.Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
buf := bytes.NewReader(writer.body.Bytes())
|
|
|
|
reader, err := gzip.NewReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
record.Status = constant.StatusFailed
|
|
|
|
record.Message = fmt.Sprintf("gzip new reader failed, err: %v", err)
|
|
|
|
latency := time.Since(now)
|
|
|
|
record.Latency = latency
|
2023-04-11 23:52:28 +08:00
|
|
|
|
2023-06-05 18:35:25 +08:00
|
|
|
if err := service.NewILogService().CreateOperationLog(record); err != nil {
|
|
|
|
global.LOG.Errorf("create operation record failed, err: %v", err)
|
|
|
|
}
|
|
|
|
return
|
2023-04-11 23:52:28 +08:00
|
|
|
}
|
2023-06-05 18:35:25 +08:00
|
|
|
defer reader.Close()
|
|
|
|
datas, _ = io.ReadAll(reader)
|
2023-04-11 23:52:28 +08:00
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
var res response
|
2023-04-11 23:52:28 +08:00
|
|
|
_ = json.Unmarshal(datas, &res)
|
2022-12-13 18:54:28 +08:00
|
|
|
if res.Code == 200 {
|
|
|
|
record.Status = constant.StatusSuccess
|
|
|
|
} else {
|
|
|
|
record.Status = constant.StatusFailed
|
|
|
|
record.Message = res.Message
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
latency := time.Since(now)
|
|
|
|
record.Latency = latency
|
|
|
|
|
2022-11-15 17:20:57 +08:00
|
|
|
if err := service.NewILogService().CreateOperationLog(record); err != nil {
|
2022-08-16 23:30:23 +08:00
|
|
|
global.LOG.Errorf("create operation record failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:57:03 +08:00
|
|
|
type swaggerJson struct {
|
|
|
|
Paths map[string]interface{} `json:"paths"`
|
|
|
|
}
|
|
|
|
|
2022-12-13 18:54:28 +08:00
|
|
|
type operationJson struct {
|
|
|
|
API string `json:"api"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
BodyKeys []string `json:"bodyKeys"`
|
|
|
|
ParamKeys []string `json:"paramKeys"`
|
|
|
|
BeforeFuntions []functionInfo `json:"beforeFuntions"`
|
|
|
|
FormatZH string `json:"formatZH"`
|
|
|
|
FormatEN string `json:"formatEN"`
|
|
|
|
}
|
|
|
|
type functionInfo struct {
|
2023-05-30 15:30:57 +08:00
|
|
|
InputColumn string `json:"input_column"`
|
2022-12-26 14:47:08 +08:00
|
|
|
InputValue string `json:"input_value"`
|
|
|
|
IsList bool `json:"isList"`
|
|
|
|
DB string `json:"db"`
|
2023-05-30 15:30:57 +08:00
|
|
|
OutputColumn string `json:"output_column"`
|
2022-12-26 14:47:08 +08:00
|
|
|
OutputValue string `json:"output_value"`
|
2022-12-13 18:54:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
type responseBodyWriter struct {
|
|
|
|
gin.ResponseWriter
|
|
|
|
body *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r responseBodyWriter) Write(b []byte) (int, error) {
|
|
|
|
r.body.Write(b)
|
|
|
|
return r.ResponseWriter.Write(b)
|
|
|
|
}
|
|
|
|
|
2022-12-13 18:54:28 +08:00
|
|
|
func loadLogInfo(path string) string {
|
2022-08-16 23:30:23 +08:00
|
|
|
path = strings.ReplaceAll(path, "/api/v1", "")
|
|
|
|
if !strings.Contains(path, "/") {
|
2022-12-13 18:54:28 +08:00
|
|
|
return ""
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
pathArrys := strings.Split(path, "/")
|
|
|
|
if len(pathArrys) < 2 {
|
2022-12-13 18:54:28 +08:00
|
|
|
return ""
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
return pathArrys[1]
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|