perf: 优化Client.go的代码,添加注释,规范输出

This commit is contained in:
ZacharyZcR 2024-12-19 14:26:20 +08:00
parent 6a33a65c94
commit 02eb3d6f7a

View File

@ -17,34 +17,43 @@ import (
"time" "time"
) )
// 全局HTTP客户端变量
var ( var (
Client *http.Client Client *http.Client // 标准HTTP客户端
ClientNoRedirect *http.Client ClientNoRedirect *http.Client // 不自动跟随重定向的HTTP客户端
dialTimout = 5 * time.Second dialTimout = 5 * time.Second // 连接超时时间
keepAlive = 5 * time.Second keepAlive = 5 * time.Second // 连接保持时间
) )
// Inithttp 初始化HTTP客户端配置
func Inithttp() { func Inithttp() {
//Common.Proxy = "http://127.0.0.1:8080" // 设置默认并发数
if Common.PocNum == 0 { if Common.PocNum == 0 {
Common.PocNum = 20 Common.PocNum = 20
} }
// 设置默认超时时间
if Common.WebTimeout == 0 { if Common.WebTimeout == 0 {
Common.WebTimeout = 5 Common.WebTimeout = 5
} }
// 初始化HTTP客户端
err := InitHttpClient(Common.PocNum, Common.Proxy, time.Duration(Common.WebTimeout)*time.Second) err := InitHttpClient(Common.PocNum, Common.Proxy, time.Duration(Common.WebTimeout)*time.Second)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
// InitHttpClient 创建HTTP客户端
func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) error { func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) error {
type DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) type DialContext = func(ctx context.Context, network, addr string) (net.Conn, error)
// 配置基础连接参数
dialer := &net.Dialer{ dialer := &net.Dialer{
Timeout: dialTimout, Timeout: dialTimout,
KeepAlive: keepAlive, KeepAlive: keepAlive,
} }
// 配置Transport参数
tr := &http.Transport{ tr := &http.Transport{
DialContext: dialer.DialContext, DialContext: dialer.DialContext,
MaxConnsPerHost: 5, MaxConnsPerHost: 5,
@ -56,6 +65,7 @@ func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) err
DisableKeepAlives: false, DisableKeepAlives: false,
} }
// 配置Socks5代理
if Common.Socks5Proxy != "" { if Common.Socks5Proxy != "" {
dialSocksProxy, err := Common.Socks5Dialer(dialer) dialSocksProxy, err := Common.Socks5Dialer(dialer)
if err != nil { if err != nil {
@ -64,9 +74,10 @@ func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) err
if contextDialer, ok := dialSocksProxy.(proxy.ContextDialer); ok { if contextDialer, ok := dialSocksProxy.(proxy.ContextDialer); ok {
tr.DialContext = contextDialer.DialContext tr.DialContext = contextDialer.DialContext
} else { } else {
return errors.New("Failed type assertion to DialContext") return errors.New("无法转换为DialContext类型")
} }
} else if DownProxy != "" { } else if DownProxy != "" {
// 处理其他代理配置
if DownProxy == "1" { if DownProxy == "1" {
DownProxy = "http://127.0.0.1:8080" DownProxy = "http://127.0.0.1:8080"
} else if DownProxy == "2" { } else if DownProxy == "2" {
@ -74,9 +85,13 @@ func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) err
} else if !strings.Contains(DownProxy, "://") { } else if !strings.Contains(DownProxy, "://") {
DownProxy = "http://127.0.0.1:" + DownProxy DownProxy = "http://127.0.0.1:" + DownProxy
} }
// 验证代理类型
if !strings.HasPrefix(DownProxy, "socks") && !strings.HasPrefix(DownProxy, "http") { if !strings.HasPrefix(DownProxy, "socks") && !strings.HasPrefix(DownProxy, "http") {
return errors.New("no support this proxy") return errors.New("不支持的代理类型")
} }
// 解析代理URL
u, err := url.Parse(DownProxy) u, err := url.Parse(DownProxy)
if err != nil { if err != nil {
return err return err
@ -84,81 +99,100 @@ func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) err
tr.Proxy = http.ProxyURL(u) tr.Proxy = http.ProxyURL(u)
} }
// 创建标准HTTP客户端
Client = &http.Client{ Client = &http.Client{
Transport: tr, Transport: tr,
Timeout: Timeout, Timeout: Timeout,
} }
// 创建不跟随重定向的HTTP客户端
ClientNoRedirect = &http.Client{ ClientNoRedirect = &http.Client{
Transport: tr, Transport: tr,
Timeout: Timeout, Timeout: Timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
} }
return nil return nil
} }
// Poc 定义漏洞检测配置结构
type Poc struct { type Poc struct {
Name string `yaml:"name"` Name string `yaml:"name"` // POC名称
Set StrMap `yaml:"set"` Set StrMap `yaml:"set"` // 单值配置映射
Sets ListMap `yaml:"sets"` Sets ListMap `yaml:"sets"` // 列表值配置映射
Rules []Rules `yaml:"rules"` Rules []Rules `yaml:"rules"` // 检测规则列表
Groups RuleMap `yaml:"groups"` Groups RuleMap `yaml:"groups"` // 规则组映射
Detail Detail `yaml:"detail"` Detail Detail `yaml:"detail"` // 漏洞详情
} }
// MapSlice 用于解析YAML的通用映射类型
type MapSlice = yaml.MapSlice type MapSlice = yaml.MapSlice
type StrMap []StrItem // 自定义映射类型
type ListMap []ListItem type (
type RuleMap []RuleItem StrMap []StrItem // 字符串键值对映射
ListMap []ListItem // 字符串键列表值映射
RuleMap []RuleItem // 字符串键规则列表映射
)
type StrItem struct { // 映射项结构定义
Key, Value string type (
} // StrItem 字符串键值对
StrItem struct {
Key string // 键名
Value string // 值
}
type ListItem struct { // ListItem 字符串键列表值对
Key string ListItem struct {
Value []string Key string // 键名
} Value []string // 值列表
}
type RuleItem struct { // RuleItem 字符串键规则列表对
Key string RuleItem struct {
Value []Rules Key string // 键名
} Value []Rules // 规则列表
}
)
// UnmarshalYAML 实现StrMap的YAML解析接口
func (r *StrMap) UnmarshalYAML(unmarshal func(interface{}) error) error { func (r *StrMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
// 临时使用MapSlice存储解析结果
var tmp yaml.MapSlice var tmp yaml.MapSlice
if err := unmarshal(&tmp); err != nil { if err := unmarshal(&tmp); err != nil {
return err return err
} }
// 转换为StrMap结构
for _, one := range tmp { for _, one := range tmp {
key, value := one.Key.(string), one.Value.(string) key, value := one.Key.(string), one.Value.(string)
*r = append(*r, StrItem{key, value}) *r = append(*r, StrItem{key, value})
} }
return nil return nil
} }
//func (r *RuleItem) UnmarshalYAML(unmarshal func(interface{}) error) error { // UnmarshalYAML 实现RuleMap的YAML解析接口
// var tmp yaml.MapSlice // 参数:
// if err := unmarshal(&tmp); err != nil { // - unmarshal: YAML解析函数
// return err //
// } // 返回:
// //for _,one := range tmp{ // - error: 解析错误
// // key,value := one.Key.(string),one.Value.(string)
// // *r = append(*r,StrItem{key,value})
// //}
// return nil
//}
func (r *RuleMap) UnmarshalYAML(unmarshal func(interface{}) error) error { func (r *RuleMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
// 使用MapSlice保持键的顺序
var tmp1 yaml.MapSlice var tmp1 yaml.MapSlice
if err := unmarshal(&tmp1); err != nil { if err := unmarshal(&tmp1); err != nil {
return err return err
} }
// 解析规则内容
var tmp = make(map[string][]Rules) var tmp = make(map[string][]Rules)
if err := unmarshal(&tmp); err != nil { if err := unmarshal(&tmp); err != nil {
return err return err
} }
// 按顺序转换为RuleMap结构
for _, one := range tmp1 { for _, one := range tmp1 {
key := one.Key.(string) key := one.Key.(string)
value := tmp[key] value := tmp[key]
@ -167,14 +201,24 @@ func (r *RuleMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil return nil
} }
// UnmarshalYAML 实现ListMap的YAML解析接口
// 参数:
// - unmarshal: YAML解析函数
//
// 返回:
// - error: 解析错误
func (r *ListMap) UnmarshalYAML(unmarshal func(interface{}) error) error { func (r *ListMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
// 解析YAML映射
var tmp yaml.MapSlice var tmp yaml.MapSlice
if err := unmarshal(&tmp); err != nil { if err := unmarshal(&tmp); err != nil {
return err return err
} }
// 转换为ListMap结构
for _, one := range tmp { for _, one := range tmp {
key := one.Key.(string) key := one.Key.(string)
var value []string var value []string
// 将接口类型转换为字符串
for _, val := range one.Value.([]interface{}) { for _, val := range one.Value.([]interface{}) {
v := fmt.Sprintf("%v", val) v := fmt.Sprintf("%v", val)
value = append(value, v) value = append(value, v)
@ -184,58 +228,68 @@ func (r *ListMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil return nil
} }
// Rules 定义POC检测规则结构
type Rules struct { type Rules struct {
Method string `yaml:"method"` Method string `yaml:"method"` // HTTP请求方法
Path string `yaml:"path"` Path string `yaml:"path"` // 请求路径
Headers map[string]string `yaml:"headers"` Headers map[string]string `yaml:"headers"` // 请求头
Body string `yaml:"body"` Body string `yaml:"body"` // 请求体
Search string `yaml:"search"` Search string `yaml:"search"` // 搜索模式
FollowRedirects bool `yaml:"follow_redirects"` FollowRedirects bool `yaml:"follow_redirects"` // 是否跟随重定向
Expression string `yaml:"expression"` Expression string `yaml:"expression"` // 匹配表达式
Continue bool `yaml:"continue"` Continue bool `yaml:"continue"` // 是否继续执行
} }
// Detail 定义POC详情结构
type Detail struct { type Detail struct {
Author string `yaml:"author"` Author string `yaml:"author"` // POC作者
Links []string `yaml:"links"` Links []string `yaml:"links"` // 相关链接
Description string `yaml:"description"` Description string `yaml:"description"` // POC描述
Version string `yaml:"version"` Version string `yaml:"version"` // POC版本
} }
// LoadMultiPoc 加载多个POC文件
func LoadMultiPoc(Pocs embed.FS, pocname string) []*Poc { func LoadMultiPoc(Pocs embed.FS, pocname string) []*Poc {
var pocs []*Poc var pocs []*Poc
// 遍历选中的POC文件
for _, f := range SelectPoc(Pocs, pocname) { for _, f := range SelectPoc(Pocs, pocname) {
if p, err := LoadPoc(f, Pocs); err == nil { if p, err := LoadPoc(f, Pocs); err == nil {
pocs = append(pocs, p) pocs = append(pocs, p)
} else { } else {
fmt.Println("[-] load poc ", f, " error:", err) fmt.Printf("[-] POC加载失败 %s: %v\n", f, err)
} }
} }
return pocs return pocs
} }
// LoadPoc 从内嵌文件系统加载单个POC
func LoadPoc(fileName string, Pocs embed.FS) (*Poc, error) { func LoadPoc(fileName string, Pocs embed.FS) (*Poc, error) {
p := &Poc{} p := &Poc{}
// 读取POC文件内容
yamlFile, err := Pocs.ReadFile("pocs/" + fileName) yamlFile, err := Pocs.ReadFile("pocs/" + fileName)
if err != nil { if err != nil {
fmt.Printf("[-] load poc %s error1: %v\n", fileName, err) fmt.Printf("[-] POC文件读取失败 %s: %v\n", fileName, err)
return nil, err return nil, err
} }
// 解析YAML内容
err = yaml.Unmarshal(yamlFile, p) err = yaml.Unmarshal(yamlFile, p)
if err != nil { if err != nil {
fmt.Printf("[-] load poc %s error2: %v\n", fileName, err) fmt.Printf("[-] POC解析失败 %s: %v\n", fileName, err)
return nil, err return nil, err
} }
return p, err return p, err
} }
// SelectPoc 根据名称关键字选择POC文件
func SelectPoc(Pocs embed.FS, pocname string) []string { func SelectPoc(Pocs embed.FS, pocname string) []string {
entries, err := Pocs.ReadDir("pocs") entries, err := Pocs.ReadDir("pocs")
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("[-] 读取POC目录失败: %v\n", err)
} }
var foundFiles []string var foundFiles []string
// 查找匹配关键字的POC文件
for _, entry := range entries { for _, entry := range entries {
if strings.Contains(entry.Name(), pocname) { if strings.Contains(entry.Name(), pocname) {
foundFiles = append(foundFiles, entry.Name()) foundFiles = append(foundFiles, entry.Name())
@ -244,16 +298,20 @@ func SelectPoc(Pocs embed.FS, pocname string) []string {
return foundFiles return foundFiles
} }
// LoadPocbyPath 从文件系统路径加载POC
func LoadPocbyPath(fileName string) (*Poc, error) { func LoadPocbyPath(fileName string) (*Poc, error) {
p := &Poc{} p := &Poc{}
// 读取POC文件内容
data, err := os.ReadFile(fileName) data, err := os.ReadFile(fileName)
if err != nil { if err != nil {
fmt.Printf("[-] load poc %s error3: %v\n", fileName, err) fmt.Printf("[-] POC文件读取失败 %s: %v\n", fileName, err)
return nil, err return nil, err
} }
// 解析YAML内容
err = yaml.Unmarshal(data, p) err = yaml.Unmarshal(data, p)
if err != nil { if err != nil {
fmt.Printf("[-] load poc %s error4: %v\n", fileName, err) fmt.Printf("[-] POC解析失败 %s: %v\n", fileName, err)
return nil, err return nil, err
} }
return p, err return p, err