gee.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package dao
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "encoding/json"
  6. "errors"
  7. "go-common/app/service/live/xcaptcha/conf"
  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. //ValidateRes 验证返回值
  21. type ValidateRes struct {
  22. Seccode string `json:"seccode"`
  23. }
  24. // GeeClient ...
  25. type GeeClient struct {
  26. client *http.Client
  27. clientPost *http.Client
  28. }
  29. // NewGeeClient new httpClient
  30. func NewGeeClient(c *conf.GeeTestConfig) (client *GeeClient) {
  31. client = &GeeClient{
  32. client: NewHttpClient(c.Get.Timeout, c.Get.KeepAlive),
  33. clientPost: NewHttpClient(c.Post.Timeout, c.Post.KeepAlive),
  34. }
  35. return client
  36. }
  37. // Register preprocessing the geetest and get to challenge
  38. func (d *Dao) Register(c context.Context, ip, clientType string, newCaptcha int, captchaID string) (challenge string, err error) {
  39. challenge = ""
  40. var (
  41. bs []byte
  42. params url.Values
  43. )
  44. params = url.Values{}
  45. params.Set("new_captcha", strconv.Itoa(newCaptcha))
  46. params.Set("client_type", clientType)
  47. params.Set("ip_address", ip)
  48. params.Set("gt", captchaID)
  49. if bs, err = d.Get(c, _register, params); err != nil {
  50. return
  51. }
  52. if len(bs) != 32 {
  53. return
  54. }
  55. challenge = string(bs)
  56. return
  57. }
  58. // Validate recheck the challenge code and get to seccode
  59. func (d *Dao) Validate(c context.Context, challenge string, seccode string, clientType string, ip string, mid int64, captchaID string) (res *ValidateRes, err error) {
  60. var (
  61. bs []byte
  62. params url.Values
  63. )
  64. params = url.Values{}
  65. params.Set("seccode", seccode)
  66. params.Set("challenge", challenge)
  67. params.Set("captchaid", captchaID)
  68. params.Set("client_type", clientType)
  69. params.Set("ip_address", ip)
  70. params.Set("json_format", "1")
  71. params.Set("sdk", "golang_3.0.0")
  72. params.Set("user_id", strconv.FormatInt(mid, 10))
  73. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  74. res = &ValidateRes{}
  75. if bs, err = d.Post(c, _validate, params); err != nil {
  76. return
  77. }
  78. if err = json.Unmarshal(bs, &res); err != nil {
  79. return
  80. }
  81. return
  82. }
  83. // NewRequest new a http request.
  84. func NewRequest(method, uri string, params url.Values) (req *http.Request, err error) {
  85. if method == "GET" {
  86. req, err = http.NewRequest(method, uri+"?"+params.Encode(), nil)
  87. } else {
  88. req, err = http.NewRequest(method, uri, strings.NewReader(params.Encode()))
  89. }
  90. if err != nil {
  91. return
  92. }
  93. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  94. return
  95. }
  96. // Do handler request
  97. func (d *Dao) Do(c context.Context, req *http.Request, client *http.Client) (body []byte, err error) {
  98. var res *http.Response
  99. req = req.WithContext(c)
  100. if res, err = client.Do(req); err != nil {
  101. return
  102. }
  103. defer res.Body.Close()
  104. if res.StatusCode >= http.StatusInternalServerError {
  105. err = errors.New("http status code 5xx")
  106. return
  107. }
  108. if body, err = ioutil.ReadAll(res.Body); err != nil {
  109. return
  110. }
  111. return
  112. }
  113. // Get client.Get send GET request
  114. func (d *Dao) Get(c context.Context, uri string, params url.Values) (body []byte, err error) {
  115. req, err := NewRequest("GET", uri, params)
  116. if err != nil {
  117. return
  118. }
  119. body, err = d.Do(c, req, d.geeClient.client)
  120. return
  121. }
  122. // Post client.Get send POST request
  123. func (d *Dao) Post(c context.Context, uri string, params url.Values) (body []byte, err error) {
  124. req, err := NewRequest("POST", uri, params)
  125. if err != nil {
  126. return
  127. }
  128. body, err = d.Do(c, req, d.geeClient.clientPost)
  129. return
  130. }
  131. // NewHttpClient new a http client.
  132. func NewHttpClient(timeout int64, keepAlive int64) (client *http.Client) {
  133. var (
  134. transport *http.Transport
  135. dialer *net.Dialer
  136. )
  137. dialer = &net.Dialer{
  138. Timeout: time.Duration(time.Duration(timeout) * time.Millisecond),
  139. KeepAlive: time.Duration(time.Duration(keepAlive) * time.Second),
  140. }
  141. transport = &http.Transport{
  142. DialContext: dialer.DialContext,
  143. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  144. }
  145. client = &http.Client{
  146. Transport: transport,
  147. }
  148. return
  149. }