mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-19 06:07:28 +00:00
feat: TLS_INSECURE_SKIP_VERIFY env
This commit is contained in:
@@ -57,6 +57,9 @@
|
|||||||
# 流模式无响应超时时间,单位秒,如果出现空补全可以尝试改为更大值
|
# 流模式无响应超时时间,单位秒,如果出现空补全可以尝试改为更大值
|
||||||
# STREAMING_TIMEOUT=300
|
# STREAMING_TIMEOUT=300
|
||||||
|
|
||||||
|
# TLS / HTTP 跳过验证设置
|
||||||
|
# TLS_INSECURE_SKIP_VERIFY=false
|
||||||
|
|
||||||
# Gemini 识别图片 最大图片数量
|
# Gemini 识别图片 最大图片数量
|
||||||
# GEMINI_VISION_MAX_IMAGE_NUM=16
|
# GEMINI_VISION_MAX_IMAGE_NUM=16
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
//"os"
|
//"os"
|
||||||
//"strconv"
|
//"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -73,6 +74,9 @@ var MemoryCacheEnabled bool
|
|||||||
|
|
||||||
var LogConsumeEnabled = true
|
var LogConsumeEnabled = true
|
||||||
|
|
||||||
|
var TLSInsecureSkipVerify bool
|
||||||
|
var InsecureTLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
|
||||||
var SMTPServer = ""
|
var SMTPServer = ""
|
||||||
var SMTPPort = 587
|
var SMTPPort = 587
|
||||||
var SMTPSSLEnabled = false
|
var SMTPSSLEnabled = false
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -81,6 +82,16 @@ func InitEnv() {
|
|||||||
DebugEnabled = os.Getenv("DEBUG") == "true"
|
DebugEnabled = os.Getenv("DEBUG") == "true"
|
||||||
MemoryCacheEnabled = os.Getenv("MEMORY_CACHE_ENABLED") == "true"
|
MemoryCacheEnabled = os.Getenv("MEMORY_CACHE_ENABLED") == "true"
|
||||||
IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
|
IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
|
||||||
|
TLSInsecureSkipVerify = GetEnvOrDefaultBool("TLS_INSECURE_SKIP_VERIFY", false)
|
||||||
|
if TLSInsecureSkipVerify {
|
||||||
|
if tr, ok := http.DefaultTransport.(*http.Transport); ok && tr != nil {
|
||||||
|
if tr.TLSClientConfig != nil {
|
||||||
|
tr.TLSClientConfig.InsecureSkipVerify = true
|
||||||
|
} else {
|
||||||
|
tr.TLSClientConfig = InsecureTLSConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse requestInterval and set RequestInterval
|
// Parse requestInterval and set RequestInterval
|
||||||
requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
|
requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
|
||||||
|
|||||||
@@ -99,6 +99,9 @@ func newHTTPClient() *http.Client {
|
|||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
ResponseHeaderTimeout: time.Duration(timeoutSec) * time.Second,
|
ResponseHeaderTimeout: time.Duration(timeoutSec) * time.Second,
|
||||||
}
|
}
|
||||||
|
if common.TLSInsecureSkipVerify {
|
||||||
|
transport.TLSClientConfig = common.InsecureTLSConfig
|
||||||
|
}
|
||||||
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
host, _, err := net.SplitHostPort(addr)
|
host, _, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -115,7 +118,17 @@ func newHTTPClient() *http.Client {
|
|||||||
return &http.Client{Transport: transport}
|
return &http.Client{Transport: transport}
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpClient = newHTTPClient()
|
var (
|
||||||
|
httpClientOnce sync.Once
|
||||||
|
httpClient *http.Client
|
||||||
|
)
|
||||||
|
|
||||||
|
func getHTTPClient() *http.Client {
|
||||||
|
httpClientOnce.Do(func() {
|
||||||
|
httpClient = newHTTPClient()
|
||||||
|
})
|
||||||
|
return httpClient
|
||||||
|
}
|
||||||
|
|
||||||
func fetchJSON[T any](ctx context.Context, url string, out *upstreamEnvelope[T]) error {
|
func fetchJSON[T any](ctx context.Context, url string, out *upstreamEnvelope[T]) error {
|
||||||
var lastErr error
|
var lastErr error
|
||||||
@@ -138,7 +151,7 @@ func fetchJSON[T any](ctx context.Context, url string, out *upstreamEnvelope[T])
|
|||||||
}
|
}
|
||||||
cacheMutex.RUnlock()
|
cacheMutex.RUnlock()
|
||||||
|
|
||||||
resp, err := httpClient.Do(req)
|
resp, err := getHTTPClient().Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lastErr = err
|
lastErr = err
|
||||||
// backoff with jitter
|
// backoff with jitter
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/QuantumNous/new-api/common"
|
||||||
"github.com/QuantumNous/new-api/logger"
|
"github.com/QuantumNous/new-api/logger"
|
||||||
|
|
||||||
"github.com/QuantumNous/new-api/dto"
|
"github.com/QuantumNous/new-api/dto"
|
||||||
@@ -110,6 +111,9 @@ func FetchUpstreamRatios(c *gin.Context) {
|
|||||||
|
|
||||||
dialer := &net.Dialer{Timeout: 10 * time.Second}
|
dialer := &net.Dialer{Timeout: 10 * time.Second}
|
||||||
transport := &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, ResponseHeaderTimeout: 10 * time.Second}
|
transport := &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, ResponseHeaderTimeout: 10 * time.Second}
|
||||||
|
if common.TLSInsecureSkipVerify {
|
||||||
|
transport.TLSClientConfig = common.InsecureTLSConfig
|
||||||
|
}
|
||||||
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
host, _, err := net.SplitHostPort(addr)
|
host, _, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ func InitHttpClient() {
|
|||||||
ForceAttemptHTTP2: true,
|
ForceAttemptHTTP2: true,
|
||||||
Proxy: http.ProxyFromEnvironment, // Support HTTP_PROXY, HTTPS_PROXY, NO_PROXY env vars
|
Proxy: http.ProxyFromEnvironment, // Support HTTP_PROXY, HTTPS_PROXY, NO_PROXY env vars
|
||||||
}
|
}
|
||||||
|
if common.TLSInsecureSkipVerify {
|
||||||
|
transport.TLSClientConfig = common.InsecureTLSConfig
|
||||||
|
}
|
||||||
|
|
||||||
if common.RelayTimeout == 0 {
|
if common.RelayTimeout == 0 {
|
||||||
httpClient = &http.Client{
|
httpClient = &http.Client{
|
||||||
@@ -102,13 +105,17 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
|
|||||||
|
|
||||||
switch parsedURL.Scheme {
|
switch parsedURL.Scheme {
|
||||||
case "http", "https":
|
case "http", "https":
|
||||||
|
transport := &http.Transport{
|
||||||
|
MaxIdleConns: common.RelayMaxIdleConns,
|
||||||
|
MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
||||||
|
ForceAttemptHTTP2: true,
|
||||||
|
Proxy: http.ProxyURL(parsedURL),
|
||||||
|
}
|
||||||
|
if common.TLSInsecureSkipVerify {
|
||||||
|
transport.TLSClientConfig = common.InsecureTLSConfig
|
||||||
|
}
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: transport,
|
||||||
MaxIdleConns: common.RelayMaxIdleConns,
|
|
||||||
MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
|
||||||
ForceAttemptHTTP2: true,
|
|
||||||
Proxy: http.ProxyURL(parsedURL),
|
|
||||||
},
|
|
||||||
CheckRedirect: checkRedirect,
|
CheckRedirect: checkRedirect,
|
||||||
}
|
}
|
||||||
client.Timeout = time.Duration(common.RelayTimeout) * time.Second
|
client.Timeout = time.Duration(common.RelayTimeout) * time.Second
|
||||||
@@ -137,17 +144,19 @@ func NewProxyHttpClient(proxyURL string) (*http.Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{
|
transport := &http.Transport{
|
||||||
Transport: &http.Transport{
|
MaxIdleConns: common.RelayMaxIdleConns,
|
||||||
MaxIdleConns: common.RelayMaxIdleConns,
|
MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
||||||
MaxIdleConnsPerHost: common.RelayMaxIdleConnsPerHost,
|
ForceAttemptHTTP2: true,
|
||||||
ForceAttemptHTTP2: true,
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
return dialer.Dial(network, addr)
|
||||||
return dialer.Dial(network, addr)
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
CheckRedirect: checkRedirect,
|
|
||||||
}
|
}
|
||||||
|
if common.TLSInsecureSkipVerify {
|
||||||
|
transport.TLSClientConfig = common.InsecureTLSConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{Transport: transport, CheckRedirect: checkRedirect}
|
||||||
client.Timeout = time.Duration(common.RelayTimeout) * time.Second
|
client.Timeout = time.Duration(common.RelayTimeout) * time.Second
|
||||||
proxyClientLock.Lock()
|
proxyClientLock.Lock()
|
||||||
proxyClients[proxyURL] = client
|
proxyClients[proxyURL] = client
|
||||||
|
|||||||
Reference in New Issue
Block a user