fscan/plugins/init.go
ZacharyZcR d570be1f50 Linus式插件系统重写第一阶段完成
- 删除460行过度工程代码,替换为273行简洁实现
- 统一三套独立注册系统为单一全局注册表
- 删除app/container.go容器依赖注入系统(107行)
- 删除app/initializer.go复杂初始化器(75行)
- 删除core/PluginAdapter.go适配器层(82行)
- 删除plugins/{services,web,local}/init.go重复代码(238行)
- 创建plugins/init.go统一插件接口(116行)
- 添加向后兼容适配层保持现有插件不变

架构简化效果:
- 代码减少: 460行 → 273行 (减少41%)
- 接口统一: 3个Plugin接口 → 1个Plugin接口
- 注册系统: 3套独立系统 → 1套全局系统
- 消除特殊情况,符合'好代码没有特殊情况'原则

编译测试通过,基本功能验证正常
2025-08-26 18:03:57 +08:00

117 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package plugins
import (
"context"
"strings"
"sync"
"github.com/shadow1ng/fscan/common"
)
// Plugin 统一插件接口 - 消除过度设计
//
// Linus哲学"好代码没有特殊情况"
// 之前3个不同的接口做同样的事情
// 现在1个接口统治所有插件
type Plugin interface {
Name() string
Scan(ctx context.Context, info *common.HostInfo) *Result
}
// Result 统一结果结构 - 合并所有类型
type Result struct {
Success bool
Service string
Username string
Password string
Banner string
Output string // web/local插件使用
Error error
// Web插件字段
Title string // 网页标题
Status int // HTTP状态码
Server string // 服务器信息
Length int // 响应长度
VulInfo string // 漏洞信息
}
// Exploiter 利用接口 - 保持向后兼容
type Exploiter interface {
Exploit(ctx context.Context, info *common.HostInfo, creds Credential) *ExploitResult
}
// ExploitResult 利用结果
type ExploitResult struct {
Success bool
Output string
Error error
}
// Credential 认证凭据
type Credential struct {
Username string
Password string
KeyData []byte
}
// 全局插件注册表 - 一个数据结构解决所有问题
var (
plugins = make(map[string]func() Plugin)
mutex sync.RWMutex
)
// Register 注册插件 - 一个函数统治所有注册
func Register(name string, factory func() Plugin) {
mutex.Lock()
defer mutex.Unlock()
plugins[name] = factory
}
// Get 获取插件实例
func Get(name string) Plugin {
mutex.RLock()
defer mutex.RUnlock()
if factory, exists := plugins[name]; exists {
return factory()
}
return nil
}
// All 获取所有插件名称
func All() []string {
mutex.RLock()
defer mutex.RUnlock()
names := make([]string, 0, len(plugins))
for name := range plugins {
names = append(names, name)
}
return names
}
// GenerateCredentials 生成测试凭据 - 从services包移到这里统一管理
func GenerateCredentials(service string) []Credential {
users := common.Userdict[service]
if len(users) == 0 {
users = []string{"admin", "root", "administrator", "user", "guest", ""}
}
passwords := common.Passwords
if len(passwords) == 0 {
passwords = []string{"", "admin", "root", "password", "123456"}
}
var credentials []Credential
for _, user := range users {
for _, pass := range passwords {
actualPass := strings.Replace(pass, "{user}", user, -1)
credentials = append(credentials, Credential{
Username: user,
Password: actualPass,
})
}
}
return credentials
}