-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
464 lines (397 loc) · 11 KB
/
app.go
File metadata and controls
464 lines (397 loc) · 11 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package main
import (
"context"
"os"
"runtime"
"strings"
"fmt"
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
"zipcracker/config"
)
// App struct
type App struct {
ctx context.Context
}
// CrackResult 破解结果
type CrackResult struct {
Success bool `json:"success"`
Password string `json:"password"`
TimeSpent string `json:"timeSpent"`
Attempts int64 `json:"attempts"`
Speed string `json:"speed"`
Error string `json:"error"`
}
// CrackProgress 破解进度
type CrackProgress struct {
Current string `json:"current"`
Progress float64 `json:"progress"`
Attempts int64 `json:"attempts"`
Speed string `json:"speed"`
TimeElapsed string `json:"timeElapsed"`
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// SelectFile 选择文件
func (a *App) SelectFile() string {
// 检查上下文是否有效
if a.ctx == nil {
return ""
}
// 根据操作系统决定是否使用过滤器
var options wailsruntime.OpenDialogOptions
// 获取当前语言设置
language := config.GetLanguage()
// 设置标题(支持多语言)
var title string
if language == "en-US" {
title = "Select Archive File"
} else {
title = "选择压缩包文件"
}
// 在macOS上不使用过滤器以避免异常
if runtime.GOOS == "darwin" {
options = wailsruntime.OpenDialogOptions{
Title: title,
}
} else {
// 在其他系统上使用过滤器
var displayName string
if language == "en-US" {
displayName = "Archive Files (*.zip;*.rar;*.7z;*.tar.gz;*.tar.bz2)"
} else {
displayName = "压缩包文件 (*.zip;*.rar;*.7z;*.tar.gz;*.tar.bz2)"
}
options = wailsruntime.OpenDialogOptions{
Title: title,
Filters: []wailsruntime.FileFilter{
{
DisplayName: displayName,
Pattern: "*.zip;*.rar;*.7z;*.tar.gz;*.tar.bz2",
},
},
}
}
filePath, err := wailsruntime.OpenFileDialog(a.ctx, options)
if err != nil {
// 记录错误但不崩溃
wailsruntime.LogError(a.ctx, "文件选择对话框错误: "+err.Error())
return ""
}
// 记录选择的文件路径
if filePath != "" {
wailsruntime.LogInfo(a.ctx, "选择的文件: "+filePath)
}
return filePath
}
// SelectDictFile 选择字典文件
func (a *App) SelectDictFile() string {
// 检查上下文是否有效
if a.ctx == nil {
return ""
}
// 根据操作系统决定是否使用过滤器
var options wailsruntime.OpenDialogOptions
// 获取当前语言设置
language := config.GetLanguage()
// 设置标题(支持多语言)
var title string
if language == "en-US" {
title = "Select Dictionary File"
} else {
title = "选择字典文件"
}
// 在macOS上不使用过滤器以避免异常
if runtime.GOOS == "darwin" {
options = wailsruntime.OpenDialogOptions{
Title: title,
}
} else {
// 在其他系统上使用过滤器
var displayName string
if language == "en-US" {
displayName = "Dictionary Files (*.txt;*.dict;*.wordlist)"
} else {
displayName = "字典文件 (*.txt;*.dict;*.wordlist)"
}
options = wailsruntime.OpenDialogOptions{
Title: title,
Filters: []wailsruntime.FileFilter{
{
DisplayName: displayName,
Pattern: "*.txt;*.dict;*.wordlist",
},
},
}
}
filePath, err := wailsruntime.OpenFileDialog(a.ctx, options)
if err != nil {
// 记录错误但不崩溃
wailsruntime.LogError(a.ctx, "字典文件选择对话框错误: "+err.Error())
return ""
}
// 记录选择的文件路径
if filePath != "" {
wailsruntime.LogInfo(a.ctx, "选择的字典文件: "+filePath)
}
return filePath
}
// ValidateDictFile 验证字典文件
func (a *App) ValidateDictFile(filePath string) (bool, string) {
fmt.Println("validate dict file", filePath)
if filePath == "" {
fmt.Println("validate dict file - empty path")
return false, "请选择字典文件"
}
// 检查文件是否存在
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Println("dict file not exist", filePath)
return false, "字典文件不存在"
}
// 检查文件扩展名
supportedFormats := []string{".txt", ".dict", ".wordlist"}
for _, format := range supportedFormats {
if strings.HasSuffix(strings.ToLower(filePath), format) {
fmt.Println("dict file format supported", filePath)
return true, ""
}
}
fmt.Println("dict file format not supported", filePath)
return false, "不支持的字典文件格式"
}
// ValidateArchive 验证压缩包文件
func (a *App) ValidateArchive(filePath string) (bool, string) {
fmt.Println("validate archive",filePath)
if filePath == "" {
fmt.Println("validate archive",filePath)
return false, "请选择文件"
}
// 检查文件是否存在
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Println("file not exist",filePath)
return false, "文件不存在"
}
// 检查文件扩展名
supportedFormats := []string{".zip", ".rar", ".7z", ".tar.gz", ".tar.bz2"}
for _, format := range supportedFormats {
if strings.HasSuffix(filePath, format) {
fmt.Println("file format supported",filePath)
return true, ""
}
}
fmt.Println("file format not supported",filePath)
return false, "不支持的文件格式"
}
// StartCracking 开始破解
func (a *App) StartCracking(filePath string, mode string, params map[string]interface{}) CrackResult {
// 验证压缩包文件
valid, errMsg := a.ValidateArchive(filePath)
if !valid {
return CrackResult{
Success: false,
Error: errMsg,
}
}
// 根据破解模式进行额外的验证
switch mode {
case "dictionary":
// 验证字典文件
if dictPath, ok := params["dictPath"].(string); ok && dictPath != "" {
dictValid, dictErrMsg := a.ValidateDictFile(dictPath)
if !dictValid {
return CrackResult{
Success: false,
Error: "字典文件验证失败: " + dictErrMsg,
}
}
} else {
return CrackResult{
Success: false,
Error: "请选择字典文件",
}
}
return a.dictionaryAttack(filePath, params)
case "bruteForce":
// 验证暴力破解参数
if err := a.validateBruteForceParams(params); err != nil {
return CrackResult{
Success: false,
Error: "暴力破解参数验证失败: " + err.Error(),
}
}
return a.bruteForceAttack(filePath, params)
case "mask":
// 验证掩码攻击参数
if err := a.validateMaskParams(params); err != nil {
return CrackResult{
Success: false,
Error: "掩码攻击参数验证失败: " + err.Error(),
}
}
return a.maskAttack(filePath, params)
default:
return CrackResult{
Success: false,
Error: "不支持的破解模式",
}
}
}
// dictionaryAttack 字典攻击
func (a *App) dictionaryAttack(filePath string, params map[string]interface{}) CrackResult {
// TODO: 实现字典攻击
return CrackResult{
Success: false,
Error: "字典攻击功能待实现",
TimeSpent: "0s",
Attempts: 0,
Speed: "0 p/s",
}
}
// bruteForceAttack 暴力破解
func (a *App) bruteForceAttack(filePath string, params map[string]interface{}) CrackResult {
// TODO: 实现暴力破解
return CrackResult{
Success: false,
Error: "暴力破解功能待实现",
TimeSpent: "0s",
Attempts: 0,
Speed: "0 p/s",
}
}
// maskAttack 掩码攻击
func (a *App) maskAttack(filePath string, params map[string]interface{}) CrackResult {
// TODO: 实现掩码攻击
return CrackResult{
Success: false,
Error: "掩码攻击功能待实现",
TimeSpent: "0s",
Attempts: 0,
Speed: "0 p/s",
}
}
// GetSupportedFormats 获取支持的格式
func (a *App) GetSupportedFormats() []string {
return []string{
"ZIP (.zip)",
"RAR (.rar)",
"7-Zip (.7z)",
"TAR.GZ (.tar.gz)",
"TAR.BZ2 (.tar.bz2)",
}
}
// GetAttackModes 获取攻击模式
func (a *App) GetAttackModes() []string {
return []string{
"dictionary",
"bruteForce",
"mask",
}
}
// GetLanguage 获取当前语言设置
func (a *App) GetLanguage() string {
return config.GetLanguage()
}
// SetLanguage 设置语言
func (a *App) SetLanguage(language string) error {
return config.UpdateLanguage(language)
}
// GetTheme 获取当前主题设置
func (a *App) GetTheme() string {
return config.GetTheme()
}
// SetTheme 设置主题
func (a *App) SetTheme(theme string) error {
return config.UpdateTheme(theme)
}
// GetConfigPath 获取配置文件路径
func (a *App) GetConfigPath() string {
return config.GetConfigPath()
}
// GetThreadCount 获取当前线程数量设置
func (a *App) GetThreadCount() int {
return config.GetThreadCount()
}
// SetThreadCount 设置线程数量
func (a *App) SetThreadCount(threadCount int) error {
return config.UpdateThreadCount(threadCount)
}
// GetAvailableThreadCounts 获取可用的线程数量选项
func (a *App) GetAvailableThreadCounts() []int {
return config.GetAvailableThreadCounts()
}
// GetCPUInfo 获取CPU信息
func (a *App) GetCPUInfo() config.CPUInfo {
return config.GetCPUInfo()
}
// GetDictFilePath 获取字典文件路径
func (a *App) GetDictFilePath() string {
return config.GetDictFilePath()
}
// SetDictFilePath 设置字典文件路径
func (a *App) SetDictFilePath(filePath string) error {
return config.UpdateDictFilePath(filePath)
}
// ClearDictFilePath 清空字典文件路径
func (a *App) ClearDictFilePath() error {
return config.UpdateDictFilePath("")
}
// validateBruteForceParams 验证暴力破解参数
func (a *App) validateBruteForceParams(params map[string]interface{}) error {
// 验证最小长度
if minLength, ok := params["minLength"].(float64); ok {
if minLength < 1 || minLength > 20 {
return fmt.Errorf("最小长度必须在1-20之间")
}
} else {
return fmt.Errorf("缺少最小长度参数")
}
// 验证最大长度
if maxLength, ok := params["maxLength"].(float64); ok {
if maxLength < 1 || maxLength > 20 {
return fmt.Errorf("最大长度必须在1-20之间")
}
} else {
return fmt.Errorf("缺少最大长度参数")
}
// 验证字符集
if charset, ok := params["charset"].(string); ok {
if charset == "" {
return fmt.Errorf("字符集不能为空")
}
} else {
return fmt.Errorf("缺少字符集参数")
}
// 验证长度范围
if minLength, ok := params["minLength"].(float64); ok {
if maxLength, ok := params["maxLength"].(float64); ok {
if minLength > maxLength {
return fmt.Errorf("最小长度不能大于最大长度")
}
}
}
return nil
}
// validateMaskParams 验证掩码攻击参数
func (a *App) validateMaskParams(params map[string]interface{}) error {
// 验证掩码模式
if maskPattern, ok := params["maskPattern"].(string); ok {
if maskPattern == "" {
return fmt.Errorf("掩码模式不能为空")
}
// 验证掩码模式格式
for _, char := range maskPattern {
if char != '?' && char != 'l' && char != 'u' && char != 'd' && char != 's' && char != 'a' && char != 'b' && char != 'h' && char != 'H' {
return fmt.Errorf("掩码模式包含无效字符")
}
}
} else {
return fmt.Errorf("缺少掩码模式参数")
}
return nil
}