captcha.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package utils
  4. import (
  5. "bytes"
  6. "github.com/jaryhe/gopkgs/cache"
  7. "math/rand"
  8. "net/http"
  9. "time"
  10. "github.com/dchest/captcha"
  11. "github.com/gin-gonic/gin"
  12. "smart-government-management-gateway/parser"
  13. )
  14. var (
  15. CaptchaWidth = 102
  16. CaptchaHight = 38
  17. )
  18. //根据length获取随机字符串
  19. func RandomString(length int) string {
  20. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  21. bytes := []byte(str)
  22. result := []byte{}
  23. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  24. for i := 0; i < length; i++ {
  25. result = append(result, bytes[r.Intn(len(bytes))])
  26. }
  27. return string(result)
  28. }
  29. func Captcha(c *gin.Context, length ...int) string {
  30. l := captcha.DefaultLen
  31. if len(length) == 1 {
  32. l = length[0]
  33. }
  34. if len(length) == 2 {
  35. CaptchaWidth = length[1]
  36. }
  37. if len(length) == 3 {
  38. CaptchaHight = length[2]
  39. }
  40. captchaId := captcha.NewLen(l)
  41. SaveCaptcha(captchaId)
  42. return captchaId
  43. /*
  44. key := fmt.Sprintf("%d%s", time.Now().UnixNano(), RandomString(5))
  45. SaveCaptcha(key, captchaId)
  46. //session := sessions.Default(c)
  47. //session.Set("captcha", captchaId)
  48. //_ = session.Save()
  49. _ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, captchaWidth, captchaHight, key)
  50. */
  51. }
  52. func CaptchaVerify(c *gin.Context, code string, captchaId string) bool {
  53. if parser.Conf.RunMode == "dev" &&code == "999999" {
  54. return true
  55. }
  56. if captchaId == "" {
  57. return false
  58. }
  59. if captcha.VerifyString(captchaId, code) {
  60. DelCaptcha(captchaId)
  61. return true
  62. }
  63. return false
  64. /*
  65. session := sessions.Default(c)
  66. if captchaId := session.Get("captcha"); captchaId != nil {
  67. session.Delete("captcha")
  68. _ = session.Save()
  69. if captcha.VerifyString(captchaId.(string), code) {
  70. return true
  71. } else {
  72. return false
  73. }
  74. } else {
  75. return false
  76. }
  77. */
  78. }
  79. func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int, reload string) error {
  80. if id == "" {
  81. http.NotFound(w, r)
  82. return nil
  83. }
  84. if !CaptchaExist(id) {
  85. http.NotFound(w, r)
  86. return nil
  87. }
  88. captcha.Reload(id)
  89. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  90. w.Header().Set("Pragma", "no-cache")
  91. w.Header().Set("Expires", "0")
  92. var content bytes.Buffer
  93. switch ext {
  94. case ".png":
  95. w.Header().Set("Content-Type", "image/png")
  96. _ = captcha.WriteImage2(&content, id, 120, 30)
  97. case ".wav":
  98. w.Header().Set("Content-Type", "audio/x-wav")
  99. _ = captcha.WriteAudio(&content, id, lang)
  100. default:
  101. return captcha.ErrNotFound
  102. }
  103. if download {
  104. w.Header().Set("Content-Type", "application/octet-stream")
  105. }
  106. http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
  107. return nil
  108. }
  109. var captchaKey = "captcha"
  110. func SaveCaptcha(id string) error {
  111. key := captchaKey + id
  112. _, err := cache.Redis().SetEx(key, 6000, "")
  113. if err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. func DelCaptcha(id string){
  119. key := captchaKey + id
  120. cache.Redis().Del(key)
  121. }
  122. func CaptchaExist(id string) bool {
  123. key := captchaKey + id
  124. ret, _ := cache.Redis().Exists(key)
  125. if ret > 0 {
  126. return true
  127. }
  128. return false
  129. }