mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00

经Linus式架构审计,发现并修复插件系统中的具体问题: ## 核心修复 ### 1. 消除local插件GetPorts()方法冗余 - 删除21个local插件中无意义的GetPorts()方法 - 简化local.Plugin接口:移除端口概念 - 理由:本地插件不涉及网络,端口概念完全多余 ### 2. 消除web插件GetPorts()方法冗余 - 删除2个web插件中无用的GetPorts()方法 - 简化web.WebPlugin接口:专注智能HTTP检测 - 理由:Web插件使用动态HTTP检测,预定义端口无价值 ### 3. 统一插件命名规范 - 统一所有插件接口使用Name()方法(符合Go惯例) - 消除GetName()与Name()不一致问题 - 简化适配器:不再需要方法名转换 ## 技术改进 接口精简: - local插件:GetName() + GetPorts() → Name() - web插件:GetName() + GetPorts() → Name() - services插件:GetName() → Name()(保留GetPorts(),业务必需) 代码减少: - 删除23个无用GetPorts()方法 - 重命名52个Name()方法 - 简化3个插件接口定义 ## 影响范围 修改文件:55个插件文件 代码变更:-155行 +61行(净减少94行) 功能影响:零破坏性,保持所有业务逻辑不变 这是基于业务需求分析的精准重构,消除真正多余的部分, 保持系统架构合理性和向后兼容性。
151 lines
4.0 KiB
Go
151 lines
4.0 KiB
Go
package local
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"os"
|
||
"os/exec"
|
||
"runtime"
|
||
"strings"
|
||
"os/user"
|
||
|
||
"github.com/shadow1ng/fscan/common"
|
||
)
|
||
|
||
// SystemInfoPlugin 系统信息收集插件 - Linus式简化版本
|
||
//
|
||
// 设计哲学:纯信息收集,无攻击性功能
|
||
// - 删除复杂的继承体系
|
||
// - 收集基本系统信息
|
||
// - 跨平台支持,运行时适配
|
||
type SystemInfoPlugin struct {
|
||
name string
|
||
}
|
||
|
||
// NewSystemInfoPlugin 创建系统信息插件
|
||
func NewSystemInfoPlugin() *SystemInfoPlugin {
|
||
return &SystemInfoPlugin{
|
||
name: "systeminfo",
|
||
}
|
||
}
|
||
|
||
// GetName 实现Plugin接口
|
||
func (p *SystemInfoPlugin) Name() string {
|
||
return p.name
|
||
}
|
||
|
||
|
||
// Scan 执行系统信息收集 - 直接、简单、有效
|
||
func (p *SystemInfoPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResult {
|
||
var output strings.Builder
|
||
|
||
output.WriteString("=== 系统信息收集 ===\n")
|
||
|
||
// 基本系统信息
|
||
output.WriteString(fmt.Sprintf("操作系统: %s\n", runtime.GOOS))
|
||
output.WriteString(fmt.Sprintf("架构: %s\n", runtime.GOARCH))
|
||
output.WriteString(fmt.Sprintf("CPU核心数: %d\n", runtime.NumCPU()))
|
||
|
||
// 主机名
|
||
if hostname, err := os.Hostname(); err == nil {
|
||
output.WriteString(fmt.Sprintf("主机名: %s\n", hostname))
|
||
}
|
||
|
||
// 当前用户
|
||
if currentUser, err := user.Current(); err == nil {
|
||
output.WriteString(fmt.Sprintf("当前用户: %s\n", currentUser.Username))
|
||
if currentUser.HomeDir != "" {
|
||
output.WriteString(fmt.Sprintf("用户目录: %s\n", currentUser.HomeDir))
|
||
}
|
||
}
|
||
|
||
// 工作目录
|
||
if workDir, err := os.Getwd(); err == nil {
|
||
output.WriteString(fmt.Sprintf("工作目录: %s\n", workDir))
|
||
}
|
||
|
||
// 临时目录
|
||
output.WriteString(fmt.Sprintf("临时目录: %s\n", os.TempDir()))
|
||
|
||
// 环境变量关键信息
|
||
if path := os.Getenv("PATH"); path != "" {
|
||
pathCount := len(strings.Split(path, string(os.PathListSeparator)))
|
||
output.WriteString(fmt.Sprintf("PATH变量条目: %d个\n", pathCount))
|
||
}
|
||
|
||
// 平台特定信息
|
||
platformInfo := p.getPlatformSpecificInfo()
|
||
if platformInfo != "" {
|
||
output.WriteString("\n=== 平台特定信息 ===\n")
|
||
output.WriteString(platformInfo)
|
||
}
|
||
|
||
return &ScanResult{
|
||
Success: true,
|
||
Output: output.String(),
|
||
Error: nil,
|
||
}
|
||
}
|
||
|
||
// getPlatformSpecificInfo 获取平台特定信息 - 运行时适配,不做预检查
|
||
func (p *SystemInfoPlugin) getPlatformSpecificInfo() string {
|
||
var info strings.Builder
|
||
|
||
switch runtime.GOOS {
|
||
case "windows":
|
||
// Windows版本信息
|
||
if output, err := p.runCommand("cmd", "/c", "ver"); err == nil {
|
||
info.WriteString(fmt.Sprintf("Windows版本: %s\n", strings.TrimSpace(output)))
|
||
}
|
||
|
||
// 域信息
|
||
if output, err := p.runCommand("cmd", "/c", "echo %USERDOMAIN%"); err == nil {
|
||
domain := strings.TrimSpace(output)
|
||
if domain != "" && domain != "%USERDOMAIN%" {
|
||
info.WriteString(fmt.Sprintf("用户域: %s\n", domain))
|
||
}
|
||
}
|
||
|
||
case "linux", "darwin":
|
||
// Unix系统信息
|
||
if output, err := p.runCommand("uname", "-a"); err == nil {
|
||
info.WriteString(fmt.Sprintf("系统内核: %s\n", strings.TrimSpace(output)))
|
||
}
|
||
|
||
// 发行版信息(Linux)
|
||
if runtime.GOOS == "linux" {
|
||
if output, err := p.runCommand("lsb_release", "-d"); err == nil {
|
||
info.WriteString(fmt.Sprintf("发行版: %s\n", strings.TrimSpace(output)))
|
||
} else if p.fileExists("/etc/os-release") {
|
||
info.WriteString("发行版: /etc/os-release 存在\n")
|
||
}
|
||
}
|
||
|
||
// whoami
|
||
if output, err := p.runCommand("whoami"); err == nil {
|
||
info.WriteString(fmt.Sprintf("当前用户(whoami): %s\n", strings.TrimSpace(output)))
|
||
}
|
||
}
|
||
|
||
return info.String()
|
||
}
|
||
|
||
// runCommand 执行命令 - 简单包装,无复杂错误处理
|
||
func (p *SystemInfoPlugin) runCommand(name string, args ...string) (string, error) {
|
||
cmd := exec.Command(name, args...)
|
||
output, err := cmd.Output()
|
||
return string(output), err
|
||
}
|
||
|
||
// fileExists 检查文件是否存在
|
||
func (p *SystemInfoPlugin) fileExists(path string) bool {
|
||
_, err := os.Stat(path)
|
||
return err == nil
|
||
}
|
||
|
||
// 注册插件
|
||
func init() {
|
||
RegisterLocalPlugin("systeminfo", func() Plugin {
|
||
return NewSystemInfoPlugin()
|
||
})
|
||
} |