mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-17 05:33:42 +08:00
🔖 chore: Rename relay/util to relay/relay_util package and add utils package
This commit is contained in:
@@ -1,87 +1,24 @@
|
||||
package requester
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
type ContextKey string
|
||||
|
||||
const ProxyHTTPAddrKey ContextKey = "proxyHttpAddr"
|
||||
const ProxySock5AddrKey ContextKey = "proxySock5Addr"
|
||||
|
||||
func proxyFunc(req *http.Request) (*url.URL, error) {
|
||||
proxyAddr := req.Context().Value(ProxyHTTPAddrKey)
|
||||
if proxyAddr == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
proxyURL, err := url.Parse(proxyAddr.(string))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing proxy address: %w", err)
|
||||
}
|
||||
|
||||
switch proxyURL.Scheme {
|
||||
case "http", "https":
|
||||
return proxyURL, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unsupported proxy scheme: %s", proxyURL.Scheme)
|
||||
}
|
||||
|
||||
func socks5ProxyFunc(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
// 设置TCP超时
|
||||
dialer := &net.Dialer{
|
||||
Timeout: time.Duration(common.GetOrDefault("connect_timeout", 5)) * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}
|
||||
|
||||
// 从上下文中获取代理地址
|
||||
proxyAddr, ok := ctx.Value(ProxySock5AddrKey).(string)
|
||||
if !ok {
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
proxyURL, err := url.Parse(proxyAddr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing proxy address: %w", err)
|
||||
}
|
||||
var auth *proxy.Auth = nil
|
||||
password, isSetPassword := proxyURL.User.Password()
|
||||
if isSetPassword {
|
||||
auth = &proxy.Auth{
|
||||
User: proxyURL.User.Username(),
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
proxyDialer, err := proxy.SOCKS5("tcp", proxyURL.Host, auth, proxy.Direct)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating socks5 dialer: %w", err)
|
||||
}
|
||||
|
||||
return proxyDialer.Dial(network, addr)
|
||||
}
|
||||
|
||||
var HTTPClient *http.Client
|
||||
|
||||
func InitHttpClient() {
|
||||
trans := &http.Transport{
|
||||
DialContext: socks5ProxyFunc,
|
||||
Proxy: proxyFunc,
|
||||
DialContext: utils.Socks5ProxyFunc,
|
||||
Proxy: utils.ProxyFunc,
|
||||
}
|
||||
|
||||
HTTPClient = &http.Client{
|
||||
Transport: trans,
|
||||
}
|
||||
|
||||
relayTimeout := common.GetOrDefault("relay_timeout", 600)
|
||||
relayTimeout := utils.GetOrDefault("relay_timeout", 600)
|
||||
if relayTimeout != 0 {
|
||||
HTTPClient.Timeout = time.Duration(relayTimeout) * time.Second
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"one-api/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -52,18 +53,7 @@ type requestOptions struct {
|
||||
type requestOption func(*requestOptions)
|
||||
|
||||
func (r *HTTPRequester) setProxy() context.Context {
|
||||
if r.proxyAddr == "" {
|
||||
return r.Context
|
||||
}
|
||||
|
||||
// 如果是以 socks5:// 开头的地址,那么使用 socks5 代理
|
||||
if strings.HasPrefix(r.proxyAddr, "socks5://") {
|
||||
return context.WithValue(r.Context, ProxySock5AddrKey, r.proxyAddr)
|
||||
}
|
||||
|
||||
// 否则使用 http 代理
|
||||
return context.WithValue(r.Context, ProxyHTTPAddrKey, r.proxyAddr)
|
||||
|
||||
return utils.SetProxy(r.Context, r.proxyAddr)
|
||||
}
|
||||
|
||||
// 创建请求
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
|
||||
func GetWSClient(proxyAddr string) *websocket.Dialer {
|
||||
dialer := &websocket.Dialer{
|
||||
HandshakeTimeout: time.Duration(common.GetOrDefault("connect_timeout", 5)) * time.Second,
|
||||
HandshakeTimeout: time.Duration(utils.GetOrDefault("connect_timeout", 5)) * time.Second,
|
||||
}
|
||||
|
||||
if proxyAddr != "" {
|
||||
@@ -38,20 +39,16 @@ func setWSProxy(dialer *websocket.Dialer, proxyAddr string) error {
|
||||
case "http", "https":
|
||||
dialer.Proxy = http.ProxyURL(proxyURL)
|
||||
case "socks5":
|
||||
var auth *proxy.Auth = nil
|
||||
password, isSetPassword := proxyURL.User.Password()
|
||||
if isSetPassword {
|
||||
auth = &proxy.Auth{
|
||||
User: proxyURL.User.Username(),
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
socks5Proxy, err := proxy.SOCKS5("tcp", proxyURL.Host, auth, proxy.Direct)
|
||||
proxyDialer, err := proxy.FromURL(proxyURL, proxy.Direct)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating socks5 dialer: %w", err)
|
||||
return fmt.Errorf("error creating proxy dialer: %w", err)
|
||||
}
|
||||
originalNetDial := dialer.NetDial
|
||||
dialer.NetDial = func(network, addr string) (net.Conn, error) {
|
||||
return socks5Proxy.Dial(network, addr)
|
||||
if originalNetDial != nil {
|
||||
return originalNetDial(network, addr)
|
||||
}
|
||||
return proxyDialer.Dial(network, addr)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unsupported proxy scheme: %s", proxyURL.Scheme)
|
||||
|
||||
Reference in New Issue
Block a user