feat(channel): 为 Telegram 和微信企业号添加代理支持
Publish Docker image (amd64) / Push Docker image to multiple registries (push) Has been cancelled
Publish Docker image (arm64) / Push Docker image to multiple registries (push) Has been cancelled
Linux Release / release (push) Has been cancelled
macOS Release / release (push) Has been cancelled
Windows Release / release (push) Has been cancelled
Publish Docker image (amd64) / Push Docker image to multiple registries (push) Has been cancelled
Publish Docker image (arm64) / Push Docker image to multiple registries (push) Has been cancelled
Linux Release / release (push) Has been cancelled
macOS Release / release (push) Has been cancelled
Windows Release / release (push) Has been cancelled
新增 HTTP 客户端工厂函数以支持通过代理发送请求,代理地址可在频道配置中设置。同时修复了微信企业号 TokenStore 更新时代理地址未同步的问题,并确保 Token 存储键值包含代理信息以避免冲突。
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package channel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
func newHTTPClient(proxyAddress string, timeout time.Duration) (*http.Client, error) {
|
||||
transport, err := newHTTPTransport(proxyAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: transport,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newHTTPTransport(proxyAddress string) (*http.Transport, error) {
|
||||
base, ok := http.DefaultTransport.(*http.Transport)
|
||||
if !ok {
|
||||
return &http.Transport{}, nil
|
||||
}
|
||||
transport := base.Clone()
|
||||
proxyAddress = strings.TrimSpace(proxyAddress)
|
||||
if proxyAddress == "" {
|
||||
transport.Proxy = nil
|
||||
return transport, nil
|
||||
}
|
||||
if !strings.Contains(proxyAddress, "://") {
|
||||
proxyAddress = "http://" + proxyAddress
|
||||
}
|
||||
u, err := url.Parse(proxyAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch strings.ToLower(u.Scheme) {
|
||||
case "http", "https":
|
||||
transport.Proxy = http.ProxyURL(u)
|
||||
return transport, nil
|
||||
case "socks5", "socks5h":
|
||||
normalized := *u
|
||||
normalized.Scheme = "socks5"
|
||||
dialer, err := proxy.FromURL(&normalized, proxy.Direct)
|
||||
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)
|
||||
}
|
||||
return transport, nil
|
||||
default:
|
||||
return nil, errors.New("unsupported proxy scheme: " + strings.TrimSpace(u.Scheme))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user