From 476e42b919d9a95a8d7111b1969083c58a9a6e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E5=A2=A8?= Date: Wed, 11 Feb 2026 11:25:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(channel):=20=E5=A2=9E=E5=8A=A0HTTP=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=E5=B9=B6?= =?UTF-8?q?=E6=94=B9=E8=BF=9BSOCKS5=E4=BB=A3=E7=90=86=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将Telegram和微信企业号通道的HTTP客户端超时统一延长至30秒,避免因网络延迟导致请求失败 - 修复SOCKS5代理连接未正确处理上下文取消的问题,现在支持带认证的SOCKS5代理 - 当无法获取默认HTTP传输时,提供完整的传输配置而非空结构 --- channel/http-client.go | 45 ++++++++++++++++++++++++++++++---- channel/telegram.go | 2 +- channel/wechat-corp-account.go | 4 +-- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/channel/http-client.go b/channel/http-client.go index 2fb7ca8..bb0f076 100644 --- a/channel/http-client.go +++ b/channel/http-client.go @@ -26,7 +26,17 @@ func newHTTPClient(proxyAddress string, timeout time.Duration) (*http.Client, er func newHTTPTransport(proxyAddress string) (*http.Transport, error) { base, ok := http.DefaultTransport.(*http.Transport) if !ok { - return &http.Transport{}, nil + return &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 10 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + }, nil } transport := base.Clone() proxyAddress = strings.TrimSpace(proxyAddress) @@ -46,15 +56,40 @@ func newHTTPTransport(proxyAddress string) (*http.Transport, error) { transport.Proxy = http.ProxyURL(u) return transport, nil case "socks5", "socks5h": - normalized := *u - normalized.Scheme = "socks5" - dialer, err := proxy.FromURL(&normalized, proxy.Direct) + var auth *proxy.Auth + if u.User != nil { + auth = &proxy.Auth{ + User: u.User.Username(), + Password: "", + } + if password, ok := u.User.Password(); ok { + auth.Password = password + } + } + dialer, err := proxy.SOCKS5("tcp", u.Host, auth, &net.Dialer{ + Timeout: 10 * time.Second, + KeepAlive: 30 * time.Second, + }) if err != nil { return nil, err } transport.Proxy = nil transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { - return dialer.Dial(network, addr) + type dialResult struct { + conn net.Conn + err error + } + ch := make(chan dialResult, 1) + go func() { + conn, err := dialer.Dial(network, addr) + ch <- dialResult{conn: conn, err: err} + }() + select { + case <-ctx.Done(): + return nil, ctx.Err() + case r := <-ch: + return r.conn, r.err + } } return transport, nil default: diff --git a/channel/telegram.go b/channel/telegram.go index 64bcc6e..69d210d 100644 --- a/channel/telegram.go +++ b/channel/telegram.go @@ -25,7 +25,7 @@ type telegramMessageResponse struct { func SendTelegramMessage(message *model.Message, user *model.User, channel_ *model.Channel) error { // https://core.telegram.org/bots/api#sendmessage - client, err := newHTTPClient(channel_.URL, 10*time.Second) + client, err := newHTTPClient(channel_.URL, 30*time.Second) if err != nil { return err } diff --git a/channel/wechat-corp-account.go b/channel/wechat-corp-account.go index 1c877eb..e445a2f 100644 --- a/channel/wechat-corp-account.go +++ b/channel/wechat-corp-account.go @@ -49,7 +49,7 @@ func (i *WeChatCorpAccountTokenStoreItem) Token() string { func (i *WeChatCorpAccountTokenStoreItem) Refresh() { // https://work.weixin.qq.com/api/doc/90000/90135/91039 - client, err := newHTTPClient(i.Proxy, 5*time.Second) + client, err := newHTTPClient(i.Proxy, 30*time.Second) if err != nil { common.SysError("failed to create http client: " + err.Error()) return @@ -159,7 +159,7 @@ func SendWeChatCorpMessage(message *model.Message, user *model.User, channel_ *m Proxy: proxyAddress, } accessToken := TokenStoreGetToken(tokenItem.Key()) - client, err := newHTTPClient(proxyAddress, 10*time.Second) + client, err := newHTTPClient(proxyAddress, 30*time.Second) if err != nil { return err }