mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-18 09:24:07 +00:00
feat: support user content download proxy & relay proxy now
This commit is contained in:
60
common/client/init.go
Normal file
60
common/client/init.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
var HTTPClient *http.Client
|
||||
var ImpatientHTTPClient *http.Client
|
||||
var UserContentRequestHTTPClient *http.Client
|
||||
|
||||
func Init() {
|
||||
if config.UserContentRequestProxy != "" {
|
||||
logger.SysLog(fmt.Sprintf("using %s as proxy to fetch user content", config.UserContentRequestProxy))
|
||||
proxyURL, err := url.Parse(config.UserContentRequestProxy)
|
||||
if err != nil {
|
||||
logger.FatalLog(fmt.Sprintf("USER_CONTENT_REQUEST_PROXY set but invalid: %s", config.UserContentRequestProxy))
|
||||
}
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
}
|
||||
UserContentRequestHTTPClient = &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: time.Second * time.Duration(config.UserContentRequestTimeout),
|
||||
}
|
||||
} else {
|
||||
UserContentRequestHTTPClient = &http.Client{}
|
||||
}
|
||||
var transport http.RoundTripper
|
||||
if config.RelayProxy != "" {
|
||||
logger.SysLog(fmt.Sprintf("using %s as api relay proxy", config.RelayProxy))
|
||||
proxyURL, err := url.Parse(config.RelayProxy)
|
||||
if err != nil {
|
||||
logger.FatalLog(fmt.Sprintf("USER_CONTENT_REQUEST_PROXY set but invalid: %s", config.UserContentRequestProxy))
|
||||
}
|
||||
transport = &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
}
|
||||
}
|
||||
|
||||
if config.RelayTimeout == 0 {
|
||||
HTTPClient = &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
} else {
|
||||
HTTPClient = &http.Client{
|
||||
Timeout: time.Duration(config.RelayTimeout) * time.Second,
|
||||
Transport: transport,
|
||||
}
|
||||
}
|
||||
|
||||
ImpatientHTTPClient = &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
Transport: transport,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user