http_client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "net/url"
  8. "one-api/common"
  9. "sync"
  10. "time"
  11. "golang.org/x/net/proxy"
  12. )
  13. var (
  14. httpClient *http.Client
  15. proxyClientLock sync.Mutex
  16. proxyClients = make(map[string]*http.Client)
  17. )
  18. func InitHttpClient() {
  19. if common.RelayTimeout == 0 {
  20. httpClient = &http.Client{}
  21. } else {
  22. httpClient = &http.Client{
  23. Timeout: time.Duration(common.RelayTimeout) * time.Second,
  24. }
  25. }
  26. }
  27. func GetHttpClient() *http.Client {
  28. return httpClient
  29. }
  30. // ResetProxyClientCache 清空代理客户端缓存,确保下次使用时重新初始化
  31. func ResetProxyClientCache() {
  32. proxyClientLock.Lock()
  33. defer proxyClientLock.Unlock()
  34. for _, client := range proxyClients {
  35. if transport, ok := client.Transport.(*http.Transport); ok && transport != nil {
  36. transport.CloseIdleConnections()
  37. }
  38. }
  39. proxyClients = make(map[string]*http.Client)
  40. }
  41. // NewProxyHttpClient 创建支持代理的 HTTP 客户端
  42. func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
  43. if proxyURL == "" {
  44. return http.DefaultClient, nil
  45. }
  46. proxyClientLock.Lock()
  47. if client, ok := proxyClients[proxyURL]; ok {
  48. proxyClientLock.Unlock()
  49. return client, nil
  50. }
  51. proxyClientLock.Unlock()
  52. parsedURL, err := url.Parse(proxyURL)
  53. if err != nil {
  54. return nil, err
  55. }
  56. switch parsedURL.Scheme {
  57. case "http", "https":
  58. client := &http.Client{
  59. Transport: &http.Transport{
  60. Proxy: http.ProxyURL(parsedURL),
  61. },
  62. }
  63. client.Timeout = time.Duration(common.RelayTimeout) * time.Second
  64. proxyClientLock.Lock()
  65. proxyClients[proxyURL] = client
  66. proxyClientLock.Unlock()
  67. return client, nil
  68. case "socks5", "socks5h":
  69. // 获取认证信息
  70. var auth *proxy.Auth
  71. if parsedURL.User != nil {
  72. auth = &proxy.Auth{
  73. User: parsedURL.User.Username(),
  74. Password: "",
  75. }
  76. if password, ok := parsedURL.User.Password(); ok {
  77. auth.Password = password
  78. }
  79. }
  80. // 创建 SOCKS5 代理拨号器
  81. // proxy.SOCKS5 使用 tcp 参数,所有 TCP 连接包括 DNS 查询都将通过代理进行。行为与 socks5h 相同
  82. dialer, err := proxy.SOCKS5("tcp", parsedURL.Host, auth, proxy.Direct)
  83. if err != nil {
  84. return nil, err
  85. }
  86. client := &http.Client{
  87. Transport: &http.Transport{
  88. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  89. return dialer.Dial(network, addr)
  90. },
  91. },
  92. }
  93. client.Timeout = time.Duration(common.RelayTimeout) * time.Second
  94. proxyClientLock.Lock()
  95. proxyClients[proxyURL] = client
  96. proxyClientLock.Unlock()
  97. return client, nil
  98. default:
  99. return nil, fmt.Errorf("unsupported proxy scheme: %s, must be http, https, socks5 or socks5h", parsedURL.Scheme)
  100. }
  101. }