// Copyright 2019 githup.com. All rights reserved. // Use of this source code is governed by githup.com. package utils import ( "bytes" "git.getensh.com/common/gopkgs/cache" "math/rand" "net/http" "property-device-gateway/parser" "time" "github.com/dchest/captcha" "github.com/gin-gonic/gin" ) var ( CaptchaWidth = 102 CaptchaHight = 38 ) //根据length获取随机字符串 func RandomString(length int) string { str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" bytes := []byte(str) result := []byte{} r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < length; i++ { result = append(result, bytes[r.Intn(len(bytes))]) } return string(result) } func Captcha(c *gin.Context, length ...int) string { l := captcha.DefaultLen if len(length) == 1 { l = length[0] } if len(length) == 2 { CaptchaWidth = length[1] } if len(length) == 3 { CaptchaHight = length[2] } captchaId := captcha.NewLen(l) SaveCaptcha(captchaId) return captchaId /* key := fmt.Sprintf("%d%s", time.Now().UnixNano(), RandomString(5)) SaveCaptcha(key, captchaId) //session := sessions.Default(c) //session.Set("captcha", captchaId) //_ = session.Save() _ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, captchaWidth, captchaHight, key) */ } func CaptchaVerify(c *gin.Context, code string, captchaId string) bool { if parser.Conf.RunMode == "dev" &&code == "999999" { return true } if captchaId == "" { return false } if captcha.VerifyString(captchaId, code) { DelCaptcha(captchaId) return true } return false /* session := sessions.Default(c) if captchaId := session.Get("captcha"); captchaId != nil { session.Delete("captcha") _ = session.Save() if captcha.VerifyString(captchaId.(string), code) { return true } else { return false } } else { return false } */ } func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int, reload string) error { if id == "" { http.NotFound(w, r) return nil } if !CaptchaExist(id) { http.NotFound(w, r) return nil } captcha.Reload(id) w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Header().Set("Pragma", "no-cache") w.Header().Set("Expires", "0") var content bytes.Buffer switch ext { case ".png": w.Header().Set("Content-Type", "image/png") _ = captcha.WriteImage(&content, id, captcha.StdWidth, captcha.StdHeight) case ".wav": w.Header().Set("Content-Type", "audio/x-wav") _ = captcha.WriteAudio(&content, id, lang) default: return captcha.ErrNotFound } if download { w.Header().Set("Content-Type", "application/octet-stream") } http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes())) return nil } var captchaKey = "captcha" func SaveCaptcha(id string) error { key := captchaKey + id _, err := cache.Redis().SetEx(key, 6000, "") if err != nil { return err } return nil } func DelCaptcha(id string){ key := captchaKey + id cache.Redis().Del(key) } func CaptchaExist(id string) bool { key := captchaKey + id ret, _ := cache.Redis().Exists(key) if ret > 0 { return true } return false }