mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-25 20:51:52 +08:00
perf: 优化注释
This commit is contained in:
+60
-56
@@ -9,56 +9,58 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 服务信息结构
|
||||
// ServiceInfo 定义服务识别的结果信息
|
||||
type ServiceInfo struct {
|
||||
Name string // 服务名称
|
||||
Banner string // 服务横幅
|
||||
Version string // 版本信息
|
||||
Extras map[string]string // 额外信息
|
||||
Name string // 服务名称,如 http、ssh 等
|
||||
Banner string // 服务返回的横幅信息
|
||||
Version string // 服务版本号
|
||||
Extras map[string]string // 其他额外信息,如操作系统、产品名等
|
||||
}
|
||||
|
||||
// Result 结构体
|
||||
// Result 定义单次探测的结果
|
||||
type Result struct {
|
||||
Service Service
|
||||
Banner string
|
||||
Extras map[string]string
|
||||
Send []byte // 发送的数据
|
||||
Recv []byte // 接收到的数据
|
||||
Service Service // 识别出的服务信息
|
||||
Banner string // 服务横幅
|
||||
Extras map[string]string // 额外信息
|
||||
Send []byte // 发送的探测数据
|
||||
Recv []byte // 接收到的响应数据
|
||||
}
|
||||
|
||||
// Service 定义服务的基本信息
|
||||
type Service struct {
|
||||
Name string
|
||||
Extras map[string]string
|
||||
Name string // 服务名称
|
||||
Extras map[string]string // 服务的额外属性
|
||||
}
|
||||
|
||||
// 扫描器相关结构
|
||||
// Info 定义单个端口探测的上下文信息
|
||||
type Info struct {
|
||||
Address string
|
||||
Port int
|
||||
Conn net.Conn
|
||||
Result Result
|
||||
Found bool
|
||||
Address string // 目标IP地址
|
||||
Port int // 目标端口
|
||||
Conn net.Conn // 网络连接
|
||||
Result Result // 探测结果
|
||||
Found bool // 是否成功识别服务
|
||||
}
|
||||
|
||||
// PortInfoScanner 定义端口服务识别器
|
||||
type PortInfoScanner struct {
|
||||
Address string
|
||||
Port int
|
||||
Conn net.Conn
|
||||
Timeout time.Duration
|
||||
info *Info
|
||||
Address string // 目标IP地址
|
||||
Port int // 目标端口
|
||||
Conn net.Conn // 网络连接
|
||||
Timeout time.Duration // 超时时间
|
||||
info *Info // 探测上下文
|
||||
}
|
||||
|
||||
// 预定义探测器
|
||||
// 预定义的基础探测器
|
||||
var (
|
||||
null = new(Probe)
|
||||
common = new(Probe)
|
||||
null = new(Probe) // 空探测器,用于基本协议识别
|
||||
common = new(Probe) // 通用探测器,用于常见服务识别
|
||||
)
|
||||
|
||||
// NewPortInfoScanner 创建新的端口服务识别器
|
||||
// NewPortInfoScanner 创建新的端口服务识别器实例
|
||||
func NewPortInfoScanner(addr string, port int, conn net.Conn, timeout time.Duration) *PortInfoScanner {
|
||||
return &PortInfoScanner{
|
||||
Address: addr,
|
||||
Port: port,
|
||||
Port: port,
|
||||
Conn: conn,
|
||||
Timeout: timeout,
|
||||
info: &Info{
|
||||
@@ -72,12 +74,12 @@ func NewPortInfoScanner(addr string, port int, conn net.Conn, timeout time.Durat
|
||||
}
|
||||
}
|
||||
|
||||
// Identify 识别端口服务
|
||||
// Identify 执行服务识别,返回识别结果
|
||||
func (s *PortInfoScanner) Identify() (*ServiceInfo, error) {
|
||||
Common.LogDebug(fmt.Sprintf("开始识别服务 %s:%d", s.Address, s.Port))
|
||||
s.info.PortInfo()
|
||||
|
||||
// 转换识别结果
|
||||
// 构造返回结果
|
||||
serviceInfo := &ServiceInfo{
|
||||
Name: s.info.Result.Service.Name,
|
||||
Banner: s.info.Result.Banner,
|
||||
@@ -94,13 +96,13 @@ func (s *PortInfoScanner) Identify() (*ServiceInfo, error) {
|
||||
return serviceInfo, nil
|
||||
}
|
||||
|
||||
// PortInfo 用于获取端口服务信息
|
||||
// PortInfo 执行端口服务识别的主要逻辑
|
||||
func (i *Info) PortInfo() {
|
||||
// 首次尝试读取响应
|
||||
// 1. 首先尝试读取服务的初始响应
|
||||
if response, err := i.Read(); err == nil && len(response) > 0 {
|
||||
Common.LogDebug(fmt.Sprintf("收到初始响应: %d 字节", len(response)))
|
||||
|
||||
// 依次使用 null 和 common 探测器检查响应
|
||||
// 使用基础探测器检查响应
|
||||
Common.LogDebug("尝试使用基础探测器(null/common)检查响应")
|
||||
if i.tryProbes(response, []*Probe{null, common}) {
|
||||
Common.LogDebug("基础探测器匹配成功")
|
||||
@@ -111,10 +113,10 @@ func (i *Info) PortInfo() {
|
||||
Common.LogDebug(fmt.Sprintf("读取初始响应失败: %v", err))
|
||||
}
|
||||
|
||||
// 记录已使用的探测器
|
||||
// 记录已使用的探测器,避免重复使用
|
||||
usedProbes := make(map[string]struct{})
|
||||
|
||||
// 处理特定端口映射的探测
|
||||
// 2. 尝试使用端口专用探测器
|
||||
Common.LogDebug(fmt.Sprintf("尝试使用端口 %d 的专用探测器", i.Port))
|
||||
if i.processPortMapProbes(usedProbes) {
|
||||
Common.LogDebug("端口专用探测器匹配成功")
|
||||
@@ -122,7 +124,7 @@ func (i *Info) PortInfo() {
|
||||
}
|
||||
Common.LogDebug("端口专用探测器未匹配")
|
||||
|
||||
// 使用默认探测器进行检测
|
||||
// 3. 使用默认探测器列表
|
||||
Common.LogDebug("尝试使用默认探测器列表")
|
||||
if i.processDefaultProbes(usedProbes) {
|
||||
Common.LogDebug("默认探测器匹配成功")
|
||||
@@ -130,14 +132,14 @@ func (i *Info) PortInfo() {
|
||||
}
|
||||
Common.LogDebug("默认探测器未匹配")
|
||||
|
||||
// 如果未能识别服务,标记为未知
|
||||
// 4. 如果所有探测都失败,标记为未知服务
|
||||
if strings.TrimSpace(i.Result.Service.Name) == "" {
|
||||
Common.LogDebug("未识别出服务,标记为 unknown")
|
||||
Common.LogDebug("未识别出服务,标记为 unknown")
|
||||
i.Result.Service.Name = "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// tryProbes 尝试使用给定的探测器列表检查响应
|
||||
// tryProbes 尝试使用指定的探测器列表检查响应
|
||||
func (i *Info) tryProbes(response []byte, probes []*Probe) bool {
|
||||
for _, probe := range probes {
|
||||
Common.LogDebug(fmt.Sprintf("尝试探测器: %s", probe.Name))
|
||||
@@ -150,16 +152,18 @@ func (i *Info) tryProbes(response []byte, probes []*Probe) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// processPortMapProbes 处理端口映射中的探测器
|
||||
// processPortMapProbes 处理端口映射中的专用探测器
|
||||
func (i *Info) processPortMapProbes(usedProbes map[string]struct{}) bool {
|
||||
// 检查是否存在端口专用探测器
|
||||
if len(Common.PortMap[i.Port]) == 0 {
|
||||
Common.LogDebug(fmt.Sprintf("端口 %d 没有专用探测器", i.Port))
|
||||
return false
|
||||
}
|
||||
|
||||
// 遍历端口专用探测器
|
||||
for _, name := range Common.PortMap[i.Port] {
|
||||
Common.LogDebug(fmt.Sprintf("尝试端口专用探测器: %s", name))
|
||||
usedProbes[name] = struct{}{}
|
||||
usedProbes[name] = struct{}{}
|
||||
probe := v.ProbesMapKName[name]
|
||||
|
||||
// 解码探测数据
|
||||
@@ -169,7 +173,7 @@ func (i *Info) processPortMapProbes(usedProbes map[string]struct{}) bool {
|
||||
continue
|
||||
}
|
||||
|
||||
// 建立连接获取响应
|
||||
// 发送探测数据并获取响应
|
||||
Common.LogDebug(fmt.Sprintf("发送探测数据: %d 字节", len(probeData)))
|
||||
if response := i.Connect(probeData); len(response) > 0 {
|
||||
Common.LogDebug(fmt.Sprintf("收到响应: %d 字节", len(response)))
|
||||
@@ -198,11 +202,12 @@ func (i *Info) processPortMapProbes(usedProbes map[string]struct{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// processDefaultProbes 处理默认探测器
|
||||
// processDefaultProbes 处理默认探测器列表
|
||||
func (i *Info) processDefaultProbes(usedProbes map[string]struct{}) bool {
|
||||
failCount := 0
|
||||
const maxFailures = 10
|
||||
const maxFailures = 10 // 最大失败次数
|
||||
|
||||
// 遍历默认探测器列表
|
||||
for _, name := range Common.DefaultMap {
|
||||
// 跳过已使用的探测器
|
||||
if _, used := usedProbes[name]; used {
|
||||
@@ -215,7 +220,7 @@ func (i *Info) processDefaultProbes(usedProbes map[string]struct{}) bool {
|
||||
continue
|
||||
}
|
||||
|
||||
// 建立连接获取响应
|
||||
// 发送探测数据并获取响应
|
||||
response := i.Connect(probeData)
|
||||
if len(response) == 0 {
|
||||
failCount++
|
||||
@@ -260,11 +265,11 @@ func (i *Info) processDefaultProbes(usedProbes map[string]struct{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetInfo 分析响应数据并获取服务信息
|
||||
// GetInfo 分析响应数据并提取服务信息
|
||||
func (i *Info) GetInfo(response []byte, probe *Probe) {
|
||||
Common.LogDebug(fmt.Sprintf("开始分析响应数据,长度: %d", len(response)))
|
||||
Common.LogDebug(fmt.Sprintf("开始分析响应数据,长度: %d", len(response)))
|
||||
|
||||
// 响应数据长度检查
|
||||
// 响应数据有效性检查
|
||||
if len(response) <= 0 {
|
||||
Common.LogDebug("响应数据为空")
|
||||
return
|
||||
@@ -276,8 +281,8 @@ func (i *Info) GetInfo(response []byte, probe *Probe) {
|
||||
softFound bool
|
||||
)
|
||||
|
||||
Common.LogDebug(fmt.Sprintf("处理探测器 %s 的主要匹配规则", probe.Name))
|
||||
// 处理主要匹配规则
|
||||
Common.LogDebug(fmt.Sprintf("处理探测器 %s 的主要匹配规则", probe.Name))
|
||||
if matched, match := i.processMatches(response, probe.Matchs); matched {
|
||||
Common.LogDebug("找到硬匹配")
|
||||
return
|
||||
@@ -304,14 +309,14 @@ func (i *Info) GetInfo(response []byte, probe *Probe) {
|
||||
|
||||
// 处理未找到匹配的情况
|
||||
if !i.Found {
|
||||
Common.LogDebug("未找到硬匹配,处理未匹配情况")
|
||||
Common.LogDebug("未找到硬匹配,处理未匹配情况")
|
||||
i.handleNoMatch(response, result, softFound, softMatch)
|
||||
}
|
||||
}
|
||||
|
||||
// processMatches 处理匹配规则集
|
||||
func (i *Info) processMatches(response []byte, matches *[]Match) (bool, *Match) {
|
||||
Common.LogDebug(fmt.Sprintf("开始处理匹配规则,共 %d 条", len(*matches)))
|
||||
Common.LogDebug(fmt.Sprintf("开始处理匹配规则,共 %d 条", len(*matches)))
|
||||
var softMatch *Match
|
||||
|
||||
for _, match := range *matches {
|
||||
@@ -387,7 +392,7 @@ func (i *Info) Connect(msg []byte) []byte {
|
||||
return reply
|
||||
}
|
||||
|
||||
const WrTimeout = 5 // 默认超时时间(秒)
|
||||
const WrTimeout = 5 // 默认读写超时时间(秒)
|
||||
|
||||
// Write 写入数据到连接
|
||||
func (i *Info) Write(msg []byte) error {
|
||||
@@ -402,7 +407,7 @@ func (i *Info) Write(msg []byte) error {
|
||||
_, err := i.Conn.Write(msg)
|
||||
if err != nil && strings.Contains(err.Error(), "close") {
|
||||
i.Conn.Close()
|
||||
// 重试连接
|
||||
// 连接关闭时重试
|
||||
i.Conn, err = net.DialTimeout("tcp4", fmt.Sprintf("%s:%d", i.Address, i.Port), time.Duration(6)*time.Second)
|
||||
if err == nil {
|
||||
i.Conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(WrTimeout)))
|
||||
@@ -430,7 +435,6 @@ func (i *Info) Read() ([]byte, error) {
|
||||
// 读取数据
|
||||
result, err := readFromConn(i.Conn)
|
||||
if err != nil && strings.Contains(err.Error(), "close") {
|
||||
// 连接关闭的错误处理
|
||||
return result, err
|
||||
}
|
||||
|
||||
@@ -444,7 +448,7 @@ func (i *Info) Read() ([]byte, error) {
|
||||
|
||||
// readFromConn 从连接读取数据的辅助函数
|
||||
func readFromConn(conn net.Conn) ([]byte, error) {
|
||||
size := 2 * 1024
|
||||
size := 2 * 1024 // 读取缓冲区大小
|
||||
var result []byte
|
||||
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user