This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
payload.go
369 lines (340 loc) · 8.35 KB
/
payload.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
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
package killcord
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/cheggaaa/pb/v3"
shell "github.com/ipfs/go-ipfs-api"
archiver "github.com/mholt/archiver/v3"
"golang.org/x/crypto/nacl/secretbox"
)
const (
maxChunkSize = 16000
sourcePrefix = "payload/source"
encryptPrefix = "payload/encrypted"
decryptPrefix = "payload/decrypted"
tempPrefix = "payload/tmp"
maxChunksCache = 900000
maxReadSize = 900000
defaultpayloadRPCPath = "https://ipfs.infura.io:5001"
defaultOutputZipName = "output.zip"
defaultOutputKilName = "output.kil"
)
var (
payloadSourcePath string
payloadEncryptedPath string
payloadTempPath string
payloadDecryptPath string
payloadRPCPath string = defaultpayloadRPCPath
)
func init() {
payloadSourcePath = filepath.Join(strings.Split(sourcePrefix, "/")...)
payloadEncryptedPath = filepath.Join(strings.Split(encryptPrefix, "/")...)
payloadTempPath = filepath.Join(strings.Split(tempPrefix, "/")...)
payloadDecryptPath = filepath.Join(strings.Split(decryptPrefix, "/")...)
}
// Chunk is a byte slice used for chunking data
type Chunk []byte
// getShellURL waterfalls settings and returns the RPC url in priority
// order from Options, Config, or Default settings
func (s *Session) setPayloadRPCPath() {
if s.Options.Payload.RPCURL != "" {
payloadRPCPath = s.Options.Payload.RPCURL
return
}
if s.Config.Payload.RPCURL != "" {
payloadRPCPath = s.Config.Payload.RPCURL
return
}
payloadRPCPath = defaultpayloadRPCPath
}
// DeployPayload takes the contents from the /payload/encrypted and adds it to
// the storage endpoint
func (s *Session) DeployPayload() error {
// check that encrypted payload exists, exit if it doesn't
if _, err := os.Stat(filepath.Join(payloadEncryptedPath, defaultOutputKilName)); os.IsNotExist(err) {
return errors.New("encrypted payload does not exist, exiting")
}
// check for payload ID in config, exti if it already exists
if s.Config.Payload.ID != "" {
return fmt.Errorf("payload %v already deployed, skipping", s.Config.Payload.ID)
}
sh := shell.NewShell(payloadRPCPath)
f, err := os.Open(filepath.Join(payloadEncryptedPath, defaultOutputKilName))
if err != nil {
return err
}
mhash, err := sh.Add(f)
if err != nil {
return err
}
s.Config.Payload.ID = mhash
if err := SetPayloadEndpoint(s.Config.Contract.Owner, s.Config.Contract.ID, s.Config.Payload.ID); err != nil {
return err
}
s.Config.Payload.Status = "deployed"
return nil
}
// GetPayload Gets the payload from the storage endpoint and stores it locally
// in the Encrypted payload folder
func (s *Session) GetPayload() error {
sh := shell.NewShell(payloadRPCPath)
if err := sh.Get(s.Config.Payload.ID, payloadEncryptedPath); err != nil {
return err
}
s.Config.Payload.Status = "synced"
if err := os.Rename(filepath.Join(payloadEncryptedPath, s.Config.Payload.ID), filepath.Join(payloadEncryptedPath, defaultOutputKilName)); err != nil {
return err
}
return nil
}
// Encrypt encrypts the payload
func (s *Session) Encrypt() error {
var key [32]byte
os.RemoveAll(payloadTempPath)
if err := os.Mkdir(payloadTempPath, 0755); err != nil {
return err
}
if err := zipSource(); err != nil {
return err
}
if err := s.setPayloadKey(); err != nil {
return err
}
secret, err := hex.DecodeString(s.Config.Payload.Secret)
if err != nil {
return err
}
copy(key[:], secret)
encryptMultiPart(key)
os.RemoveAll(payloadTempPath)
s.Config.Payload.Status = "encrypted"
return nil
}
// Decrypt descrypts the payload and returns an error
func (s *Session) Decrypt() error {
var key [32]byte
secret, err := hex.DecodeString(s.Config.Payload.Secret)
if err != nil {
return err
}
copy(key[:], secret)
os.RemoveAll(payloadTempPath)
os.Mkdir(payloadTempPath, 0755)
decryptMultiPart(key)
if err := unzipSource(); err != nil {
return err
}
os.RemoveAll(payloadDecryptPath)
if err := os.Rename(filepath.Join(payloadTempPath, "source"), payloadDecryptPath); err != nil {
return err
}
os.RemoveAll(payloadTempPath)
return nil
}
func getFileSize(path string) (int64, error) {
info, err := os.Stat(path)
if err != nil {
return 0, err
}
return info.Size(), nil
}
func zipSource() error {
fmt.Println("-- compressing payload (this could take a while) --")
zip := filepath.Join(payloadTempPath, defaultOutputZipName)
z := archiver.NewZip()
if err := z.Archive([]string{payloadSourcePath}, zip); err != nil {
return err
}
return nil
}
func unzipSource() error {
fmt.Println("-- uncompressing payload (this could take a while) --")
zip := filepath.Join(payloadTempPath, defaultOutputZipName)
z := archiver.NewZip()
if err := z.Unarchive(zip, payloadTempPath); err != nil {
return err
}
return nil
}
func (s *Session) setPayloadKey() error {
if s.Config.Payload.Secret != "" {
return errors.New("encryption secret already set")
}
s.Config.Payload.Secret = generateKey()
return nil
}
func initFile(file string) *os.File {
i, err := os.Create(file)
if err != nil {
log.Fatal(err)
}
i.Close()
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
return f
}
func encryptMultiPart(k [32]byte) {
s := filepath.Join(payloadTempPath, defaultOutputZipName)
d := filepath.Join(payloadEncryptedPath, defaultOutputKilName)
totalSize, _ := getFileSize(d)
f := initFile(d)
r := make(chan Chunk)
w := make(chan Chunk)
go reader(s, r)
go encrypter(r, w, k, totalSize)
writer(w, f)
}
func decryptMultiPart(k [32]byte) {
s := filepath.Join(payloadEncryptedPath, defaultOutputKilName)
d := filepath.Join(payloadTempPath, defaultOutputZipName)
totalSize, _ := getFileSize(s)
f := initFile(d)
r := make(chan Chunk)
w := make(chan Chunk)
go reader(s, r)
go decrypter(r, w, k, totalSize)
writer(w, f)
}
func encrypter(r, w chan Chunk, k [32]byte, totalSize int64) {
var block []byte
count := int((totalSize / maxChunkSize) + 1)
fmt.Println("-- encrypting payload --")
bar := pb.StartNew(count)
for {
x, ok := <-r
if len(r) == 0 {
if !ok {
break
}
}
for _, b := range x {
if len(block) < maxChunkSize {
block = append(block, b)
}
if len(block) == maxChunkSize {
e := encrypt(block, k)
bar.Increment()
w <- e
block = []byte{}
}
}
}
e := encrypt(block, k)
w <- e
bar.Set(nil, count)
bar.Finish()
close(w)
}
func decrypter(r, w chan Chunk, k [32]byte, totalSize int64) {
var block []byte
var chunkSize = maxChunkSize + 40
count := int((totalSize / maxChunkSize) + 1)
fmt.Println("-- decrypting payload --")
bar := pb.StartNew(count)
for {
x, ok := <-r
if len(r) == 0 {
if !ok {
break
}
}
for _, b := range x {
if len(block) < chunkSize {
block = append(block, b)
}
if len(block) == chunkSize {
e := decrypt(block, k)
bar.Increment()
w <- e
block = []byte{}
}
}
}
e := decrypt(block, k)
w <- e
bar.Set(nil, count)
bar.Finish()
close(w)
}
func reader(file string, r chan Chunk) {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
fi, err := f.Stat()
if err != nil {
log.Fatal(err)
}
remaining := fi.Size()
var offset int64
for {
if len(r) == 0 {
if remaining <= maxReadSize {
rc := make([]byte, remaining)
f.ReadAt(rc, offset)
r <- rc
f.Close()
close(r)
return
} else {
rc := make([]byte, maxReadSize)
f.ReadAt(rc, offset)
r <- rc
remaining = remaining - maxReadSize
offset = offset + maxReadSize
}
}
}
}
func writer(w chan Chunk, f *os.File) {
var writeCache []byte
for {
x, ok := <-w
if len(w) == 0 {
if !ok {
writeToFile(writeCache, f)
f.Close()
return
}
}
for _, b := range x {
if len(writeCache) < maxChunksCache {
writeCache = append(writeCache, b)
}
if len(writeCache) == maxChunksCache {
writeToFile(writeCache, f)
writeCache = []byte{}
}
}
}
}
func writeToFile(data []byte, f *os.File) {
if _, err := f.Write(data); err != nil {
log.Fatal(err)
}
}
func encrypt(data []byte, key [32]byte) []byte {
var nonce [24]byte
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
panic(err)
}
return secretbox.Seal(nonce[:], data, &nonce, &key)
}
func decrypt(data []byte, key [32]byte) []byte {
var nonce [24]byte
copy(nonce[:], data[:24])
d, ok := secretbox.Open(nil, data[24:], &nonce, &key)
if !ok {
panic("decryption error")
}
return d
}