refactor: 重构SMTP和Telnet插件使用统一发包控制

- 修改SMTP插件,在多个连接点添加发包控制
- 修改Telnet插件,在identifyService中使用SafeTCPDial包装器
- 保持现有功能不变,统一发包控制逻辑
This commit is contained in:
ZacharyZcR 2025-09-02 11:43:42 +00:00
parent 5f7669a537
commit a23c82142d
2 changed files with 39 additions and 7 deletions

View File

@ -90,12 +90,23 @@ func (p *SMTPPlugin) Scan(ctx context.Context, info *common.HostInfo) *plugins.R
func (p *SMTPPlugin) testOpenRelay(ctx context.Context, info *common.HostInfo) *plugins.Result {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
// 检查发包限制
if canSend, reason := common.CanSendPacket(); !canSend {
common.LogError(fmt.Sprintf("SMTP连接 %s 受限: %s", target, reason))
return nil
}
// 设置超时的Dialer
dialer := &net.Dialer{
Timeout: time.Duration(common.Timeout) * time.Second,
}
conn, err := dialer.DialContext(ctx, "tcp", target)
if err == nil {
common.IncrementTCPSuccessPacketCount()
} else {
common.IncrementTCPFailedPacketCount()
}
if err != nil {
return nil
}
@ -132,12 +143,23 @@ func (p *SMTPPlugin) testCredential(ctx context.Context, info *common.HostInfo,
common.LogDebug(fmt.Sprintf("SMTP测试凭据: %s:%s", cred.Username, cred.Password))
// 检查发包限制
if canSend, reason := common.CanSendPacket(); !canSend {
common.LogError(fmt.Sprintf("SMTP凭据测试 %s 受限: %s", target, reason))
return false
}
// 设置连接超时
dialer := &net.Dialer{
Timeout: timeout,
}
conn, err := dialer.DialContext(ctx, "tcp", target)
if err == nil {
common.IncrementTCPSuccessPacketCount()
} else {
common.IncrementTCPFailedPacketCount()
}
if err != nil {
common.LogDebug(fmt.Sprintf("SMTP连接失败: %v", err))
return false
@ -177,7 +199,8 @@ func (p *SMTPPlugin) testCredential(ctx context.Context, info *common.HostInfo,
func (p *SMTPPlugin) getServerInfo(ctx context.Context, info *common.HostInfo) string {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
conn, err := net.DialTimeout("tcp", target, time.Duration(common.Timeout)*time.Second)
// 使用统一TCP包装器
conn, err := common.SafeTCPDial(target, time.Duration(common.Timeout)*time.Second)
if err != nil {
return ""
}
@ -207,11 +230,8 @@ func (p *SMTPPlugin) identifyService(ctx context.Context, info *common.HostInfo)
if serverInfo != "" {
banner = fmt.Sprintf("SMTP邮件服务 (%s)", serverInfo)
} else {
// 简单连接测试
dialer := &net.Dialer{
Timeout: time.Duration(common.Timeout) * time.Second,
}
conn, err := dialer.DialContext(ctx, "tcp", target)
// 使用统一TCP包装器进行简单连接测试
conn, err := common.SafeTCPDial(target, time.Duration(common.Timeout)*time.Second)
if err != nil {
return &plugins.Result{
Success: false,

View File

@ -65,8 +65,19 @@ func (p *TelnetPlugin) Scan(ctx context.Context, info *common.HostInfo) *plugins
func (p *TelnetPlugin) testTelnetCredential(ctx context.Context, info *common.HostInfo, cred plugins.Credential) bool {
address := fmt.Sprintf("%s:%s", info.Host, info.Ports)
// 检查发包限制
if canSend, reason := common.CanSendPacket(); !canSend {
common.LogError(fmt.Sprintf("Telnet连接 %s 受限: %s", address, reason))
return false
}
// 创建带超时的连接
conn, err := net.DialTimeout("tcp", address, time.Duration(common.Timeout)*time.Second)
if err == nil {
common.IncrementTCPSuccessPacketCount()
} else {
common.IncrementTCPFailedPacketCount()
}
if err != nil {
return false
}
@ -304,7 +315,8 @@ func (p *TelnetPlugin) isLoginFailed(data string) bool {
func (p *TelnetPlugin) identifyService(ctx context.Context, info *common.HostInfo) *plugins.Result {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
conn, err := net.DialTimeout("tcp", target, time.Duration(common.Timeout)*time.Second)
// 使用统一TCP包装器
conn, err := common.SafeTCPDial(target, time.Duration(common.Timeout)*time.Second)
if err != nil {
return &plugins.Result{
Success: false,