mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-26 13:11:53 +08:00
fix: POC 扫描接入调用方 context,修复 cachedPocPath 竞争和 ProxyStats data race
- webscan/web_scan.go: WebScan 接受 ctx 参数,替换 context.Background(); sync.Once 改为 sync.Mutex 保护 POC 加载,消除 cachedPocPath 并发写竞争 - webtitle.go: ctx 从 Scan 穿透到 identifyFingerprintsMulti → triggerPocScan → WebScan - webpoc.go: 传递 ctx 到 WebScan - proxy/types.go: ProxyStats 增加 sync.Mutex - proxy/manager.go: LastConnectTime/LastError/AverageConnectTime 读写加锁
This commit is contained in:
+16
-2
@@ -264,11 +264,16 @@ func (d *directDialer) DialContext(ctx context.Context, network, address string)
|
||||
conn, err := dialer.DialContext(ctx, network, address)
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
d.stats.mu.Lock()
|
||||
d.stats.LastConnectTime = start
|
||||
d.stats.mu.Unlock()
|
||||
|
||||
if err != nil {
|
||||
atomic.AddInt64(&d.stats.FailedConnections, 1)
|
||||
d.stats.mu.Lock()
|
||||
d.stats.LastError = err.Error()
|
||||
d.stats.mu.Unlock()
|
||||
return nil, NewProxyError(ErrTypeConnection, ErrMsgDirectConnFailed, ErrCodeDirectConnFailed, err)
|
||||
}
|
||||
|
||||
@@ -323,15 +328,22 @@ func (s *socks5Dialer) DialContext(ctx context.Context, network, address string)
|
||||
select {
|
||||
case <-dialCtx.Done():
|
||||
atomic.AddInt64(&s.stats.FailedConnections, 1)
|
||||
s.stats.mu.Lock()
|
||||
s.stats.LastError = dialCtx.Err().Error()
|
||||
s.stats.mu.Unlock()
|
||||
return nil, NewProxyError(ErrTypeTimeout, ErrMsgSOCKS5ConnTimeout, ErrCodeSOCKS5ConnTimeout, dialCtx.Err())
|
||||
case result := <-connChan:
|
||||
duration := time.Since(start)
|
||||
|
||||
s.stats.mu.Lock()
|
||||
s.stats.LastConnectTime = start
|
||||
s.stats.mu.Unlock()
|
||||
|
||||
if result.err != nil {
|
||||
atomic.AddInt64(&s.stats.FailedConnections, 1)
|
||||
s.stats.mu.Lock()
|
||||
s.stats.LastError = result.err.Error()
|
||||
s.stats.mu.Unlock()
|
||||
return nil, NewProxyError(ErrTypeConnection, ErrMsgSOCKS5ConnFailed, ErrCodeSOCKS5ConnFailed, result.err)
|
||||
}
|
||||
|
||||
@@ -347,7 +359,8 @@ func (s *socks5Dialer) DialContext(ctx context.Context, network, address string)
|
||||
|
||||
// updateAverageConnectTime 更新平均连接时间
|
||||
func (d *directDialer) updateAverageConnectTime(duration time.Duration) {
|
||||
// 简单的移动平均
|
||||
d.stats.mu.Lock()
|
||||
defer d.stats.mu.Unlock()
|
||||
if d.stats.AverageConnectTime == 0 {
|
||||
d.stats.AverageConnectTime = duration
|
||||
} else {
|
||||
@@ -356,7 +369,8 @@ func (d *directDialer) updateAverageConnectTime(duration time.Duration) {
|
||||
}
|
||||
|
||||
func (s *socks5Dialer) updateAverageConnectTime(duration time.Duration) {
|
||||
// 简单的移动平均
|
||||
s.stats.mu.Lock()
|
||||
defer s.stats.mu.Unlock()
|
||||
if s.stats.AverageConnectTime == 0 {
|
||||
s.stats.AverageConnectTime = duration
|
||||
} else {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -95,9 +96,10 @@ type ProxyManager interface {
|
||||
//
|
||||
//nolint:revive // 保持与现有代码的向后兼容性
|
||||
type ProxyStats struct {
|
||||
TotalConnections int64 `json:"total_connections"`
|
||||
ActiveConnections int64 `json:"active_connections"`
|
||||
FailedConnections int64 `json:"failed_connections"`
|
||||
TotalConnections int64 `json:"total_connections"`
|
||||
ActiveConnections int64 `json:"active_connections"`
|
||||
FailedConnections int64 `json:"failed_connections"`
|
||||
mu sync.Mutex `json:"-"`
|
||||
AverageConnectTime time.Duration `json:"average_connect_time"`
|
||||
LastConnectTime time.Time `json:"last_connect_time"`
|
||||
LastError string `json:"last_error,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user