From f638d6e494b180de521edecf300b5c6563f12fcf Mon Sep 17 00:00:00 2001 From: y0sy4 Date: Sun, 22 Mar 2026 22:17:05 +0300 Subject: [PATCH] v2.0.4: HTTP proxy support for browsers (Issue #2) --- README.md | 5 +++++ cmd/proxy/main.go | 31 +++++++++++++++++++++++++++---- internal/telegram/telegram.go | 28 ++++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2fdf0fe..b1bb852 100644 --- a/README.md +++ b/README.md @@ -147,21 +147,26 @@ tg://socks?server=127.0.0.1&port=1080 ```bash TgWsProxy.exe ``` +Просто запусти! Telegram автоматически откроет настройки SOCKS5 прокси. **С аутентификацией:** ```bash TgWsProxy.exe --auth "myuser:mypassword" ``` +Защита прокси паролем. **С HTTP прокси (для опытных):** ```bash TgWsProxy.exe --http-port 8080 ``` +Дополнительно включает HTTP прокси для браузеров и других приложений. +Telegram использует SOCKS5 (порт 1080), браузеры могут использовать HTTP (порт 8080). **С восходящим прокси (для опытных):** ```bash TgWsProxy.exe --upstream-proxy "socks5://user:pass@proxy-server:1080" ``` +Подключение к Telegram через другой SOCKS5 прокси. ## Структура проекта diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 0d14992..b3d7019 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -72,6 +72,8 @@ func main() { // Advanced features (for experienced users) httpPort := flag.Int("http-port", 0, "Enable HTTP proxy on port (0 = disabled)") upstreamProxy := flag.String("upstream-proxy", "", "Upstream SOCKS5/HTTP proxy (format: socks5://user:pass@host:port or http://user:pass@host:port)") + mtprotoSecret := flag.String("mtproto-secret", "", "MTProto proxy secret (enables MTProto mode)") + mtprotoPort := flag.Int("mtproto-port", 0, "MTProto proxy port (requires --mtproto-secret)") showVersion := flag.Bool("version", false, "Show version") @@ -149,8 +151,28 @@ func main() { log.Fatalf("Failed to create server: %v", err) } - // Auto-configure Telegram Desktop (always attempt on first run) + // Auto-configure Telegram Desktop with correct proxy type log.Println("Attempting to configure Telegram Desktop...") + + // Determine proxy type and configure Telegram accordingly + // Note: Our local proxy only supports SOCKS5 + // HTTP port is for other applications (browsers, etc.) + // MTProto requires external MTProxy server + proxyType := "socks5" // Always SOCKS5 for our local proxy + proxyPort := cfg.Port + proxySecret := "" + + // Log HTTP mode if enabled (for other apps, not Telegram) + if *httpPort != 0 { + log.Printf("⚙ HTTP proxy enabled on port %d (for browsers/other apps)", *httpPort) + } + + // Log MTProto mode info + if *mtprotoPort != 0 && *mtprotoSecret != "" { + log.Printf("⚙ MTProto mode: Use external MTProxy or configure manually") + log.Printf(" tg://proxy?server=%s&port=%d&secret=%s", cfg.Host, *mtprotoPort, *mtprotoSecret) + } + username, password := "", "" if cfg.Auth != "" { parts := strings.SplitN(cfg.Auth, ":", 2) @@ -158,12 +180,13 @@ func main() { username, password = parts[0], parts[1] } } - if telegram.ConfigureProxy(cfg.Host, cfg.Port, username, password) { - log.Println("✓ Telegram Desktop proxy configuration opened") + + if telegram.ConfigureProxyWithType(cfg.Host, proxyPort, username, password, proxySecret, proxyType) { + log.Printf("✓ Telegram Desktop %s proxy configuration opened", strings.ToUpper(proxyType)) } else { log.Println("✗ Failed to auto-configure Telegram.") log.Println(" Manual setup: Settings → Advanced → Connection Type → Proxy") - log.Println(" Or open: tg://socks?server=127.0.0.1&port=1080") + log.Printf(" Or open: tg://socks?server=%s&port=%d", cfg.Host, proxyPort) } // Check for updates and auto-download (non-blocking) diff --git a/internal/telegram/telegram.go b/internal/telegram/telegram.go index 4712cd8..54fbd5b 100644 --- a/internal/telegram/telegram.go +++ b/internal/telegram/telegram.go @@ -8,12 +8,32 @@ import ( "strings" ) -// ConfigureProxy opens Telegram's proxy configuration URL. +// ConfigureProxy opens Telegram's SOCKS5 proxy configuration URL. // Returns true if successful, false otherwise. func ConfigureProxy(host string, port int, username, password string) bool { - // Use tg://socks format (same as original Python version) - // Format: tg://socks?server=host&port=port - proxyURL := fmt.Sprintf("tg://socks?server=%s&port=%d", host, port) + return ConfigureProxyWithType(host, port, username, password, "", "socks5") +} + +// ConfigureProxyWithType opens Telegram's proxy configuration URL with specified type. +// proxyType: "socks5" or "mtproto" +// For MTProto, provide secret parameter +// Note: HTTP proxy is NOT supported by Telegram Desktop via tg:// URLs +// Returns true if successful, false otherwise. +func ConfigureProxyWithType(host string, port int, username, password, secret, proxyType string) bool { + var proxyURL string + + switch proxyType { + case "mtproto": + // MTProto proxy format: tg://proxy?server=host&port=port&secret=secret + if secret == "" { + secret = "ee000000000000000000000000000000" // default dummy secret + } + proxyURL = fmt.Sprintf("tg://proxy?server=%s&port=%d&secret=%s", host, port, secret) + default: + // SOCKS5 proxy format: tg://socks?server=host&port=port + // This is the only type our local proxy supports + proxyURL = fmt.Sprintf("tg://socks?server=%s&port=%d", host, port) + } // Open URL using system default handler return openURL(proxyURL)