Backfill license usage and restore bot proxy fallbacks

This commit is contained in:
ShukeBta
2026-06-17 22:22:46 +08:00
parent a0aeb07f03
commit fb362e1fec
5 changed files with 54 additions and 75 deletions
+1
View File
@@ -18,6 +18,7 @@ const (
type LicenseActivationState struct {
Valid bool `json:"valid"`
LicenseKey string `json:"license_key,omitempty"`
LicenseType string `json:"license_type,omitempty"`
ExpiryDate string `json:"expiry_date,omitempty"`
MaxDevices int `json:"max_devices,omitempty"`
+14 -61
View File
@@ -53,47 +53,26 @@ 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{}
addProxy := func(proxyURL *url.URL) {
if proxyURL == nil {
return
for _, proxyRaw := range telegramProxyCandidates(cfg) {
proxyURL, err := normalizeProxyURL(proxyRaw, "http")
if err != nil || proxyURL == nil {
continue
}
key := proxyURL.String()
if seen[key] {
return
continue
}
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)
}
if telegramAPIBaseURL(cfg) == defaultTelegramAPIBaseURL {
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 telegramExplicitProxyCandidates(cfg map[string]string) []string {
func telegramProxyCandidates(cfg map[string]string) []string {
out := []string{}
for _, value := range []string{
cfg["proxy_url"],
@@ -103,48 +82,22 @@ func telegramExplicitProxyCandidates(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
}
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",
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",
"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)
}
}
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)
return out
}
func telegramPostForm(ctx context.Context, cfg map[string]string, method string, form url.Values, timeout time.Duration) error {
+1 -1
View File
@@ -242,7 +242,7 @@ func telegramCommandNames(commands []telegramBotCommand) map[string]bool {
func TestTelegramProxyCandidatesDefaultLocalFallbacks(t *testing.T) {
got := telegramProxyCandidates(map[string]string{})
joined := strings.Join(got, ",")
for _, want := range []string{"host.docker.internal:20171", "socks5://172.17.0.1:20170", "127.0.0.1:10808", "172.17.0.1:7890"} {
for _, want := range []string{"127.0.0.1:10808", "127.0.0.1:7890", "host.docker.internal:7890", "172.17.0.1:7890"} {
if !strings.Contains(joined, want) {
t.Fatalf("default proxy candidates %q missing %q", joined, want)
}