mirror of https://github.com/cloudreve/Cloudreve
				
				
				
			Feat: captcha config
							parent
							
								
									7d4e212d4e
								
							
						
					
					
						commit
						9660d2f9c1
					
				| 
						 | 
				
			
			@ -13,7 +13,7 @@ TablePrefix = v3_
 | 
			
		|||
[Captcha]
 | 
			
		||||
Height = 60
 | 
			
		||||
Width = 240
 | 
			
		||||
Mode = NumberAlphabet
 | 
			
		||||
Mode = 3
 | 
			
		||||
ComplexOfNoiseText = 0
 | 
			
		||||
ComplexOfNoiseDot = 0
 | 
			
		||||
IsShowHollowLine = false
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,7 @@ var DB *gorm.DB
 | 
			
		|||
 | 
			
		||||
// Database 初始化 MySQL 链接
 | 
			
		||||
func Init() {
 | 
			
		||||
	util.Log().Info("初始化数据库连接\n")
 | 
			
		||||
	util.Log().Info("初始化数据库连接")
 | 
			
		||||
 | 
			
		||||
	var (
 | 
			
		||||
		db  *gorm.DB
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 极端错误
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue