forked from mhsh68y/hashcat.launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
80 lines (62 loc) · 1.38 KB
/
settings.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
package hashcatlauncher
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"sync"
)
type Settings struct {
mu *sync.Mutex
TaskCounter int `json:"taskCounter"`
}
func (settings *Settings) CurrentTaskCounter() int {
settings.mu.Lock()
defer settings.mu.Unlock()
return settings.TaskCounter
}
func (settings *Settings) NextTaskCounter() int {
settings.mu.Lock()
defer settings.mu.Unlock()
settings.TaskCounter++
return settings.TaskCounter
}
func (settings *Settings) ResetTaskCounter() int {
settings.mu.Lock()
defer settings.mu.Unlock()
settings.TaskCounter = 0
return settings.TaskCounter
}
var DefaultSettings = &Settings{}
func (a *App) LoadSettings() error {
a.Settings = DefaultSettings
a.Settings.mu = new(sync.Mutex)
settingsFile := filepath.Join(a.Dir, "settings.json")
if _, err := os.Stat(settingsFile); err == nil {
raw, err := ioutil.ReadFile(settingsFile)
if err != nil {
return err
}
if err := json.Unmarshal(raw, &a.Settings); err != nil {
return err
}
}
return nil
}
func (a *App) SaveSettings() error {
settingsFile := filepath.Join(a.Dir, "settings.json")
b, err := json.Marshal(a.Settings)
if err != nil {
return err
}
file, err := os.OpenFile(settingsFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(b)
if err != nil {
return err
}
return nil
}