dao.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package geetest
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "errors"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "go-common/app/interface/main/answer/conf"
  14. "go-common/app/interface/main/answer/model"
  15. "go-common/library/log"
  16. bm "go-common/library/net/http/blademaster"
  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 *bm.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: bm.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, geeType string, gc conf.GeetestConfig, 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("ip_address", ip)
  56. params.Set("client_type", geeType)
  57. params.Set("gt", gc.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.validateURI, params.Encode(), err)
  60. return
  61. }
  62. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  63. if res, err = d.client.Do(req); err != nil {
  64. log.Error("client.Do(%s) error(%v)", d.validateURI+"?"+params.Encode(), err)
  65. return
  66. }
  67. defer res.Body.Close()
  68. if res.StatusCode >= http.StatusInternalServerError {
  69. err = errors.New("http status code 5xx")
  70. log.Error("gtServerErr uri(%s) error(%v)", d.validateURI+"?"+params.Encode(), err)
  71. return
  72. }
  73. if bs, err = ioutil.ReadAll(res.Body); err != nil {
  74. log.Error("ioutil.ReadAll(%s) uri(%s) error(%v)", bs, d.validateURI+"?"+params.Encode(), err)
  75. return
  76. }
  77. if len(bs) != 32 {
  78. log.Error("d.preprocess len(%s) the length not equate 32byte", string(bs))
  79. return
  80. }
  81. challenge = string(bs)
  82. return
  83. }
  84. // Validate recheck the challenge code and get to seccode
  85. func (d *Dao) Validate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (res *model.ValidateRes, err error) {
  86. params := url.Values{}
  87. params.Set("seccode", seccode)
  88. params.Set("challenge", challenge)
  89. params.Set("captchaid", captchaID)
  90. params.Set("client_type", clientType)
  91. params.Set("ip_address", ip)
  92. params.Set("json_format", "1")
  93. params.Set("sdk", "golang_3.0.0")
  94. params.Set("user_id", strconv.FormatInt(mid, 10))
  95. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  96. req, err := http.NewRequest("POST", d.validateURI, strings.NewReader(params.Encode()))
  97. if err != nil {
  98. log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, d.validateURI, params.Encode())
  99. return
  100. }
  101. log.Info("Validate(%v) start", params)
  102. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  103. if err = d.clientx.Do(c, req, &res); err != nil {
  104. log.Error("d.client.Do error(%v) | uri(%s) params(%s) res(%v)", err, d.validateURI, params.Encode(), res)
  105. return
  106. }
  107. log.Info("Validate(%v) suc", res)
  108. return
  109. }
  110. // NewClient new a http client.
  111. func NewClient(c *conf.HTTPClient) (client *http.Client) {
  112. var (
  113. transport *http.Transport
  114. dialer *net.Dialer
  115. )
  116. dialer = &net.Dialer{
  117. Timeout: time.Duration(c.Slow.Dial),
  118. KeepAlive: time.Duration(c.Slow.KeepAlive),
  119. }
  120. transport = &http.Transport{
  121. DialContext: dialer.DialContext,
  122. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  123. }
  124. client = &http.Client{
  125. Transport: transport,
  126. }
  127. return
  128. }
  129. // GeeConfig get geetest config.
  130. func (d *Dao) GeeConfig(t string, c *conf.Geetest) (gc conf.GeetestConfig, geetype string) {
  131. switch t {
  132. case "pc":
  133. gc = c.PC
  134. case "h5":
  135. gc = c.H5
  136. default:
  137. gc = c.PC
  138. }
  139. geetype = "web"
  140. return
  141. }