2022-08-16 23:30:23 +08:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-08-16 23:30:23 +08:00
|
|
|
"github.com/mojocn/base64Captcha"
|
|
|
|
)
|
|
|
|
|
|
|
|
var store = base64Captcha.DefaultMemStore
|
|
|
|
|
|
|
|
func VerifyCode(codeID string, code string) error {
|
|
|
|
if codeID == "" {
|
|
|
|
return constant.ErrCaptchaCode
|
|
|
|
}
|
|
|
|
vv := store.Get(codeID, true)
|
|
|
|
vv = strings.TrimSpace(vv)
|
|
|
|
code = strings.TrimSpace(code)
|
|
|
|
|
|
|
|
if strings.EqualFold(vv, code) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return constant.ErrCaptchaCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateCaptcha() (*dto.CaptchaResponse, error) {
|
|
|
|
var driverString base64Captcha.DriverString
|
2022-12-02 18:52:43 +08:00
|
|
|
driverString.Source = "1234567890QWERTYUPLKJHGFDSAZXCVBNMqwertyupkjhgfdsazxcvbnm"
|
2022-09-08 18:47:15 +08:00
|
|
|
driverString.Width = 120
|
|
|
|
driverString.Height = 50
|
|
|
|
driverString.NoiseCount = 0
|
|
|
|
driverString.Length = 4
|
2023-02-24 18:50:07 +08:00
|
|
|
driverString.Fonts = []string{"RitaSmith.ttf", "actionj.ttf", "chromohv.ttf"}
|
2022-08-16 23:30:23 +08:00
|
|
|
driver := driverString.ConvertFonts()
|
|
|
|
c := base64Captcha.NewCaptcha(driver, store)
|
2023-12-13 11:28:09 +08:00
|
|
|
id, b64s, _, err := c.Generate()
|
2022-08-16 23:30:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &dto.CaptchaResponse{
|
|
|
|
CaptchaID: id,
|
|
|
|
ImagePath: b64s,
|
|
|
|
}, nil
|
|
|
|
}
|