/
files.go
247 lines (228 loc) · 5.23 KB
/
files.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
package croc
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"time"
log "github.com/cihub/seelog"
"github.com/pkg/errors"
)
func (c *Croc) processFile(src string) (err error) {
log.Debug("processing file")
defer func() {
log.Debug("finished processing file")
}()
fd := FileMetaData{}
// pathToFile and filename are the files that should be used internally
var pathToFile, filename string
// first check if it is stdin
if src == "stdin" {
var f *os.File
f, err = ioutil.TempFile(".", "croc-stdin-")
if err != nil {
return
}
_, err = io.Copy(f, os.Stdin)
if err != nil {
return
}
pathToFile = "."
filename = f.Name()
err = f.Close()
if err != nil {
return
}
// fd.Name is what the user will see
fd.Name = filename
fd.DeleteAfterSending = true
} else {
if !exists(src) {
err = errors.Errorf("file/folder '%s' does not exist", src)
return
}
pathToFile, filename = filepath.Split(filepath.Clean(src))
fd.Name = filename
}
// check wether the file is a dir
info, err := os.Stat(path.Join(pathToFile, filename))
if err != nil {
log.Error(err)
return
}
fd.IsDir = info.Mode().IsDir()
// zip file
log.Debug("zipping file")
c.crocFile, err = zipFile(path.Join(pathToFile, filename), c.UseCompression)
if err != nil {
log.Error(err)
return
}
log.Debug("...finished zipping")
fd.IsCompressed = c.UseCompression
fd.IsEncrypted = c.UseEncryption
fd.Hash, err = hashFile(c.crocFile)
if err != nil {
log.Error(err)
return err
}
fd.Size, err = fileSize(c.crocFile)
if err != nil {
err = errors.Wrap(err, "could not determine filesize")
log.Error(err)
return err
}
c.cs.Lock()
defer c.cs.Unlock()
c.cs.channel.fileMetaData = fd
go showIntro(c.cs.channel.codePhrase, fd)
return
}
func (c *Croc) getFilesReady() (err error) {
log.Debug("getting files ready")
defer func() {
log.Debug("files ready")
}()
c.cs.Lock()
defer c.cs.Unlock()
c.cs.channel.notSentMetaData = true
// send metadata
// wait until data is ready
for {
if c.cs.channel.fileMetaData.Name != "" {
break
}
c.cs.Unlock()
time.Sleep(10 * time.Millisecond)
c.cs.Lock()
}
// get passphrase
var passphrase []byte
passphrase, err = c.cs.channel.Pake.SessionKey()
if err != nil {
log.Error(err)
return
}
if c.UseEncryption {
// encrypt file data
c.crocFileEncrypted = tempFileName("croc-encrypted")
err = encryptFile(c.crocFile, c.crocFileEncrypted, passphrase)
if err != nil {
log.Error(err)
return
}
// remove the unencrypted versoin
if err = os.Remove(c.crocFile); err != nil {
log.Error(err)
return
}
c.cs.channel.fileMetaData.IsEncrypted = true
} else {
c.crocFileEncrypted = c.crocFile
}
// split into pieces to send
log.Debugf("splitting %s", c.crocFileEncrypted)
if err = splitFile(c.crocFileEncrypted, len(c.cs.channel.Ports)); err != nil {
log.Error(err)
return
}
// remove the file now since we still have pieces
if err = os.Remove(c.crocFileEncrypted); err != nil {
log.Error(err)
return
}
// encrypt meta data
var metaDataBytes []byte
metaDataBytes, err = json.Marshal(c.cs.channel.fileMetaData)
if err != nil {
log.Error(err)
return
}
c.cs.channel.EncryptedFileMetaData = encrypt(metaDataBytes, passphrase)
c.cs.channel.Update = true
log.Debugf("updating channel with file information")
errWrite := c.cs.channel.ws.WriteJSON(c.cs.channel)
if errWrite != nil {
log.Error(errWrite)
}
c.cs.channel.Update = false
go func() {
c.cs.Lock()
c.cs.channel.fileReady = true
c.cs.Unlock()
}()
return
}
func (c *Croc) processReceivedFile() (err error) {
// cat the file received
c.cs.Lock()
defer c.cs.Unlock()
c.cs.channel.FileReceived = true
defer func() {
c.cs.channel.Update = true
errWrite := c.cs.channel.ws.WriteJSON(c.cs.channel)
if errWrite != nil {
log.Error(errWrite)
return
}
c.cs.channel.Update = false
}()
filesToCat := make([]string, len(c.cs.channel.Ports))
for i := range c.cs.channel.Ports {
filesToCat[i] = c.crocFileEncrypted + "." + strconv.Itoa(i)
log.Debugf("going to cat file %s", filesToCat[i])
}
// defer os.Remove(c.crocFile)
log.Debugf("catting file into %s", c.crocFile)
err = catFiles(filesToCat, c.crocFileEncrypted, true)
if err != nil {
log.Error(err)
return
}
// unencrypt
c.crocFile = tempFileName("croc-unencrypted")
var passphrase []byte
passphrase, err = c.cs.channel.Pake.SessionKey()
if err != nil {
log.Error(err)
return
}
// decrypt if was encrypted on the other side
if c.cs.channel.fileMetaData.IsEncrypted {
err = decryptFile(c.crocFileEncrypted, c.crocFile, passphrase)
if err != nil {
log.Error(err)
return
}
os.Remove(c.crocFileEncrypted)
} else {
c.crocFile = c.crocFileEncrypted
}
// check hash
log.Debug("checking hash")
var hashString string
hashString, err = hashFile(c.crocFile)
if err != nil {
log.Error(err)
return
}
if hashString == c.cs.channel.fileMetaData.Hash {
log.Debug("hashes match")
} else {
err = errors.Errorf("hashes do not match, %s != %s", c.cs.channel.fileMetaData.Hash, hashString)
log.Error(err)
return
}
// unzip file
err = unzipFile(c.crocFile, ".")
if err != nil {
log.Error(err)
return
}
os.Remove(c.crocFile)
c.cs.channel.finishedHappy = true
return
}