refactor(logging): 统一日志前缀,删除废弃的 LogBase

- 删除 LogBase 函数,所有调用迁移到 LogInfo/LogError
- 新增 PrefixDebug ([.]) 前缀,所有日志级别现在都有前缀
- 修复日志输出缩进不一致的问题
- 删除未使用的 PrefixDefault 常量
This commit is contained in:
ZacharyZcR
2026-01-21 18:29:47 +08:00
parent 5e04ad97e7
commit 8312808769
15 changed files with 46 additions and 47 deletions
+1 -1
View File
@@ -283,7 +283,7 @@ func checkParameterConflicts() error {
// 检查 -ao 和 -m icmp 同时指定的情况(向后兼容提示)
if fv.AliveOnly && fv.ScanMode == "icmp" {
LogBase(i18n.GetText("param_conflict_ao_icmp_both"))
LogInfo(i18n.GetText("param_conflict_ao_icmp_both"))
}
// 检查本地插件参数
-3
View File
@@ -66,9 +66,6 @@ func InitLogger() {
// LogDebug 输出调试日志
func LogDebug(msg string) { getGlobalLogger().Debug(msg) }
// LogBase 输出基础日志
func LogBase(msg string) { getGlobalLogger().Base(msg) }
// LogInfo 输出信息日志
func LogInfo(msg string) { getGlobalLogger().Info(msg) }
+4 -4
View File
@@ -60,16 +60,16 @@ const (
// =============================================================================
const (
// PrefixDebug 调试日志前缀
PrefixDebug = "[.]"
// PrefixInfo 信息日志前缀
PrefixInfo = "[*]"
// PrefixSuccess 成功日志前缀
PrefixSuccess = "[+]"
// PrefixVuln 漏洞/重要发现前缀
PrefixVuln = "[!]"
// PrefixInfo 信息日志前缀
PrefixInfo = "[*]"
// PrefixError 错误日志前缀
PrefixError = "[-]"
// PrefixDefault 默认日志前缀
PrefixDefault = " "
)
// =============================================================================
+7 -5
View File
@@ -201,15 +201,17 @@ func (l *Logger) formatElapsedTime(elapsed time.Duration) string {
// getLevelPrefix 获取日志级别前缀
func (l *Logger) getLevelPrefix(level LogLevel) string {
switch level {
case LevelVuln:
return PrefixVuln
case LevelSuccess:
return PrefixSuccess
case LevelDebug:
return PrefixDebug
case LevelInfo:
return PrefixInfo
case LevelSuccess:
return PrefixSuccess
case LevelVuln:
return PrefixVuln
case LevelError:
return PrefixError
default:
return PrefixDefault
return PrefixInfo // 默认使用 Info 前缀
}
}
+2 -2
View File
@@ -137,14 +137,14 @@ func TestLogger_AllLevels(t *testing.T) {
logFunc: logger.Debug,
message: "debug message",
wantMsg: "debug message",
wantPfx: PrefixDefault,
wantPfx: PrefixDebug,
},
{
name: "Base级别",
logFunc: logger.Base,
message: "base message",
wantMsg: "base message",
wantPfx: PrefixDefault,
wantPfx: PrefixInfo, // Base 已废弃,默认使用 Info 前缀
},
{
name: "Info级别",
+2 -2
View File
@@ -237,9 +237,9 @@ func (b *BaseScanStrategy) LogScanStart() {
// 仅在本地/Web等特殊模式下显示
switch b.filterType {
case FilterLocal:
common.LogBase(i18n.GetText("start_local_scan"))
common.LogInfo(i18n.GetText("start_local_scan"))
case FilterWeb:
common.LogBase(i18n.GetText("start_web_scan"))
common.LogInfo(i18n.GetText("start_web_scan"))
}
}
+4 -4
View File
@@ -137,7 +137,7 @@ func probeWithICMP(hostslist []string, chanHosts chan string, aliveHosts *[]stri
}
common.LogError(i18n.Tr("icmp_listen_failed", err))
common.LogBase(i18n.GetText("trying_no_listen_icmp"))
common.LogInfo(i18n.GetText("trying_no_listen_icmp"))
// 尝试无监听ICMP探测
conn2, err := net.DialTimeout("ip4:icmp", "127.0.0.1", 3*time.Second)
@@ -147,9 +147,9 @@ func probeWithICMP(hostslist []string, chanHosts chan string, aliveHosts *[]stri
return
}
common.LogBase(i18n.Tr("icmp_connect_failed", err))
common.LogBase(i18n.GetText("insufficient_privileges"))
common.LogBase(i18n.GetText("switching_to_ping"))
common.LogError(i18n.Tr("icmp_connect_failed", err))
common.LogError(i18n.GetText("insufficient_privileges"))
common.LogInfo(i18n.GetText("switching_to_ping"))
// 降级使用ping探测
RunPing(hostslist, chanHosts, livewg)
+3 -3
View File
@@ -24,9 +24,9 @@ func NewLocalScanStrategy() *LocalScanStrategy {
func (s *LocalScanStrategy) LogPluginInfo(config *common.Config) {
localPlugin := config.LocalPlugin
if localPlugin != "" {
common.LogBase(i18n.Tr("local_plugin_info", localPlugin))
common.LogInfo(i18n.Tr("local_plugin_info", localPlugin))
} else {
common.LogBase(i18n.GetText("local_plugin_not_specified"))
common.LogError(i18n.GetText("local_plugin_not_specified"))
}
}
@@ -54,7 +54,7 @@ func (s *LocalScanStrategy) Execute(config *common.Config, state *common.State,
// 验证本地插件是否存在
if config.LocalPlugin != "" {
if !plugins.Exists(config.LocalPlugin) {
common.LogBase(i18n.Tr("local_plugin_not_found", config.LocalPlugin))
common.LogError(i18n.Tr("local_plugin_not_found", config.LocalPlugin))
return
}
}
+4 -4
View File
@@ -213,7 +213,7 @@ func EnhancedPortScan(hosts []string, ports string, timeout int64, config *commo
// 检查代理可靠性,如果存在全回显问题则警告
if common.IsProxyEnabled() && !common.IsProxyReliable() {
common.LogBase("[!] 检测到代理存在全回显问题,端口扫描结果可能不准确")
common.LogError("检测到代理存在全回显问题,端口扫描结果可能不准确")
}
// 创建流式迭代器(O(1) 内存,端口喷洒策略)
@@ -226,12 +226,12 @@ func EnhancedPortScan(hosts []string, ports string, timeout int64, config *commo
// 大规模扫描警告和线程数自动调整
if totalTasks > 100000 {
common.LogBase(fmt.Sprintf("[*] 大规模扫描: %d 个目标 (%d主机 × %d端口)", totalTasks, len(hosts), len(portList)))
common.LogInfo(fmt.Sprintf("大规模扫描: %d 个目标 (%d主机 × %d端口)", totalTasks, len(hosts), len(portList)))
// 如果任务数超过100万且线程数大于300,自动降低线程数
if totalTasks > 1000000 && threadNum > 300 {
oldThreadNum := threadNum
threadNum = 300
common.LogBase(fmt.Sprintf("[*] 自动调整线程数: %d -> %d (大规模扫描优化)", oldThreadNum, threadNum))
common.LogInfo(fmt.Sprintf("自动调整线程数: %d -> %d (大规模扫描优化)", oldThreadNum, threadNum))
}
}
@@ -285,7 +285,7 @@ func EnhancedPortScan(hosts []string, ports string, timeout int64, config *commo
common.FinishProgressBar()
}
common.LogBase(i18n.Tr("port_scan_complete", count))
common.LogInfo(i18n.Tr("port_scan_complete", count))
// 检查扫描失败率,如果过高则警告用户
resourceErrors := state.GetResourceExhaustedCount()
+6 -6
View File
@@ -96,21 +96,21 @@ func RunScan(info common.HostInfo, config *common.Config, state *common.State) {
// 检查是否有活跃的连接需要维持
if state.IsReverseShellActive() || state.IsSocks5ProxyActive() || state.IsForwardShellActive() {
if state.IsReverseShellActive() {
common.LogBase(i18n.GetText("active_reverse_shell"))
common.LogInfo(i18n.GetText("active_reverse_shell"))
}
if state.IsSocks5ProxyActive() {
common.LogBase(i18n.GetText("active_socks5_proxy"))
common.LogInfo(i18n.GetText("active_socks5_proxy"))
}
if state.IsForwardShellActive() {
common.LogBase(i18n.GetText("active_forward_shell"))
common.LogInfo(i18n.GetText("active_forward_shell"))
}
common.LogBase(i18n.GetText("press_ctrl_c_exit"))
common.LogInfo(i18n.GetText("press_ctrl_c_exit"))
// 优雅等待信号
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
common.LogBase(i18n.GetText("received_exit_signal"))
common.LogInfo(i18n.GetText("received_exit_signal"))
}
// 完成扫描
@@ -125,7 +125,7 @@ func finishScan(config *common.Config, state *common.State) {
}
// 输出扫描完成信息
common.LogBase(i18n.Tr("scan_task_complete", time.Since(state.GetStartTime()).Round(time.Millisecond), state.GetNum()))
common.LogInfo(i18n.Tr("scan_task_complete", time.Since(state.GetStartTime()).Round(time.Millisecond), state.GetNum()))
// 输出性能统计 JSON(如果启用)
if config.Output.PerfStats {
+7 -7
View File
@@ -70,12 +70,12 @@ func (s *ServiceScanStrategy) showPluginsForSpecifiedPorts(config *common.Config
if len(applicablePlugins) > 0 {
pluginStr := formatPluginList(applicablePlugins)
if isCustomMode {
common.LogBase(i18n.Tr("service_plugin_custom", pluginStr))
common.LogInfo(i18n.Tr("service_plugin_custom", pluginStr))
} else {
common.LogBase(i18n.Tr("service_plugin_info", pluginStr))
common.LogInfo(i18n.Tr("service_plugin_info", pluginStr))
}
} else {
common.LogBase(i18n.GetText("service_plugin_none"))
common.LogInfo(i18n.GetText("service_plugin_none"))
}
}
@@ -201,9 +201,9 @@ func (s *ServiceScanStrategy) LogVulnerabilityPluginInfo(targets []common.HostIn
// 输出插件信息
if len(servicePlugins) > 0 {
common.LogBase(i18n.Tr("service_plugin_info", strings.Join(servicePlugins, ", ")))
common.LogInfo(i18n.Tr("service_plugin_info", strings.Join(servicePlugins, ", ")))
} else {
common.LogBase(i18n.GetText("scan_no_service_plugins"))
common.LogInfo(i18n.GetText("scan_no_service_plugins"))
}
}
@@ -227,7 +227,7 @@ func (s *ServiceScanStrategy) discoverTargets(hostInput string, baseInfo common.
// 主机存活检测
if s.shouldPerformLivenessCheck(hosts, config) {
hosts = CheckLive(hosts, false, config, state)
common.LogBase(i18n.Tr("alive_hosts_count_info", len(hosts)))
common.LogInfo(i18n.Tr("alive_hosts_count_info", len(hosts)))
}
// 端口扫描
@@ -253,7 +253,7 @@ func (s *ServiceScanStrategy) discoverAlivePorts(hosts []string, config *common.
hostPorts := state.GetHostPorts()
if len(hostPorts) > 0 {
alivePorts = hostPorts
common.LogBase(i18n.Tr("alive_ports_count", len(alivePorts)))
common.LogInfo(i18n.Tr("alive_ports_count", len(alivePorts)))
state.ClearHostPorts()
return alivePorts
}
+1 -1
View File
@@ -59,7 +59,7 @@ func main() {
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
common.LogBase(i18n.GetText("received_exit_signal"))
common.LogInfo(i18n.GetText("received_exit_signal"))
_ = common.Cleanup() // 确保结果写入磁盘
os.Exit(130) // 128 + SIGINT(2) = 130,标准的中断退出码
}()
+2 -2
View File
@@ -49,7 +49,7 @@ func (p *Socks5ProxyPlugin) Scan(ctx context.Context, info *common.HostInfo, con
output.WriteString(fmt.Sprintf("监听端口: %d\n", port))
output.WriteString(fmt.Sprintf("平台: %s\n\n", runtime.GOOS))
common.LogBase(i18n.Tr("socks5_starting", port))
common.LogInfo(i18n.Tr("socks5_starting", port))
// 启动SOCKS5代理服务器
err := p.startSocks5Server(ctx, port, state)
@@ -96,7 +96,7 @@ func (p *Socks5ProxyPlugin) startSocks5Server(ctx context.Context, port int, sta
for {
select {
case <-ctx.Done():
common.LogBase(i18n.GetText("socks5_cancelled"))
common.LogInfo(i18n.GetText("socks5_cancelled"))
return ctx.Err()
default:
}
+2 -2
View File
@@ -85,7 +85,7 @@ func StartServer(port int) error {
go func() {
<-quit
common.LogBase(i18n.GetText("web_shutting_down"))
common.LogInfo(i18n.GetText("web_shutting_down"))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
@@ -98,7 +98,7 @@ func StartServer(port int) error {
// 启动服务器
common.LogSuccess(i18n.Tr("web_server_started", port))
common.LogBase(fmt.Sprintf("http://localhost:%d", port))
fmt.Printf(" http://localhost:%d\n", port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("server error: %w", err)
+1 -1
View File
@@ -304,7 +304,7 @@ func loadPocsConcurrently(pocFiles []string, isEmbedded bool, pocPath string) {
}
wg.Wait()
common.LogBase(i18n.Tr("poc_load_complete", pocCount, successCount, failCount))
common.LogInfo(i18n.Tr("poc_load_complete", pocCount, successCount, failCount))
}
// directoryExists 检查目录是否存在