forked from mhsh68y/hashcat.launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
102 lines (88 loc) · 2.2 KB
/
watcher.go
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
package hashcatlauncher
import (
"log"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
func (a *App) NewWatcher() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
lazyWatcher := func(c <-chan bool, callback func()) {
call := false
for {
select {
case <-c:
call = true
case <-time.After(1 * time.Second):
if call {
call = false
callback()
}
}
}
}
watcherHashcatChan := make(chan bool)
watcherHashesChan := make(chan bool)
watcherDictionariesChan := make(chan bool)
watcherRulesChan := make(chan bool)
watcherMasksChan := make(chan bool)
go lazyWatcher(watcherHashcatChan, a.WatcherHashcatCallback)
go lazyWatcher(watcherHashesChan, a.WatcherHashesCallback)
go lazyWatcher(watcherDictionariesChan, a.WatcherDictionariesCallback)
go lazyWatcher(watcherRulesChan, a.WatcherRulesCallback)
go lazyWatcher(watcherMasksChan, a.WatcherMasksCallback)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create != fsnotify.Create && event.Op&fsnotify.Remove != fsnotify.Remove && event.Op&fsnotify.Rename != fsnotify.Rename {
continue
}
if event.Name == a.Hashcat.BinaryFile {
watcherHashcatChan <- true
} else {
dir, _ := filepath.Split(event.Name)
dir = filepath.Join(dir) // to be compatible with below directories as they are all constructed by filepath (and to avoid trailing slash issue)
switch dir {
case a.HashesDir:
watcherHashesChan <- true
case a.DictionariesDir:
watcherDictionariesChan <- true
case a.RulesDir:
watcherRulesChan <- true
case a.MasksDir:
watcherMasksChan <- true
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("watcher error:", err)
}
}
}()
if err := watcher.Add(a.HashcatDir); err != nil {
return err
}
if err := watcher.Add(a.HashesDir); err != nil {
return err
}
if err := watcher.Add(a.DictionariesDir); err != nil {
return err
}
if err := watcher.Add(a.RulesDir); err != nil {
return err
}
if err := watcher.Add(a.MasksDir); err != nil {
return err
}
a.Watcher = watcher
return nil
}