forked from methos2016/DuckyManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.go
313 lines (252 loc) · 6.75 KB
/
scripts.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
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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"os"
"sort"
"strings"
)
// Script holds all the data from a script
type Script struct {
// RemotePath holds the online path to the script
RemotePath string
// ID is the unique id of the script
ID string
// Path holds the path to the script
Path string
// Name holds the name of the script
Name string
// Desc holds the description for the script
Desc string
// Tags holds the tags for the script, such as OS, Desktop Enviroment, etc.
Tags string
// User saves the creator's name/nick
User string
// Hash holds a md5 of the last known state of the script
Hash string
// Remote indicates if the script is currently placed at a remote repository or local
Remote bool
}
// SearchNewLocal will search on the path for valid scripts and load them onto the already loaded scripts.
// It ignores the ones already loaded (evades repeating)
func SearchNewLocal(path string, scripts *[]Script) (count uint, err error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return
}
for _, f := range files {
isNew := true
for _, script := range *scripts {
if script.Path == path+"/"+f.Name() {
isNew = false
break
}
}
if isNew {
h, err := HashFile(path + "/" + f.Name())
if err != nil {
return count, err
}
count++
*scripts = append(*scripts, Script{
Path: path + "/" + f.Name(),
Hash: h})
}
}
return
}
// CheckChanged will report any changes to the scripts.
// That is, which ones got deleted, modified and the total number of valid ones (not deleted)
func CheckLocalChanged(scripts []Script) (deleted, modified, totalValid uint) {
for i, script := range scripts {
// Only checks local scripts
if !script.Remote {
fE, hE, hash := script.CheckIntegrity()
if fE {
deleted++
scripts = append(scripts[:i], scripts[i+1:]...)
} else if !hE {
scripts[i].Hash = hash
modified++
totalValid++
} else {
totalValid++
}
}
}
return
}
// CreateNewDatabase will create a new DB in the path
func CreateNewDatabase(path string) (f []byte, err error) {
f2, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return nil, err
}
_, err = f2.WriteString("[{}]")
if err != nil {
return nil, err
}
err = f2.Close()
if err != nil {
return nil, err
}
f, err = ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return
}
// OpenDatabase will read the data of the database, and create a new one if it does not exist
func OpenDatabase(path string) (f []byte, err error) {
f, err = ioutil.ReadFile(path)
if err != nil && os.IsNotExist(err) {
f, err = CreateNewDatabase(path)
}
return
}
// CheckLocal will check the local database of scripts.
// Will create a new one if it doesn't exists.
// Returns all the loaded data, plus info about the changes made
func CheckLocal(path, scriptsPath string) (
scripts []Script,
totalValid, deleted, modified, newOnes uint,
err error,
) {
f, err := OpenDatabase(path)
if err != nil {
return
}
if err = json.Unmarshal(f, &scripts); err != nil {
return
}
deleted, modified, totalValid = CheckLocalChanged(scripts)
newOnes, err = SearchNewLocal(scriptsPath, &scripts)
if err != nil {
return
}
totalValid += newOnes
err = Save(path, scripts)
return
}
// HashFile will hash a file and return the hexsum
func HashFile(path string) (h string, err error) {
var result []byte
file, err := os.Open(path)
if err != nil {
return
}
defer func() { err = file.Close() }()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return
}
h = hex.EncodeToString(hash.Sum(result))
return
}
// GetName will return the name for the script, or it's path if it does not
// have a name assigned
func (s *Script) GetName() string {
if s.Name != "" {
return s.Name
}
return s.Path
}
// CheckIntegrity will load the file from the path and check it against known data.
// It will also check if it's empty, and remove it if so
func (s *Script) CheckIntegrity() (fileErr, hashEq bool, h string) {
if s.Path == "" {
fileErr = true
return
}
h, err := HashFile(s.Path)
if err == nil {
fileErr = false
if h != s.Hash {
hashEq = false
} else {
hashEq = true
}
} else {
fileErr = true
hashEq = false
}
return
}
// Equals will check if the scripts are the same object
func (s *Script) Equals(s2 Script) bool {
return s.ID == s2.ID || s.Hash == s2.Hash
}
/*** SORT FUNCTIONS ***/
// Scripts is a dummy type for qsort
type Scripts []Script
func (s Scripts) Len() int { return len(s) }
func (s Scripts) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Scripts) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() }
/*** END SORT FUNCTIONS***/
// SortScripts sorts the slice based on name and path
func SortScripts(scripts Scripts) { sort.Sort(scripts) }
// ListByName will return all scripts which contains the name on it
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByName(scripts []Script, name string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.GetName()), strings.ToLower(name)) {
matches = append(matches, script)
}
}
return
}
// ListByUser will return all scripts that were created by a user containing the string.
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByUser(scripts []Script, user string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.User), strings.ToLower(user)) {
matches = append(matches, script)
}
}
return
}
// ListByTags will return all scripts that contain a tag which contains the passed keyword
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByTags(scripts []Script, inTag string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.Tags), strings.ToLower(inTag)) {
matches = append(matches, script)
}
}
return
}
// ListByDesc will return all scripts that has a description containing a string
// An empty string is interpreted as "any".
// This function IS NOT case sensitive.
func ListByDesc(scripts []Script, desc string) (matches []Script) {
for _, script := range scripts {
if strings.Contains(strings.ToLower(script.Desc), strings.ToLower(desc)) {
matches = append(matches, script)
}
}
return
}
// TrimRepeated will return an slice formed from the passed one,
// but the result will only include unique values.
func TrimRepeated(scripts []Script) (valid []Script) {
for _, s := range scripts {
eq := false
for _, v := range valid {
if s.Equals(v) {
eq = true
break
}
}
if !eq {
valid = append(valid, s)
}
}
return
}