mirror of
https://github.com/schollz/croc.git
synced 2024-11-23 23:54:17 +03:00
Store file data in memory when transfering
This commit is contained in:
parent
3ab0f92bd8
commit
eb1b5a076c
38
connect.go
38
connect.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@ -90,9 +91,6 @@ func runClient(connectionType string, codePhrase string) {
|
||||
} else {
|
||||
fmt.Printf("\nDownloaded %s!", fileName)
|
||||
}
|
||||
} else {
|
||||
log.Info("cleaning up")
|
||||
os.Remove(fileName + ".encrypted")
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,26 +187,28 @@ func sendFile(id int, connection net.Conn, codePhrase string) {
|
||||
})
|
||||
defer connection.Close()
|
||||
|
||||
// Open the file that needs to be send to the client
|
||||
file, err := os.Open(fileName + ".encrypted")
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
// Get the filename and filesize
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
var err error
|
||||
|
||||
numChunks := math.Ceil(float64(fileInfo.Size()) / float64(BUFFERSIZE))
|
||||
// // Open the file that needs to be send to the client
|
||||
// file, err := os.Open(fileName + ".encrypted")
|
||||
// if err != nil {
|
||||
// logger.Error(err)
|
||||
// return
|
||||
// }
|
||||
// defer file.Close()
|
||||
// // Get the filename and filesize
|
||||
// fileInfo, err := file.Stat()
|
||||
// if err != nil {
|
||||
// logger.Error(err)
|
||||
// return
|
||||
// }
|
||||
|
||||
numChunks := math.Ceil(float64(len(fileBytes)) / float64(BUFFERSIZE))
|
||||
chunksPerWorker := int(math.Ceil(numChunks / float64(numberConnections)))
|
||||
|
||||
bytesPerConnection := int64(chunksPerWorker * BUFFERSIZE)
|
||||
if id+1 == numberConnections {
|
||||
bytesPerConnection = fileInfo.Size() - (numberConnections-1)*bytesPerConnection
|
||||
bytesPerConnection = int64(len(fileBytes)) - (numberConnections-1)*bytesPerConnection
|
||||
}
|
||||
fileSize := fillString(strconv.FormatInt(int64(bytesPerConnection), 10), 10)
|
||||
|
||||
@ -242,7 +242,7 @@ func sendFile(id int, connection net.Conn, codePhrase string) {
|
||||
connection.Write([]byte(fillString(fileHash, BUFFERSIZE)))
|
||||
|
||||
sendBuffer := make([]byte, BUFFERSIZE)
|
||||
|
||||
file := bytes.NewBuffer(fileBytes)
|
||||
chunkI := 0
|
||||
for {
|
||||
_, err = file.Read(sendBuffer)
|
||||
|
9
main.go
9
main.go
@ -21,6 +21,7 @@ var server, file string
|
||||
var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
|
||||
var runAsRelay, debugFlag bool
|
||||
var fileSalt, fileIV, fileHash string
|
||||
var fileBytes []byte
|
||||
|
||||
func main() {
|
||||
flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
|
||||
@ -63,13 +64,7 @@ func main() {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
var encrypted []byte
|
||||
encrypted, fileSalt, fileIV = Encrypt(fdata, codePhraseFlag)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
ioutil.WriteFile(fileName+".encrypted", encrypted, 0644)
|
||||
fileBytes, fileSalt, fileIV = Encrypt(fdata, codePhraseFlag)
|
||||
fileHash = HashBytes(fdata)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user