/
utils.go
220 lines (179 loc) · 4.98 KB
/
utils.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
package cmd
import (
b64 "encoding/base64"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"os/user"
"regexp"
"strings"
"syscall"
"golang.org/x/crypto/blake2b"
"golang.org/x/term"
"github.com/cespare/xxhash"
"github.com/gtank/cryptopasta"
)
var sqlog = log.New(os.Stdout, "", 0)
func sqFatal(v ...interface{}) {
sqlog.Fatal("Error : ", styleFailure.Render(fmt.Sprint(v...)))
}
func sqComplete() {
sqlog.Print(styleSuccess.Render("[ Complete ]"))
}
const (
iOSPathDataContainer = "/var/mobile/Containers/Data/Application/"
iOSPathAppContainer = "/var/containers/Bundle/Application/"
iOSPathEndlesssInstruments = "Library/Application Support/Endlesss/Presets/Instruments/"
iOSPathEndlesssDataRoot = "Library/Application Support/Endlesss/"
iOSPathEndlesssApp = "Endlesss.app/Assets/"
iOSPathEndlesssImagePack = "Endlesss.app/Assets/Images/Packs/"
iOSPresetDatabaseDir = "/production/Data/" // offset from iOSPathEndlesssDataRoot
iOSPresetDatabaseName = "presets.cblite2"
iOSPresetDatabase_1 = "db.sqlite3"
iOSPresetDatabase_2 = "db.sqlite3-shm"
iOSPresetDatabase_3 = "db.sqlite3-wal"
)
const (
osxPathEndlesssInstruments = "~/Library/Containers/fm.endlesss.app/Data/Library/Application Support/Endlesss/Presets/Instruments/"
osxPathEndlesssImagePack = "/Applications/Endlesss.app/Contents/Resources/Assets/Images/Packs/"
)
func readPwdFromTerminal(secretName string) (string, error) {
fmt.Print("Enter ", secretName, " : ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
password := string(bytePassword)
return strings.TrimSpace(password), nil
}
func stringToCryptoKeyBytes(secretString string, keyLength int) ([]byte, error) {
// hash the cypher input to use as a random seed
rngSeed := xxhash.Sum64String(secretString)
rand.Seed(int64(rngSeed))
key := make([]byte, keyLength)
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
// scrobble a string into an encrypted lump, expressed as base64
func encryptStringToBase64(inputString, secretString string) (string, error) {
randomKey, err := stringToCryptoKeyBytes(secretString, 32)
if err != nil {
return "", err
}
var randomKey32 [32]byte
copy(randomKey32[:], randomKey)
cryptedBytes, err := cryptopasta.Encrypt([]byte(inputString), &randomKey32)
if err != nil {
return "", err
}
return b64.StdEncoding.EncodeToString(cryptedBytes), nil
}
// unscrobble the result of encryptStringToBase64
func decryptStringFromBase64(inputString, secretString string) (string, error) {
randomKey, err := stringToCryptoKeyBytes(secretString, 32)
if err != nil {
return "", err
}
var randomKey32 [32]byte
copy(randomKey32[:], randomKey)
cryptedBytes, err := b64.StdEncoding.DecodeString(inputString)
if err != nil {
return "", err
}
decryptedBytes, err := cryptopasta.Decrypt(cryptedBytes, &randomKey32)
if err != nil {
return "", err
}
return string(decryptedBytes), nil
}
func indexOf(word string, data []string) int {
for k, v := range data {
if word == v {
return k
}
}
return -1
}
func indexOfCaseInvariant(word string, data []string) int {
for k, v := range data {
if strings.EqualFold(word, v) {
return k
}
}
return -1
}
func localFileCopy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func recursiveGatherFilesFromDir(path string) []string {
var fileList []string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
filePath := path + "/" + file.Name()
if !file.IsDir() {
fileList = append(fileList, filePath)
} else {
fileList = append(fileList, recursiveGatherFilesFromDir(filePath)...)
}
}
return fileList
}
func isRunningAsRoot() bool {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("[isRunningAsRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root"
}
func fastFileHash(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
hasher, _ := blake2b.New256(nil)
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
hash := hasher.Sum(nil)
encodedHex := hex.EncodeToString(hash[:])
return encodedHex, nil
}
var validateInstrument = regexp.MustCompile(`^[a-zA-Z0-9 ]+$`).MatchString
func isValidInstrumentName(name string) (bool, string) {
if len(name) > 14 {
return false, "Instrument name is too long, keep it below 14 characters"
}
if !validateInstrument(name) {
return false, "Instrument name should be alphanumeric only"
}
return true, ""
}