geetest.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package geetest
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "go-common/app/interface/main/account/conf"
  13. "go-common/app/interface/main/account/model"
  14. "go-common/library/log"
  15. bm "go-common/library/net/http/blademaster"
  16. "go-common/library/net/metadata"
  17. "github.com/pkg/errors"
  18. )
  19. const (
  20. _register = "http://api.geetest.com/register.php"
  21. _validate = "http://api.geetest.com/validate.php"
  22. )
  23. // Dao is account dao.
  24. type Dao struct {
  25. c *conf.Config
  26. // url
  27. registerURI string
  28. validateURI string
  29. // http client
  30. client *http.Client
  31. clientx *bm.Client
  32. }
  33. // New new a dao.
  34. func New(c *conf.Config) (d *Dao) {
  35. d = &Dao{
  36. c: c,
  37. registerURI: _register,
  38. validateURI: _validate,
  39. // http client
  40. client: NewClient(c.HTTPClient),
  41. clientx: bm.NewClient(c.HTTPClient.Slow),
  42. }
  43. return
  44. }
  45. // PreProcess preprocessing the geetest and get to challenge
  46. func (d *Dao) PreProcess(c context.Context, mid int64, geeType string, gc conf.GeetestConfig, newCaptcha int) (challenge string, err error) {
  47. var (
  48. req *http.Request
  49. res *http.Response
  50. bs []byte
  51. params url.Values
  52. )
  53. params = url.Values{}
  54. params.Set("user_id", strconv.FormatInt(mid, 10))
  55. params.Set("new_captcha", strconv.Itoa(newCaptcha))
  56. params.Set("ip_address", metadata.String(c, metadata.RemoteIP))
  57. params.Set("client_type", geeType)
  58. params.Set("gt", gc.CaptchaID)
  59. if req, err = http.NewRequest("GET", d.registerURI+"?"+params.Encode(), nil); err != nil {
  60. log.Error("d.preprocess uri(%s) params(%s) error(%v)", d.registerURI, params.Encode(), err)
  61. return
  62. }
  63. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  64. if res, err = d.client.Do(req); err != nil {
  65. log.Error("client.Do(%s) error(%v)", d.registerURI+"?"+params.Encode(), err)
  66. return
  67. }
  68. defer res.Body.Close()
  69. if res.StatusCode >= http.StatusInternalServerError {
  70. err = errors.New("http status code 5xx")
  71. log.Error("gtServerErr uri(%s) error(%v)", d.validateURI+"?"+params.Encode(), err)
  72. return
  73. }
  74. if bs, err = ioutil.ReadAll(res.Body); err != nil {
  75. log.Error("ioutil.ReadAll(%s) uri(%s) error(%v)", bs, d.validateURI+"?"+params.Encode(), err)
  76. return
  77. }
  78. if len(bs) != 32 {
  79. log.Error("d.preprocess len(%s) the length not equate 32byte", string(bs))
  80. return
  81. }
  82. challenge = string(bs)
  83. return
  84. }
  85. // Validate recheck the challenge code and get to seccode
  86. func (d *Dao) Validate(c context.Context, challenge, seccode, clientType, captchaID string, mid int64) (res *model.ValidateRes, err error) {
  87. params := url.Values{}
  88. params.Set("seccode", seccode)
  89. params.Set("challenge", challenge)
  90. params.Set("captchaid", captchaID)
  91. params.Set("client_type", clientType)
  92. params.Set("ip_address", metadata.String(c, metadata.RemoteIP))
  93. params.Set("json_format", "1")
  94. params.Set("sdk", "golang_3.0.0")
  95. params.Set("user_id", strconv.FormatInt(mid, 10))
  96. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  97. req, err := http.NewRequest("POST", d.validateURI, strings.NewReader(params.Encode()))
  98. if err != nil {
  99. log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, d.validateURI, params.Encode())
  100. return
  101. }
  102. log.Info("Validate(%v) start", params)
  103. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  104. res = new(model.ValidateRes)
  105. if err = d.clientx.Do(c, req, &res); err != nil {
  106. log.Error("d.client.Do error(%v) | uri(%s) params(%s) res(%v)", err, d.validateURI, params.Encode(), res)
  107. return
  108. }
  109. log.Info("Validate(%v) suc", res)
  110. return
  111. }
  112. // NewClient new a http client.
  113. func NewClient(c *conf.HTTPClient) (client *http.Client) {
  114. var (
  115. transport *http.Transport
  116. dialer *net.Dialer
  117. )
  118. dialer = &net.Dialer{
  119. Timeout: time.Duration(c.Slow.Dial),
  120. KeepAlive: time.Duration(c.Slow.KeepAlive),
  121. }
  122. transport = &http.Transport{
  123. DialContext: dialer.DialContext,
  124. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  125. }
  126. client = &http.Client{
  127. Transport: transport,
  128. }
  129. return
  130. }
  131. // GeeConfig get geetest config.
  132. func (d *Dao) GeeConfig(ct string, c *conf.Geetest) (gc conf.GeetestConfig, clientType string) {
  133. if ct == "" {
  134. return c.PC, "web"
  135. }
  136. switch ct {
  137. case "web":
  138. gc = c.PC
  139. clientType = "web"
  140. case "h5":
  141. gc = c.H5
  142. clientType = "h5"
  143. default:
  144. gc = c.PC
  145. clientType = "web"
  146. }
  147. return
  148. }