mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
add crypto, utils
This commit is contained in:
parent
ce8135002c
commit
7656b102c7
9 changed files with 645 additions and 1 deletions
3
main.go
3
main.go
|
@ -3,7 +3,8 @@ package main
|
||||||
import croc "github.com/schollz/croc/src"
|
import croc "github.com/schollz/croc/src"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := croc.Relay([]string{"27001", "27002", "27003", "27004"}, "8002")
|
c := croc.Init()
|
||||||
|
err := c.Relay()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
125
src/crypto.go
Normal file
125
src/crypto.go
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
package croc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
mathrand "math/rand"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/cihub/seelog"
|
||||||
|
"github.com/mars9/crypt"
|
||||||
|
"github.com/schollz/mnemonicode"
|
||||||
|
"golang.org/x/crypto/pbkdf2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
mathrand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRandomName() string {
|
||||||
|
result := []string{}
|
||||||
|
bs := make([]byte, 4)
|
||||||
|
binary.LittleEndian.PutUint32(bs, mathrand.Uint32())
|
||||||
|
result = mnemonicode.EncodeWordList(result, bs)
|
||||||
|
return strings.Join(result, "-")
|
||||||
|
}
|
||||||
|
|
||||||
|
func encrypt(plaintext []byte, passphrase string, dontencrypt ...bool) (encrypted []byte, salt string, iv string) {
|
||||||
|
if len(dontencrypt) > 0 && dontencrypt[0] {
|
||||||
|
return plaintext, "salt", "iv"
|
||||||
|
}
|
||||||
|
key, saltBytes := deriveKey(passphrase, nil)
|
||||||
|
ivBytes := make([]byte, 12)
|
||||||
|
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
||||||
|
// Section 8.2
|
||||||
|
rand.Read(ivBytes)
|
||||||
|
b, _ := aes.NewCipher(key)
|
||||||
|
aesgcm, _ := cipher.NewGCM(b)
|
||||||
|
encrypted = aesgcm.Seal(nil, ivBytes, plaintext, nil)
|
||||||
|
salt = hex.EncodeToString(saltBytes)
|
||||||
|
iv = hex.EncodeToString(ivBytes)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func decrypt(data []byte, passphrase string, salt string, iv string, dontencrypt ...bool) (plaintext []byte, err error) {
|
||||||
|
if len(dontencrypt) > 0 && dontencrypt[0] {
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
saltBytes, _ := hex.DecodeString(salt)
|
||||||
|
ivBytes, _ := hex.DecodeString(iv)
|
||||||
|
key, _ := deriveKey(passphrase, saltBytes)
|
||||||
|
b, _ := aes.NewCipher(key)
|
||||||
|
aesgcm, _ := cipher.NewGCM(b)
|
||||||
|
plaintext, err = aesgcm.Open(nil, ivBytes, data, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func deriveKey(passphrase string, salt []byte) ([]byte, []byte) {
|
||||||
|
if salt == nil {
|
||||||
|
salt = make([]byte, 8)
|
||||||
|
// http://www.ietf.org/rfc/rfc2898.txt
|
||||||
|
// Salt.
|
||||||
|
rand.Read(salt)
|
||||||
|
}
|
||||||
|
return pbkdf2.Key([]byte(passphrase), salt, 1000, 32, sha256.New), salt
|
||||||
|
}
|
||||||
|
|
||||||
|
func hash(data string) string {
|
||||||
|
return hashBytes([]byte(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func hashBytes(data []byte) string {
|
||||||
|
sum := sha256.Sum256(data)
|
||||||
|
return fmt.Sprintf("%x", sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func encryptFile(inputFilename string, outputFilename string, password string) error {
|
||||||
|
return cryptFile(inputFilename, outputFilename, password, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decryptFile(inputFilename string, outputFilename string, password string) error {
|
||||||
|
return cryptFile(inputFilename, outputFilename, password, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cryptFile(inputFilename string, outputFilename string, password string, encrypt bool) error {
|
||||||
|
in, err := os.Open(inputFilename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
out, err := os.Create(outputFilename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := out.Sync(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
if err := out.Close(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
c := &crypt.Crypter{
|
||||||
|
HashFunc: sha1.New,
|
||||||
|
HashSize: sha1.Size,
|
||||||
|
Key: crypt.NewPbkdf2Key([]byte(password), 32),
|
||||||
|
}
|
||||||
|
if encrypt {
|
||||||
|
if err := c.Encrypt(out, in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := c.Decrypt(out, in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
49
src/crypto_test.go
Normal file
49
src/crypto_test.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package croc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncrypt(t *testing.T) {
|
||||||
|
key := getRandomName()
|
||||||
|
encrypted, salt, iv := encrypt([]byte("hello, world"), key)
|
||||||
|
decrypted, err := decrypt(encrypted, key, salt, iv)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if string(decrypted) != "hello, world" {
|
||||||
|
t.Error("problem decrypting")
|
||||||
|
}
|
||||||
|
_, err = decrypt(encrypted, "wrong passphrase", salt, iv)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("should not work!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncryptFiles(t *testing.T) {
|
||||||
|
key := getRandomName()
|
||||||
|
if err := ioutil.WriteFile("temp", []byte("hello, world!"), 0644); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := encryptFile("temp", "temp.enc", key); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := decryptFile("temp.enc", "temp.dec", key); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadFile("temp.dec")
|
||||||
|
if string(data) != "hello, world!" {
|
||||||
|
t.Errorf("Got something weird: " + string(data))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := decryptFile("temp.enc", "temp.dec", key+"wrong password"); err == nil {
|
||||||
|
t.Error("should throw error!")
|
||||||
|
}
|
||||||
|
os.Remove("temp.dec")
|
||||||
|
os.Remove("temp.enc")
|
||||||
|
os.Remove("temp")
|
||||||
|
}
|
135
src/testing_data/README.md
Normal file
135
src/testing_data/README.md
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
<p align="center">
|
||||||
|
<img
|
||||||
|
src="https://user-images.githubusercontent.com/6550035/31846899-2b8a7034-b5cf-11e7-9643-afe552226c59.png"
|
||||||
|
width="100%" border="0" alt="croc">
|
||||||
|
<br>
|
||||||
|
<a href="https://github.com/schollz/croc/releases/latest"><img src="https://img.shields.io/badge/version-β2.0.0-brightgreen.svg?style=flat-square" alt="Version"></a>
|
||||||
|
<a href="https://saythanks.io/to/schollz"><img src="https://img.shields.io/badge/Say%20Thanks-!-yellow.svg?style=flat-square" alt="Go Report Card"></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
|
||||||
|
<p align="center">Easily and securely transfer stuff from one computer to another.</p>
|
||||||
|
|
||||||
|
*croc* allows any two computers to directly and securely transfer files and folders. When sending a file, *croc* generates a random code phrase which must be shared with the recipient so they can receive the file. The code phrase encrypts all data and metadata and also serves to authorize the connection between the two computers in a intermediary relay. The relay connects the TCP ports between the two computers and does not store any information (and all information passing through it is encrypted).
|
||||||
|
|
||||||
|
## New version released June 24th, 2018 - please upgrade if you are using the public relay.
|
||||||
|
|
||||||
|
I hear you asking, *Why another 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, after review, [I found it was useful to make another](https://schollz.github.io/sending-a-file/). Namely, *croc* has no dependencies (just [download a binary and run](https://github.com/schollz/croc/releases/latest)), it works on any operating system, and its blazingly fast because it does parallel transfer over multiple TCP ports.
|
||||||
|
|
||||||
|
# Example
|
||||||
|
|
||||||
|
_These two gifs should run in sync if you force-reload (Ctl+F5)_
|
||||||
|
|
||||||
|
**Sender:**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**Receiver:**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
**Sender:**
|
||||||
|
|
||||||
|
```
|
||||||
|
$ croc -send some-file-or-folder
|
||||||
|
Sending 4.4 MB file named 'some-file-or-folder'
|
||||||
|
Code is: cement-galaxy-alpha
|
||||||
|
Your public key: F9Ky3WU2yG4y7KKppF4KnEhrmtY9ZlTsEMkqXfC1
|
||||||
|
Send to public key: xHVRlQ2Yp6otQXBoLMcUJmmtNPXl7z8tOf019sGw
|
||||||
|
ok? (y/n): y
|
||||||
|
|
||||||
|
Sending (->[1]63982)..
|
||||||
|
89% |███████████████████████████████████ | [12s:1s]
|
||||||
|
File sent (2.6 MB/s)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Receiver:**
|
||||||
|
|
||||||
|
```
|
||||||
|
$ croc
|
||||||
|
Your public key: xHVRlQ2Yp6otQXBoLMcUJmmtNPXl7z8tOf019sGw
|
||||||
|
Enter receive code: cement-galaxy-alpha
|
||||||
|
Receiving file (4.4 MB) into: some-file-or-folder
|
||||||
|
from public key: F9Ky3WU2yG4y7KKppF4KnEhrmtY9ZlTsEMkqXfC1
|
||||||
|
ok? (y/n): y
|
||||||
|
|
||||||
|
Receiving (<-[1]63975)..
|
||||||
|
97% |██████████████████████████████████████ | [13s:0s]
|
||||||
|
Received file written to some-file-or-folder (2.6 MB/s)
|
||||||
|
```
|
||||||
|
|
||||||
|
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).
|
||||||
|
|
||||||
|
## Using *croc* in pipes
|
||||||
|
|
||||||
|
You can easily use *croc* in pipes when you need to send data through stdin or get data from stdout.
|
||||||
|
|
||||||
|
**Sender:**
|
||||||
|
|
||||||
|
```
|
||||||
|
$ cat some_file_or_folder | croc
|
||||||
|
```
|
||||||
|
|
||||||
|
In this case *croc* will automatically use the stdin data and send and assign a filename like "croc-stdin-123456789".
|
||||||
|
|
||||||
|
**Receiver:**
|
||||||
|
|
||||||
|
```
|
||||||
|
$ croc --code code-phrase --yes --stdout | more
|
||||||
|
```
|
||||||
|
|
||||||
|
Here the reciever specified the code (`--code`) so it will not be prompted, and also specified `--yes` so the file will be automatically accepted. The output goes to stdout when flagged with `--stdout`.
|
||||||
|
|
||||||
|
|
||||||
|
# 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. The standard relay is on a public IP address (default `cowyo.com`), but before transmitting the file the two instances of *croc* send out UDP broadcasts to determine if they are both on the local network, and use a local relay instead of the cloud relay in the case that they are both local.
|
||||||
|
|
||||||
|
The code phrase for transfering files is just three words which are 16 random bits that are [menemonic encoded](http://web.archive.org/web/20101031205747/http://www.tothink.com/mnemonic/). This code phrase is hashed using sha256 and sent to the relay which maps that hashed code phrase to that connection. When the relay finds a matching code phrase hash 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 AES-256 with a pbkdf2 derived key (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 a 30-day uptime of 99.989% ([click here to check the current status of the public relay](https://stats.uptimerobot.com/lOwJYIgRm)).
|
||||||
|
|
||||||
|
You can also 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. 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), [@callmefever](https://github.com/callmefever), [@El-JojA](https://github.com/El-JojA), [@anatolyyyyyy](https://github.com/anatolyyyyyy), [@goggle](https://github.com/goggle), [@smileboywtu](https://github.com/smileboywtu)!
|
38
src/testing_data/README.md.2
Normal file
38
src/testing_data/README.md.2
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
tion is wiped from the relay server. The encrypted file data never is stored on the relay.
|
||||||
|
|
||||||
|
**Encryption**
|
||||||
|
|
||||||
|
Encryption uses AES-256 with a pbkdf2 derived key (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 a 30-day uptime of 99.989% ([click here to check the current status of the public relay](https://stats.uptimerobot.com/lOwJYIgRm)).
|
||||||
|
|
||||||
|
You can also 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. 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), [@callmefever](https://github.com/callmefever), [@El-JojA](https://github.com/El-JojA), [@anatolyyyyyy](https://github.com/anatolyyyyyy), [@goggle](https://github.com/goggle), [@smileboywtu](https://github.com/smileboywtu)!
|
1
src/testing_data/catFile1.txt
Normal file
1
src/testing_data/catFile1.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Some simple text to see if it works
|
1
src/testing_data/catFile2.txt
Normal file
1
src/testing_data/catFile2.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
More data to see if it 100% works
|
175
src/utils.go
Normal file
175
src/utils.go
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
package croc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// catFiles copies data from n files to a single one and removes source files
|
||||||
|
// if Debug mode is set to false
|
||||||
|
func catFiles(files []string, outfile string, remove bool) error {
|
||||||
|
finished, err := os.Create(outfile)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "CatFiles create: ")
|
||||||
|
}
|
||||||
|
defer finished.Close()
|
||||||
|
for _, file := range files {
|
||||||
|
fh, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, fmt.Sprintf("CatFiles open %v: ", file))
|
||||||
|
}
|
||||||
|
_, err = io.Copy(finished, fh)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "CatFiles copy: ")
|
||||||
|
}
|
||||||
|
fh.Close()
|
||||||
|
if remove {
|
||||||
|
os.Remove(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// splitFile creates a bunch of smaller files with the data from source splited into them
|
||||||
|
func splitFile(fileName string, numPieces int) (err error) {
|
||||||
|
file, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
fi, err := file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bytesPerPiece := int(math.Ceil(float64(fi.Size()) / float64(numPieces)))
|
||||||
|
|
||||||
|
buf := make([]byte, bytesPerPiece)
|
||||||
|
for i := 0; i < numPieces; i++ {
|
||||||
|
|
||||||
|
out, err := os.Create(fileName + "." + strconv.Itoa(i))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := file.Read(buf)
|
||||||
|
out.Write(buf[:n])
|
||||||
|
out.Close()
|
||||||
|
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyFile copies a file from src to dst. If src and dst files exist, and are
|
||||||
|
// the same, then return success. Otherise, attempt to create a hard link
|
||||||
|
// between the two files. If that fail, copy the file contents from src to dst.
|
||||||
|
func copyFile(src, dst string) (err error) {
|
||||||
|
sfi, err := os.Stat(src)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !sfi.Mode().IsRegular() {
|
||||||
|
// cannot copy non-regular files (e.g., directories,
|
||||||
|
// symlinks, devices, etc.)
|
||||||
|
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
|
||||||
|
}
|
||||||
|
dfi, err := os.Stat(dst)
|
||||||
|
if err != nil {
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !(dfi.Mode().IsRegular()) {
|
||||||
|
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
|
||||||
|
}
|
||||||
|
if os.SameFile(sfi, dfi) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = os.Link(src, dst); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = copyFileContents(src, dst)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyFileContents copies the contents of the file named src to the file named
|
||||||
|
// by dst. The file will be created if it does not already exist. If the
|
||||||
|
// destination file exists, all it's contents will be replaced by the contents
|
||||||
|
// of the source file.
|
||||||
|
func copyFileContents(src, dst string) (err error) {
|
||||||
|
in, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
out, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
cerr := out.Close()
|
||||||
|
if err == nil {
|
||||||
|
err = cerr
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if _, err = io.Copy(out, in); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = out.Sync()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// hashFile does a md5 hash on the file
|
||||||
|
// from https://golang.org/pkg/crypto/md5/#example_New_file
|
||||||
|
func hashFile(filename string) (hash string, err error) {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
h := md5.New()
|
||||||
|
if _, err = io.Copy(h, f); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hash = fmt.Sprintf("%x", h.Sum(nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// fileSize returns the size of a file
|
||||||
|
func fileSize(filename string) (int, error) {
|
||||||
|
fi, err := os.Stat(filename)
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
size := int(fi.Size())
|
||||||
|
return size, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLocalIP returns the local ip address
|
||||||
|
func getLocalIP() string {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
bestIP := ""
|
||||||
|
for _, address := range addrs {
|
||||||
|
// check the address type and if it is not a loopback the display it
|
||||||
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
return ipnet.IP.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestIP
|
||||||
|
}
|
119
src/utils_test.go
Normal file
119
src/utils_test.go
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
package croc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSplitFile(t *testing.T) {
|
||||||
|
err := splitFile("testing_data/README.md", 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
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 != -1 {
|
||||||
|
t.Errorf("size should be -1, got: %d", s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCopyFile(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)
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue