From 40bdc4abc74e2760e4c3b3153b2d4c2357eb7f4e Mon Sep 17 00:00:00 2001 From: Lavish <64689132+lavish440@users.noreply.github.com> Date: Tue, 22 Oct 2024 22:49:00 +0530 Subject: [PATCH] feat(fasthttpproxy): add dual-stack connection support to enable IPv6 proxies for HTTP and SOCKS5 dialers (#1885) * feat(fasthttpproxy): add dual-stack connection support to enable IPv6 proxies for HTTP and SOCKS5 dialers - Added `FasthttpHTTPDialerDualStack` to support dual-stack (IPv4 and IPv6) connections for HTTP proxies, enabling IPv6 proxy usage. - Added `FasthttpSocksDialerDualStack` to support dual-stack (IPv4 and IPv6) connections for SOCKS5 proxies, enabling IPv6 proxy usage. - Improved dialer configuration to ensure compatibility with both IPv4 and IPv6 proxies. * fix(lint): address linting issues in code --- fasthttpproxy/http.go | 30 ++++++++++++++++++++++++++++++ fasthttpproxy/socks5.go | 14 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/fasthttpproxy/http.go b/fasthttpproxy/http.go index 80885d01e1..7ab1174f44 100644 --- a/fasthttpproxy/http.go +++ b/fasthttpproxy/http.go @@ -33,3 +33,33 @@ func FasthttpHTTPDialerTimeout(proxy string, timeout time.Duration) fasthttp.Dia dialFunc, _ := d.GetDialFunc(false) return dialFunc } + +// FasthttpHTTPDialerDualStack returns a fasthttp.DialFunc that dials using +// the provided HTTP proxy with support for both IPv4 and IPv6. +// +// Example usage: +// +// c := &fasthttp.Client{ +// Dial: fasthttpproxy.FasthttpHTTPDialerDualStack("username:password@localhost:9050"), +// } +func FasthttpHTTPDialerDualStack(proxy string) fasthttp.DialFunc { + return FasthttpHTTPDialerDualStackTimeout(proxy, 0) +} + +// FasthttpHTTPDialerDualStackTimeout returns a fasthttp.DialFunc that dials using +// the provided HTTP proxy with support for both IPv4 and IPv6, using the given timeout. +// The timeout parameter determines both the dial timeout and the CONNECT request timeout. +// +// Example usage: +// +// c := &fasthttp.Client{ +// Dial: fasthttpproxy.FasthttpHTTPDialerDualStackTimeout("username:password@localhost:9050", time.Second * 2), +// } +func FasthttpHTTPDialerDualStackTimeout(proxy string, timeout time.Duration) fasthttp.DialFunc { + d := Dialer{ + Config: httpproxy.Config{HTTPProxy: proxy, HTTPSProxy: proxy}, Timeout: timeout, ConnectTimeout: timeout, + DialDualStack: true, + } + dialFunc, _ := d.GetDialFunc(false) + return dialFunc +} diff --git a/fasthttpproxy/socks5.go b/fasthttpproxy/socks5.go index 0328cd25cd..12c65e2b2a 100644 --- a/fasthttpproxy/socks5.go +++ b/fasthttpproxy/socks5.go @@ -18,3 +18,17 @@ func FasthttpSocksDialer(proxyAddr string) fasthttp.DialFunc { dialFunc, _ := d.GetDialFunc(false) return dialFunc } + +// FasthttpSocksDialerDualStack returns a fasthttp.DialFunc that dials using +// the provided SOCKS5 proxy with support for both IPv4 and IPv6. +// +// Example usage: +// +// c := &fasthttp.Client{ +// Dial: fasthttpproxy.FasthttpSocksDialerDualStack("socks5://localhost:9050"), +// } +func FasthttpSocksDialerDualStack(proxyAddr string) fasthttp.DialFunc { + d := Dialer{Config: httpproxy.Config{HTTPProxy: proxyAddr, HTTPSProxy: proxyAddr}, DialDualStack: true} + dialFunc, _ := d.GetDialFunc(false) + return dialFunc +}