mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-23 03:31:53 +08:00
## 架构重构
- 全局变量消除,迁移至 Config/State 对象
- SMB 插件融合(smb/smb2/smbghost/smbinfo)
- 服务探测重构,实现 Nmap 风格 fallback 机制
- 输出系统重构,TXT 实时刷盘 + 双写机制
- i18n 框架升级至 go-i18n
## 性能优化
- 正则表达式预编译
- 内存优化 map[string]struct{}
- 并发指纹匹配
- SOCKS5 连接复用
- 滑动窗口调度 + 自适应线程池
## 新功能
- Web 管理界面
- 多格式 POC 适配(xray/afrog)
- 增强指纹库(3139条)
- Favicon hash 指纹识别
- 插件选择性编译(Build Tags)
- fscan-lab 靶场环境
- 默认端口扩展(62→133)
## 构建系统
- 添加 no_local tag 支持排除本地插件
- 多版本构建:fscan/fscan-nolocal/fscan-web
- CI 添加 snapshot 模式支持仅测试构建
## Bug 修复
- 修复 120+ 个问题,包括 RDP panic、批量扫描漏报、
JSON 输出格式、Redis 检测、Context 超时等
## 测试增强
- 单元测试覆盖率 74-100%
- 并发安全测试
- 集成测试(Web/端口/服务/SSH/ICMP)
158 lines
3.8 KiB
Go
158 lines
3.8 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() // TLS握手失败,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
|
|
}
|