mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 05:56:46 +08:00

主要清理内容: - logging包:移除DetailedFormatter、JSONFormatter及相关方法 - output包:清理Manager和Statistics中的未使用方法 - parsers包:移除各Parser中的Validate()和GetStatistics()方法 - proxy包:清理Factory和Global中的所有未使用函数 - ProgressManager:移除未使用的进度条方法和字段 - globals:清理未使用的status变量 技术改进: - 使用deadcode和golangci-lint工具系统性检测 - 保持代码结构清晰,添加清理注释 - 修复相关依赖引用问题 - 确保编译通过和功能正常 代码减少:移除约40个死代码函数和多个未使用字段
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, "建立TCP连接失败", 5001, 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, "TLS握手失败", 5002, 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
|
||
} |