mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 05:56:46 +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检测只对指定端口进行协议检测 - 时间显示正确
112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package proxy
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// httpDialer HTTP代理拨号器
|
|
type httpDialer struct {
|
|
config *ProxyConfig
|
|
stats *ProxyStats
|
|
baseDial *net.Dialer
|
|
}
|
|
|
|
func (h *httpDialer) Dial(network, address string) (net.Conn, error) {
|
|
return h.DialContext(context.Background(), network, address)
|
|
}
|
|
|
|
func (h *httpDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
start := time.Now()
|
|
atomic.AddInt64(&h.stats.TotalConnections, 1)
|
|
|
|
// 连接到HTTP代理服务器
|
|
proxyConn, err := h.baseDial.DialContext(ctx, NetworkTCP, h.config.Address)
|
|
if err != nil {
|
|
atomic.AddInt64(&h.stats.FailedConnections, 1)
|
|
h.stats.LastError = err.Error()
|
|
return nil, NewProxyError(ErrTypeConnection, ErrMsgHTTPConnFailed, ErrCodeHTTPConnFailed, err)
|
|
}
|
|
|
|
// 发送CONNECT请求
|
|
if err := h.sendConnectRequest(proxyConn, address); err != nil {
|
|
proxyConn.Close()
|
|
atomic.AddInt64(&h.stats.FailedConnections, 1)
|
|
h.stats.LastError = err.Error()
|
|
return nil, err
|
|
}
|
|
|
|
duration := time.Since(start)
|
|
h.stats.LastConnectTime = start
|
|
atomic.AddInt64(&h.stats.ActiveConnections, 1)
|
|
h.updateAverageConnectTime(duration)
|
|
|
|
return &trackedConn{
|
|
Conn: proxyConn,
|
|
stats: h.stats,
|
|
}, nil
|
|
}
|
|
|
|
// sendConnectRequest 发送HTTP CONNECT请求
|
|
func (h *httpDialer) sendConnectRequest(conn net.Conn, address string) error {
|
|
// 构建CONNECT请求
|
|
req := fmt.Sprintf(HTTPConnectRequestFormat, address, address)
|
|
|
|
// 添加认证头
|
|
if h.config.Username != "" {
|
|
auth := base64.StdEncoding.EncodeToString(
|
|
[]byte(h.config.Username + AuthSeparator + h.config.Password))
|
|
req += fmt.Sprintf(HTTPAuthHeaderFormat, auth)
|
|
}
|
|
|
|
req += HTTPRequestEndFormat
|
|
|
|
// 设置写超时
|
|
if err := conn.SetWriteDeadline(time.Now().Add(h.config.Timeout)); err != nil {
|
|
return NewProxyError(ErrTypeTimeout, ErrMsgHTTPSetWriteTimeout, ErrCodeHTTPSetWriteTimeout, err)
|
|
}
|
|
|
|
// 发送请求
|
|
if _, err := conn.Write([]byte(req)); err != nil {
|
|
return NewProxyError(ErrTypeConnection, ErrMsgHTTPSendConnectFail, ErrCodeHTTPSendConnectFail, err)
|
|
}
|
|
|
|
// 设置读超时
|
|
if err := conn.SetReadDeadline(time.Now().Add(h.config.Timeout)); err != nil {
|
|
return NewProxyError(ErrTypeTimeout, ErrMsgHTTPSetReadTimeout, ErrCodeHTTPSetReadTimeout, err)
|
|
}
|
|
|
|
// 读取响应
|
|
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
|
|
if err != nil {
|
|
return NewProxyError(ErrTypeProtocol, ErrMsgHTTPReadRespFailed, ErrCodeHTTPReadRespFailed, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 检查响应状态
|
|
if resp.StatusCode != HTTPStatusOK {
|
|
return NewProxyError(ErrTypeAuth,
|
|
fmt.Sprintf(ErrMsgHTTPProxyAuthFailed, resp.StatusCode), ErrCodeHTTPProxyAuthFailed, nil)
|
|
}
|
|
|
|
// 清除deadline
|
|
conn.SetDeadline(time.Time{})
|
|
|
|
return nil
|
|
}
|
|
|
|
// updateAverageConnectTime 更新平均连接时间
|
|
func (h *httpDialer) updateAverageConnectTime(duration time.Duration) {
|
|
// 简单的移动平均
|
|
if h.stats.AverageConnectTime == 0 {
|
|
h.stats.AverageConnectTime = duration
|
|
} else {
|
|
h.stats.AverageConnectTime = (h.stats.AverageConnectTime + duration) / 2
|
|
}
|
|
} |