proxyClient.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package proxyClient
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gcfg"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "jygit.jydev.jianyu360.cn/dataservice/tripartite_gateway/common/db"
  8. "net"
  9. "net/http"
  10. "net/http/httputil"
  11. "net/url"
  12. "regexp"
  13. "strings"
  14. "time"
  15. )
  16. var transport = &http.Transport{}
  17. func CreateCustomProxyClient(target *url.URL, errFunc func(http.ResponseWriter, *http.Request, error), change func(resp *http.Response) error) *httputil.ReverseProxy {
  18. return &httputil.ReverseProxy{
  19. Director: func(req *http.Request) {
  20. url_ := req.URL.Path
  21. bidReg := regexp.MustCompile(".*/getAllbid/(.*)")
  22. winnerReg := regexp.MustCompile(".*/getWinner")
  23. if bidReg.MatchString(url_) || winnerReg.MatchString(url_) {
  24. urlArr := strings.Split(url_, "/")
  25. query := req.URL.Query()
  26. startTime, endTime := TimeHandle(query.Get("date"))
  27. fmt.Println(query, target.Path)
  28. req.URL.Path = target.Path
  29. req.Method = "POST"
  30. if bidReg.MatchString(url_) {
  31. req.URL.RawQuery = fmt.Sprintf("startTime=%d&page=%s&business=%s&endTime=%d", startTime, query.Get("page"), gconv.String(db.AddressMap[urlArr[len(urlArr)-1]]), endTime)
  32. }
  33. }
  34. req.URL.Scheme = target.Scheme
  35. req.URL.Host = target.Host
  36. },
  37. Transport: transport,
  38. ModifyResponse: change,
  39. ErrorHandler: errFunc}
  40. }
  41. func ReLoadClient() {
  42. transport = &http.Transport{
  43. Proxy: http.ProxyFromEnvironment,
  44. DialContext: (&net.Dialer{
  45. Timeout: time.Duration(gcfg.Instance().MustGet(gctx.New(), "proxy.timeout", 30).Int()) * time.Second, //连接超时
  46. KeepAlive: time.Duration(gcfg.Instance().MustGet(gctx.New(), "proxy.keepAlive", 60).Int()) * time.Second, //长连接超时时间
  47. DualStack: true,
  48. }).DialContext,
  49. MaxIdleConns: gcfg.Instance().MustGet(gctx.New(), "proxy.maxIdleConns", 120).Int(), //最大空闲连接 0没有限制
  50. IdleConnTimeout: time.Duration(gcfg.Instance().MustGet(gctx.New(), "proxy.idleConnTimeout", 90).Int()) * time.Second, //空闲超时时间
  51. TLSHandshakeTimeout: time.Duration(gcfg.Instance().MustGet(gctx.New(), "proxy.tLSHandshakeTimeout", 1).Int()) * time.Second, //tls握手超时时间
  52. ExpectContinueTimeout: time.Duration(gcfg.Instance().MustGet(gctx.New(), "proxy.expectContinueTimeout", 1).Int()) * time.Second, //100-continue 超时时间
  53. MaxIdleConnsPerHost: gcfg.Instance().MustGet(gctx.New(), "proxy.maxIdleConnsPerHost", 5).Int(), //客户端可以持有的最大空闲连接
  54. }
  55. }
  56. func TimeHandle(timeStr string) (int64, int64) {
  57. // 解析时间字符串
  58. specificTime, _ := time.Parse(time.DateOnly, timeStr)
  59. // 获取该时间的开始时间(00:00:00)
  60. startOfDay := time.Date(specificTime.Year(), specificTime.Month(), specificTime.Day(), 0, 0, 0, 0, specificTime.Location())
  61. // 获取该时间的截止时间(23:59:59)
  62. endOfDay := time.Date(specificTime.Year(), specificTime.Month(), specificTime.Day(), 23, 59, 59, 0, specificTime.Location())
  63. return startOfDay.Unix(), endOfDay.Unix()
  64. }