mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 12:06:38 +08:00
fix(telegram): honor proxy fallbacks for bot api
This commit is contained in:
@@ -53,26 +53,45 @@ func telegramHTTPClient(timeout time.Duration, cfg map[string]string) *http.Clie
|
||||
func telegramHTTPClients(timeout time.Duration, cfg map[string]string) []*http.Client {
|
||||
clients := []*http.Client{}
|
||||
seen := map[string]bool{}
|
||||
for _, proxyRaw := range telegramProxyCandidates(cfg) {
|
||||
proxyURL, err := normalizeProxyURL(proxyRaw, "http")
|
||||
if err != nil || proxyURL == nil {
|
||||
continue
|
||||
|
||||
addProxy := func(proxyURL *url.URL) {
|
||||
if proxyURL == nil {
|
||||
return
|
||||
}
|
||||
key := proxyURL.String()
|
||||
if seen[key] {
|
||||
continue
|
||||
return
|
||||
}
|
||||
seen[key] = true
|
||||
transport := NewExternalTransport()
|
||||
transport.Proxy = http.ProxyURL(proxyURL)
|
||||
clients = append(clients, &http.Client{Timeout: timeout, Transport: transport})
|
||||
}
|
||||
|
||||
for _, proxyRaw := range telegramExplicitProxyCandidates(cfg) {
|
||||
proxyURL, err := normalizeProxyURL(proxyRaw, "http")
|
||||
if err != nil || proxyURL == nil {
|
||||
continue
|
||||
}
|
||||
addProxy(proxyURL)
|
||||
}
|
||||
if proxyURL, err := telegramAutoProxyURL(cfg); err == nil {
|
||||
addProxy(proxyURL)
|
||||
}
|
||||
for _, proxyRaw := range telegramFallbackProxyCandidates() {
|
||||
proxyURL, err := normalizeProxyURL(proxyRaw, "http")
|
||||
if err != nil || proxyURL == nil {
|
||||
continue
|
||||
}
|
||||
addProxy(proxyURL)
|
||||
}
|
||||
transport := NewExternalTransport()
|
||||
transport.Proxy = nil
|
||||
clients = append(clients, &http.Client{Timeout: timeout, Transport: transport})
|
||||
return clients
|
||||
}
|
||||
|
||||
func telegramProxyCandidates(cfg map[string]string) []string {
|
||||
func telegramExplicitProxyCandidates(cfg map[string]string) []string {
|
||||
out := []string{}
|
||||
for _, value := range []string{
|
||||
cfg["proxy_url"],
|
||||
@@ -82,22 +101,48 @@ func telegramProxyCandidates(cfg map[string]string) []string {
|
||||
out = append(out, value)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func telegramProxyCandidates(cfg map[string]string) []string {
|
||||
out := telegramExplicitProxyCandidates(cfg)
|
||||
if len(out) > 0 {
|
||||
return out
|
||||
}
|
||||
for _, value := range []string{
|
||||
"http://127.0.0.1:10808",
|
||||
"http://127.0.0.1:10809",
|
||||
"http://127.0.0.1:7890",
|
||||
"http://127.0.0.1:7891",
|
||||
return telegramFallbackProxyCandidates()
|
||||
}
|
||||
|
||||
func telegramFallbackProxyCandidates() []string {
|
||||
// Common local proxy ports used by Clash / v2rayN / v2rayA. Docker on Linux
|
||||
// reaches host services through host.docker.internal (when extra_hosts is
|
||||
// configured) or the bridge gateway 172.17.0.1. These are only fallbacks:
|
||||
// explicit channel/env proxy and environment/system proxy are tried first.
|
||||
return []string{
|
||||
"http://host.docker.internal:20171",
|
||||
"socks5://host.docker.internal:20170",
|
||||
"http://172.17.0.1:20171",
|
||||
"socks5://172.17.0.1:20170",
|
||||
"http://host.docker.internal:7890",
|
||||
"http://host.docker.internal:10808",
|
||||
"http://172.17.0.1:7890",
|
||||
"http://172.17.0.1:10808",
|
||||
} {
|
||||
out = append(out, value)
|
||||
"http://127.0.0.1:10808",
|
||||
"http://127.0.0.1:10809",
|
||||
"http://127.0.0.1:7890",
|
||||
"http://127.0.0.1:7891",
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func telegramAutoProxyURL(cfg map[string]string) (*url.URL, error) {
|
||||
base := telegramAPIBaseURL(cfg)
|
||||
if _, err := url.ParseRequestURI(base); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, base, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ProxyFromEnvironmentOrSystem(req)
|
||||
}
|
||||
|
||||
func telegramPostForm(ctx context.Context, cfg map[string]string, method string, form url.Values, timeout time.Duration) error {
|
||||
@@ -193,6 +238,39 @@ func telegramPostJSONDecode(ctx context.Context, cfg map[string]string, method s
|
||||
return errors.New("telegram request failed")
|
||||
}
|
||||
|
||||
func telegramGetJSONDecode(ctx context.Context, cfg map[string]string, method string, timeout time.Duration, out any) error {
|
||||
apiURL, err := telegramMethodURL(cfg, cfg["bot_token"], method)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var lastErr error
|
||||
for _, client := range telegramHTTPClients(timeout, cfg) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
lastErr = sanitizeTelegramError(err)
|
||||
continue
|
||||
}
|
||||
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode >= 400 {
|
||||
lastErr = fmt.Errorf("telegram api error %d: %s", resp.StatusCode, sanitizeTelegramText(string(respBody)))
|
||||
continue
|
||||
}
|
||||
if out != nil {
|
||||
return json.Unmarshal(respBody, out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if lastErr != nil {
|
||||
return lastErr
|
||||
}
|
||||
return errors.New("telegram request failed")
|
||||
}
|
||||
|
||||
func telegramStringConfigFromAny(cfg map[string]any) map[string]string {
|
||||
out := make(map[string]string, len(cfg))
|
||||
for key, value := range cfg {
|
||||
|
||||
Reference in New Issue
Block a user