1
0
mirror of https://github.com/schollz/croc.git synced 2024-12-11 06:35:01 +03:00

toggle encryption/compression

This commit is contained in:
Zack Scholl 2018-09-22 10:49:33 -07:00
parent f977ac24c4
commit c6374da410
3 changed files with 25 additions and 12 deletions

View File

@ -141,7 +141,7 @@ func (c *Croc) sendReceive(websocketAddress, fname, codephrase string, isSender
if isSender {
// start peerdiscovery relay server
go sender.Send(done, sock, fname, codephrase)
go sender.Send(done, sock, fname, codephrase, c.UseCompression, c.UseEncryption)
} else {
go recipient.Receive(done, sock, codephrase, c.NoRecipientPrompt, c.Stdout)
}

View File

@ -162,17 +162,18 @@ func receive(c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool
if err != nil {
return err
}
decrypted, err := enc.Decrypt(sessionKey, true)
decrypted, err := enc.Decrypt(sessionKey, !fstats.IsEncrypted)
if err != nil {
return err
}
// do decompression
decompressed := compress.Decompress(decrypted)
// decompressed := decrypted
if fstats.IsCompressed {
decrypted = compress.Decompress(decrypted)
}
// write to file
n, err := f.Write(decompressed)
n, err := f.Write(decrypted)
if err != nil {
return err
}

View File

@ -29,10 +29,10 @@ import (
var DebugLevel string
// Send is the async call to send data
func Send(done chan struct{}, c *websocket.Conn, fname string, codephrase string) {
func Send(done chan struct{}, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) {
logger.SetLogLevel(DebugLevel)
log.Debugf("sending %s", fname)
err := send(c, fname, codephrase)
err := send(c, fname, codephrase, useCompression, useEncryption)
if err != nil {
if strings.HasPrefix(err.Error(), "websocket: close 100") {
err = nil
@ -41,7 +41,7 @@ func Send(done chan struct{}, c *websocket.Conn, fname string, codephrase string
done <- struct{}{}
}
func send(c *websocket.Conn, fname string, codephrase string) (err error) {
func send(c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) (err error) {
var f *os.File
var fstats models.FileStats
var fileHash []byte
@ -88,7 +88,15 @@ func send(c *websocket.Conn, fname string, codephrase string) (err error) {
if err != nil {
return err
}
fstats = models.FileStats{filename, fstat.Size(), fstat.ModTime(), fstat.IsDir(), fstat.Name()}
fstats = models.FileStats{
Name: filename,
Size: fstat.Size(),
ModTime: fstat.ModTime(),
IsDir: fstat.IsDir(),
SentName: fstat.Name(),
IsCompressed: useCompression,
IsEncrypted: useEncryption,
}
if fstats.IsDir {
// zip the directory
fstats.SentName, err = zipper.ZipFile(fname, true)
@ -170,11 +178,15 @@ func send(c *websocket.Conn, fname string, codephrase string) (err error) {
bar.Add(bytesread)
if bytesread > 0 {
// do compression
compressedBytes := compress.Compress(buffer[:bytesread])
// compressedBytes := buffer[:bytesread]
var compressedBytes []byte
if useCompression {
compressedBytes = compress.Compress(buffer[:bytesread])
} else {
compressedBytes = buffer[:bytesread]
}
// do encryption
enc := crypt.Encrypt(compressedBytes, sessionKey, true)
enc := crypt.Encrypt(compressedBytes, sessionKey, !useEncryption)
encBytes, err := json.Marshal(enc)
if err != nil {
return err