This commit is contained in:
Zack Scholl 2018-06-23 10:02:46 -07:00
parent 30c9c3317f
commit 51a87f1110
5 changed files with 93 additions and 74 deletions

View File

@ -78,6 +78,7 @@ func NewConnection(config *AppConfig) (*Connection, error) {
c.rate = config.Rate
c.Local = config.Local
c.keypair, _ = keypair.New()
fmt.Fprintf(os.Stderr, "Your public key: %s\n", c.keypair.Public)
if c.Local {
c.Yes = true
@ -135,6 +136,10 @@ func NewConnection(config *AppConfig) (*Connection, error) {
c.AskPath = config.PathSpec
c.Path = config.Path
}
c.File.IsEncrypted = true
if c.DontEncrypt {
c.File.IsEncrypted = false
}
if c.Debug {
SetLogLevel("debug")
@ -202,27 +207,6 @@ func (c *Connection) Run() error {
go relay.Run()
time.Sleep(200 * time.Millisecond)
if c.DontEncrypt {
// don't encrypt
CopyFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc")
c.File.IsEncrypted = false
} else {
// encrypt
log.Debug("encrypting...")
if err := EncryptFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc", c.Code); err != nil {
return err
}
c.File.IsEncrypted = true
}
// split file into pieces to send
if err := SplitFile(c.File.Name+".enc", c.NumberOfConnections); err != nil {
return err
}
// remove the file now since we still have pieces
if err := os.Remove(c.File.Name + ".enc"); err != nil {
return err
}
// get file hash
var err error
c.File.Hash, err = HashFile(path.Join(c.File.Path, c.File.Name))
@ -243,8 +227,6 @@ func (c *Connection) Run() error {
}
}
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
// broadcast local connection from sender
if c.Server == "" {
log.Debug("settings payload to ", c.Code)
@ -290,6 +272,7 @@ func (c *Connection) Run() error {
// runClient spawns threads for parallel uplink/downlink via TCP
func (c *Connection) runClient(serverName string) error {
c.HashedCode = Hash(c.Code)
c.NumberOfConnections = MAX_NUMBER_THREADS
var wg sync.WaitGroup
@ -328,6 +311,18 @@ func (c *Connection) runClient(serverName string) error {
os.Exit(1)
}
defer connection.Close()
err = connection.SetReadDeadline(time.Now().Add(1 * time.Hour))
if err != nil {
log.Warn(err)
}
err = connection.SetDeadline(time.Now().Add(1 * time.Hour))
if err != nil {
log.Warn(err)
}
err = connection.SetWriteDeadline(time.Now().Add(1 * time.Hour))
if err != nil {
log.Warn(err)
}
message := receiveMessage(connection)
log.Debugf("relay says: %s", message)
@ -370,15 +365,37 @@ func (c *Connection) runClient(serverName string) error {
// message is IP address, lets check next message
log.Debugf("[%d] got ok from relay: %s", id, message)
publicKeyRecipient := receiveMessage(connection)
// check if okay again
sendMessage("okay with sender", connection)
if id == 0 {
fmt.Fprintf(os.Stderr, "\nSending (->%s@%s)..\n", publicKeyRecipient, message)
// check if okay again
// TODO
encryptedPassword, err := c.keypair.Encrypt([]byte(RandStringBytesMaskImprSrc(20)), publicKeyRecipient)
passphraseString := RandStringBytesMaskImprSrc(20)
log.Debugf("passphrase: [%s]", passphraseString)
encryptedPassword, err := c.keypair.Encrypt([]byte(passphraseString), publicKeyRecipient)
if err != nil {
panic(err)
}
// encrypt files
if c.DontEncrypt {
// don't encrypt
CopyFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc")
c.File.IsEncrypted = false
} else {
// encrypt
log.Debugf("encrypting file with passphrase [%s]", passphraseString)
if err := EncryptFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc", passphraseString); err != nil {
panic(err)
}
c.File.IsEncrypted = true
}
// split file into pieces to send
if err := SplitFile(c.File.Name+".enc", c.NumberOfConnections); err != nil {
panic(err)
}
// remove the file now since we still have pieces
if err := os.Remove(c.File.Name + ".enc"); err != nil {
panic(err)
}
c.encryptedPassword = base64.StdEncoding.EncodeToString(encryptedPassword)
}
@ -393,7 +410,9 @@ func (c *Connection) runClient(serverName string) error {
sendMessage(c.encryptedPassword, connection)
// wait for relay go
receiveMessage(connection)
if id == 0 {
fmt.Fprintf(os.Stderr, "\nSending (->%s@%s)..\n", publicKeyRecipient, message)
}
// wait for pipe to be made
time.Sleep(100 * time.Millisecond)
// Write data from file
@ -453,6 +472,7 @@ func (c *Connection) runClient(serverName string) error {
fType = "folder"
fName = fName[:len(fName)-4]
}
fmt.Fprintf(os.Stderr, "Incoming file from "+publicKeySender+"\n")
if _, err := os.Stat(path.Join(c.Path, c.File.Name)); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Receiving %s (%s) into: %s\n", fType, humanize.Bytes(uint64(c.File.Size)), fName)
} else {
@ -470,7 +490,6 @@ func (c *Connection) runClient(serverName string) error {
fmt.Fprintf(os.Stderr, "Will not overwrite file!")
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "incoming file from "+publicKeySender+"\n")
getOK := "y"
if !c.Yes {
getOK = getInput("ok? (y/n): ")
@ -502,12 +521,22 @@ func (c *Connection) runClient(serverName string) error {
sendMessage("not ok", connection)
} else {
sendMessage("ok", connection)
c.encryptedPassword = receiveMessage(connection)
log.Debugf("[%d] got encrypted passphrase: %s", id, c.encryptedPassword)
encryptedPassword := receiveMessage(connection)
log.Debugf("[%d] got encrypted passphrase: %s", id, encryptedPassword)
encryptedPasswordBytes, err := base64.StdEncoding.DecodeString(encryptedPassword)
if err != nil {
panic(err)
}
decryptedPassphrase, err := c.keypair.Decrypt(encryptedPasswordBytes, publicKeySender)
c.encryptedPassword = string(decryptedPassphrase)
log.Debugf("decrypted password to: %s", c.encryptedPassword)
if err != nil {
panic(err)
}
sendMessage("ok", connection)
log.Debug("receive file")
if id == 0 {
fmt.Fprintf(os.Stderr, "\nReceiving (<-%s)..\n", sendersAddress)
fmt.Fprintf(os.Stderr, "\nReceiving (<-%s@%s)..\n", publicKeySender, sendersAddress)
}
responses.Lock()
responses.startTime = time.Now()
@ -562,15 +591,20 @@ func (c *Connection) runClient(serverName string) error {
log.Debugf("Code: [%s]", c.Code)
if c.DontEncrypt {
if err := CopyFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name)); err != nil {
log.Error(err)
return err
}
} else {
log.Debugf("is encrypted: %+v", c.File.IsEncrypted)
if c.File.IsEncrypted {
if err := DecryptFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name), c.Code); err != nil {
log.Debugf("decrypting file with [%s]", c.encryptedPassword)
if err := DecryptFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name), c.encryptedPassword); err != nil {
log.Error(err)
return errors.Wrap(err, "Problem decrypting file")
}
} else {
if err := CopyFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name)); err != nil {
log.Error(err)
return errors.Wrap(err, "Problem copying file")
}
}
@ -587,6 +621,7 @@ func (c *Connection) runClient(serverName string) error {
log.Debugf("\n\n\nrelayed hash: [%s]", c.File.Hash)
if c.File.Hash != fileHash {
log.Flush()
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
}
if c.File.IsDir { // if the file was originally a dir

View File

@ -91,14 +91,10 @@ func (r *Relay) runServer() {
}
func (r *Relay) listenerThread(id int, wg *sync.WaitGroup) {
logger := log.WithFields(log.Fields{
"function": "listenerThread:" + strconv.Itoa(27000+id),
})
defer wg.Done()
if err := r.listener(id); err != nil {
logger.Error(err)
return
}
}
@ -179,7 +175,11 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
logger.Debug("telling sender ok")
sendMessage(receiversAddress, connection)
sendMessage(receiversPublicKey, connection)
// TODO ASK FOR OKAY HERE TOO
isokay := receiveMessage(connection)
logger.Debug(isokay)
logger.Debug("waiting for encrypted passphrase")
encryptedPassphrase := receiveMessage(connection)
r.connections.Lock()
@ -214,6 +214,10 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
delete(r.connections.receiver, key)
delete(r.connections.metadata, key)
delete(r.connections.potentialReceivers, key)
delete(r.connections.spublicKey, key)
delete(r.connections.rpublicKey, key)
delete(r.connections.receiverReady, key)
delete(r.connections.passphrase, key)
r.connections.Unlock()
logger.Debug("deleted sender and receiver")
case "r", "c": // receiver
@ -319,13 +323,22 @@ func receiveMessage(connection net.Conn) string {
"ip": connection.RemoteAddr().String(),
})
messageByte := make([]byte, BUFFERSIZE)
err := connection.SetDeadline(time.Now().Add(60 * time.Minute))
err := connection.SetReadDeadline(time.Now().Add(60 * time.Minute))
if err != nil {
logger.Warn(err)
}
err = connection.SetDeadline(time.Now().Add(60 * time.Minute))
if err != nil {
logger.Warn(err)
}
err = connection.SetWriteDeadline(time.Now().Add(60 * time.Minute))
if err != nil {
logger.Warn(err)
}
_, err = connection.Read(messageByte)
if err != nil {
logger.Warn("read deadline, no response")
logger.Warn(err)
logger.Warn("no response")
return ""
}
return strings.TrimRight(string(messageByte), ":")

View File

@ -3,11 +3,11 @@
src="https://user-images.githubusercontent.com/6550035/31846899-2b8a7034-b5cf-11e7-9643-afe552226c59.png"
width="100%" border="0" alt="croc">
<br>
<a href="https://travis-ci.org/schollz/croc"><img src="https://travis-ci.org/schollz/croc.svg?branch=master" alt="Build Status"></a>
<a href="https://github.com/schollz/croc/releases/latest"><img src="https://img.shields.io/badge/version-1.0.0-brightgreen.svg?style=flat-square" alt="Version"></a>
<a href="https://goreportcard.com/report/github.com/schollz/croc"><img src="https://goreportcard.com/badge/github.com/schollz/croc" alt="Go Report Card"></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).
@ -54,33 +54,6 @@ 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).
## Transfering files between local computers
Its even easier if you want to transfer files between two computers on the same network.
**Sender:**
```
$ croc -send some-file-or-folder -local
```
**Receiver:**
```
$ croc -local
```
Yes, when you run locally you don't even need to use a code. When you run locally, the *croc* receiver will use UDP broadcast packets to automatically find the correct IP address and code to use to transfer the file. When run locally, there is also no encryption so it is even faster.
**Sender:**
![Running locally](https://raw.githubusercontent.com/schollz/croc/master/logo/1.gif)
**Receiver:**
![Running locally](https://raw.githubusercontent.com/schollz/croc/master/logo/2.gif)
## Using *croc* in pipes
You can easily use *croc* in pipes when you need to send data through stdin or get data from stdout.
@ -111,15 +84,15 @@ Or, you can [install Go](https://golang.org/dl/) and build from source with `go
# 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.
*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.
In *croc*, code phrase is 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 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.
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 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).
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**

View File

@ -1 +0,0 @@
Some simple text to see if it works

View File

@ -1 +0,0 @@
More data to see if it 100% works