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

- 创建app包实现依赖注入容器和初始化器模式 - 重构main.go为六阶段清晰的初始化流程 - 新增结构化错误处理替代简陋的os.Exit调用 - 为HostInfo添加辅助函数增强功能但保持向后兼容 - 引入TargetInfo包装器支持上下文和元数据管理 - 优化代码组织提升可维护性和可测试性
109 lines
2.1 KiB
Go
109 lines
2.1 KiB
Go
package common
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
)
|
||
|
||
// HostInfoHelper 提供HostInfo的辅助方法
|
||
// 使用函数而不是方法,保持向后兼容
|
||
|
||
// GetHost 获取主机地址
|
||
func GetHost(h *HostInfo) string {
|
||
return h.Host
|
||
}
|
||
|
||
// GetPort 获取端口号(转换为整数)
|
||
func GetPort(h *HostInfo) (int, error) {
|
||
if h.Ports == "" {
|
||
return 0, fmt.Errorf("端口未设置")
|
||
}
|
||
return strconv.Atoi(h.Ports)
|
||
}
|
||
|
||
// GetURL 获取URL地址
|
||
func GetURL(h *HostInfo) string {
|
||
return h.Url
|
||
}
|
||
|
||
// IsWebTarget 判断是否为Web目标
|
||
func IsWebTarget(h *HostInfo) bool {
|
||
return h.Url != ""
|
||
}
|
||
|
||
// HasPort 检查是否设置了端口
|
||
func HasPort(h *HostInfo) bool {
|
||
return h.Ports != ""
|
||
}
|
||
|
||
// HasHost 检查是否设置了主机
|
||
func HasHost(h *HostInfo) bool {
|
||
return h.Host != ""
|
||
}
|
||
|
||
// CloneHostInfo 克隆HostInfo(深拷贝)
|
||
func CloneHostInfo(h *HostInfo) HostInfo {
|
||
cloned := HostInfo{
|
||
Host: h.Host,
|
||
Ports: h.Ports,
|
||
Url: h.Url,
|
||
}
|
||
|
||
// 深拷贝Infostr切片
|
||
if h.Infostr != nil {
|
||
cloned.Infostr = make([]string, len(h.Infostr))
|
||
copy(cloned.Infostr, h.Infostr)
|
||
}
|
||
|
||
return cloned
|
||
}
|
||
|
||
// ValidateHostInfo 验证HostInfo的有效性
|
||
func ValidateHostInfo(h *HostInfo) error {
|
||
if h.Host == "" && h.Url == "" {
|
||
return fmt.Errorf("主机地址或URL必须至少指定一个")
|
||
}
|
||
|
||
// 验证端口格式(如果指定了)
|
||
if h.Ports != "" {
|
||
if _, err := GetPort(h); err != nil {
|
||
return fmt.Errorf("端口格式无效: %v", err)
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// HostInfoString 返回HostInfo的字符串表示
|
||
func HostInfoString(h *HostInfo) string {
|
||
if IsWebTarget(h) {
|
||
return h.Url
|
||
}
|
||
|
||
if HasPort(h) {
|
||
return fmt.Sprintf("%s:%s", h.Host, h.Ports)
|
||
}
|
||
|
||
return h.Host
|
||
}
|
||
|
||
// AddInfo 添加附加信息
|
||
func AddInfo(h *HostInfo, info string) {
|
||
if h.Infostr == nil {
|
||
h.Infostr = make([]string, 0)
|
||
}
|
||
h.Infostr = append(h.Infostr, info)
|
||
}
|
||
|
||
// GetInfo 获取所有附加信息
|
||
func GetInfo(h *HostInfo) []string {
|
||
if h.Infostr == nil {
|
||
return []string{}
|
||
}
|
||
return h.Infostr
|
||
}
|
||
|
||
// HasInfo 检查是否有附加信息
|
||
func HasInfo(h *HostInfo) bool {
|
||
return len(h.Infostr) > 0
|
||
} |