diff --git a/common/config_builder.go b/common/config_builder.go index 331f8d7..0d7ff8b 100644 --- a/common/config_builder.go +++ b/common/config_builder.go @@ -100,6 +100,8 @@ func parseUsernames(fv *FlagVars) []string { if fv.UsersFile != "" { if lines, err := parsers.ReadLinesFromFile(fv.UsersFile); err == nil { usernames = append(usernames, lines...) + } else { + LogError(fmt.Sprintf("读取用户名文件 %s 失败: %v", fv.UsersFile, err)) } } @@ -128,6 +130,8 @@ func parsePasswords(fv *FlagVars) []string { if fv.PasswordsFile != "" { if lines, err := parsers.ReadLinesFromFile(fv.PasswordsFile); err == nil { passwords = append(passwords, lines...) + } else { + LogError(fmt.Sprintf("读取密码文件 %s 失败: %v", fv.PasswordsFile, err)) } } @@ -246,6 +250,8 @@ func parseURLs(fv *FlagVars) []string { for _, line := range lines { urls = append(urls, normalizeURL(line)) } + } else { + LogError(fmt.Sprintf("读取URL文件 %s 失败: %v", fv.URLsFile, err)) } } diff --git a/plugins/local/forwardshell.go b/plugins/local/forwardshell.go index 1da9c75..5918501 100644 --- a/plugins/local/forwardshell.go +++ b/plugins/local/forwardshell.go @@ -114,14 +114,20 @@ func (p *ForwardShellPlugin) startForwardShellServer(ctx context.Context, port i } common.LogSuccess(i18n.Tr("forwardshell_client_connected", conn.RemoteAddr().String())) - go p.handleClient(conn) + go p.handleClient(ctx, conn) } } // handleClient 处理客户端连接 -func (p *ForwardShellPlugin) handleClient(clientConn net.Conn) { +func (p *ForwardShellPlugin) handleClient(ctx context.Context, clientConn net.Conn) { defer func() { _ = clientConn.Close() }() + // ctx 取消时关闭连接,解除阻塞的读操作 + go func() { + <-ctx.Done() + _ = clientConn.Close() + }() + // 发送欢迎信息 welcome := fmt.Sprintf("FScan Forward Shell - %s\nType 'exit' to disconnect\n\n", runtime.GOOS) _, _ = clientConn.Write([]byte(welcome)) @@ -145,7 +151,7 @@ func (p *ForwardShellPlugin) handleClient(clientConn net.Conn) { p.executeCommand(clientConn, command) } - if err := scanner.Err(); err != nil { + if err := scanner.Err(); err != nil && ctx.Err() == nil { common.LogError(i18n.Tr("forwardshell_read_failed", err)) } } diff --git a/plugins/local/reverseshell.go b/plugins/local/reverseshell.go index 53704a7..ac7f695 100644 --- a/plugins/local/reverseshell.go +++ b/plugins/local/reverseshell.go @@ -5,6 +5,7 @@ package local import ( "bufio" "context" + "errors" "fmt" "io" "net" @@ -13,6 +14,7 @@ import ( "runtime" "strconv" "strings" + "time" "github.com/shadow1ng/fscan/common" "github.com/shadow1ng/fscan/common/i18n" @@ -123,12 +125,20 @@ func (p *ReverseShellPlugin) startNativeReverseShell(ctx context.Context, host s prompt := fmt.Sprintf("%s> ", getCurrentDir()) _, _ = conn.Write([]byte(prompt)) + // 设置读取超时,以便能响应 ctx 取消 + _ = conn.SetReadDeadline(time.Now().Add(1 * time.Second)) + // 读取命令 cmdLine, err := reader.ReadString('\n') if err != nil { if err == io.EOF { return nil } + // 超时继续循环检查 ctx + var netErr net.Error + if errors.As(err, &netErr) && netErr.Timeout() { + continue + } return fmt.Errorf("读取命令错误: %w", err) } diff --git a/plugins/local/socks5proxy.go b/plugins/local/socks5proxy.go index 8c74a9d..d24d075 100644 --- a/plugins/local/socks5proxy.go +++ b/plugins/local/socks5proxy.go @@ -118,24 +118,34 @@ func (p *Socks5ProxyPlugin) startSocks5Server(ctx context.Context, port int, sta } // 并发处理客户端连接 - go p.handleClient(conn) + go p.handleClient(ctx, conn) } } // handleClient 处理客户端连接 -func (p *Socks5ProxyPlugin) handleClient(clientConn net.Conn) { +func (p *Socks5ProxyPlugin) handleClient(ctx context.Context, clientConn net.Conn) { defer func() { _ = clientConn.Close() }() + // ctx 取消时关闭连接,解除阻塞的 IO + go func() { + <-ctx.Done() + _ = clientConn.Close() + }() + // SOCKS5握手阶段 if err := p.handleSocks5Handshake(clientConn); err != nil { - common.LogError(i18n.Tr("socks5_handshake_failed", err)) + if ctx.Err() == nil { + common.LogError(i18n.Tr("socks5_handshake_failed", err)) + } return } // SOCKS5请求阶段 targetConn, _, err := p.handleSocks5Request(clientConn) if err != nil { - common.LogError(i18n.Tr("socks5_request_failed", err)) + if ctx.Err() == nil { + common.LogError(i18n.Tr("socks5_request_failed", err)) + } return } defer func() { _ = targetConn.Close() }() diff --git a/web/api/scan.go b/web/api/scan.go index 8634cc7..7337c4e 100644 --- a/web/api/scan.go +++ b/web/api/scan.go @@ -206,9 +206,11 @@ func (h *ScanHandler) runScan(req ScanRequest) { fv.DisableSave = true // Web模式不保存到文件 fv.Silent = true // 静默模式 - // 构建Config + // 构建Config,同步到全局实例供 network/限速等模块使用 config := common.BuildConfigFromFlags(fv) state := common.NewState() + common.SetGlobalConfig(config) + common.SetGlobalState(state) // 设置WebSocket结果回调 common.SetResultCallback(func(result interface{}) {