geetest.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package dao
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "encoding/json"
  6. "errors"
  7. "go-common/app/common/openplatform/geetest/model"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. const (
  17. _register = "http://api.geetest.com/register.php"
  18. _validate = "http://api.geetest.com/validate.php"
  19. )
  20. // PreProcess preprocessing the geetest and get to challenge
  21. func PreProcess(c context.Context, mid int64, ip, clientType string, newCaptcha int, captchaID string) (challenge string, err error) {
  22. var (
  23. bs []byte
  24. params url.Values
  25. )
  26. params = url.Values{}
  27. params.Set("user_id", strconv.FormatInt(mid, 10))
  28. params.Set("new_captcha", strconv.Itoa(newCaptcha))
  29. params.Set("client_type", clientType)
  30. params.Set("ip_address", ip)
  31. params.Set("gt", captchaID)
  32. if bs, err = Get(c, _register, params); err != nil {
  33. return
  34. }
  35. if len(bs) != 32 {
  36. return
  37. }
  38. challenge = string(bs)
  39. return
  40. }
  41. // Validate recheck the challenge code and get to seccode
  42. func Validate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (res *model.ValidateRes, err error) {
  43. var (
  44. bs []byte
  45. params url.Values
  46. )
  47. params = url.Values{}
  48. params.Set("seccode", seccode)
  49. params.Set("challenge", challenge)
  50. params.Set("captchaid", captchaID)
  51. params.Set("client_type", clientType)
  52. params.Set("ip_address", ip)
  53. params.Set("json_format", "1")
  54. params.Set("sdk", "golang_3.0.0")
  55. params.Set("user_id", strconv.FormatInt(mid, 10))
  56. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  57. if bs, err = Post(c, _validate, params); err != nil {
  58. return
  59. }
  60. if err = json.Unmarshal(bs, &res); err != nil {
  61. return
  62. }
  63. return
  64. }
  65. // NewRequest new a http request.
  66. func NewRequest(method, uri string, params url.Values) (req *http.Request, err error) {
  67. if method == "GET" {
  68. req, err = http.NewRequest(method, uri+"?"+params.Encode(), nil)
  69. } else {
  70. req, err = http.NewRequest(method, uri, strings.NewReader(params.Encode()))
  71. }
  72. if err != nil {
  73. return
  74. }
  75. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  76. return
  77. }
  78. // Do handler request
  79. func Do(c context.Context, req *http.Request) (body []byte, err error) {
  80. var res *http.Response
  81. dialer := &net.Dialer{
  82. Timeout: time.Duration(1 * int64(time.Second)),
  83. KeepAlive: time.Duration(1 * int64(time.Second)),
  84. }
  85. transport := &http.Transport{
  86. DialContext: dialer.DialContext,
  87. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  88. }
  89. client := &http.Client{
  90. Transport: transport,
  91. }
  92. req = req.WithContext(c)
  93. if res, err = client.Do(req); err != nil {
  94. return
  95. }
  96. defer res.Body.Close()
  97. if res.StatusCode >= http.StatusInternalServerError {
  98. err = errors.New("http status code 5xx")
  99. return
  100. }
  101. if body, err = ioutil.ReadAll(res.Body); err != nil {
  102. return
  103. }
  104. return
  105. }
  106. // Get client.Get send GET request
  107. func Get(c context.Context, uri string, params url.Values) (body []byte, err error) {
  108. req, err := NewRequest("GET", uri, params)
  109. if err != nil {
  110. return
  111. }
  112. body, err = Do(c, req)
  113. return
  114. }
  115. // Post client.Get send POST request
  116. func Post(c context.Context, uri string, params url.Values) (body []byte, err error) {
  117. req, err := NewRequest("POST", uri, params)
  118. if err != nil {
  119. return
  120. }
  121. body, err = Do(c, req)
  122. return
  123. }