From a525d4b1a748ae11182429fa6ec476e3faa9b5b7 Mon Sep 17 00:00:00 2001 From: yaooort Date: Sat, 9 Oct 2021 19:44:04 +0800 Subject: [PATCH 01/15] =?UTF-8?q?Revert=20"=E5=8F=91=E5=B8=83V2.4.5=20RC"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 4465450b481d23a1f9eaf343e82b89e52b8029c4. --- server/core/server.go | 2 +- web/src/core/gin-vue-admin.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/core/server.go b/server/core/server.go index df0aa994..7d51edee 100644 --- a/server/core/server.go +++ b/server/core/server.go @@ -38,7 +38,7 @@ func RunWindowsServer() { fmt.Printf(` 欢迎使用 github.com/flipped-aurora/gin-vue-admin/server - 当前版本:V2.4.5 RC + 当前版本:V2.4.5 beta.1 加群方式:微信号:shouzi_1994 QQ群:622360840 默认自动化文档地址:http://127.0.0.1%s/swagger/index.html 默认前端文件运行地址:http://127.0.0.1:8080 diff --git a/web/src/core/gin-vue-admin.js b/web/src/core/gin-vue-admin.js index ab572ca7..f7025209 100644 --- a/web/src/core/gin-vue-admin.js +++ b/web/src/core/gin-vue-admin.js @@ -10,7 +10,7 @@ export default { register(app) console.log(` 欢迎使用 Gin-Vue-Admin - 当前版本:V2.4.5 RC + 当前版本:V2.4.5 beta.1 加群方式:微信:shouzi_1994 QQ群:622360840 默认自动化文档地址:http://127.0.0.1:${import.meta.env.VITE_SERVER_PORT}/swagger/index.html 默认前端文件运行地址:http://127.0.0.1:${import.meta.env.VITE_CLI_PORT} From 2264fb375046336d5bc53189ea6c6f0a655bdb16 Mon Sep 17 00:00:00 2001 From: yaooort Date: Sat, 9 Oct 2021 19:58:59 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9zap=E5=88=86=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=E6=89=93=E5=8D=B0=E5=88=B0=E6=97=A5=E5=BF=97=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 1 - server/config/zap.go | 1 - server/core/zap.go | 54 +++++++++++++----------------- server/go.mod | 1 + server/utils/rotatelogs.go | 28 ++++++++++++++++ server/utils/rotatelogs_unix.go | 32 ------------------ server/utils/rotatelogs_windows.go | 28 ---------------- 7 files changed, 53 insertions(+), 92 deletions(-) create mode 100644 server/utils/rotatelogs.go delete mode 100644 server/utils/rotatelogs_unix.go delete mode 100644 server/utils/rotatelogs_windows.go diff --git a/server/config.yaml b/server/config.yaml index 18454d82..76d293eb 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -12,7 +12,6 @@ zap: format: 'console' prefix: '[github.com/flipped-aurora/gin-vue-admin/server]' director: 'log' - link-name: 'latest_log' show-line: true encode-level: 'LowercaseColorLevelEncoder' stacktrace-key: 'stacktrace' diff --git a/server/config/zap.go b/server/config/zap.go index f681ca83..e5a00916 100644 --- a/server/config/zap.go +++ b/server/config/zap.go @@ -5,7 +5,6 @@ type Zap struct { Format string `mapstructure:"format" json:"format" yaml:"format"` // 输出 Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 日志前缀 Director string `mapstructure:"director" json:"director" yaml:"director"` // 日志文件夹 - LinkName string `mapstructure:"link-name" json:"linkName" yaml:"link-name"` // 软链接名称 ShowLine bool `mapstructure:"show-line" json:"showLine" yaml:"showLine"` // 显示行 EncodeLevel string `mapstructure:"encode-level" json:"encodeLevel" yaml:"encode-level"` // 编码级 StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktraceKey" yaml:"stacktrace-key"` // 栈名 diff --git a/server/core/zap.go b/server/core/zap.go index a0f40fe7..1abec961 100644 --- a/server/core/zap.go +++ b/server/core/zap.go @@ -11,38 +11,36 @@ import ( "go.uber.org/zap/zapcore" ) -var level zapcore.Level - func Zap() (logger *zap.Logger) { if ok, _ := utils.PathExists(global.GVA_CONFIG.Zap.Director); !ok { // 判断是否有Director文件夹 fmt.Printf("create %v directory\n", global.GVA_CONFIG.Zap.Director) _ = os.Mkdir(global.GVA_CONFIG.Zap.Director, os.ModePerm) } + // 调试级别 + debugPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev == zap.DebugLevel + }) + // 日志级别 + infoPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev == zap.InfoLevel + }) + // 警告级别 + warnPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev == zap.WarnLevel + }) + // 错误级别 + errorPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool { + return lev >= zap.ErrorLevel + }) - switch global.GVA_CONFIG.Zap.Level { // 初始化配置文件的Level - case "debug": - level = zap.DebugLevel - case "info": - level = zap.InfoLevel - case "warn": - level = zap.WarnLevel - case "error": - level = zap.ErrorLevel - case "dpanic": - level = zap.DPanicLevel - case "panic": - level = zap.PanicLevel - case "fatal": - level = zap.FatalLevel - default: - level = zap.InfoLevel + cores := [...]zapcore.Core{ + getEncoderCore(fmt.Sprintf("./%s/server_debug.log", global.GVA_CONFIG.Zap.Director), debugPriority), + getEncoderCore(fmt.Sprintf("./%s/server_info.log", global.GVA_CONFIG.Zap.Director), infoPriority), + getEncoderCore(fmt.Sprintf("./%s/server_warn.log", global.GVA_CONFIG.Zap.Director), warnPriority), + getEncoderCore(fmt.Sprintf("./%s/server_error.log", global.GVA_CONFIG.Zap.Director), errorPriority), } + logger = zap.New(zapcore.NewTee(cores[:]...), zap.AddCaller()) - if level == zap.DebugLevel || level == zap.ErrorLevel { - logger = zap.New(getEncoderCore(), zap.AddStacktrace(level)) - } else { - logger = zap.New(getEncoderCore()) - } if global.GVA_CONFIG.Zap.ShowLine { logger = logger.WithOptions(zap.AddCaller()) } @@ -88,12 +86,8 @@ func getEncoder() zapcore.Encoder { } // getEncoderCore 获取Encoder的zapcore.Core -func getEncoderCore() (core zapcore.Core) { - writer, err := utils.GetWriteSyncer() // 使用file-rotatelogs进行日志分割 - if err != nil { - fmt.Printf("Get Write Syncer Failed err:%v", err.Error()) - return - } +func getEncoderCore(fileName string, level zapcore.LevelEnabler) (core zapcore.Core) { + writer := utils.GetWriteSyncer(fileName) // 使用file-rotatelogs进行日志分割 return zapcore.NewCore(getEncoder(), writer, level) } diff --git a/server/go.mod b/server/go.mod index 0cfd9d05..3fd450a3 100644 --- a/server/go.mod +++ b/server/go.mod @@ -19,6 +19,7 @@ require ( github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible github.com/mojocn/base64Captcha v1.3.1 + github.com/natefinch/lumberjack v2.0.0+incompatible // indirect github.com/qiniu/api.v7/v7 v7.4.1 github.com/robfig/cron/v3 v3.0.1 github.com/satori/go.uuid v1.2.0 diff --git a/server/utils/rotatelogs.go b/server/utils/rotatelogs.go new file mode 100644 index 00000000..ad205205 --- /dev/null +++ b/server/utils/rotatelogs.go @@ -0,0 +1,28 @@ +package utils + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/natefinch/lumberjack" + "go.uber.org/zap/zapcore" + "os" +) + +//@author: [SliverHorn](https://github.com/SliverHorn) +//@function: GetWriteSyncer +//@description: zap logger中加入file-rotatelogs +//@return: zapcore.WriteSyncer, error + +func GetWriteSyncer(file string) zapcore.WriteSyncer { + lumberJackLogger := &lumberjack.Logger{ + Filename: file, //日志文件的位置 + MaxSize: 10, //在进行切割之前,日志文件的最大大小(以MB为单位) + MaxBackups: 200, //保留旧文件的最大个数 + MaxAge: 30, //保留旧文件的最大天数 + Compress: true, //是否压缩/归档旧文件 + } + + if global.GVA_CONFIG.Zap.LogInConsole { + return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberJackLogger)) + } + return zapcore.AddSync(lumberJackLogger) +} diff --git a/server/utils/rotatelogs_unix.go b/server/utils/rotatelogs_unix.go deleted file mode 100644 index 44199f7c..00000000 --- a/server/utils/rotatelogs_unix.go +++ /dev/null @@ -1,32 +0,0 @@ -//go:build !windows -// +build !windows - -package utils - -import ( - "os" - "path" - "time" - - "github.com/flipped-aurora/gin-vue-admin/server/global" - zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" - "go.uber.org/zap/zapcore" -) - -//@author: [SliverHorn](https://github.com/SliverHorn) -//@function: GetWriteSyncer -//@description: zap logger中加入file-rotatelogs -//@return: zapcore.WriteSyncer, error - -func GetWriteSyncer() (zapcore.WriteSyncer, error) { - fileWriter, err := zaprotatelogs.New( - path.Join(global.GVA_CONFIG.Zap.Director, "%Y-%m-%d.log"), - zaprotatelogs.WithLinkName(global.GVA_CONFIG.Zap.LinkName), - zaprotatelogs.WithMaxAge(7*24*time.Hour), - zaprotatelogs.WithRotationTime(24*time.Hour), - ) - if global.GVA_CONFIG.Zap.LogInConsole { - return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err - } - return zapcore.AddSync(fileWriter), err -} diff --git a/server/utils/rotatelogs_windows.go b/server/utils/rotatelogs_windows.go deleted file mode 100644 index ae61d733..00000000 --- a/server/utils/rotatelogs_windows.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "os" - "path" - "time" - - "github.com/flipped-aurora/gin-vue-admin/server/global" - zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" - "go.uber.org/zap/zapcore" -) - -//@author: [SliverHorn](https://github.com/SliverHorn) -//@function: GetWriteSyncer -//@description: zap logger中加入file-rotatelogs -//@return: zapcore.WriteSyncer, error - -func GetWriteSyncer() (zapcore.WriteSyncer, error) { - fileWriter, err := zaprotatelogs.New( - path.Join(global.GVA_CONFIG.Zap.Director, "%Y-%m-%d.log"), - zaprotatelogs.WithMaxAge(7*24*time.Hour), - zaprotatelogs.WithRotationTime(24*time.Hour), - ) - if global.GVA_CONFIG.Zap.LogInConsole { - return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err - } - return zapcore.AddSync(fileWriter), err -} From b18059b45e85e75dbfecd0ae0d596e0f46e9f626 Mon Sep 17 00:00:00 2001 From: yaooort Date: Sat, 9 Oct 2021 20:26:26 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=99=90=E6=B5=81?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6=EF=BC=8C=E6=A0=B9=E6=8D=AEIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 4 +++ server/config/system.go | 2 ++ server/middleware/limit_ip.go | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 server/middleware/limit_ip.go diff --git a/server/config.yaml b/server/config.yaml index 18454d82..a256df76 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -45,6 +45,10 @@ system: db-type: 'mysql' oss-type: 'local' # 控制oss选择走本期还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置 use-multipoint: false + # IP限制次数 一个小时15000次 + iplimit-count: 15000 + # IP限制一个小时 + iplimit-time: 3600 # captcha configuration captcha: diff --git a/server/config/system.go b/server/config/system.go index 768788ab..8cf92b06 100644 --- a/server/config/system.go +++ b/server/config/system.go @@ -6,4 +6,6 @@ type System struct { DbType string `mapstructure:"db-type" json:"dbType" yaml:"db-type"` // 数据库类型:mysql(默认)|sqlite|sqlserver|postgresql OssType string `mapstructure:"oss-type" json:"ossType" yaml:"oss-type"` // Oss类型 UseMultipoint bool `mapstructure:"use-multipoint" json:"useMultipoint" yaml:"use-multipoint"` // 多点登录拦截 + LimitCountIP int `mapstructure:"iplimit-count" json:"iplimitCount" yaml:"iplimit-count"` + LimitTimeIP int `mapstructure:"iplimit-time" json:"iplimitTime" yaml:"iplimit-time"` } diff --git a/server/middleware/limit_ip.go b/server/middleware/limit_ip.go new file mode 100644 index 00000000..21e66d2a --- /dev/null +++ b/server/middleware/limit_ip.go @@ -0,0 +1,53 @@ +package middleware + +import ( + "context" + "errors" + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/common/response" + "github.com/gin-gonic/gin" + "time" +) + +// ip限制 +func IPLimit() gin.HandlerFunc { + return func(c *gin.Context) { + key := "RequestClientIPLimit===" + c.ClientIP() + limit := global.GVA_CONFIG.System.LimitCountIP + second := global.GVA_CONFIG.System.LimitTimeIP + expiration := time.Duration(second) * time.Second + if err := SetLimitWithTime(key, limit, expiration); err != nil { + response.FailWithMessage(err.Error(), c) + return + } + // 继续往下处理 + c.Next() + } +} + +// 设置访问次数 +func SetLimitWithTime(key string, limit int, expiration time.Duration) error { + count, err := global.GVA_REDIS.Exists(context.Background(), key).Result() + if err != nil || count == 0 { + pipe := global.GVA_REDIS.TxPipeline() + pipe.Incr(context.Background(), key) + pipe.Expire(context.Background(), key, expiration) + _, err := pipe.Exec(context.Background()) + return err + } else { + //次数 + if times, err := global.GVA_REDIS.Get(context.Background(), key).Int(); err != nil { + return err + } else { + if times >= limit { + if t, err := global.GVA_REDIS.PTTL(context.Background(), key).Result(); err != nil { + return errors.New("请求太过频繁,请稍后再试") + } else { + return errors.New("请求太过频繁, 请 " + t.String() + " 秒后尝试") + } + } else { + return global.GVA_REDIS.Incr(context.Background(), key).Err() + } + } + } +} From 52852e6ce2881785e6f03ba6a9f8dfcb536504a1 Mon Sep 17 00:00:00 2001 From: yaooort Date: Sat, 9 Oct 2021 20:37:28 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E6=8B=A6=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/middleware/limit_ip.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/middleware/limit_ip.go b/server/middleware/limit_ip.go index 21e66d2a..0e18cac9 100644 --- a/server/middleware/limit_ip.go +++ b/server/middleware/limit_ip.go @@ -18,6 +18,7 @@ func IPLimit() gin.HandlerFunc { expiration := time.Duration(second) * time.Second if err := SetLimitWithTime(key, limit, expiration); err != nil { response.FailWithMessage(err.Error(), c) + c.Abort() return } // 继续往下处理 From eefb4e4728abf2ab79a394f354c982351b8fc8da Mon Sep 17 00:00:00 2001 From: qimiao <303176530@qq.com> Date: Mon, 11 Oct 2021 10:40:42 +0800 Subject: [PATCH 05/15] fix:#745 --- server/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/go.mod b/server/go.mod index 3fd450a3..be694c54 100644 --- a/server/go.mod +++ b/server/go.mod @@ -35,6 +35,6 @@ require ( go.uber.org/zap v1.16.0 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c gorm.io/driver/mysql v1.0.1 - gorm.io/gorm v1.20.7 + gorm.io/gorm v1.20.11 nhooyr.io/websocket v1.8.6 ) From 656b998f38f1878f31aaa4df8fc812c5ce6877bf Mon Sep 17 00:00:00 2001 From: piexlmax Date: Mon, 11 Oct 2021 10:49:21 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=E5=9B=9E?= =?UTF-8?q?=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/server.go | 2 +- web/src/core/gin-vue-admin.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/core/server.go b/server/core/server.go index 7d51edee..df0aa994 100644 --- a/server/core/server.go +++ b/server/core/server.go @@ -38,7 +38,7 @@ func RunWindowsServer() { fmt.Printf(` 欢迎使用 github.com/flipped-aurora/gin-vue-admin/server - 当前版本:V2.4.5 beta.1 + 当前版本:V2.4.5 RC 加群方式:微信号:shouzi_1994 QQ群:622360840 默认自动化文档地址:http://127.0.0.1%s/swagger/index.html 默认前端文件运行地址:http://127.0.0.1:8080 diff --git a/web/src/core/gin-vue-admin.js b/web/src/core/gin-vue-admin.js index f7025209..ab572ca7 100644 --- a/web/src/core/gin-vue-admin.js +++ b/web/src/core/gin-vue-admin.js @@ -10,7 +10,7 @@ export default { register(app) console.log(` 欢迎使用 Gin-Vue-Admin - 当前版本:V2.4.5 beta.1 + 当前版本:V2.4.5 RC 加群方式:微信:shouzi_1994 QQ群:622360840 默认自动化文档地址:http://127.0.0.1:${import.meta.env.VITE_SERVER_PORT}/swagger/index.html 默认前端文件运行地址:http://127.0.0.1:${import.meta.env.VITE_CLI_PORT} From 7aaf3187d697c32b07bc4b6cba3092a7f6096cf1 Mon Sep 17 00:00:00 2001 From: piexlmax Date: Tue, 12 Oct 2021 15:39:50 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E6=A0=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/view/layout/bottomInfo/bottomInfo.vue | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web/src/view/layout/bottomInfo/bottomInfo.vue b/web/src/view/layout/bottomInfo/bottomInfo.vue index 98b3e2b3..03464240 100644 --- a/web/src/view/layout/bottomInfo/bottomInfo.vue +++ b/web/src/view/layout/bottomInfo/bottomInfo.vue @@ -15,6 +15,8 @@