简化输出格式

This commit is contained in:
shadow1ng
2025-04-17 16:18:21 +08:00
parent 875d128e53
commit 805af82a1e
12 changed files with 149 additions and 129 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"sync"
)
var version = "2.0.0"
var version = "2.0.1"
var Userdict = map[string][]string{
"ftp": {"ftp", "admin", "www", "web", "root", "db", "wwwroot", "data"},
"mysql": {"root", "mysql"},
+1 -2
View File
@@ -94,7 +94,6 @@ func Flag(Info *HostInfo) {
flag.Int64Var(&WebTimeout, "wt", 5, GetText("flag_web_timeout"))
flag.StringVar(&HttpProxy, "proxy", "", GetText("flag_http_proxy"))
flag.StringVar(&Socks5Proxy, "socks5", "", GetText("flag_socks5_proxy"))
// 本地扫描配置
flag.BoolVar(&LocalMode, "local", false, GetText("flag_local_mode"))
@@ -128,7 +127,7 @@ func Flag(Info *HostInfo) {
flag.BoolVar(&Silent, "silent", false, GetText("flag_silent_mode"))
flag.BoolVar(&NoColor, "nocolor", false, GetText("flag_no_color"))
flag.BoolVar(&JsonFormat, "json", false, GetText("flag_json_format"))
flag.StringVar(&LogLevel, "log", LogLevelSuccess, GetText("flag_log_level"))
flag.StringVar(&LogLevel, "log", LogLevelBase, GetText("flag_log_level"))
flag.BoolVar(&ShowProgress, "pg", false, GetText("flag_show_progress"))
flag.StringVar(&Language, "lang", "zh", GetText("flag_language"))
+79 -60
View File
@@ -44,6 +44,7 @@ type LogEntry struct {
const (
LogLevelAll = "ALL" // 显示所有级别日志
LogLevelError = "ERROR" // 仅显示错误日志
LogLevelBase = "BASE" // 仅显示信息日志
LogLevelInfo = "INFO" // 仅显示信息日志
LogLevelSuccess = "SUCCESS" // 仅显示成功日志
LogLevelDebug = "DEBUG" // 仅显示调试日志
@@ -51,10 +52,11 @@ const (
// 日志级别对应的显示颜色映射
var logColors = map[string]color.Attribute{
LogLevelError: color.FgRed, // 错误日志显示
LogLevelInfo: color.FgYellow, // 信息日志显示黄色
LogLevelSuccess: color.FgGreen, // 成功日志显示绿色
LogLevelDebug: color.FgBlue, // 调试日志显示
LogLevelError: color.FgBlue, // 错误日志显示
LogLevelBase: color.FgYellow, // 信息日志显示黄色
LogLevelInfo: color.FgGreen, // 信息日志显示绿色
LogLevelSuccess: color.FgRed, // 成功日志显示
LogLevelDebug: color.FgWhite, // 调试日志显示白色
}
// InitLogger 初始化日志系统
@@ -63,42 +65,50 @@ func InitLogger() {
log.SetOutput(io.Discard)
}
var StartTime = time.Now()
// formatLogMessage 格式化日志消息为标准格式
// 返回格式:[时间] [级别] 内容
func formatLogMessage(entry *LogEntry) string {
timeStr := entry.Time.Format("2006-01-02 15:04:05")
return fmt.Sprintf("[%s] [%s] %s", timeStr, entry.Level, entry.Content)
elapsed := time.Since(StartTime)
var timeStr string
// 根据时间长短选择合适的单位
switch {
case elapsed < time.Second:
// 毫秒显示,不需要小数
timeStr = fmt.Sprintf("%dms", elapsed.Milliseconds())
case elapsed < time.Minute:
// 秒显示,保留一位小数
timeStr = fmt.Sprintf("%.1fs", elapsed.Seconds())
case elapsed < time.Hour:
// 分钟和秒显示
minutes := int(elapsed.Minutes())
seconds := int(elapsed.Seconds()) % 60
timeStr = fmt.Sprintf("%dm%ds", minutes, seconds)
default:
// 小时、分钟和秒显示
hours := int(elapsed.Hours())
minutes := int(elapsed.Minutes()) % 60
seconds := int(elapsed.Seconds()) % 60
timeStr = fmt.Sprintf("%dh%dm%ds", hours, minutes, seconds)
}
str := " "
switch entry.Level {
case LogLevelSuccess:
str = "[+]"
case LogLevelInfo:
str = "[*]"
case LogLevelError:
str = "[-]"
}
return fmt.Sprintf("[%s] %s %s", timeStr, str, entry.Content)
}
// printLog 根据日志级别打印日志
func printLog(entry *LogEntry) {
// 根据当前设置的日志级别过滤日志
shouldPrint := false
switch LogLevel {
case LogLevelDebug:
// DEBUG级别显示所有日志
shouldPrint = true
case LogLevelError:
// ERROR级别显示 ERROR、SUCCESS、INFO
shouldPrint = entry.Level == LogLevelError ||
entry.Level == LogLevelSuccess ||
entry.Level == LogLevelInfo
case LogLevelSuccess:
// SUCCESS级别显示 SUCCESS、INFO
shouldPrint = entry.Level == LogLevelSuccess ||
entry.Level == LogLevelInfo
case LogLevelInfo:
// INFO级别只显示 INFO
shouldPrint = entry.Level == LogLevelInfo
case LogLevelAll:
// ALL显示所有日志
shouldPrint = true
default:
// 默认只显示 INFO
shouldPrint = entry.Level == LogLevelInfo
}
if !shouldPrint {
if LogLevel != "debug" && entry.Level == LogLevelDebug {
return
}
@@ -139,27 +149,6 @@ func clearAndWaitProgress() {
}
}
// LogError 记录错误日志,自动包含文件名和行号信息
func LogError(errMsg string) {
// 获取调用者的文件名和行号
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
line = 0
}
file = filepath.Base(file)
errorMsg := fmt.Sprintf("%s:%d - %s", file, line, errMsg)
entry := &LogEntry{
Level: LogLevelError,
Time: time.Now(),
Content: errorMsg,
}
handleLog(entry)
}
// handleLog 统一处理日志的输出
func handleLog(entry *LogEntry) {
if ProgressBar != nil {
@@ -173,6 +162,24 @@ func handleLog(entry *LogEntry) {
}
}
// LogDebug 记录调试日志
func LogDebug(msg string) {
handleLog(&LogEntry{
Level: LogLevelDebug,
Time: time.Now(),
Content: msg,
})
}
// LogInfo 记录进度信息
func LogBase(msg string) {
handleLog(&LogEntry{
Level: LogLevelBase,
Time: time.Now(),
Content: msg,
})
}
// LogInfo 记录信息日志
func LogInfo(msg string) {
handleLog(&LogEntry{
@@ -198,13 +205,25 @@ func LogSuccess(result string) {
status.mu.Unlock()
}
// LogDebug 记录调试日志
func LogDebug(msg string) {
handleLog(&LogEntry{
Level: LogLevelDebug,
// LogError 记录错误日志,自动包含文件名和行号信息
func LogError(errMsg string) {
// 获取调用者的文件名和行号
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
line = 0
}
file = filepath.Base(file)
errorMsg := fmt.Sprintf("%s:%d - %s", file, line, errMsg)
entry := &LogEntry{
Level: LogLevelError,
Time: time.Now(),
Content: msg,
})
Content: errorMsg,
}
handleLog(entry)
}
// CheckErrs 检查是否为需要重试的错误
+17 -17
View File
@@ -31,7 +31,7 @@ func ParseUser() error {
// 处理直接指定的用户名列表
if Username != "" {
usernames = strings.Split(Username, ",")
LogInfo(GetText("no_username_specified", len(usernames)))
LogBase(GetText("no_username_specified", len(usernames)))
}
// 从文件加载用户名列表
@@ -47,12 +47,12 @@ func ParseUser() error {
usernames = append(usernames, user)
}
}
LogInfo(GetText("load_usernames_from_file", len(users)))
LogBase(GetText("load_usernames_from_file", len(users)))
}
// 去重处理
usernames = RemoveDuplicate(usernames)
LogInfo(GetText("total_usernames", len(usernames)))
LogBase(GetText("total_usernames", len(usernames)))
// 更新用户字典
for name := range Userdict {
@@ -74,7 +74,7 @@ func ParsePass(Info *HostInfo) error {
}
}
Passwords = pwdList
LogInfo(GetText("load_passwords", len(pwdList)))
LogBase(GetText("load_passwords", len(pwdList)))
}
// 从文件加载密码列表
@@ -89,7 +89,7 @@ func ParsePass(Info *HostInfo) error {
}
}
Passwords = pwdList
LogInfo(GetText("load_passwords_from_file", len(passes)))
LogBase(GetText("load_passwords_from_file", len(passes)))
}
// 处理哈希文件
@@ -111,7 +111,7 @@ func ParsePass(Info *HostInfo) error {
LogError(GetText("invalid_hash", line))
}
}
LogInfo(GetText("load_valid_hashes", validCount))
LogBase(GetText("load_valid_hashes", validCount))
}
// 处理直接指定的URL列表
@@ -126,7 +126,7 @@ func ParsePass(Info *HostInfo) error {
}
}
}
LogInfo(GetText("load_urls", len(URLs)))
LogBase(GetText("load_urls", len(URLs)))
}
// 从文件加载URL列表
@@ -145,7 +145,7 @@ func ParsePass(Info *HostInfo) error {
}
}
}
LogInfo(GetText("load_urls_from_file", len(urls)))
LogBase(GetText("load_urls_from_file", len(urls)))
}
// 从文件加载主机列表
@@ -168,7 +168,7 @@ func ParsePass(Info *HostInfo) error {
}
}
}
LogInfo(GetText("load_hosts_from_file", len(hosts)))
LogBase(GetText("load_hosts_from_file", len(hosts)))
}
// 从文件加载端口列表
@@ -186,7 +186,7 @@ func ParsePass(Info *HostInfo) error {
}
}
Ports = newport.String()
LogInfo(GetText("load_ports_from_file"))
LogBase(GetText("load_ports_from_file"))
}
return nil
@@ -222,7 +222,7 @@ func Readfile(filename string) ([]string, error) {
return nil, err
}
LogInfo(GetText("read_file_success", filename, lineCount))
LogBase(GetText("read_file_success", filename, lineCount))
return content, nil
}
@@ -251,7 +251,7 @@ func ParseInput(Info *HostInfo) error {
// 处理爆破线程配置
if BruteThreads <= 0 {
BruteThreads = 1
LogInfo(GetText("brute_threads", BruteThreads))
LogBase(GetText("brute_threads", BruteThreads))
}
// 处理端口配置
@@ -265,7 +265,7 @@ func ParseInput(Info *HostInfo) error {
} else {
Ports += "," + AddPorts
}
LogInfo(GetText("extra_ports", AddPorts))
LogBase(GetText("extra_ports", AddPorts))
}
// 处理用户名配置
@@ -275,7 +275,7 @@ func ParseInput(Info *HostInfo) error {
Userdict[dict] = append(Userdict[dict], users...)
Userdict[dict] = RemoveDuplicate(Userdict[dict])
}
LogInfo(GetText("extra_usernames", AddUsers))
LogBase(GetText("extra_usernames", AddUsers))
}
// 处理密码配置
@@ -283,7 +283,7 @@ func ParseInput(Info *HostInfo) error {
passes := strings.Split(AddPasswords, ",")
Passwords = append(Passwords, passes...)
Passwords = RemoveDuplicate(Passwords)
LogInfo(GetText("extra_passwords", AddPasswords))
LogBase(GetText("extra_passwords", AddPasswords))
}
// 处理Socks5代理配置
@@ -301,7 +301,7 @@ func ParseInput(Info *HostInfo) error {
return fmt.Errorf(GetText("socks5_proxy_error", err))
}
DisablePing = true
LogInfo(GetText("socks5_proxy", Socks5Proxy))
LogBase(GetText("socks5_proxy", Socks5Proxy))
}
// 处理HTTP代理配置
@@ -325,7 +325,7 @@ func ParseInput(Info *HostInfo) error {
if err != nil {
return fmt.Errorf(GetText("proxy_format_error", err))
}
LogInfo(GetText("http_proxy", HttpProxy))
LogBase(GetText("http_proxy", HttpProxy))
}
// 处理Hash配置
+13 -13
View File
@@ -24,7 +24,7 @@ func ParseIP(host string, filename string, nohosts ...string) (hosts []string, e
host = hostport[0]
hosts = ParseIPs(host)
Ports = hostport[1]
LogInfo(GetText("host_port_parsed", Ports))
LogBase(GetText("host_port_parsed", Ports))
}
} else {
// 解析主机地址
@@ -37,7 +37,7 @@ func ParseIP(host string, filename string, nohosts ...string) (hosts []string, e
LogError(GetText("read_host_file_failed", err))
} else {
hosts = append(hosts, fileHosts...)
LogInfo(GetText("extra_hosts_loaded", len(fileHosts)))
LogBase(GetText("extra_hosts_loaded", len(fileHosts)))
}
}
}
@@ -64,13 +64,13 @@ func ParseIP(host string, filename string, nohosts ...string) (hosts []string, e
}
hosts = newHosts
sort.Strings(hosts)
LogInfo(GetText("hosts_excluded", len(excludeHosts)))
LogBase(GetText("hosts_excluded", len(excludeHosts)))
}
}
// 去重处理
hosts = RemoveDuplicate(hosts)
LogInfo(GetText("final_valid_hosts", len(hosts)))
LogBase(GetText("final_valid_hosts", len(hosts)))
// 检查解析结果
if len(hosts) == 0 && len(HostPort) == 0 && (host != "" || filename != "") {
@@ -132,7 +132,7 @@ func parseIP2(host string) []string {
ipRange := IPRange(ipNet)
hosts := parseIP1(ipRange)
LogInfo(GetText("parse_cidr_to_range", host, ipRange))
LogBase(GetText("parse_cidr_to_range", host, ipRange))
return hosts
}
@@ -164,7 +164,7 @@ func parseIP1(ip string) []string {
allIP = append(allIP, prefixIP+"."+strconv.Itoa(i))
}
LogInfo(GetText("generate_ip_range", prefixIP, startNum, prefixIP, endNum))
LogBase(GetText("generate_ip_range", prefixIP, startNum, prefixIP, endNum))
} else {
// 处理完整IP范围格式
splitIP1 := strings.Split(ipRange[0], ".")
@@ -197,7 +197,7 @@ func parseIP1(ip string) []string {
allIP = append(allIP, ip)
}
LogInfo(GetText("generate_ip_range", ipRange[0], ipRange[1]))
LogBase(GetText("generate_ip_range", ipRange[0], ipRange[1]))
}
return allIP
@@ -217,7 +217,7 @@ func IPRange(c *net.IPNet) string {
end := bcst.String()
result := fmt.Sprintf("%s-%s", start, end)
LogInfo(GetText("cidr_range", result))
LogBase(GetText("cidr_range", result))
return result
}
@@ -253,11 +253,11 @@ func Readipfile(filename string) ([]string, error) {
for _, host := range hosts {
HostPort = append(HostPort, fmt.Sprintf("%s:%s", host, port))
}
LogInfo(GetText("parse_ip_port", line))
LogBase(GetText("parse_ip_port", line))
} else {
hosts := ParseIPs(line)
content = append(content, hosts...)
LogInfo(GetText("parse_ip_address", line))
LogBase(GetText("parse_ip_address", line))
}
}
@@ -266,7 +266,7 @@ func Readipfile(filename string) ([]string, error) {
return content, err
}
LogInfo(GetText("file_parse_complete", len(content)))
LogBase(GetText("file_parse_complete", len(content)))
return content, nil
}
@@ -300,7 +300,7 @@ func parseIP8(ip string) []string {
ipRange := strings.Split(ip, ".")[0]
var allIP []string
LogInfo(GetText("parse_subnet", ipRange))
LogBase(GetText("parse_subnet", ipRange))
// 遍历所有可能的第二、三段
for a := 0; a <= 255; a++ {
@@ -321,7 +321,7 @@ func parseIP8(ip string) []string {
}
}
LogInfo(GetText("sample_ip_generated", len(allIP)))
LogBase(GetText("sample_ip_generated", len(allIP)))
return allIP
}
+2 -2
View File
@@ -1,9 +1,9 @@
package Common
import (
"sort"
"strconv"
"strings"
"sort"
)
// ParsePort 解析端口配置字符串为端口号列表
@@ -73,7 +73,7 @@ func ParsePort(ports string) []int {
scanPorts = removeDuplicate(scanPorts)
sort.Ints(scanPorts)
LogInfo(GetText("valid_port_count", len(scanPorts)))
LogBase(GetText("valid_port_count", len(scanPorts)))
return scanPorts
}
+6 -6
View File
@@ -46,7 +46,7 @@ var PluginGroups = map[string][]string{
// ParseScanMode 解析扫描模式
func ParseScanMode(mode string) {
LogInfo(GetText("parse_scan_mode", mode))
LogBase(GetText("parse_scan_mode", mode))
// 检查是否是预设模式
presetModes := []string{
@@ -58,9 +58,9 @@ func ParseScanMode(mode string) {
if mode == presetMode {
ScanMode = mode
if plugins := GetPluginsForMode(mode); plugins != nil {
LogInfo(GetText("using_preset_mode_plugins", mode, plugins))
LogBase(GetText("using_preset_mode_plugins", mode, plugins))
} else {
LogInfo(GetText("using_preset_mode", mode))
LogBase(GetText("using_preset_mode", mode))
}
return
}
@@ -69,14 +69,14 @@ func ParseScanMode(mode string) {
// 检查是否是有效的插件名
if _, exists := PluginManager[mode]; exists {
ScanMode = mode
LogInfo(GetText("using_single_plugin", mode))
LogBase(GetText("using_single_plugin", mode))
return
}
// 默认使用All模式
ScanMode = ModeAll
LogInfo(GetText("using_default_mode", ModeAll))
LogInfo(GetText("included_plugins", PluginGroups[ModeAll]))
LogBase(GetText("using_default_mode", ModeAll))
LogBase(GetText("included_plugins", PluginGroups[ModeAll]))
}
// GetPluginsForMode 获取指定模式下的插件列表