mirror of
				https://github.com/cloudreve/cloudreve.git
				synced 2025-10-31 00:27:31 +08:00 
			
		
		
		
	Feat: captcha config
This commit is contained in:
		| @ -13,7 +13,7 @@ TablePrefix = v3_ | |||||||
| [Captcha] | [Captcha] | ||||||
| Height = 60 | Height = 60 | ||||||
| Width = 240 | Width = 240 | ||||||
| Mode = NumberAlphabet | Mode = 3 | ||||||
| ComplexOfNoiseText = 0 | ComplexOfNoiseText = 0 | ||||||
| ComplexOfNoiseDot = 0 | ComplexOfNoiseDot = 0 | ||||||
| IsShowHollowLine = false | IsShowHollowLine = false | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ var DB *gorm.DB | |||||||
|  |  | ||||||
| // Database 初始化 MySQL 链接 | // Database 初始化 MySQL 链接 | ||||||
| func Init() { | func Init() { | ||||||
| 	util.Log().Info("初始化数据库连接\n") | 	util.Log().Info("初始化数据库连接") | ||||||
|  |  | ||||||
| 	var ( | 	var ( | ||||||
| 		db  *gorm.DB | 		db  *gorm.DB | ||||||
|  | |||||||
| @ -3,6 +3,8 @@ package conf | |||||||
| import ( | import ( | ||||||
| 	"cloudreve/pkg/util" | 	"cloudreve/pkg/util" | ||||||
| 	"github.com/go-ini/ini" | 	"github.com/go-ini/ini" | ||||||
|  | 	"github.com/mojocn/base64Captcha" | ||||||
|  | 	"gopkg.in/go-playground/validator.v8" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // database 数据库 | // database 数据库 | ||||||
| @ -15,17 +17,51 @@ type database struct { | |||||||
| 	TablePrefix string | 	TablePrefix string | ||||||
| } | } | ||||||
|  |  | ||||||
| var DatabaseConfig = &database{ |  | ||||||
| 	Type: "UNSET", |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // system 系统通用配置 | // system 系统通用配置 | ||||||
| type system struct { | type system struct { | ||||||
| 	Debug         bool | 	Debug         bool | ||||||
| 	SessionSecret string | 	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 | var cfg *ini.File | ||||||
|  |  | ||||||
| @ -42,6 +78,7 @@ func Init(path string) { | |||||||
| 	sections := map[string]interface{}{ | 	sections := map[string]interface{}{ | ||||||
| 		"Database": DatabaseConfig, | 		"Database": DatabaseConfig, | ||||||
| 		"System":   SystemConfig, | 		"System":   SystemConfig, | ||||||
|  | 		"Captcha":  CaptchaConfig, | ||||||
| 	} | 	} | ||||||
| 	for sectionName, sectionStruct := range sections { | 	for sectionName, sectionStruct := range sections { | ||||||
| 		err = mapSection(sectionName, sectionStruct) | 		err = mapSection(sectionName, sectionStruct) | ||||||
| @ -58,5 +95,13 @@ func mapSection(section string, confStruct interface{}) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// 验证合法性 | ||||||
|  | 	validate := validator.New(&validator.Config{TagName: "validate"}) | ||||||
|  | 	err = validate.Struct(confStruct) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ type Logger struct { | |||||||
|  |  | ||||||
| // Println 打印 | // Println 打印 | ||||||
| func (ll *Logger) Println(msg string) { | 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 极端错误 | // Panic 极端错误 | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| package controllers | package controllers | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"cloudreve/pkg/conf" | ||||||
| 	"cloudreve/pkg/serializer" | 	"cloudreve/pkg/serializer" | ||||||
| 	"cloudreve/pkg/util" | 	"cloudreve/pkg/util" | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| @ -9,26 +10,29 @@ import ( | |||||||
|  |  | ||||||
| // Captcha 获取验证码 | // Captcha 获取验证码 | ||||||
| func Captcha(c *gin.Context) { | func Captcha(c *gin.Context) { | ||||||
|  | 	// 验证码配置 | ||||||
| 	var configD = base64Captcha.ConfigCharacter{ | 	var configD = base64Captcha.ConfigCharacter{ | ||||||
| 		Height: 60, | 		Height: conf.CaptchaConfig.Height, | ||||||
| 		Width:  240, | 		Width:  conf.CaptchaConfig.Width, | ||||||
| 		//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. | 		//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. | ||||||
| 		Mode:               base64Captcha.CaptchaModeNumberAlphabet, | 		Mode:               conf.CaptchaConfig.Mode, | ||||||
| 		ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, | 		ComplexOfNoiseText: conf.CaptchaConfig.ComplexOfNoiseText, | ||||||
| 		ComplexOfNoiseDot:  base64Captcha.CaptchaComplexLower, | 		ComplexOfNoiseDot:  conf.CaptchaConfig.ComplexOfNoiseDot, | ||||||
| 		IsShowHollowLine:   false, | 		IsShowHollowLine:   conf.CaptchaConfig.IsShowHollowLine, | ||||||
| 		IsShowNoiseDot:     false, | 		IsShowNoiseDot:     conf.CaptchaConfig.IsShowNoiseDot, | ||||||
| 		IsShowNoiseText:    false, | 		IsShowNoiseText:    conf.CaptchaConfig.IsShowNoiseText, | ||||||
| 		IsShowSlimeLine:    false, | 		IsShowSlimeLine:    conf.CaptchaConfig.IsShowSlimeLine, | ||||||
| 		IsShowSineLine:     false, | 		IsShowSineLine:     conf.CaptchaConfig.IsShowSineLine, | ||||||
| 		CaptchaLen:         6, | 		CaptchaLen:         conf.CaptchaConfig.CaptchaLen, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// 生成验证码 | ||||||
| 	idKeyD, capD := base64Captcha.GenerateCaptcha("", configD) | 	idKeyD, capD := base64Captcha.GenerateCaptcha("", configD) | ||||||
|  | 	// 将验证码UID存入Session以便后续验证 | ||||||
| 	util.SetSession(c, map[string]interface{}{ | 	util.SetSession(c, map[string]interface{}{ | ||||||
| 		"captchaID": idKeyD, | 		"captchaID": idKeyD, | ||||||
| 	}) | 	}) | ||||||
|  | 	// 将验证码图像编码为Base64 | ||||||
| 	base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD) | 	base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD) | ||||||
|  |  | ||||||
| 	c.JSON(200, serializer.Response{ | 	c.JSON(200, serializer.Response{ | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 HFO4
					HFO4