geetest.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package service
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "go-common/app/interface/main/videoup/model/archive"
  7. gmdl "go-common/app/interface/main/videoup/model/geetest"
  8. "go-common/library/log"
  9. )
  10. // Validate fn
  11. func (s *Service) Validate(c context.Context, geetest *archive.Geetest, upFormStr string, mid int64) (stat bool, err error) {
  12. if geetest == nil {
  13. log.Error("arc param geetest is nil, mid(%d)", mid)
  14. return
  15. }
  16. var res *gmdl.ValidateRes
  17. validate := geetest.Validate
  18. seccode := geetest.Seccode
  19. challenge := geetest.Challenge
  20. if len(validate) != 32 {
  21. log.Error("s.Validate(%s,%s,%s,%d) err(validate not eq 32byte)", challenge, validate, seccode, mid)
  22. return
  23. }
  24. if geetest.Success != 1 {
  25. slice := md5.Sum([]byte(challenge))
  26. stat = hex.EncodeToString(slice[:]) == validate
  27. return
  28. }
  29. slice := md5.Sum([]byte(s.c.Geetest.PrivateKEY + "geetest" + challenge))
  30. if hex.EncodeToString(slice[:]) != validate {
  31. log.Error("s.Validate(%s,%s,%s,%d) err(challenge not found)", challenge, validate, seccode, mid)
  32. return
  33. }
  34. res, err = s.gt.Validate(c, challenge, seccode, upFormStr, s.c.Geetest.CaptchaID, mid)
  35. if err != nil {
  36. log.Error("s.Validate(%s,%s,%s,%d) err(gtServer validate failed.)", challenge, validate, seccode, mid)
  37. return
  38. }
  39. slice = md5.Sum([]byte(seccode))
  40. stat = hex.EncodeToString(slice[:]) == res.Seccode
  41. return
  42. }