dao.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package geetest
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "go-common/app/interface/main/creative/conf"
  6. "go-common/app/interface/main/creative/model/geetest"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. httpx "go-common/library/net/http/blademaster"
  10. "io/ioutil"
  11. "net"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. const (
  19. _register = "/register.php"
  20. _validate = "/validate.php"
  21. )
  22. // Dao is account dao.
  23. type Dao struct {
  24. c *conf.Config
  25. // url
  26. registerURI string
  27. validateURI string
  28. // http client
  29. client *http.Client
  30. clientx *httpx.Client
  31. }
  32. // New new a dao.
  33. func New(c *conf.Config) (d *Dao) {
  34. d = &Dao{
  35. c: c,
  36. registerURI: c.Host.Geetest + _register,
  37. validateURI: c.Host.Geetest + _validate,
  38. // http client
  39. client: NewClient(c.HTTPClient),
  40. clientx: httpx.NewClient(c.HTTPClient.Slow),
  41. }
  42. return
  43. }
  44. // PreProcess preprocessing the geetest and get to challenge
  45. func (d *Dao) PreProcess(c context.Context, mid int64, ip, clientType string, newCaptcha int) (challenge string, err error) {
  46. var (
  47. req *http.Request
  48. res *http.Response
  49. bs []byte
  50. params url.Values
  51. )
  52. params = url.Values{}
  53. params.Set("user_id", strconv.FormatInt(mid, 10))
  54. params.Set("new_captcha", strconv.Itoa(newCaptcha))
  55. params.Set("client_type", clientType)
  56. params.Set("ip_address", ip)
  57. params.Set("gt", d.c.Geetest.CaptchaID)
  58. if req, err = http.NewRequest("GET", d.registerURI+"?"+params.Encode(), nil); err != nil {
  59. log.Error("d.preprocess uri(%s) params(%s) error(%v)", d.registerURI, params.Encode(), err)
  60. err = ecode.CreativeGeetestAPIErr
  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. err = ecode.CreativeGeetestAPIErr
  67. return
  68. }
  69. defer res.Body.Close()
  70. if res.StatusCode >= http.StatusInternalServerError {
  71. log.Error("gtServerErr uri(%s) error(%v)", d.registerURI+"?"+params.Encode(), err)
  72. err = ecode.CreativeGeetestAPIErr
  73. return
  74. }
  75. if bs, err = ioutil.ReadAll(res.Body); err != nil {
  76. log.Error("ioutil.ReadAll(%s) uri(%s) error(%v)", bs, d.registerURI+"?"+params.Encode(), err)
  77. return
  78. }
  79. if len(bs) != 32 {
  80. log.Error("d.preprocess len(%s) the length not equate 32byte", string(bs))
  81. return
  82. }
  83. challenge = string(bs)
  84. return
  85. }
  86. // Validate recheck the challenge code and get to seccode
  87. func (d *Dao) Validate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (res *geetest.ValidateRes, err error) {
  88. params := url.Values{}
  89. params.Set("seccode", seccode)
  90. params.Set("challenge", challenge)
  91. params.Set("captchaid", captchaID)
  92. params.Set("client_type", clientType)
  93. params.Set("ip_address", ip)
  94. params.Set("json_format", "1")
  95. params.Set("sdk", "golang_3.0.0")
  96. params.Set("user_id", strconv.FormatInt(mid, 10))
  97. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  98. req, err := http.NewRequest("POST", d.validateURI, strings.NewReader(params.Encode()))
  99. if err != nil {
  100. log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, d.validateURI, params.Encode())
  101. err = ecode.CreativeGeetestAPIErr
  102. return
  103. }
  104. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  105. if err = d.clientx.Do(c, req, &res); err != nil {
  106. log.Error("d.client.Do error(%v)", err)
  107. err = ecode.CreativeGeetestAPIErr
  108. return
  109. }
  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.Timeout),
  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. }