From fab208c1215d1e251e4ab3d9a18855a1852f7aee Mon Sep 17 00:00:00 2001 From: ayflying Date: Fri, 28 Feb 2025 14:21:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=97=A5=E5=BF=97=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/admin/v1/log.go | 12 ++ cmd/middleware.go | 261 ++++++++++++++++++++++++++ internal/logic/logic.go | 1 + internal/logic/systemLog/systemLog.go | 66 +++++++ service/system_log.go | 34 ++++ 5 files changed, 374 insertions(+) create mode 100644 api/admin/v1/log.go create mode 100644 cmd/middleware.go create mode 100644 internal/logic/systemLog/systemLog.go create mode 100644 service/system_log.go diff --git a/api/admin/v1/log.go b/api/admin/v1/log.go new file mode 100644 index 0000000..043cbcb --- /dev/null +++ b/api/admin/v1/log.go @@ -0,0 +1,12 @@ +package v1 + +import ( + "github.com/ayflying/utility_go/internal/model/entity" + "github.com/gogf/gf/v2/frame/g" +) + +type SystemLog struct { + entity.SystemLog + Data g.Map `json:"data" dc:"操作数据"` + //Post g.Map `json:"post" dc:"提交数据"` +} diff --git a/cmd/middleware.go b/cmd/middleware.go new file mode 100644 index 0000000..b22ebb5 --- /dev/null +++ b/cmd/middleware.go @@ -0,0 +1,261 @@ +package cmd + +import ( + "github.com/ayflying/utility_go/service" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" + "github.com/gogf/gf/v2/text/gstr" +) + +//func MiddlewareAnonymous(r *ghttp.Request) { +// // 中间件处理逻辑 +// r.Response.CORSDefault() +// +// ip := r.GetClientIp() +// r.SetCtxVar("ip", ip) +// +// //各种回调的日志返回 +// //get, _ := r.GetJson() +// get := r.GetRequestMapStrStr() +// delete(get, "r") +// delete(get, "s") +// delete(get, "t") +// delete(get, "data") +// getJson, _ := gjson.EncodeString(get) +// g.Log("cmd").Debugf(r.GetCtx(), "from|%v|%v|%v", 0, r.RequestURI, getJson) +// +// r.Middleware.Next() +// +// //中间件后置 +// err := r.GetError() +// if err != nil { +// code, err2 := strconv.Atoi(err.Error()) +// if err2 != nil { +// return +// } +// if _, ok := consts.ErrCodeList[code]; ok { +// //g.Dump("===error:", gerror.Code(r.GetError()).Code()) +// msg := g.Map{ +// "code": code, +// "message": consts.ErrCodeList[code], +// //"data": r.GetHandlerResponse(), +// } +// r.Response.WriteJson(msg) +// //错误码置空 +// r.SetError(nil) +// //g.Log("cmd").Debugf(r.GetCtx(), "to|%v|%v|%v", uid, r.RequestURI, msg) +// return +// } +// } +// +// //回复 +// res, _ := gjson.EncodeString(r.GetHandlerResponse()) +// g.Log("cmd").Debugf(r.GetCtx(), "to|%v|%v|%v", 0, r.RequestURI, res) +//} + +func MiddlewareAdmin(r *ghttp.Request) { + // 中间件处理逻辑 + r.Response.CORSDefault() + + ip := r.GetClientIp() + r.SetCtxVar("ip", ip) + + get := r.Cookie.Get("uid") + + if get == nil { + //调试模式允许不验证用户名 + debug, _ := g.Cfg().GetWithEnv(nil, "debug") + if !debug.Bool() { + + msg := g.Map{ + "code": 403, + "message": "登录失败", + //"data": r.GetHandlerResponse(), + } + //r.SetError(http.Error(r,"403",http.StatusForbidden)) + r.Response.WriteJson(msg) + gerror.NewCode(gcode.CodeNil, "登录失败") + return + } + + } + + uid := get.Int() + + r.Middleware.Next() + + //后置,所有post都写入日志 + if r.Method == "POST" { + //黑名单列表 + LogUrl := []string{ + "/system/chatgpt", + } + if !gstr.InArray(LogUrl, r.RequestURI) { + //写入日志 + service.SystemLog().AddLog(uid, r.RequestURI, ip, r.GetFormMap()) + } + } else { + //需要写入的get + LogUrl := []string{ + "/admin/config/mall/del", + "/admin/config/shop/del", + "/admin/group/del", + "/admin/user/del", + "/admin/community/posts/del", + "/admin/community/posts/limit", + "/admin/community/posts/limit/del", + "/admin/community/reply/del", + "/admin/community/recommend", + } + for _, item := range LogUrl { + if item == r.RequestURI { + service.SystemLog().AddLog(uid, r.RequestURI, ip, r.GetFormMap()) + } + } + } + +} + +//// 中间件 +//func Middleware(r *ghttp.Request) { +// // 中间件处理逻辑 +// r.Response.CORSDefault() +// +// //获取玩家的guid +// guid := r.Header.Get("guid") +// +// //获取所有请求的信息 +// get := r.GetRequestMapStrStr() +// +// //cacheKey := fmt.Sprintf("sign:%s", guid) +// +// ////进入debug模式 +// //debugBool := g.Cfg().MustGetWithCmd(nil, "debug") +// ////如果收到签名,开始验证签名 +// //if sign, _ := get["s"]; !debugBool.Bool() && sign != "" { +// // +// // //如果连续两次使用相同sign,直接抛出 +// // getSign, _ := aycache.New().Get(nil, cacheKey) +// // if getSign.String() == sign { +// // //中间件授权错误 +// // msg := g.Map{ +// // "code": 11000, +// // "message": consts.ErrCodeList[11000], +// // } +// // r.Response.WriteJson(msg) +// // return +// // } +// // aycache.New().Set(nil, cacheKey, sign, time.Minute*10) +// // +// // secretKey := "asdkjqwhiasdoplmwofjk/aws" +// // nonce := get["r"] +// // timestamp := get["t"] +// // +// // message := timestamp + nonce +// // +// // timeUnix := time.Now().Unix() - gconv.Int64(timestamp) +// // if timeUnix > 600 || timeUnix < -600 { +// // //中间件授权错误 +// // msg := g.Map{ +// // "code": 11000, +// // "message": consts.ErrCodeList[11000], +// // } +// // r.Response.WriteJson(msg) +// // return +// // } +// // +// // // 创建 HMAC 对象 +// // h := hmac.New(sha256.New, []byte(secretKey)) +// // +// // // 更新 HMAC 对象的数据 +// // h.Write([]byte(message)) +// // +// // // 获取 HMAC 的十六进制表示 +// // signature := hex.EncodeToString(h.Sum(nil)) +// // +// // //如果加密算法不一致 +// // if signature != sign { +// // //中间件授权错误 +// // msg := g.Map{ +// // "code": 11000, +// // "message": consts.ErrCodeList[11000], +// // } +// // r.Response.WriteJson(msg) +// // return +// // } +// //} +// +// uid, _ := service.MemberUser().Guid2uid(guid) +// if uid == 0 { +// //中间件授权错误 +// msg := g.Map{ +// "code": 11000, +// "message": consts.ErrCodeList[11000], +// //"data": r.GetHandlerResponse(), +// } +// r.Response.WriteJson(msg) +// +// return +// } +// r.SetCtxVar("guid", guid) +// r.SetCtxVar("uid", uid) +// +// ip := r.GetClientIp() +// r.SetCtxVar("ip", ip) +// +// delete(get, "r") +// delete(get, "s") +// delete(get, "t") +// delete(get, "data") +// //前置输出服务器收到信息 +// getJson, _ := gjson.EncodeString(get) +// //后置输出服务器返回信息 +// if r.GetCtxVar("not_log").IsEmpty() { +// g.Log("cmd").Debugf(r.GetCtx(), "from|%v|%v|%v", uid, r.RequestURI, getJson) +// } +// +// //运行开始时间 +// RunStartTime := gtime.Now() +// +// //proto.Marshal() +// +// //中间件核心 +// r.Middleware.Next() +// +// //返回运行时 +// if getTime := gtime.Now().Sub(RunStartTime); getTime > time.Millisecond*1000 { +// g.Log().Debugf(nil, "当前运行时间:%v,uid=%d,url=%s", getTime, uid, r.RequestURI) +// } +// +// //中间件后置 +// err := r.GetError() +// if err != nil { +// code, err2 := strconv.Atoi(err.Error()) +// if err2 != nil { +// return +// } +// if _, ok := consts.ErrCodeList[code]; ok { +// //g.Dump("===error:", gerror.Code(r.GetError()).Code()) +// msg := g.Map{ +// "code": code, +// "message": consts.ErrCodeList[code], +// //"data": r.GetHandlerResponse(), +// } +// msgJson, _ := gjson.EncodeString(msg) +// r.Response.WriteJson(msgJson) +// //错误码置空 +// r.SetError(nil) +// g.Log("cmd").Debugf(r.GetCtx(), "to|%v|%v|%v", uid, r.RequestURI, msg) +// return +// } +// } +// +// //后置输出服务器返回信息 +// if r.GetCtxVar("not_log").IsEmpty() { +// res, _ := gjson.EncodeString(r.GetHandlerResponse()) +// g.Log("cmd").Debugf(r.GetCtx(), "to|%v|%v|%v", uid, r.RequestURI, res) +// } +// +//} diff --git a/internal/logic/logic.go b/internal/logic/logic.go index face655..63ff1ad 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -7,4 +7,5 @@ package logic import ( _ "github.com/ayflying/utility_go/internal/logic/gameAct" _ "github.com/ayflying/utility_go/internal/logic/systemCron" + _ "github.com/ayflying/utility_go/internal/logic/systemLog" ) diff --git a/internal/logic/systemLog/systemLog.go b/internal/logic/systemLog/systemLog.go new file mode 100644 index 0000000..1a83f32 --- /dev/null +++ b/internal/logic/systemLog/systemLog.go @@ -0,0 +1,66 @@ +package systemLog + +import ( + "context" + v1 "github.com/ayflying/utility_go/api/admin/v1" + "github.com/ayflying/utility_go/service" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gctx" +) + +type sSystemLog struct { + ctx context.Context +} + +var ( + ctx = gctx.New() + //db = dao.AdminSystemLog.Ctx(ctx) + name = "system_log" +) + +func init() { + service.RegisterSystemLog(New()) +} + +func New() *sSystemLog { + return &sSystemLog{} +} + +func (s *sSystemLog) List(page int) (list []*v1.SystemLog, max int, err error) { + + //var list = []*AdminSystemLog{} + max, _ = g.Model(name).Count() + g.Model(name).OrderDesc("created_at").Page(page, 100).Scan(&list) + return +} + +// 写入操作日志 +func (s *sSystemLog) AddLog(uid int, url string, ip string, data g.Map) (id int64, err error) { + //跳过空日志 + if data == nil { + return + } + //如果存在这些值,直接跳过不写入日志 + paichu := []string{ + "/api/install", + "/activity/url/log/add", + "/system/update", + "/api/cdkey", + } + + for _, item := range paichu { + if item == url { + return + } + } + + var post v1.SystemLog + //uid := g.RequestFromCtx(ctx).Header.Get("x-uid") + post.Uid = uid + post.Url = url + post.Ip = ip + post.Data = data + + id, err = g.Model(name).InsertAndGetId(post) + return +} diff --git a/service/system_log.go b/service/system_log.go new file mode 100644 index 0000000..02103c4 --- /dev/null +++ b/service/system_log.go @@ -0,0 +1,34 @@ +// ================================================================================ +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// You can delete these comments if you wish manually maintain this interface file. +// ================================================================================ + +package service + +import ( + v1 "github.com/ayflying/utility_go/api/admin/v1" + "github.com/gogf/gf/v2/frame/g" +) + +type ( + ISystemLog interface { + List(page int) (list []*v1.SystemLog, max int, err error) + // 写入操作日志 + AddLog(uid int, url string, ip string, data g.Map) (id int64, err error) + } +) + +var ( + localSystemLog ISystemLog +) + +func SystemLog() ISystemLog { + if localSystemLog == nil { + panic("implement not found for interface ISystemLog, forgot register?") + } + return localSystemLog +} + +func RegisterSystemLog(i ISystemLog) { + localSystemLog = i +}