From f084cea2925b52417a002c9a32b27d64f8048c71 Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 14:45:30 +0300 Subject: [PATCH 1/7] Added a new dir for storing testing files --- testing_data/README.md | 113 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 testing_data/README.md diff --git a/testing_data/README.md b/testing_data/README.md new file mode 100644 index 00000000..bd2c9316 --- /dev/null +++ b/testing_data/README.md @@ -0,0 +1,113 @@ +

+croc +
+Version +Version +

+ +

Secure transfer of stuff from one side of the internet to the other.

+ +This is more or less (but mostly *less*) a Golang port of [@warner's](https://github.com/warner) [*magic-wormhole*](https://github.com/warner/magic-wormhole) which allows you to directly transfer files and folders between computers. I decided to make this because I wanted to send my friend Jessie a file using *magic-wormhole* and when I told Jessie how to install the dependencies she made this face: :sob:. So, nominally, *croc* does the same thing (encrypted file transfer directly between computers) without dependencies so you can just double-click on your computer, even if you use Windows. + +**Don't we have enough open-source peer-to-peer file-transfer utilities?** + +[There](https://github.com/cowbell/sharedrop) [are](https://github.com/webtorrent/instant.io) [great](https://github.com/kern/filepizza) [tools](https://github.com/warner/magic-wormhole) [that](https://github.com/zerotier/toss) [already](https://github.com/ipfs/go-ipfs) [do](https://github.com/zerotier/toss) [this](https://github.com/nils-werner/zget). But, no we don't, because after review, [I found it was useful to make a new one](https://schollz.github.io/sending-a-file/). + +# Example + +_These two gifs should run in sync if you force-reload (Ctl+F5)_ + +**Sender:** + +![send](https://user-images.githubusercontent.com/6550035/31864532-ad80c6ae-b71b-11e7-91f9-bcba8143d3cf.gif) + +**Receiver:** + +![receive](https://user-images.githubusercontent.com/6550035/31864531-ad6e22c4-b71b-11e7-901a-02a210057cf1.gif) + + +**Sender:** + +``` +$ croc -send croc.exe +Sending 4.4 MB file named 'croc.exe' +Code is: 4-cement-galaxy-alpha + +Sending (->24.65.41.43:50843).. + 0s [==========================================================] 100% +File sent. +``` + +**Receiver:** + +``` +$ croc +Enter receive code: 4-cement-galaxy-alpha +Receiving file (4.4 MB) into: croc.exe +ok? (y/n): y + +Receiving (<-50.32.38.188:50843).. + 0s [==========================================================] 100% +Received file written to croc.exe +``` + +Note, by default, you don't need any arguments for receiving! This makes it possible for you to just double click the executable to run (nice for those of us that aren't computer wizards). + + +# Install + +[Download the latest release for your system](https://github.com/schollz/croc/releases/latest). + +Or, you can [install Go](https://golang.org/dl/) and build from source with `go get github.com/schollz/croc`. + + + +# How does it work? + +*croc* is similar to [magic-wormhole](https://github.com/warner/magic-wormhole#design) in spirit and design. Like *magic-wormhole*, *croc* generates a code phrase for you to share with your friend which allows secure end-to-end transfering of files and folders through a intermediary relay that connects the TCP ports between the two computers. + +In *croc*, code phrase is 16 random bits that are [menemonic encoded](http://web.archive.org/web/20101031205747/http://www.tothink.com/mnemonic/) plus a prepended integer to specify number of threads. This code phrase is hashed using sha256 and sent to a relay which maps that key to that connection. When the relay finds a matching key for both the receiver and the sender (i.e. they both have the same code phrase), then the sender transmits the encrypted metadata to the receiver through the relay. Then the receiver decrypts and reviews the metadata (file name, size), and chooses whether to consent to the transfer. + +After the receiver consents to the transfer, the sender transmits encrypted data through the relay. The relay setups up [Go channels](https://golang.org/doc/effective_go.html?h=chan#channels) for each connection which pipes all the data incoming from that sender's connection out to the receiver's connection. After the transmission the channels are destroyed and all the connection and meta data information is wiped from the relay server. The encrypted file data never is stored on the relay. + +**Encryption** + +Encryption uses PBKDF2 (see [RFC2898](http://www.ietf.org/rfc/rfc2898.txt)) where the code phrase shared between the sender and receiver is used as the passphrase. For each of the two encrypted data blocks (metadata stored on relay server, and file data transmitted), a random 8-byte salt is used and a IV is generated according to [NIST Recommendation for Block ciphers, Section 8.2](http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf). + + +**Decryption** + +On the receiver's computer, each piece of received encrypted data is written to a separate file. These files are concatenated and then decrypted. The hash of the decrypted file is then checked against the hash transmitted from the sender (part of the meta data block). + +## Run your own relay + +*croc* relies on a TCP relay to staple the parallel incoming and outgoing connections. The relay temporarily stores connection information and the encrypted meta information. The default uses a public relay at, `cowyo.com`, which has no guarantees except that I guarantee to turn if off as soon as it gets abused ([click here to check the current status of the public relay](https://stats.uptimerobot.com/lOwJYIgRm)). + +I recommend you run your own relay, it is very easy. On your server, `your-server.com`, just run + +``` +$ croc -relay +``` + +Now, when you use *croc* to send and receive you should add `-server your-server.com` to use your relay server. + +_Note:_ If you are behind a firewall, make sure to open up TCP ports 27001-27009. + +# Contribute + +I am awed by all the [great contributions](#acknowledgements) made! If you feel like contributing, in any way, by all means you can send an Issue, a PR, ask a question, or tweet me ([@yakczar](http://ctt.ec/Rq054)). + +# License + +MIT + +# Acknowledgements + +Thanks... + +- ...[@warner](https://github.com/warner) for the [idea](https://github.com/warner/magic-wormhole). +- ...[@tscholl2](https://github.com/tscholl2) for the [encryption gists](https://gist.github.com/tscholl2/dc7dc15dc132ea70a98e8542fefffa28). +- ...[@skorokithakis](https://github.com/skorokithakis) for [code on proxying two connections](https://www.stavros.io/posts/proxying-two-connections-go/). +- ...for making pull requests [@Girbons](https://github.com/Girbons), [@techtide](https://github.com/techtide), [@heymatthew](https://github.com/heymatthew), [@Lunsford94](https://github.com/Lunsford94), [@lummie](https://github.com/lummie), [@jesuiscamille](https://github.com/jesuiscamille), [@threefjord](https://github.com/threefjord), [@marcossegovia](https://github.com/marcossegovia), [@csleong98](https://github.com/csleong98), [@afotescu](https://github.com/afotescu)! From fe9ccb8677919ce2d6f41616ce18c23801a5c8e0 Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 14:49:31 +0300 Subject: [PATCH 2/7] Tests for FileSize --- utils_test.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/utils_test.go b/utils_test.go index 8dcab25a..ec0cdcef 100644 --- a/utils_test.go +++ b/utils_test.go @@ -6,10 +6,28 @@ import ( ) func TestSplitFile(t *testing.T) { - err := SplitFile("README.md", 3) + err := SplitFile("testing_data/README.md", 3) if err != nil { t.Error(err) } - os.Remove("README.md.0") - os.Remove("README.md.1") + os.Remove("testing_data/README.md.0") + os.Remove("testing_data/README.md.1") +} + +func TestFileSize(t *testing.T) { + t.Run("File is ok ", func(t *testing.T) { + _, err := FileSize("testing_data/README.md") + if err != nil { + t.Errorf("should pass with no error, got: %v", err) + } + }) + t.Run("File does not exist", func(t *testing.T) { + s, err := FileSize("testing_data/someStrangeFile") + if err == nil { + t.Error("should return an error") + } + if s > 0 { + t.Errorf("size should be 0, got: %d", s) + } + }) } From 55ae98a588ad7ba23ad16edf20e6176a81b80dbe Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 14:49:49 +0300 Subject: [PATCH 3/7] Tests for HashFile --- utils_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/utils_test.go b/utils_test.go index ec0cdcef..fd2295a3 100644 --- a/utils_test.go +++ b/utils_test.go @@ -31,3 +31,27 @@ func TestFileSize(t *testing.T) { } }) } + +func TestHashFile(t *testing.T) { + t.Run("Hash created successfully", func(t *testing.T) { + h, err := HashFile("testing_data/README.md") + if err != nil { + t.Errorf("should pass with no error, got: %v", err) + } + if len(h) != 32 { + t.Errorf("invalid md5 hash, length should be 32 got: %d", len(h)) + } + }) + t.Run("File does not exist", func(t *testing.T) { + h, err := HashFile("testing_data/someStrangeFile") + if err == nil { + t.Error("should return an error") + } + if len(h) > 0 { + t.Errorf("hash length should be 0, got: %d", len(h)) + } + if h != "" { + t.Errorf("hash should be empty string, got: %s", h) + } + }) +} From 25acbbc3f1acaf97065f23d0d7f71bc09801f62d Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 14:50:03 +0300 Subject: [PATCH 4/7] Tests for copyFileContents --- utils_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/utils_test.go b/utils_test.go index fd2295a3..9c8a2a65 100644 --- a/utils_test.go +++ b/utils_test.go @@ -55,3 +55,27 @@ func TestHashFile(t *testing.T) { } }) } + +func TestCopyFileContents(t *testing.T) { + t.Run("Content copied successfully", func(t *testing.T) { + f1 := "testing_data/README.md" + f2 := "testing_data/CopyOfREADME.md" + err := copyFileContents(f1, f2) + if err != nil { + t.Errorf("should pass with no error, got: %v", err) + } + f1Length, err := FileSize(f1) + if err != nil { + t.Errorf("can't get file nr1 size: %v", err) + } + f2Length, err := FileSize(f2) + if err != nil { + t.Errorf("can't get file nr2 size: %v", err) + } + + if f1Length != f2Length { + t.Errorf("size of both files should be same got: file1: %d file2: %d", f1Length, f2Length) + } + os.Remove(f2) + }) +} From 7458135202380931f296e396562cd0783211b0fe Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 14:52:27 +0300 Subject: [PATCH 5/7] Test for CopyFile --- utils_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/utils_test.go b/utils_test.go index 9c8a2a65..7b3ba390 100644 --- a/utils_test.go +++ b/utils_test.go @@ -79,3 +79,27 @@ func TestCopyFileContents(t *testing.T) { os.Remove(f2) }) } + +func TestCopyFile(t *testing.T) { + t.Run("Content copied successfully", func(t *testing.T) { + f1 := "testing_data/README.md" + f2 := "testing_data/CopyOfREADME.md" + err := CopyFile(f1, f2) + if err != nil { + t.Errorf("should pass with no error, got: %v", err) + } + f1Length, err := FileSize(f1) + if err != nil { + t.Errorf("can't get file nr1 size: %v", err) + } + f2Length, err := FileSize(f2) + if err != nil { + t.Errorf("can't get file nr2 size: %v", err) + } + + if f1Length != f2Length { + t.Errorf("size of both files should be same got: file1: %d file2: %d", f1Length, f2Length) + } + os.Remove(f2) + }) +} From 3bb1c015c8e00d70bd6147e730698bb8757a3cc6 Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 15:01:37 +0300 Subject: [PATCH 6/7] Tests for CatFiles --- testing_data/catFile1.txt | 1 + testing_data/catFile2.txt | 1 + utils_test.go | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 testing_data/catFile1.txt create mode 100644 testing_data/catFile2.txt diff --git a/testing_data/catFile1.txt b/testing_data/catFile1.txt new file mode 100644 index 00000000..06513323 --- /dev/null +++ b/testing_data/catFile1.txt @@ -0,0 +1 @@ +Some simple text to see if it works \ No newline at end of file diff --git a/testing_data/catFile2.txt b/testing_data/catFile2.txt new file mode 100644 index 00000000..e9d648e4 --- /dev/null +++ b/testing_data/catFile2.txt @@ -0,0 +1 @@ +More data to see if it 100% works \ No newline at end of file diff --git a/utils_test.go b/utils_test.go index 7b3ba390..6d2de664 100644 --- a/utils_test.go +++ b/utils_test.go @@ -81,7 +81,7 @@ func TestCopyFileContents(t *testing.T) { } func TestCopyFile(t *testing.T) { - t.Run("Content copied successfully", func(t *testing.T) { + t.Run("Files copied successfully", func(t *testing.T) { f1 := "testing_data/README.md" f2 := "testing_data/CopyOfREADME.md" err := CopyFile(f1, f2) @@ -103,3 +103,17 @@ func TestCopyFile(t *testing.T) { os.Remove(f2) }) } + +func TestCatFiles(t *testing.T) { + t.Run("CatFiles passing", func(t *testing.T) { + files := []string{"testing_data/catFile1.txt", "testing_data/catFile2.txt"} + err := CatFiles(files, "testing_data/CatFile.txt", false) + if err != nil { + t.Errorf("should pass with no error, got: %v", err) + } + if _, err := os.Stat("testing_data/CatFile.txt"); os.IsNotExist(err) { + t.Errorf("file were not created: %v", err) + } + os.Remove("testing_data/CatFile.txt") + }) +} From b10d8a075bb86391fd0ece1a1165b0d96c113aa9 Mon Sep 17 00:00:00 2001 From: h3yEllex Date: Wed, 25 Oct 2017 15:03:36 +0300 Subject: [PATCH 7/7] Simplified FileSize function --- utils.go | 8 +------- utils_test.go | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/utils.go b/utils.go index ccc5c46d..1171e1a9 100644 --- a/utils.go +++ b/utils.go @@ -163,13 +163,7 @@ func HashFile(filename string) (hash string, err error) { // FileSize returns the size of a file func FileSize(filename string) (int, error) { - f, err := os.Open(filename) - if err != nil { - return -1, err - } - defer f.Close() - - fi, err := f.Stat() + fi, err := os.Stat(filename) if err != nil { return -1, err } diff --git a/utils_test.go b/utils_test.go index 6d2de664..e7e08300 100644 --- a/utils_test.go +++ b/utils_test.go @@ -26,8 +26,8 @@ func TestFileSize(t *testing.T) { if err == nil { t.Error("should return an error") } - if s > 0 { - t.Errorf("size should be 0, got: %d", s) + if s != -1 { + t.Errorf("size should be -1, got: %d", s) } }) }