mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00

主要修复: 1. 修复时间显示Bug - StartTime初始化问题 2. 修复Web智能探测错误检测预定义端口而非用户指定端口 3. 修复本地插件被错误调用到端口扫描中的问题 4. 修复host:port格式双重处理导致的多余端口扫描 5. 统一插件过滤逻辑,消除接口不一致性 6. 优化Web检测缓存机制,减少重复HTTP请求 技术改进: - 重构插件适用性检查逻辑,确保策略过滤器正确工作 - 区分Web检测的自动发现模式和用户指定端口模式 - 在解析阶段正确处理host:port格式,避免与默认端口冲突 - 完善缓存机制,提升性能 测试验证: - ./fscan -h 127.0.0.1:3306 现在只检测3306端口 - 本地插件不再参与端口扫描 - Web检测只对指定端口进行协议检测 - 时间显示正确
157 lines
3.7 KiB
Go
157 lines
3.7 KiB
Go
package proxy
|
||
|
||
import (
|
||
"context"
|
||
"crypto/tls"
|
||
"net"
|
||
"sync/atomic"
|
||
"time"
|
||
)
|
||
|
||
// tlsDialerWrapper TLS拨号器包装器
|
||
type tlsDialerWrapper struct {
|
||
dialer Dialer
|
||
config *ProxyConfig
|
||
stats *ProxyStats
|
||
}
|
||
|
||
func (t *tlsDialerWrapper) Dial(network, address string) (net.Conn, error) {
|
||
return t.dialer.Dial(network, address)
|
||
}
|
||
|
||
func (t *tlsDialerWrapper) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||
return t.dialer.DialContext(ctx, network, address)
|
||
}
|
||
|
||
func (t *tlsDialerWrapper) DialTLS(network, address string, config *tls.Config) (net.Conn, error) {
|
||
return t.DialTLSContext(context.Background(), network, address, config)
|
||
}
|
||
|
||
func (t *tlsDialerWrapper) DialTLSContext(ctx context.Context, network, address string, tlsConfig *tls.Config) (net.Conn, error) {
|
||
start := time.Now()
|
||
|
||
// 首先建立TCP连接
|
||
tcpConn, err := t.dialer.DialContext(ctx, network, address)
|
||
if err != nil {
|
||
return nil, NewProxyError(ErrTypeConnection, ErrMsgTLSTCPConnFailed, ErrCodeTLSTCPConnFailed, err)
|
||
}
|
||
|
||
// 创建TLS连接
|
||
tlsConn := tls.Client(tcpConn, tlsConfig)
|
||
|
||
// 设置TLS握手超时
|
||
if deadline, ok := ctx.Deadline(); ok {
|
||
tlsConn.SetDeadline(deadline)
|
||
} else {
|
||
tlsConn.SetDeadline(time.Now().Add(t.config.Timeout))
|
||
}
|
||
|
||
// 进行TLS握手
|
||
if err := tlsConn.Handshake(); err != nil {
|
||
tcpConn.Close()
|
||
atomic.AddInt64(&t.stats.FailedConnections, 1)
|
||
t.stats.LastError = err.Error()
|
||
return nil, NewProxyError(ErrTypeConnection, ErrMsgTLSHandshakeFailed, ErrCodeTLSHandshakeFailed, err)
|
||
}
|
||
|
||
// 清除deadline,让上层代码管理超时
|
||
tlsConn.SetDeadline(time.Time{})
|
||
|
||
duration := time.Since(start)
|
||
t.updateAverageConnectTime(duration)
|
||
|
||
return &trackedTLSConn{
|
||
trackedConn: &trackedConn{
|
||
Conn: tlsConn,
|
||
stats: t.stats,
|
||
},
|
||
isTLS: true,
|
||
}, nil
|
||
}
|
||
|
||
// updateAverageConnectTime 更新平均连接时间
|
||
func (t *tlsDialerWrapper) updateAverageConnectTime(duration time.Duration) {
|
||
// 简单的移动平均
|
||
if t.stats.AverageConnectTime == 0 {
|
||
t.stats.AverageConnectTime = duration
|
||
} else {
|
||
t.stats.AverageConnectTime = (t.stats.AverageConnectTime + duration) / 2
|
||
}
|
||
}
|
||
|
||
// trackedConn 带统计的连接
|
||
type trackedConn struct {
|
||
net.Conn
|
||
stats *ProxyStats
|
||
bytesSent int64
|
||
bytesRecv int64
|
||
}
|
||
|
||
func (tc *trackedConn) Read(b []byte) (n int, err error) {
|
||
n, err = tc.Conn.Read(b)
|
||
if n > 0 {
|
||
atomic.AddInt64(&tc.bytesRecv, int64(n))
|
||
}
|
||
return n, err
|
||
}
|
||
|
||
func (tc *trackedConn) Write(b []byte) (n int, err error) {
|
||
n, err = tc.Conn.Write(b)
|
||
if n > 0 {
|
||
atomic.AddInt64(&tc.bytesSent, int64(n))
|
||
}
|
||
return n, err
|
||
}
|
||
|
||
func (tc *trackedConn) Close() error {
|
||
atomic.AddInt64(&tc.stats.ActiveConnections, -1)
|
||
return tc.Conn.Close()
|
||
}
|
||
|
||
// trackedTLSConn 带统计的TLS连接
|
||
type trackedTLSConn struct {
|
||
*trackedConn
|
||
isTLS bool
|
||
}
|
||
|
||
func (ttc *trackedTLSConn) ConnectionState() tls.ConnectionState {
|
||
if tlsConn, ok := ttc.Conn.(*tls.Conn); ok {
|
||
return tlsConn.ConnectionState()
|
||
}
|
||
return tls.ConnectionState{}
|
||
}
|
||
|
||
func (ttc *trackedTLSConn) Handshake() error {
|
||
if tlsConn, ok := ttc.Conn.(*tls.Conn); ok {
|
||
return tlsConn.Handshake()
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (ttc *trackedTLSConn) OCSPResponse() []byte {
|
||
if tlsConn, ok := ttc.Conn.(*tls.Conn); ok {
|
||
return tlsConn.OCSPResponse()
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (ttc *trackedTLSConn) PeerCertificates() []*tls.Certificate {
|
||
if tlsConn, ok := ttc.Conn.(*tls.Conn); ok {
|
||
state := tlsConn.ConnectionState()
|
||
var certs []*tls.Certificate
|
||
for _, cert := range state.PeerCertificates {
|
||
certs = append(certs, &tls.Certificate{
|
||
Certificate: [][]byte{cert.Raw},
|
||
})
|
||
}
|
||
return certs
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (ttc *trackedTLSConn) VerifyHostname(host string) error {
|
||
if tlsConn, ok := ttc.Conn.(*tls.Conn); ok {
|
||
return tlsConn.VerifyHostname(host)
|
||
}
|
||
return nil
|
||
} |