diff --git a/Common/common.go b/Common/common.go index 5186863..c557aed 100644 --- a/Common/common.go +++ b/Common/common.go @@ -18,6 +18,7 @@ import ( "github.com/shadow1ng/fscan/common/base" "github.com/shadow1ng/fscan/common/logging" "github.com/shadow1ng/fscan/common/output" + "github.com/shadow1ng/fscan/common/proxy" ) // ============================================================================= @@ -177,14 +178,58 @@ func WrapperTcpWithTimeout(network, address string, timeout time.Duration) (net. return net.DialTimeout(network, address, timeout) } -// WrapperTcpWithContext TCP连接包装器,带上下文 +// WrapperTcpWithContext TCP连接包装器,带上下文和代理支持 func WrapperTcpWithContext(ctx context.Context, network, address string) (net.Conn, error) { + // 检查是否配置了SOCKS5代理 + if Socks5Proxy != "" { + proxyConfig := &proxy.ProxyConfig{ + Type: proxy.ProxyTypeSOCKS5, + Address: Socks5Proxy, + Timeout: time.Second * 10, + } + + proxyManager := proxy.NewProxyManager(proxyConfig) + dialer, err := proxyManager.GetDialer() + if err != nil { + LogDebug(fmt.Sprintf("SOCKS5代理连接失败,回退到直连: %v", err)) + // 代理失败时回退到直连 + var d net.Dialer + return d.DialContext(ctx, network, address) + } + + LogDebug(fmt.Sprintf("使用SOCKS5代理连接: %s -> %s", Socks5Proxy, address)) + return dialer.DialContext(ctx, network, address) + } + + // 没有配置代理,使用直连 var d net.Dialer return d.DialContext(ctx, network, address) } -// WrapperTlsWithContext TLS连接包装器,带上下文 +// WrapperTlsWithContext TLS连接包装器,带上下文和代理支持 func WrapperTlsWithContext(ctx context.Context, network, address string, config *tls.Config) (net.Conn, error) { + // 检查是否配置了SOCKS5代理 + if Socks5Proxy != "" { + proxyConfig := &proxy.ProxyConfig{ + Type: proxy.ProxyTypeSOCKS5, + Address: Socks5Proxy, + Timeout: time.Second * 10, + } + + proxyManager := proxy.NewProxyManager(proxyConfig) + tlsDialer, err := proxyManager.GetTLSDialer() + if err != nil { + LogDebug(fmt.Sprintf("SOCKS5代理TLS连接失败,回退到直连: %v", err)) + // 代理失败时回退到直连 + d := &tls.Dialer{Config: config} + return d.DialContext(ctx, network, address) + } + + LogDebug(fmt.Sprintf("使用SOCKS5代理TLS连接: %s -> %s", Socks5Proxy, address)) + return tlsDialer.DialTLSContext(ctx, network, address, config) + } + + // 没有配置代理,使用直连 d := &tls.Dialer{Config: config} return d.DialContext(ctx, network, address) }