From 9660d2f9c1c95d46cd0889f7e5d0e3d2533fc43c Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 13 Nov 2019 17:22:45 +0800 Subject: [PATCH] Feat: captcha config --- conf/conf.ini | 2 +- models/init.go | 2 +- pkg/conf/conf.go | 55 ++++++++++++++++++++++++++++++---- pkg/util/logger.go | 2 +- routers/controllers/general.go | 28 +++++++++-------- 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/conf/conf.ini b/conf/conf.ini index 957081b..57a5394 100644 --- a/conf/conf.ini +++ b/conf/conf.ini @@ -13,7 +13,7 @@ TablePrefix = v3_ [Captcha] Height = 60 Width = 240 -Mode = NumberAlphabet +Mode = 3 ComplexOfNoiseText = 0 ComplexOfNoiseDot = 0 IsShowHollowLine = false diff --git a/models/init.go b/models/init.go index b965b0e..5a26444 100644 --- a/models/init.go +++ b/models/init.go @@ -15,7 +15,7 @@ var DB *gorm.DB // Database 初始化 MySQL 链接 func Init() { - util.Log().Info("初始化数据库连接\n") + util.Log().Info("初始化数据库连接") var ( db *gorm.DB diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 70b69c6..fbc0875 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -3,6 +3,8 @@ package conf import ( "cloudreve/pkg/util" "github.com/go-ini/ini" + "github.com/mojocn/base64Captcha" + "gopkg.in/go-playground/validator.v8" ) // database 数据库 @@ -15,17 +17,51 @@ type database struct { TablePrefix string } -var DatabaseConfig = &database{ - Type: "UNSET", -} - // system 系统通用配置 type system struct { Debug bool SessionSecret string } -var SystemConfig = &system{} +// captcha 验证码配置 +type captcha struct { + Height int `validate:"gte=0"` + Width int `validate:"gte=0"` + Mode int `validate:"gte=0,lte=3"` + ComplexOfNoiseText int `validate:"gte=0,lte=2"` + ComplexOfNoiseDot int `validate:"gte=0,lte=2"` + IsShowHollowLine bool + IsShowNoiseDot bool + IsShowNoiseText bool + IsShowSlimeLine bool + IsShowSineLine bool + CaptchaLen int `validate:"gte=0"` +} + +// DatabaseConfig 数据库配置 +var DatabaseConfig = &database{ + Type: "UNSET", +} + +// SystemConfig 系统公用配置 +var SystemConfig = &system{ + Debug: false, +} + +// CaptchaConfig 验证码配置 +var CaptchaConfig = &captcha{ + Height: 60, + Width: 240, + Mode: 3, + ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, + ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower, + IsShowHollowLine: false, + IsShowNoiseDot: false, + IsShowNoiseText: false, + IsShowSlimeLine: false, + IsShowSineLine: false, + CaptchaLen: 6, +} var cfg *ini.File @@ -42,6 +78,7 @@ func Init(path string) { sections := map[string]interface{}{ "Database": DatabaseConfig, "System": SystemConfig, + "Captcha": CaptchaConfig, } for sectionName, sectionStruct := range sections { err = mapSection(sectionName, sectionStruct) @@ -58,5 +95,13 @@ func mapSection(section string, confStruct interface{}) error { if err != nil { return err } + + // 验证合法性 + validate := validator.New(&validator.Config{TagName: "validate"}) + err = validate.Struct(confStruct) + if err != nil { + return err + } + return nil } diff --git a/pkg/util/logger.go b/pkg/util/logger.go index b6ff8d4..ddac83e 100644 --- a/pkg/util/logger.go +++ b/pkg/util/logger.go @@ -25,7 +25,7 @@ type Logger struct { // Println 打印 func (ll *Logger) Println(msg string) { - fmt.Printf("%s %s", time.Now().Format("2006-01-02 15:04:05 -0700"), msg) + fmt.Printf("%s %s\n", time.Now().Format("2006-01-02 15:04:05 -0700"), msg) } // Panic 极端错误 diff --git a/routers/controllers/general.go b/routers/controllers/general.go index 5e0054a..28c6f95 100644 --- a/routers/controllers/general.go +++ b/routers/controllers/general.go @@ -1,6 +1,7 @@ package controllers import ( + "cloudreve/pkg/conf" "cloudreve/pkg/serializer" "cloudreve/pkg/util" "github.com/gin-gonic/gin" @@ -9,26 +10,29 @@ import ( // Captcha 获取验证码 func Captcha(c *gin.Context) { - + // 验证码配置 var configD = base64Captcha.ConfigCharacter{ - Height: 60, - Width: 240, + Height: conf.CaptchaConfig.Height, + Width: conf.CaptchaConfig.Width, //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. - Mode: base64Captcha.CaptchaModeNumberAlphabet, - ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, - ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower, - IsShowHollowLine: false, - IsShowNoiseDot: false, - IsShowNoiseText: false, - IsShowSlimeLine: false, - IsShowSineLine: false, - CaptchaLen: 6, + Mode: conf.CaptchaConfig.Mode, + ComplexOfNoiseText: conf.CaptchaConfig.ComplexOfNoiseText, + ComplexOfNoiseDot: conf.CaptchaConfig.ComplexOfNoiseDot, + IsShowHollowLine: conf.CaptchaConfig.IsShowHollowLine, + IsShowNoiseDot: conf.CaptchaConfig.IsShowNoiseDot, + IsShowNoiseText: conf.CaptchaConfig.IsShowNoiseText, + IsShowSlimeLine: conf.CaptchaConfig.IsShowSlimeLine, + IsShowSineLine: conf.CaptchaConfig.IsShowSineLine, + CaptchaLen: conf.CaptchaConfig.CaptchaLen, } + // 生成验证码 idKeyD, capD := base64Captcha.GenerateCaptcha("", configD) + // 将验证码UID存入Session以便后续验证 util.SetSession(c, map[string]interface{}{ "captchaID": idKeyD, }) + // 将验证码图像编码为Base64 base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD) c.JSON(200, serializer.Response{