service.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package geetest
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "math/rand"
  7. "strconv"
  8. "go-common/app/interface/main/creative/conf"
  9. "go-common/app/interface/main/creative/dao/account"
  10. d "go-common/app/interface/main/creative/dao/geetest"
  11. m "go-common/app/interface/main/creative/model/geetest"
  12. "go-common/app/interface/main/creative/service"
  13. "go-common/library/log"
  14. "go-common/library/stat/prom"
  15. )
  16. //Service struct.
  17. type Service struct {
  18. c *conf.Config
  19. gt *d.Dao
  20. acc *account.Dao
  21. pErr *prom.Prom
  22. }
  23. //New get service.
  24. func New(c *conf.Config, rpcdaos *service.RPCDaos) *Service {
  25. s := &Service{
  26. c: c,
  27. gt: d.New(c),
  28. acc: rpcdaos.Acc,
  29. pErr: prom.BusinessErrCount,
  30. }
  31. return s
  32. }
  33. // PreProcessAdd fn
  34. func (s *Service) PreProcessAdd(c context.Context, mid int64, ip, clientType string, newCaptcha int) (res *m.ProcessRes, err error) {
  35. var pre string
  36. res = &m.ProcessRes{
  37. Limit: map[string]bool{
  38. "add": false,
  39. },
  40. }
  41. if exist, _, _ := s.acc.HalfMin(c, mid); exist {
  42. log.Info("halfMin exist | mid(%d)", mid)
  43. res.Limit["add"] = true
  44. }
  45. if res.Limit["add"] {
  46. res.CaptchaID = s.c.Geetest.CaptchaID
  47. res.NewCaptcha = newCaptcha
  48. if pre, err = s.gt.PreProcess(c, mid, ip, clientType, newCaptcha); err != nil || pre == "" {
  49. randOne := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  50. randTwo := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  51. challenge := hex.EncodeToString(randOne[:]) + hex.EncodeToString(randTwo[:])[0:2]
  52. res.Challenge = challenge
  53. log.Info("s.gt.PreProcess err != nil (%s,%d,%d) ", clientType, newCaptcha, mid)
  54. s.pErr.Incr("geetest_preprocess")
  55. res.Success = 0
  56. err = nil
  57. return
  58. }
  59. res.Success = 1
  60. slice := md5.Sum([]byte(pre + s.c.Geetest.PrivateKEY))
  61. res.Challenge = hex.EncodeToString(slice[:])
  62. }
  63. return
  64. }
  65. // PreProcess getGeetestChal
  66. func (s *Service) PreProcess(c context.Context, mid int64, ip, clientType string, newCaptcha int) (res *m.ProcessRes, err error) {
  67. var pre string
  68. res = &m.ProcessRes{
  69. Limit: map[string]bool{
  70. "add": false,
  71. },
  72. }
  73. if exist, _, _ := s.acc.HalfMin(c, mid); exist {
  74. log.Info("halfMin exist | mid(%d)", mid)
  75. res.Limit["add"] = true
  76. }
  77. res.CaptchaID = s.c.Geetest.CaptchaID
  78. res.NewCaptcha = newCaptcha
  79. if pre, err = s.gt.PreProcess(c, mid, ip, clientType, newCaptcha); err != nil || pre == "" {
  80. randOne := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  81. randTwo := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  82. challenge := hex.EncodeToString(randOne[:]) + hex.EncodeToString(randTwo[:])[0:2]
  83. res.Challenge = challenge
  84. log.Info("s.gt.PreProcess err != nil (%s,%d,%d) ", clientType, newCaptcha, mid)
  85. s.pErr.Incr("geetest_preprocess")
  86. res.Success = 0
  87. err = nil
  88. return
  89. }
  90. res.Success = 1
  91. slice := md5.Sum([]byte(pre + s.c.Geetest.PrivateKEY))
  92. res.Challenge = hex.EncodeToString(slice[:])
  93. return
  94. }
  95. // Validate recheck the seccode
  96. func (s *Service) Validate(c context.Context, challenge, validate, seccode, clientType, ip string, success int, mid int64) (stat bool) {
  97. if len(validate) != 32 {
  98. log.Error("s.Validate(%s,%s,%s,%d) err(validate not eq 32byte)", challenge, validate, seccode, mid)
  99. return
  100. }
  101. if success != 1 {
  102. slice := md5.Sum([]byte(challenge))
  103. stat = hex.EncodeToString(slice[:]) == validate
  104. return
  105. }
  106. slice := md5.Sum([]byte(s.c.Geetest.PrivateKEY + "geetest" + challenge))
  107. if hex.EncodeToString(slice[:]) != validate {
  108. log.Error("s.Validate(%s,%s,%s,%d) err(challenge not found)", challenge)
  109. return
  110. }
  111. res, err := s.gt.Validate(c, challenge, seccode, clientType, ip, s.c.Geetest.CaptchaID, mid)
  112. if err != nil {
  113. s.pErr.Incr("geetest_validate")
  114. log.Error("s.Validate(%s,%s,%s,%d) err(gtServer validate failed.)", challenge, validate, seccode, mid)
  115. return
  116. }
  117. slice = md5.Sum([]byte(seccode))
  118. stat = hex.EncodeToString(slice[:]) == res.Seccode
  119. return
  120. }