-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypes.go
More file actions
244 lines (206 loc) · 5.78 KB
/
types.go
File metadata and controls
244 lines (206 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package gogo
import (
"context"
"fmt"
gogopkg "github.com/chainreactors/gogo/v2/pkg"
sdkfingers "github.com/chainreactors/sdk/fingers"
"github.com/chainreactors/sdk/neutron"
"github.com/chainreactors/sdk/pkg/types"
)
const (
ModDefault = gogopkg.Default
ModSmart = gogopkg.SMART
ModSuperSmart = gogopkg.SUPERSMART
ModSmartB = gogopkg.SUPERSMARTB
)
// ========================================
// Context 实现
// ========================================
// Context GoGo 上下文
type Context struct {
ctx context.Context
threads int
mod string
excludes []string
opt *types.GogoOption
statsHandler func(types.Stats)
proxy []string // per-execution 代理覆盖(优先级高于 Config / Client)
}
var _ types.Context = (*Context)(nil)
// NewContext 创建 GoGo 上下文
func NewContext() *Context {
return &Context{
ctx: context.Background(),
threads: 1000,
opt: types.NewDefaultGogoOption(),
}
}
// WithContext 基于给定的 context.Context 复制 Context
func (c *Context) WithContext(ctx context.Context) *Context {
return &Context{
ctx: ctx,
threads: c.threads,
mod: c.mod,
excludes: c.excludes,
opt: types.CloneGogoOption(c.opt),
statsHandler: c.statsHandler,
proxy: c.proxy,
}
}
// SetMod 设置扫描模式 ("s"=smart, "ss"=super-smart, "sc"=super-smart-B, ""=default)
func (c *Context) SetMod(mod string) *Context {
c.mod = mod
return c
}
// SetExcludes 设置排除的 IP/CIDR 列表
func (c *Context) SetExcludes(excludes ...string) *Context {
c.excludes = excludes
return c
}
func (c *Context) Context() context.Context {
return c.ctx
}
// SetThreads 设置线程数
func (c *Context) SetThreads(threads int) *Context {
if threads > 0 {
c.threads = threads
}
return c
}
// SetProxy 设置本次执行使用的代理(支持多级代理链)。
// 例如 SetProxy("socks5://127.0.0.1:1080") 或 SetProxy("http://a:8080", "socks5://b:1080")。
// 传入空参数表示清除 Context 级代理,回退到 Config / Client 级配置。
func (c *Context) SetProxy(proxies ...string) *Context {
c.proxy = proxies
return c
}
// SetOption 设置运行选项
func (c *Context) SetOption(opt *types.GogoOption) *Context {
c.opt = types.CloneGogoOption(opt)
return c
}
func (c *Context) SetStatsHandler(handler func(types.Stats)) *Context {
c.statsHandler = handler
return c
}
func (c *Context) emitStats(stats types.Stats) {
if c != nil && c.statsHandler != nil {
c.statsHandler(stats)
}
}
// SetVersionLevel 设置指纹识别级别
func (c *Context) SetVersionLevel(level int) *Context {
if c.opt == nil {
c.opt = types.NewDefaultGogoOption()
}
c.opt.VersionLevel = level
return c
}
// SetExploit 设置漏洞检测模式
func (c *Context) SetExploit(exploit string) *Context {
if c.opt == nil {
c.opt = types.NewDefaultGogoOption()
}
c.opt.Exploit = exploit
return c
}
// SetDelay 设置超时时间(秒)
func (c *Context) SetDelay(delay int) *Context {
if c.opt == nil {
c.opt = types.NewDefaultGogoOption()
}
c.opt.Delay = delay
return c
}
// ========================================
// Config 实现
// ========================================
// Config GoGo 配置
type Config struct {
Providers []types.Provider
FingersEngine *sdkfingers.Engine
NeutronEngine *neutron.Engine
ResourceProvider func(string) []byte
Capacity int
Proxy []string // 引擎级默认代理,作用于该引擎所有执行(可被 Context 覆盖)
}
// NewConfig 创建默认配置
func NewConfig() *Config {
return &Config{}
}
func (c *Config) Validate() error {
return nil
}
// WithProvider 追加数据源,支持多次调用自动合并
func (c *Config) WithProvider(providers ...types.Provider) *Config {
c.Providers = append(c.Providers, providers...)
return c
}
// WithFingersEngine 设置自定义 fingers 引擎
func (c *Config) WithFingersEngine(engine *sdkfingers.Engine) *Config {
c.FingersEngine = engine
return c
}
// WithNeutronEngine 设置自定义 neutron 引擎
func (c *Config) WithNeutronEngine(engine *neutron.Engine) *Config {
c.NeutronEngine = engine
return c
}
// WithResourceProvider sets a provider used by the underlying gogo package.
func (c *Config) WithResourceProvider(provider func(string) []byte) *Config {
c.ResourceProvider = provider
return c
}
// WithCapacity sets the total capacity for concurrent thread usage across all
// simultaneous invocations. When set, each Execute call acquires its thread
// count from this shared bucket and blocks if capacity is exhausted.
func (c *Config) WithCapacity(total int) *Config {
c.Capacity = total
return c
}
// WithProxy 设置引擎级默认代理(支持多级代理链)。可被 Context.SetProxy 覆盖。
func (c *Config) WithProxy(proxies ...string) *Config {
c.Proxy = proxies
return c
}
// ========================================
// Task 实现
// ========================================
// ScanTask 扫描任务
type ScanTask struct {
IP string
Ports string
}
// NewScanTask 创建扫描任务
func NewScanTask(ip, ports string) *ScanTask {
return &ScanTask{IP: ip, Ports: ports}
}
func (t *ScanTask) Type() string {
return "scan"
}
func (t *ScanTask) Validate() error {
if t.IP == "" {
return fmt.Errorf("IP cannot be empty")
}
if t.Ports == "" {
return fmt.Errorf("Ports cannot be empty")
}
return nil
}
// WorkflowTask 工作流任务
type WorkflowTask struct {
Workflow *types.Workflow
}
// NewWorkflowTask 创建工作流任务
func NewWorkflowTask(workflow *types.Workflow) *WorkflowTask {
return &WorkflowTask{Workflow: workflow}
}
func (t *WorkflowTask) Type() string {
return "workflow"
}
func (t *WorkflowTask) Validate() error {
if t.Workflow == nil {
return fmt.Errorf("Workflow cannot be nil")
}
return nil
}