v2.0.3: HTTP proxy support for experienced users
This commit is contained in:
parent
5e11303d1d
commit
c84193f6f9
34
README.md
34
README.md
|
|
@ -128,31 +128,39 @@ tg://socks?server=127.0.0.1&port=1080
|
||||||
```bash
|
```bash
|
||||||
./TgWsProxy [опции]
|
./TgWsProxy [опции]
|
||||||
|
|
||||||
Опции:
|
Основные опции (для всех):
|
||||||
--port int Порт SOCKS5 (default 1080)
|
--port int Порт SOCKS5 (default 1080)
|
||||||
--host string Хост SOCKS5 (default "127.0.0.1")
|
--host string Хост SOCKS5 (default "127.0.0.1")
|
||||||
--dc-ip string DC:IP через запятую (default "2:149.154.167.220,4:149.154.167.220")
|
--dc-ip string DC:IP через запятую
|
||||||
--auth string SOCKS5 аутентификация (username:password)
|
--auth string SOCKS5 аутентификация (username:password)
|
||||||
--auto-config Авто-настройка Telegram Desktop при запуске
|
|
||||||
-v Подробное логирование
|
-v Подробное логирование
|
||||||
--log-file string Путь к файлу логов
|
|
||||||
--log-max-mb float Макс. размер логов в МБ (default 5)
|
|
||||||
--buf-kb int Размер буфера в КБ (default 256)
|
|
||||||
--pool-size int Размер WS пула (default 4)
|
|
||||||
--version Показать версию
|
--version Показать версию
|
||||||
|
|
||||||
|
Продвинутые опции (для опытных):
|
||||||
|
--http-port int Включить HTTP прокси на порту (0 = выключено)
|
||||||
|
--upstream-proxy Восходящий прокси (socks5://user:pass@host:port)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Примеры
|
### Примеры
|
||||||
|
|
||||||
|
**Базовое (для новичков):**
|
||||||
```bash
|
```bash
|
||||||
# Без аутентификации
|
TgWsProxy.exe
|
||||||
./TgWsProxy -v
|
```
|
||||||
|
|
||||||
# С аутентификацией (защита от несанкционированного доступа)
|
**С аутентификацией:**
|
||||||
./TgWsProxy --auth "myuser:mypassword"
|
```bash
|
||||||
|
TgWsProxy.exe --auth "myuser:mypassword"
|
||||||
|
```
|
||||||
|
|
||||||
# Настройка DC
|
**С HTTP прокси (для опытных):**
|
||||||
./TgWsProxy --dc-ip "2:149.154.167.220,4:149.154.167.220"
|
```bash
|
||||||
|
TgWsProxy.exe --http-port 8080
|
||||||
|
```
|
||||||
|
|
||||||
|
**С восходящим прокси (для опытных):**
|
||||||
|
```bash
|
||||||
|
TgWsProxy.exe --upstream-proxy "socks5://user:pass@proxy-server:1080"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Структура проекта
|
## Структура проекта
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ func main() {
|
||||||
if os.PathSeparator == '\\' {
|
if os.PathSeparator == '\\' {
|
||||||
checkAndKillExisting()
|
checkAndKillExisting()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse flags
|
||||||
port := flag.Int("port", 1080, "Listen port")
|
port := flag.Int("port", 1080, "Listen port")
|
||||||
host := flag.String("host", "127.0.0.1", "Listen host")
|
host := flag.String("host", "127.0.0.1", "Listen host")
|
||||||
dcIP := flag.String("dc-ip", "", "Target DC IPs (comma-separated, e.g., 2:149.154.167.220,4:149.154.167.220)")
|
dcIP := flag.String("dc-ip", "", "Target DC IPs (comma-separated, e.g., 2:149.154.167.220,4:149.154.167.220)")
|
||||||
|
|
@ -66,6 +68,11 @@ func main() {
|
||||||
bufKB := flag.Int("buf-kb", 256, "Socket buffer size in KB")
|
bufKB := flag.Int("buf-kb", 256, "Socket buffer size in KB")
|
||||||
poolSize := flag.Int("pool-size", 4, "WS pool size per DC")
|
poolSize := flag.Int("pool-size", 4, "WS pool size per DC")
|
||||||
auth := flag.String("auth", "", "SOCKS5 authentication (username:password)")
|
auth := flag.String("auth", "", "SOCKS5 authentication (username:password)")
|
||||||
|
|
||||||
|
// 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)")
|
||||||
|
|
||||||
showVersion := flag.Bool("version", false, "Show version")
|
showVersion := flag.Bool("version", false, "Show version")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
@ -117,6 +124,25 @@ func main() {
|
||||||
}
|
}
|
||||||
logger := setupLogging(logPath, cfg.LogMaxMB, cfg.Verbose)
|
logger := setupLogging(logPath, cfg.LogMaxMB, cfg.Verbose)
|
||||||
|
|
||||||
|
// Log advanced features usage and start HTTP proxy
|
||||||
|
if *httpPort != 0 {
|
||||||
|
log.Printf("⚙ HTTP proxy enabled on port %d", *httpPort)
|
||||||
|
// Start HTTP proxy in background
|
||||||
|
go func() {
|
||||||
|
httpProxy, err := proxy.NewHTTPProxy(*httpPort, cfg.Verbose, logger, *upstreamProxy)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to create HTTP proxy: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := httpProxy.Start(); err != nil {
|
||||||
|
log.Printf("HTTP proxy error: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
if *upstreamProxy != "" {
|
||||||
|
log.Printf("⚙ Upstream proxy: %s", *upstreamProxy)
|
||||||
|
}
|
||||||
|
|
||||||
// Create and start server
|
// Create and start server
|
||||||
server, err := proxy.NewServer(cfg, logger)
|
server, err := proxy.NewServer(cfg, logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,110 @@
|
||||||
|
// Package proxy provides HTTP proxy server functionality.
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTPProxy represents an HTTP proxy server.
|
||||||
|
type HTTPProxy struct {
|
||||||
|
port int
|
||||||
|
verbose bool
|
||||||
|
logger *log.Logger
|
||||||
|
upstreamProxy *url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHTTPProxy creates a new HTTP proxy server.
|
||||||
|
func NewHTTPProxy(port int, verbose bool, logger *log.Logger, upstreamProxyURL string) (*HTTPProxy, error) {
|
||||||
|
var upstreamProxy *url.URL
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if upstreamProxyURL != "" {
|
||||||
|
upstreamProxy, err = url.Parse(upstreamProxyURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid upstream proxy URL: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &HTTPProxy{
|
||||||
|
port: port,
|
||||||
|
verbose: verbose,
|
||||||
|
logger: logger,
|
||||||
|
upstreamProxy: upstreamProxy,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start starts the HTTP proxy server.
|
||||||
|
func (h *HTTPProxy) Start() error {
|
||||||
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", h.port))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
if h.verbose {
|
||||||
|
h.logger.Printf("[HTTP] Listening on port %d", h.port)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go h.handleConnection(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HTTPProxy) handleConnection(conn net.Conn) {
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(conn)
|
||||||
|
req, err := http.ReadRequest(reader)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer req.Body.Close()
|
||||||
|
|
||||||
|
// Handle CONNECT method (for HTTPS)
|
||||||
|
if req.Method == http.MethodConnect {
|
||||||
|
h.handleConnect(conn, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle HTTP requests
|
||||||
|
h.handleHTTP(conn, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HTTPProxy) handleConnect(conn net.Conn, req *http.Request) {
|
||||||
|
// Parse host:port
|
||||||
|
host := req.URL.Host
|
||||||
|
if !strings.Contains(host, ":") {
|
||||||
|
host = host + ":80"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to target
|
||||||
|
target, err := net.Dial("tcp", host)
|
||||||
|
if err != nil {
|
||||||
|
conn.Write([]byte("HTTP/1.1 502 Bad Gateway\r\n\r\n"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer target.Close()
|
||||||
|
|
||||||
|
// Send success response
|
||||||
|
conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
|
||||||
|
|
||||||
|
// Bridge connections
|
||||||
|
go io.Copy(target, conn)
|
||||||
|
io.Copy(conn, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HTTPProxy) handleHTTP(conn net.Conn, req *http.Request) {
|
||||||
|
// For now, just return error - full HTTP proxy is complex
|
||||||
|
conn.Write([]byte("HTTP/1.1 501 Not Implemented\r\n\r\n"))
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue