geetest.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package realname
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "fmt"
  7. "math/rand"
  8. "net/url"
  9. "strconv"
  10. "go-common/app/interface/main/account/conf"
  11. "go-common/app/interface/main/account/model"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. )
  15. // CaptchaGTRegister register geetest
  16. func (s *Service) CaptchaGTRegister(c context.Context, mid int64, ip, gtType string) (urlstr string, remote int, err error) {
  17. var (
  18. p = url.Values{}
  19. challenge string
  20. )
  21. p.Set("ct", "1")
  22. p.Set("gt", conf.Conf.Realname.Geetest.CaptchaID)
  23. if challenge, err = s.realnameDao.RealnameCaptchaGTRegister(c, mid, ip, gtType, 1); err != nil || challenge == "" {
  24. p.Set("success", "0")
  25. remote = 0
  26. err = nil
  27. randOne := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  28. randTwo := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  29. p.Set("challenge", hex.EncodeToString(randOne[:])+hex.EncodeToString(randTwo[:])[0:2])
  30. } else {
  31. p.Set("success", "1")
  32. remote = 1
  33. array := md5.Sum([]byte(challenge + conf.Conf.Realname.Geetest.PrivateKey))
  34. p.Set("challenge", hex.EncodeToString(array[:]))
  35. }
  36. p.Set("hash", fmt.Sprintf("%x", md5.Sum([]byte(p.Encode()))))
  37. urlstr = "http://passport.bilibili.com/register/verification.html?" + p.Encode()
  38. return
  39. }
  40. // CaptchaGTRefresh refresh geetest
  41. func (s *Service) CaptchaGTRefresh(c context.Context, mid int64, ip, gtType string, hash string) (challenge string, gt string, success int, err error) {
  42. log.Info("CaptchaGTRefresh got hash : %s", hash)
  43. gt = conf.Conf.Realname.Geetest.CaptchaID
  44. if challenge, err = s.realnameDao.RealnameCaptchaGTRegister(c, mid, ip, gtType, 1); err != nil || challenge == "" {
  45. success = 0
  46. randOne := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  47. randTwo := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
  48. challenge = hex.EncodeToString(randOne[:]) + hex.EncodeToString(randTwo[:])[0:2]
  49. } else {
  50. success = 1
  51. array := md5.Sum([]byte(challenge + conf.Conf.Realname.Geetest.PrivateKey))
  52. challenge = hex.EncodeToString(array[:])
  53. }
  54. return
  55. }
  56. // CaptchaGTValidate validate geetest
  57. func (s *Service) CaptchaGTValidate(c context.Context, mid int64, ip, clientType string, param *model.ParamRealnameCaptchaGTCheck) (res *model.RealnameCaptchaGTValidate, err error) {
  58. res = &model.RealnameCaptchaGTValidate{
  59. State: model.RealnameFalse,
  60. }
  61. switch param.Remote {
  62. // 极验远程校验
  63. case model.RealnameTrue:
  64. md5Array := md5.Sum([]byte(conf.Conf.Realname.Geetest.PrivateKey + "geetest" + param.Challenge))
  65. if hex.EncodeToString(md5Array[:]) != param.Validate {
  66. return
  67. }
  68. var remoteSeccode string
  69. if remoteSeccode, err = s.realnameDao.RealnameCaptchaGTRegisterValidate(c, param.Challenge, param.Seccode, clientType, ip, conf.Conf.Realname.Geetest.CaptchaID, mid); err != nil {
  70. return
  71. }
  72. log.Info("CaptchaGTValidate remoteSec : %s", remoteSeccode)
  73. md5Array = md5.Sum([]byte(remoteSeccode))
  74. res.State = model.RealnameTrue
  75. // if hex.EncodeToString(md5Array[:]) == param.Seccode {
  76. // res.State = model.RealnameTrue
  77. // }
  78. // 极验本地校验
  79. case model.RealnameFalse:
  80. // md5Array := md5.Sum([]byte(param.Challenge))
  81. // if hex.EncodeToString(md5Array[:]) == param.Validate {
  82. // res.State = model.RealnameTrue
  83. // }
  84. res.State = model.RealnameTrue
  85. default:
  86. err = ecode.RequestErr
  87. }
  88. s.addmiss(func() {
  89. if missErr := s.setAlipayAntispamPassFlag(context.Background(), mid, true); missErr != nil {
  90. log.Error("%+v", err)
  91. }
  92. })
  93. return
  94. }