mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-24 20:21:52 +08:00
perf: 优化注释
This commit is contained in:
+101
-50
@@ -13,76 +13,89 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 定义在文件开头
|
||||
// 全局变量定义
|
||||
var (
|
||||
LocalScan bool // 本地扫描模式标识
|
||||
WebScan bool // Web扫描模式标识
|
||||
)
|
||||
|
||||
// Scan 执行扫描主流程
|
||||
// info: 主机信息结构体,包含扫描目标的基本信息
|
||||
func Scan(info Common.HostInfo) {
|
||||
Common.LogInfo("开始信息扫描")
|
||||
|
||||
// 初始化HTTP客户端
|
||||
// 初始化HTTP客户端配置
|
||||
lib.Inithttp()
|
||||
|
||||
// 初始化并发控制
|
||||
ch := make(chan struct{}, Common.ThreadNum)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
// 执行扫描逻辑
|
||||
// 根据扫描模式执行不同的扫描策略
|
||||
switch {
|
||||
case Common.LocalMode:
|
||||
// 本地信息收集模式
|
||||
executeLocalScan(info, &ch, &wg)
|
||||
case len(Common.URLs) > 0:
|
||||
// Web扫描模式
|
||||
executeWebScan(info, &ch, &wg)
|
||||
default:
|
||||
// 主机扫描模式
|
||||
executeHostScan(info, &ch, &wg)
|
||||
}
|
||||
|
||||
// 等待扫描完成
|
||||
// 等待所有扫描任务完成
|
||||
finishScan(&wg)
|
||||
}
|
||||
|
||||
// 执行本地扫描
|
||||
// executeLocalScan 执行本地扫描
|
||||
// info: 主机信息
|
||||
// ch: 并发控制通道
|
||||
// wg: 等待组
|
||||
func executeLocalScan(info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
Common.LogInfo("执行本地信息收集")
|
||||
|
||||
// 定义本地模式允许的插件
|
||||
// 获取本地模式支持的插件列表
|
||||
validLocalPlugins := getValidPlugins(Common.ModeLocal)
|
||||
|
||||
// 校验扫描模式
|
||||
// 验证扫描模式的合法性
|
||||
if err := validateScanMode(validLocalPlugins, Common.ModeLocal); err != nil {
|
||||
Common.LogError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 输出使用的插件信息
|
||||
if Common.ScanMode == Common.ModeLocal {
|
||||
Common.LogInfo("使用全部本地插件")
|
||||
} else {
|
||||
Common.LogInfo(fmt.Sprintf("使用插件: %s", Common.ScanMode))
|
||||
}
|
||||
|
||||
// 执行扫描
|
||||
// 执行扫描任务
|
||||
executeScans([]Common.HostInfo{info}, ch, wg)
|
||||
}
|
||||
|
||||
// 执行Web扫描
|
||||
// executeWebScan 执行Web扫描
|
||||
// info: 主机信息
|
||||
// ch: 并发控制通道
|
||||
// wg: 等待组
|
||||
func executeWebScan(info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
Common.LogInfo("开始Web扫描")
|
||||
|
||||
// 从 pluginGroups 获取Web模式允许的插件
|
||||
// 获取Web模式支持的插件列表
|
||||
validWebPlugins := getValidPlugins(Common.ModeWeb)
|
||||
|
||||
// 校验扫描模式
|
||||
// 验证扫描模式的合法性
|
||||
if err := validateScanMode(validWebPlugins, Common.ModeWeb); err != nil {
|
||||
Common.LogError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 创建目标URL信息
|
||||
// 处理目标URL列表
|
||||
var targetInfos []Common.HostInfo
|
||||
for _, url := range Common.URLs {
|
||||
urlInfo := info
|
||||
// 确保URL包含协议头
|
||||
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
||||
url = "http://" + url
|
||||
}
|
||||
@@ -90,23 +103,29 @@ func executeWebScan(info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup)
|
||||
targetInfos = append(targetInfos, urlInfo)
|
||||
}
|
||||
|
||||
// 输出使用的插件信息
|
||||
if Common.ScanMode == Common.ModeWeb {
|
||||
Common.LogInfo("使用全部Web插件")
|
||||
} else {
|
||||
Common.LogInfo(fmt.Sprintf("使用插件: %s", Common.ScanMode))
|
||||
}
|
||||
|
||||
// 执行扫描
|
||||
// 执行扫描任务
|
||||
executeScans(targetInfos, ch, wg)
|
||||
}
|
||||
|
||||
// 执行主机扫描
|
||||
// executeHostScan 执行主机扫描
|
||||
// info: 主机信息
|
||||
// ch: 并发控制通道
|
||||
// wg: 等待组
|
||||
func executeHostScan(info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
// 验证扫描目标
|
||||
if info.Host == "" {
|
||||
Common.LogError("未指定扫描目标")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析目标主机
|
||||
hosts, err := Common.ParseIP(info.Host, Common.HostsFile, Common.ExcludeHosts)
|
||||
if err != nil {
|
||||
Common.LogError(fmt.Sprintf("解析主机错误: %v", err))
|
||||
@@ -117,7 +136,9 @@ func executeHostScan(info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup
|
||||
executeScan(hosts, info, ch, wg)
|
||||
}
|
||||
|
||||
// 获取合法的插件列表
|
||||
// getValidPlugins 获取指定模式下的有效插件列表
|
||||
// mode: 扫描模式
|
||||
// 返回: 有效插件映射表
|
||||
func getValidPlugins(mode string) map[string]bool {
|
||||
validPlugins := make(map[string]bool)
|
||||
for _, plugin := range Common.PluginGroups[mode] {
|
||||
@@ -126,7 +147,10 @@ func getValidPlugins(mode string) map[string]bool {
|
||||
return validPlugins
|
||||
}
|
||||
|
||||
// 校验扫描模式是否有效
|
||||
// validateScanMode 验证扫描模式的合法性
|
||||
// validPlugins: 有效插件列表
|
||||
// mode: 扫描模式
|
||||
// 返回: 错误信息
|
||||
func validateScanMode(validPlugins map[string]bool, mode string) error {
|
||||
if Common.ScanMode == "" || Common.ScanMode == "All" {
|
||||
Common.ScanMode = mode
|
||||
@@ -137,12 +161,16 @@ func validateScanMode(validPlugins map[string]bool, mode string) error {
|
||||
}
|
||||
|
||||
// executeScan 执行主扫描流程
|
||||
// hosts: 目标主机列表
|
||||
// info: 主机信息
|
||||
// ch: 并发控制通道
|
||||
// wg: 等待组
|
||||
func executeScan(hosts []string, info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
var targetInfos []Common.HostInfo
|
||||
|
||||
// 扫描主机和端口
|
||||
// 处理主机和端口扫描
|
||||
if len(hosts) > 0 || len(Common.HostPort) > 0 {
|
||||
// 处理活跃主机
|
||||
// 检查主机存活性
|
||||
if shouldPingScan(hosts) {
|
||||
hosts = CheckLive(hosts, Common.UsePing)
|
||||
Common.LogInfo(fmt.Sprintf("存活主机数量: %d", len(hosts)))
|
||||
@@ -151,42 +179,48 @@ func executeScan(hosts []string, info Common.HostInfo, ch *chan struct{}, wg *sy
|
||||
}
|
||||
}
|
||||
|
||||
// 处理活跃端口
|
||||
// 获取存活端口
|
||||
alivePorts := getAlivePorts(hosts)
|
||||
if len(alivePorts) > 0 {
|
||||
targetInfos = prepareTargetInfos(alivePorts, info)
|
||||
}
|
||||
}
|
||||
|
||||
// 添加 URL 扫描目标
|
||||
// 添加URL扫描目标
|
||||
targetInfos = appendURLTargets(targetInfos, info)
|
||||
|
||||
// 如果有扫描目标,执行漏洞扫描
|
||||
// 执行漏洞扫描
|
||||
if len(targetInfos) > 0 {
|
||||
Common.LogInfo("开始漏洞扫描")
|
||||
executeScans(targetInfos, ch, wg)
|
||||
}
|
||||
}
|
||||
|
||||
// shouldPingScan 判断是否需要进行 ping 扫描
|
||||
// shouldPingScan 判断是否需要执行ping扫描
|
||||
// hosts: 目标主机列表
|
||||
// 返回: 是否需要ping扫描
|
||||
func shouldPingScan(hosts []string) bool {
|
||||
return (Common.DisablePing == false && len(hosts) > 1) || Common.IsICMPScan()
|
||||
}
|
||||
|
||||
// getAlivePorts 获取存活端口
|
||||
// getAlivePorts 获取存活端口列表
|
||||
// hosts: 目标主机列表
|
||||
// 返回: 存活端口列表
|
||||
func getAlivePorts(hosts []string) []string {
|
||||
var alivePorts []string
|
||||
|
||||
// 根据扫描模式选择端口扫描方式
|
||||
if Common.IsWebScan() {
|
||||
alivePorts = NoPortScan(hosts, Common.Ports)
|
||||
} else if len(hosts) > 0 {
|
||||
alivePorts = PortScan(hosts, Common.Ports, Common.Timeout)
|
||||
Common.LogInfo(fmt.Sprintf("存活端口数量: %d", len(alivePorts)))
|
||||
if Common.IsPortScan() {
|
||||
return nil // 结束扫描
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 合并传入的端口信息
|
||||
// 合并额外指定的端口
|
||||
if len(Common.HostPort) > 0 {
|
||||
alivePorts = append(alivePorts, Common.HostPort...)
|
||||
alivePorts = Common.RemoveDuplicate(alivePorts)
|
||||
@@ -197,7 +231,10 @@ func getAlivePorts(hosts []string) []string {
|
||||
return alivePorts
|
||||
}
|
||||
|
||||
// appendURLTargets 添加 URL 扫描目标
|
||||
// appendURLTargets 添加URL扫描目标
|
||||
// targetInfos: 现有目标列表
|
||||
// baseInfo: 基础主机信息
|
||||
// 返回: 更新后的目标列表
|
||||
func appendURLTargets(targetInfos []Common.HostInfo, baseInfo Common.HostInfo) []Common.HostInfo {
|
||||
for _, url := range Common.URLs {
|
||||
urlInfo := baseInfo
|
||||
@@ -208,6 +245,9 @@ func appendURLTargets(targetInfos []Common.HostInfo, baseInfo Common.HostInfo) [
|
||||
}
|
||||
|
||||
// prepareTargetInfos 准备扫描目标信息
|
||||
// alivePorts: 存活端口列表
|
||||
// baseInfo: 基础主机信息
|
||||
// 返回: 目标信息列表
|
||||
func prepareTargetInfos(alivePorts []string, baseInfo Common.HostInfo) []Common.HostInfo {
|
||||
var infos []Common.HostInfo
|
||||
for _, targetIP := range alivePorts {
|
||||
@@ -224,24 +264,27 @@ func prepareTargetInfos(alivePorts []string, baseInfo Common.HostInfo) []Common.
|
||||
return infos
|
||||
}
|
||||
|
||||
// 扫描任务结构体定义
|
||||
// ScanTask 扫描任务结构体
|
||||
type ScanTask struct {
|
||||
pluginName string
|
||||
target Common.HostInfo
|
||||
pluginName string // 插件名称
|
||||
target Common.HostInfo // 目标信息
|
||||
}
|
||||
|
||||
// executeScans 执行扫描任务
|
||||
// targets: 目标列表
|
||||
// ch: 并发控制通道
|
||||
// wg: 等待组
|
||||
func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
mode := Common.GetScanMode()
|
||||
|
||||
// 获取待执行的插件列表
|
||||
// 获取要执行的插件列表
|
||||
pluginsToRun, isSinglePlugin := getPluginsToRun(mode)
|
||||
|
||||
var tasks []ScanTask
|
||||
actualTasks := 0
|
||||
loadedPlugins := make([]string, 0)
|
||||
|
||||
// 遍历目标,收集任务
|
||||
// 收集扫描任务
|
||||
for _, target := range targets {
|
||||
targetPort, _ := strconv.Atoi(target.Ports)
|
||||
for _, pluginName := range pluginsToRun {
|
||||
@@ -259,22 +302,22 @@ func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGro
|
||||
}
|
||||
}
|
||||
|
||||
// 去重并排序插件
|
||||
// 处理插件列表
|
||||
finalPlugins := getUniquePlugins(loadedPlugins)
|
||||
|
||||
// 输出加载的插件信息
|
||||
Common.LogInfo(fmt.Sprintf("加载的插件: %s", strings.Join(finalPlugins, ", ")))
|
||||
|
||||
// 初始化进度条
|
||||
initializeProgressBar(actualTasks)
|
||||
|
||||
// 执行收集的任务
|
||||
// 执行扫描任务
|
||||
for _, task := range tasks {
|
||||
AddScan(task.pluginName, task.target, ch, wg)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取待执行插件列表
|
||||
// getPluginsToRun 获取要执行的插件列表
|
||||
// mode: 扫描模式
|
||||
// 返回: 插件列表和是否为单插件模式
|
||||
func getPluginsToRun(mode string) ([]string, bool) {
|
||||
var pluginsToRun []string
|
||||
isSinglePlugin := false
|
||||
@@ -289,12 +332,17 @@ func getPluginsToRun(mode string) ([]string, bool) {
|
||||
return pluginsToRun, isSinglePlugin
|
||||
}
|
||||
|
||||
// 收集扫描任务
|
||||
// collectScanTasks 收集扫描任务
|
||||
// plugin: 插件信息
|
||||
// target: 目标信息
|
||||
// targetPort: 目标端口
|
||||
// pluginName: 插件名称
|
||||
// isSinglePlugin: 是否为单插件模式
|
||||
// 返回: 是否添加任务和任务列表
|
||||
func collectScanTasks(plugin Common.ScanPlugin, target Common.HostInfo, targetPort int, pluginName string, isSinglePlugin bool) (bool, []ScanTask) {
|
||||
var tasks []ScanTask
|
||||
taskAdded := false
|
||||
|
||||
// Web模式特殊处理
|
||||
if WebScan || LocalScan || isSinglePlugin || len(plugin.Ports) == 0 || plugin.HasPort(targetPort) {
|
||||
taskAdded = true
|
||||
tasks = append(tasks, ScanTask{
|
||||
@@ -306,7 +354,9 @@ func collectScanTasks(plugin Common.ScanPlugin, target Common.HostInfo, targetPo
|
||||
return taskAdded, tasks
|
||||
}
|
||||
|
||||
// 获取去重后的插件列表
|
||||
// getUniquePlugins 获取去重后的插件列表
|
||||
// loadedPlugins: 已加载的插件列表
|
||||
// 返回: 去重并排序后的插件列表
|
||||
func getUniquePlugins(loadedPlugins []string) []string {
|
||||
uniquePlugins := make(map[string]struct{})
|
||||
for _, p := range loadedPlugins {
|
||||
@@ -322,7 +372,8 @@ func getUniquePlugins(loadedPlugins []string) []string {
|
||||
return finalPlugins
|
||||
}
|
||||
|
||||
// 初始化进度条
|
||||
// initializeProgressBar 初始化进度条
|
||||
// actualTasks: 实际任务数量
|
||||
func initializeProgressBar(actualTasks int) {
|
||||
if Common.ShowProgress {
|
||||
Common.ProgressBar = progressbar.NewOptions(actualTasks,
|
||||
@@ -345,20 +396,24 @@ func initializeProgressBar(actualTasks int) {
|
||||
}
|
||||
|
||||
// finishScan 完成扫描任务
|
||||
// wg: 等待组
|
||||
func finishScan(wg *sync.WaitGroup) {
|
||||
wg.Wait()
|
||||
// 确保进度条完成,只在存在进度条时调用
|
||||
if Common.ProgressBar != nil {
|
||||
Common.ProgressBar.Finish()
|
||||
fmt.Println() // 添加一个换行
|
||||
fmt.Println()
|
||||
}
|
||||
Common.LogSuccess(fmt.Sprintf("扫描已完成: %v/%v", Common.End, Common.Num))
|
||||
}
|
||||
|
||||
// Mutex用于保护共享资源的并发访问
|
||||
// Mutex 用于保护共享资源的并发访问
|
||||
var Mutex = &sync.Mutex{}
|
||||
|
||||
// AddScan 添加扫描任务并启动扫描
|
||||
// plugin: 插件名称
|
||||
// info: 目标信息
|
||||
// ch: 并发控制通道
|
||||
// wg: 等待组
|
||||
func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
*ch <- struct{}{}
|
||||
wg.Add(1)
|
||||
@@ -369,18 +424,15 @@ func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.Wa
|
||||
<-*ch
|
||||
}()
|
||||
|
||||
// 使用原子操作更新扫描计数
|
||||
atomic.AddInt64(&Common.Num, 1)
|
||||
|
||||
// 执行扫描插件
|
||||
ScanFunc(&plugin, &info)
|
||||
|
||||
// 更新扫描结束后的状态
|
||||
updateScanProgress(&info)
|
||||
}()
|
||||
}
|
||||
|
||||
// ScanFunc 执行扫描插件
|
||||
// name: 插件名称
|
||||
// info: 目标信息
|
||||
func ScanFunc(name *string, info *Common.HostInfo) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
@@ -400,12 +452,11 @@ func ScanFunc(name *string, info *Common.HostInfo) {
|
||||
}
|
||||
|
||||
// updateScanProgress 更新扫描进度
|
||||
// info: 目标信息
|
||||
func updateScanProgress(info *Common.HostInfo) {
|
||||
// 输出互斥锁更新进度条
|
||||
Common.OutputMutex.Lock()
|
||||
atomic.AddInt64(&Common.End, 1)
|
||||
if Common.ProgressBar != nil {
|
||||
// 清除当前行并更新进度条
|
||||
fmt.Print("\033[2K\r")
|
||||
Common.ProgressBar.Add(1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user