refactor: 部分日志请求入参调整为json

This commit is contained in:
meilin.huang
2023-09-09 23:34:26 +08:00
parent 08c381fa60
commit 8e75e1f6ef
16 changed files with 98 additions and 88 deletions

View File

@@ -16,6 +16,7 @@ import (
"mayfly-go/pkg/otp"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/jsonx"
"strconv"
"time"
)
@@ -41,7 +42,7 @@ func (a *AccountLogin) Login(rc *req.Ctx) {
username := loginForm.Username
clientIp := getIpAndRegion(rc)
rc.ReqParam = fmt.Sprintf("username: %s | ip: %s", username, clientIp)
rc.ReqParam = jsonx.Kvs("username", username, "ip", clientIp)
originPwd, err := cryptox.DefaultRsaDecrypt(loginForm.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")

View File

@@ -14,6 +14,7 @@ import (
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/jsonx"
"strconv"
"strings"
"time"
@@ -48,7 +49,7 @@ func (a *LdapLogin) Login(rc *req.Ctx) {
username := loginForm.Username
clientIp := getIpAndRegion(rc)
rc.ReqParam = fmt.Sprintf("username: %s | ip: %s", username, clientIp)
rc.ReqParam = jsonx.Kvs("username", username, "ip", clientIp)
originPwd, err := cryptox.DefaultRsaDecrypt(loginForm.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")

View File

@@ -98,7 +98,7 @@ func (a *Oauth2Login) OAuth2Callback(rc *req.Ctx) {
account.Id = accountId
err = a.AccountApp.GetAccount(account, "username")
biz.ErrIsNilAppendErr(err, "该账号不存在")
rc.ReqParam = fmt.Sprintf("oauth2 bind username: %s", account.Username)
rc.ReqParam = jsonx.Kvs("username", account.Username, "type", "bind")
err = a.Oauth2App.GetOAuthAccount(&entity.Oauth2Account{
AccountId: accountId,
@@ -173,7 +173,7 @@ func (a *Oauth2Login) doLoginAction(rc *req.Ctx, userId string, oauth *config.Oa
biz.ErrIsNilAppendErr(err, "获取用户信息失败: %s")
clientIp := getIpAndRegion(rc)
rc.ReqParam = fmt.Sprintf("oauth2 login username: %s | ip: %s", account.Username, clientIp)
rc.ReqParam = jsonx.Kvs("username", account.Username, "ip", clientIp, "type", "login")
res := LastLoginCheck(account, config.GetAccountLoginSecurity(), clientIp)
res["action"] = "oauthLogin"

View File

@@ -15,7 +15,6 @@ import (
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/utils/structx"
"mayfly-go/pkg/ws"
"os"
"path"
@@ -78,13 +77,7 @@ func (m *Machine) SaveMachine(rc *req.Ctx) {
}
func (m *Machine) TestConn(rc *req.Ctx) {
g := rc.GinCtx
machineForm := new(form.MachineForm)
ginx.BindJsonAndValid(g, machineForm)
me := new(entity.Machine)
structx.Copy(me, machineForm)
me := ginx.BindJsonAndCopyTo(rc.GinCtx, new(form.MachineForm), new(entity.Machine))
m.MachineApp.TestConn(me)
}
@@ -194,7 +187,7 @@ func (m *Machine) WsSSH(g *gin.Context) {
// 记录系统操作日志
rc.WithLog(req.NewLogSave("机器-终端操作"))
rc.ReqParam = cli.GetMachine().GetLogDesc()
rc.ReqParam = cli.GetMachine()
req.LogHandler(rc)
mts.Start()

View File

@@ -68,12 +68,16 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) {
path := form.Path
mi := m.MachineFileApp.GetMachine(fid)
attrs := jsonx.Kvs("machine", mi, "path", path)
rc.ReqParam = attrs
if form.Type == dir {
attrs["type"] = "目录"
m.MachineFileApp.MkDir(fid, form.Path)
rc.ReqParam = fmt.Sprintf("%s -> 创建目录: %s", mi.GetLogDesc(), path)
} else {
attrs["type"] = "文件"
m.MachineFileApp.CreateFile(fid, form.Path)
rc.ReqParam = fmt.Sprintf("%s -> 创建文件: %s", mi.GetLogDesc(), path)
}
}
@@ -87,23 +91,23 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
defer sftpFile.Close()
fileInfo, _ := sftpFile.Stat()
filesize := fileInfo.Size()
// 如果是读取文件内容,则校验文件大小
if readType != "1" {
biz.IsTrue(fileInfo.Size() < max_read_size, "文件超过1m请使用下载查看")
biz.IsTrue(filesize < max_read_size, "文件超过1m请使用下载查看")
}
mi := m.MachineFileApp.GetMachine(fid)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", readPath, "filesize", filesize)
// 如果读取类型为下载,则下载文件,否则获取文件内容
if readType == "1" {
// 截取文件名,如/usr/local/test.java -》 test.java
path := strings.Split(readPath, "/")
rc.Download(sftpFile, path[len(path)-1])
rc.ReqParam = fmt.Sprintf("%s -> 下载文件: %s", mi.GetLogDesc(), readPath)
} else {
datas, err := io.ReadAll(sftpFile)
biz.ErrIsNilAppendErr(err, "读取文件内容失败: %s")
rc.ResData = string(datas)
rc.ReqParam = fmt.Sprintf("%s -> 查看文件: %s", mi.GetLogDesc(), readPath)
}
}
@@ -157,10 +161,10 @@ func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
ginx.BindJsonAndValid(g, form)
path := form.Path
m.MachineFileApp.WriteFileContent(fid, path, []byte(form.Content))
mi := m.MachineFileApp.GetMachine(fid)
rc.ReqParam = fmt.Sprintf("%s -> 修改文件内容: %s", mi.GetLogDesc(), path)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", path)
m.MachineFileApp.WriteFileContent(fid, path, []byte(form.Content))
}
const MaxUploadFileSize int64 = 1024 * 1024 * 1024
@@ -175,10 +179,10 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) {
biz.IsTrue(fileheader.Size <= MaxUploadFileSize, "文件大小不能超过%d字节", MaxUploadFileSize)
file, _ := fileheader.Open()
rc.ReqParam = fmt.Sprintf("path: %s", path)
defer file.Close()
mi := m.MachineFileApp.GetMachine(fid)
rc.ReqParam = fmt.Sprintf("%s -> 上传文件: %s/%s", mi.GetLogDesc(), path, fileheader.Filename)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename))
la := rc.LoginAccount
defer func() {
@@ -191,7 +195,6 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) {
}
}()
defer file.Close()
m.MachineFileApp.UploadFile(fid, path, fileheader.Filename, file)
// 保存消息并发送文件上传成功通知
m.MsgApp.CreateAndSend(la, ws.SuccessMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至 %s[%s:%s]", fileheader.Filename, mi.Name, mi.Ip, path)))
@@ -222,7 +225,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
folderName := filepath.Dir(paths[0])
mi := m.MachineFileApp.GetMachine(fid)
rc.ReqParam = fmt.Sprintf("%s -> 上传文件夹: %s/%s", mi.GetLogDesc(), basePath, folderName)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", basePath, folderName))
folderFiles := make([]FolderFile, len(paths))
// 先创建目录并将其包装为folderFile结构
@@ -288,10 +291,10 @@ func (m *MachineFile) RemoveFile(rc *req.Ctx) {
rmForm := new(form.MachineFileOpForm)
ginx.BindJsonAndValid(g, rmForm)
m.MachineFileApp.RemoveFile(fid, rmForm.Path...)
mi := m.MachineFileApp.GetMachine(fid)
rc.ReqParam = fmt.Sprintf("%s -> 删除文件: %s", mi.GetLogDesc(), strings.Join(rmForm.Path, " | "))
rc.ReqParam = jsonx.Kvs("machine", mi, "path", rmForm.Path)
m.MachineFileApp.RemoveFile(fid, rmForm.Path...)
}
func (m *MachineFile) CopyFile(rc *req.Ctx) {
@@ -301,8 +304,7 @@ func (m *MachineFile) CopyFile(rc *req.Ctx) {
cpForm := new(form.MachineFileOpForm)
ginx.BindJsonAndValid(g, cpForm)
mi := m.MachineFileApp.Copy(fid, cpForm.ToPath, cpForm.Path...)
rc.ReqParam = fmt.Sprintf("%s -> 拷贝文件: %s -> %s", mi.GetLogDesc(), strings.Join(cpForm.Path, " | "), cpForm.ToPath)
rc.ReqParam = jsonx.Kvs("machine", mi, "cp", cpForm)
}
func (m *MachineFile) MvFile(rc *req.Ctx) {
@@ -312,8 +314,7 @@ func (m *MachineFile) MvFile(rc *req.Ctx) {
cpForm := new(form.MachineFileOpForm)
ginx.BindJsonAndValid(g, cpForm)
mi := m.MachineFileApp.Mv(fid, cpForm.ToPath, cpForm.Path...)
rc.ReqParam = fmt.Sprintf("%s -> 移动文件: %s -> %s", mi.GetLogDesc(), strings.Join(cpForm.Path, " | "), cpForm.ToPath)
rc.ReqParam = jsonx.Kvs("machine", mi, "mv", cpForm)
}
func (m *MachineFile) Rename(rc *req.Ctx) {
@@ -325,7 +326,7 @@ func (m *MachineFile) Rename(rc *req.Ctx) {
biz.ErrIsNilAppendErr(m.MachineFileApp.Rename(fid, rename.Oldname, rename.Newname), "文件重命名失败: %s")
mi := m.MachineFileApp.GetMachine(fid)
rc.ReqParam = fmt.Sprintf("%s -> 文件重命名: %s", mi.GetLogDesc(), jsonx.ToStr(rename))
rc.ReqParam = jsonx.Kvs("machine", mi, "rename", rename)
}
func getFileType(fm fs.FileMode) string {

View File

@@ -1,7 +1,6 @@
package api
import (
"fmt"
"mayfly-go/internal/machine/api/form"
"mayfly-go/internal/machine/api/vo"
"mayfly-go/internal/machine/application"
@@ -71,7 +70,7 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
res, err := cli.Run(script)
// 记录请求参数
rc.ReqParam = fmt.Sprintf("[machine: %s, scriptId: %d, name: %s]", cli.GetMachine().GetLogDesc(), scriptId, ms.Name)
rc.ReqParam = jsonx.Kvs("machine", cli.GetMachine(), "scriptId", scriptId, "name", ms.Name)
if res == "" {
biz.ErrIsNilAppendErr(err, "执行命令失败:%s")
}

View File

@@ -18,20 +18,20 @@ import (
// 机器信息
type Info struct {
Id uint64
Id uint64 `json:"id"`
Name string `json:"name"`
Ip string `json:"ip"` // IP地址
Port int `json:"port"` // 端口号
Ip string `json:"ip"` // IP地址
Port int `json:"-"` // 端口号
AuthMethod int8 `json:"authMethod"` // 授权认证方式
Username string `json:"username"` // 用户名
AuthMethod int8 `json:"-"` // 授权认证方式
Username string `json:"-"` // 用户名
Password string `json:"-"`
Passphrase string `json:"-"` // 私钥口令
Status int8 `json:"status"` // 状态 1:启用2:停用
SshTunnelMachineId int `json:"sshTunnelMachineId"` // ssh隧道机器id
EnableRecorder int8 `json:"enableRecorder"` // 是否启用终端回放记录
TagId uint64 `json:"tagId"`
Status int8 `json:"-"` // 状态 1:启用2:停用
SshTunnelMachineId int `json:"-"` // ssh隧道机器id
EnableRecorder int8 `json:"-"` // 是否启用终端回放记录
TagId uint64 `json:"-"`
TagPath string `json:"tagPath"`
}

View File

@@ -27,7 +27,7 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
req.NewGet(":machineId/files/:fileId/read", mf.ReadFileContent).Log(req.NewLogSave("机器-获取文件内容")),
req.NewGet(":machineId/files/:fileId/read-dir", mf.GetDirEntry).Log(req.NewLogSave("机器-获取目录")),
req.NewGet(":machineId/files/:fileId/read-dir", mf.GetDirEntry),
req.NewGet(":machineId/files/:fileId/dir-size", mf.GetDirSize),

View File

@@ -93,11 +93,7 @@ func (m *Mongo) RunCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(rc.GinCtx, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(rc.GinCtx))
rc.ReqParam = jsonx.ToStr(map[string]any{
"info": inst.Info.GetLogDesc(),
"req": commandForm,
})
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
// 顺序执行
commands := bson.D{}
@@ -160,10 +156,7 @@ func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(g, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(g))
rc.ReqParam = jsonx.ToStr(map[string]any{
"info": inst.Info.GetLogDesc(),
"req": commandForm,
})
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
// 解析docId文档id如果为string类型则使用ObjectId解析解析失败则为普通字符串
docId := commandForm.DocId
@@ -187,10 +180,7 @@ func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(g, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(g))
rc.ReqParam = jsonx.ToStr(map[string]any{
"info": inst.Info.GetLogDesc(),
"req": commandForm,
})
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
// 解析docId文档id如果为string类型则使用ObjectId解析解析失败则为普通字符串
docId := commandForm.DocId
@@ -213,10 +203,7 @@ func (m *Mongo) InsertOneCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(g, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(g))
rc.ReqParam = jsonx.ToStr(map[string]any{
"info": inst.Info.GetLogDesc(),
"req": commandForm,
})
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
res, err := inst.Cli.Database(commandForm.Database).Collection(commandForm.Collection).InsertOne(context.TODO(), commandForm.Doc)
biz.ErrIsNilAppendErr(err, "命令执行失败: %s")

View File

@@ -142,10 +142,10 @@ func DeleteMongoCache(mongoId uint64) {
}
type MongoInfo struct {
Id uint64
Name string
TagPath string
SshTunnelMachineId int // ssh隧道机器id
Id uint64 `json:"id"`
Name string `json:"name"`
TagPath string `json:"tagPath"`
SshTunnelMachineId int `json:"-"` // ssh隧道机器id
}
func (m *MongoInfo) GetLogDesc() string {

View File

@@ -2,11 +2,11 @@ package api
import (
"context"
"fmt"
"mayfly-go/internal/redis/api/form"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"time"
)
@@ -35,7 +35,7 @@ func (r *Redis) Hdel(rc *req.Ctx) {
ri, key := r.checkKeyAndGetRedisIns(rc)
field := rc.GinCtx.Query("field")
rc.ReqParam = fmt.Sprintf("key=%s, field=%s", key, field)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "key", key, "field", field)
delRes, err := ri.GetCmdable().HDel(context.TODO(), key, field).Result()
biz.ErrIsNilAppendErr(err, "hdel err: %s")
rc.ResData = delRes
@@ -54,10 +54,12 @@ func (r *Redis) Hset(rc *req.Ctx) {
g := rc.GinCtx
hashValue := new(form.HashValue)
ginx.BindJsonAndValid(g, hashValue)
rc.ReqParam = hashValue
hv := hashValue.Value[0]
res, err := r.getRedisIns(rc).GetCmdable().HSet(context.TODO(), hashValue.Key, hv["field"].(string), hv["value"]).Result()
ri := r.getRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "hash", hv)
res, err := ri.GetCmdable().HSet(context.TODO(), hashValue.Key, hv["field"].(string), hv["value"]).Result()
biz.ErrIsNilAppendErr(err, "hset失败: %s")
rc.ResData = res
}
@@ -68,6 +70,7 @@ func (r *Redis) SetHashValue(rc *req.Ctx) {
ginx.BindJsonAndValid(g, hashValue)
ri := r.getRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "hash", hashValue)
cmd := ri.GetCmdable()
key := hashValue.Key

View File

@@ -2,13 +2,13 @@ package api
import (
"context"
"fmt"
"mayfly-go/internal/redis/api/form"
"mayfly-go/internal/redis/api/vo"
"mayfly-go/internal/redis/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"strings"
"sync"
"time"
@@ -132,7 +132,7 @@ func (r *Redis) TtlKey(rc *req.Ctx) {
func (r *Redis) DeleteKey(rc *req.Ctx) {
ri, key := r.checkKeyAndGetRedisIns(rc)
rc.ReqParam = fmt.Sprintf("%s -> 删除key: %s", ri.Info.GetLogDesc(), key)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "key", key)
ri.GetCmdable().Del(context.Background(), key)
}
@@ -141,7 +141,7 @@ func (r *Redis) RenameKey(rc *req.Ctx) {
ginx.BindJsonAndValid(rc.GinCtx, form)
ri := r.getRedisIns(rc)
rc.ReqParam = fmt.Sprintf("%s -> 重命名key[%s] -> [%s]", ri.Info.GetLogDesc(), form.Key, form.NewKey)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "rename", form)
ri.GetCmdable().Rename(context.Background(), form.Key, form.NewKey)
}
@@ -150,20 +150,20 @@ func (r *Redis) ExpireKey(rc *req.Ctx) {
ginx.BindJsonAndValid(rc.GinCtx, form)
ri := r.getRedisIns(rc)
rc.ReqParam = fmt.Sprintf("%s -> 重置key[%s]过期时间为%d", ri.Info.GetLogDesc(), form.Key, form.Seconds)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "expire", form)
ri.GetCmdable().Expire(context.Background(), form.Key, time.Duration(form.Seconds)*time.Second)
}
// 移除过期时间
func (r *Redis) PersistKey(rc *req.Ctx) {
ri, key := r.checkKeyAndGetRedisIns(rc)
rc.ReqParam = fmt.Sprintf("%s -> 移除key[%s]的过期时间", ri.Info.GetLogDesc(), key)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "key", key)
ri.GetCmdable().Persist(context.Background(), key)
}
// 清空库
func (r *Redis) FlushDb(rc *req.Ctx) {
ri := r.getRedisIns(rc)
rc.ReqParam = fmt.Sprintf("%s -> flushdb", ri.Info.GetLogDesc())
rc.ReqParam = ri.Info
ri.GetCmdable().FlushDB(context.Background())
}

View File

@@ -2,12 +2,11 @@ package api
import (
"context"
"fmt"
"mayfly-go/internal/redis/api/form"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/utils/jsonx"
"time"
)
@@ -25,7 +24,7 @@ func (r *Redis) SetStringValue(rc *req.Ctx) {
ri := r.getRedisIns(rc)
cmd := ri.GetCmdable()
rc.ReqParam = fmt.Sprintf("%s -> %s", ri.Info.GetLogDesc(), stringx.AnyToStr(keyValue))
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "string", keyValue)
str, err := cmd.Set(context.TODO(), keyValue.Key, keyValue.Value, time.Second*time.Duration(keyValue.Timed)).Result()
biz.ErrIsNilAppendErr(err, "保存字符串值失败: %s")

View File

@@ -304,14 +304,14 @@ func TestRedisConnection(re *entity.Redis) {
}
type RedisInfo struct {
Id uint64
Host string
Db int // 库号
TagPath string
Mode string
Name string
Id uint64 `json:"id"`
Host string `json:"host"`
Db int `json:"db"` // 库号
TagPath string `json:"tagPath"`
Mode string `json:"-"`
Name string `json:"-"`
SshTunnelMachineId int
SshTunnelMachineId int `json:"-"`
}
// 获取记录日志的描述

View File

@@ -3,6 +3,7 @@ package jsonx
import (
"encoding/json"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/stringx"
"strings"
"github.com/buger/jsonparser"
@@ -33,6 +34,31 @@ func ToStr(val any) string {
}
}
// 将偶数个元素转为对应的map并转为json
//
// 偶数索引为key奇数为value
func AnysToStr(elements ...any) string {
return ToStr(Kvs(elements...))
}
// 将偶数个元素转为对应的map[string]any
//
// 偶数索引为json key奇数为value
func Kvs(elements ...any) map[string]any {
myMap := make(map[string]any)
for i := 0; i < len(elements); i += 2 {
key := stringx.AnyToStr(elements[i])
if i+1 < len(elements) {
value := elements[i+1]
myMap[key] = value
} else {
myMap[key] = nil
}
}
return myMap
}
// 根据json字节数组获取对应字段路径的string类型值
//
// @param fieldPath字段路径。如user.username等

View File

@@ -126,6 +126,8 @@ func AnyToStr(value any) string {
}
switch it := value.(type) {
case string:
return it
case float64:
return strconv.FormatFloat(it, 'f', -1, 64)
case float32:
@@ -150,8 +152,6 @@ func AnyToStr(value any) string {
return strconv.FormatInt(it, 10)
case uint64:
return strconv.FormatUint(it, 10)
case string:
return it
case []byte:
return string(value.([]byte))
default: