mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-24 04:01:52 +08:00
feat: tcp端口扫描支持socks5 (#527)
* feat: tcp端口扫描支持socks5 * feat: PG插件支持socks5 * feat: 完成大部分插件的socks5支持
This commit is contained in:
+114
-1
@@ -1,13 +1,16 @@
|
||||
package Common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/net/proxy"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
// WrapperTcpWithTimeout 创建一个带超时的TCP连接
|
||||
@@ -16,6 +19,12 @@ func WrapperTcpWithTimeout(network, address string, timeout time.Duration) (net.
|
||||
return WrapperTCP(network, address, d)
|
||||
}
|
||||
|
||||
// WrapperTcpWithContext 创建一个带上下文的TCP连接
|
||||
func WrapperTcpWithContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
d := &net.Dialer{}
|
||||
return WrapperTCPWithContext(ctx, network, address, d)
|
||||
}
|
||||
|
||||
// WrapperTCP 根据配置创建TCP连接
|
||||
func WrapperTCP(network, address string, forward *net.Dialer) (net.Conn, error) {
|
||||
// 直连模式
|
||||
@@ -41,6 +50,54 @@ func WrapperTCP(network, address string, forward *net.Dialer) (net.Conn, error)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// WrapperTCPWithContext 根据配置创建支持上下文的TCP连接
|
||||
func WrapperTCPWithContext(ctx context.Context, network, address string, forward *net.Dialer) (net.Conn, error) {
|
||||
// 直连模式
|
||||
if Socks5Proxy == "" {
|
||||
conn, err := forward.DialContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(GetText("tcp_conn_failed"), err)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// Socks5代理模式
|
||||
dialer, err := Socks5Dialer(forward)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(GetText("socks5_create_failed"), err)
|
||||
}
|
||||
|
||||
// 创建一个结果通道来处理连接和取消
|
||||
connChan := make(chan struct {
|
||||
conn net.Conn
|
||||
err error
|
||||
}, 1)
|
||||
|
||||
go func() {
|
||||
conn, err := dialer.Dial(network, address)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if conn != nil {
|
||||
conn.Close()
|
||||
}
|
||||
case connChan <- struct {
|
||||
conn net.Conn
|
||||
err error
|
||||
}{conn, err}:
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case result := <-connChan:
|
||||
if result.err != nil {
|
||||
return nil, fmt.Errorf(GetText("socks5_conn_failed"), result.err)
|
||||
}
|
||||
return result.conn, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Socks5Dialer 创建Socks5代理拨号器
|
||||
func Socks5Dialer(forward *net.Dialer) (proxy.Dialer, error) {
|
||||
// 解析代理URL
|
||||
@@ -76,3 +133,59 @@ func Socks5Dialer(forward *net.Dialer) (proxy.Dialer, error) {
|
||||
|
||||
return dialer, nil
|
||||
}
|
||||
|
||||
// WrapperTlsWithContext 创建一个通过代理的TLS连接
|
||||
func WrapperTlsWithContext(ctx context.Context, network, address string, tlsConfig *tls.Config) (net.Conn, error) {
|
||||
// 直连模式
|
||||
if Socks5Proxy == "" {
|
||||
dialer := &net.Dialer{}
|
||||
|
||||
tcpConn, err := dialer.DialContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("直连TCP连接失败: %v", err)
|
||||
}
|
||||
|
||||
// 在TCP连接上进行TLS握手
|
||||
tlsConn := tls.Client(tcpConn, tlsConfig)
|
||||
|
||||
// 使用ctx的deadline设置TLS握手超时
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
tlsConn.SetDeadline(deadline)
|
||||
}
|
||||
|
||||
if err := tlsConn.Handshake(); err != nil {
|
||||
tcpConn.Close()
|
||||
return nil, fmt.Errorf("TLS握手失败: %v", err)
|
||||
}
|
||||
|
||||
// 清除deadline,让上层代码自己管理超时
|
||||
tlsConn.SetDeadline(time.Time{})
|
||||
|
||||
return tlsConn, nil
|
||||
}
|
||||
|
||||
// Socks5代理模式
|
||||
// 首先通过代理建立到目标的TCP连接
|
||||
tcpConn, err := WrapperTcpWithContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("通过代理建立TCP连接失败: %v", err)
|
||||
}
|
||||
|
||||
// 在TCP连接上进行TLS握手
|
||||
tlsConn := tls.Client(tcpConn, tlsConfig)
|
||||
|
||||
// 使用ctx的deadline设置TLS握手超时
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
tlsConn.SetDeadline(deadline)
|
||||
}
|
||||
|
||||
if err := tlsConn.Handshake(); err != nil {
|
||||
tcpConn.Close()
|
||||
return nil, fmt.Errorf("TLS握手失败: %v", err)
|
||||
}
|
||||
|
||||
// 清除deadline,让上层代码自己管理超时
|
||||
tlsConn.SetDeadline(time.Time{})
|
||||
|
||||
return tlsConn, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user