mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Store file data in memory when transfering
This commit is contained in:
parent
3ab0f92bd8
commit
eb1b5a076c
2 changed files with 21 additions and 26 deletions
38
connect.go
38
connect.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -90,9 +91,6 @@ func runClient(connectionType string, codePhrase string) {
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\nDownloaded %s!", fileName)
|
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()
|
defer connection.Close()
|
||||||
|
|
||||||
// Open the file that needs to be send to the client
|
var err error
|
||||||
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(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)))
|
chunksPerWorker := int(math.Ceil(numChunks / float64(numberConnections)))
|
||||||
|
|
||||||
bytesPerConnection := int64(chunksPerWorker * BUFFERSIZE)
|
bytesPerConnection := int64(chunksPerWorker * BUFFERSIZE)
|
||||||
if id+1 == numberConnections {
|
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)
|
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)))
|
connection.Write([]byte(fillString(fileHash, BUFFERSIZE)))
|
||||||
|
|
||||||
sendBuffer := make([]byte, BUFFERSIZE)
|
sendBuffer := make([]byte, BUFFERSIZE)
|
||||||
|
file := bytes.NewBuffer(fileBytes)
|
||||||
chunkI := 0
|
chunkI := 0
|
||||||
for {
|
for {
|
||||||
_, err = file.Read(sendBuffer)
|
_, 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 serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
|
||||||
var runAsRelay, debugFlag bool
|
var runAsRelay, debugFlag bool
|
||||||
var fileSalt, fileIV, fileHash string
|
var fileSalt, fileIV, fileHash string
|
||||||
|
var fileBytes []byte
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
|
flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
|
||||||
|
@ -63,13 +64,7 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var encrypted []byte
|
fileBytes, fileSalt, fileIV = Encrypt(fdata, codePhraseFlag)
|
||||||
encrypted, fileSalt, fileIV = Encrypt(fdata, codePhraseFlag)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ioutil.WriteFile(fileName+".encrypted", encrypted, 0644)
|
|
||||||
fileHash = HashBytes(fdata)
|
fileHash = HashBytes(fdata)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue