mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
works
This commit is contained in:
parent
eb3251a93f
commit
fa1b2af314
3 changed files with 89 additions and 23 deletions
|
@ -3,7 +3,6 @@ package croc
|
|||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
|
@ -276,17 +275,7 @@ func (c *Croc) spawnConnections(role int) (err error) {
|
|||
err = c.dialUp()
|
||||
if err == nil {
|
||||
if role == 1 {
|
||||
c.cs.Lock()
|
||||
c.cs.channel.Update = true
|
||||
c.cs.channel.finishedHappy = true
|
||||
c.cs.channel.FileReceived = true
|
||||
log.Debugf("got file successfully")
|
||||
errWrite := c.cs.channel.ws.WriteJSON(c.cs.channel)
|
||||
if errWrite != nil {
|
||||
log.Error(errWrite)
|
||||
}
|
||||
c.cs.channel.Update = false
|
||||
c.cs.Unlock()
|
||||
err = c.processReceivedFile()
|
||||
}
|
||||
} else {
|
||||
log.Error(err)
|
||||
|
@ -303,18 +292,13 @@ func (c *Croc) dialUp() (err error) {
|
|||
c.cs.Unlock()
|
||||
errorChan := make(chan error, len(ports))
|
||||
|
||||
// generate a receive filename
|
||||
var f *os.File
|
||||
f, err = ioutil.TempFile(".", "croc-received")
|
||||
if err != nil {
|
||||
return
|
||||
if role == 1 {
|
||||
// generate a receive filename
|
||||
c.crocFileEncrypted = tempFileName("croc-received")
|
||||
}
|
||||
receiveFileName := f.Name()
|
||||
f.Close()
|
||||
os.Remove(receiveFileName)
|
||||
|
||||
for i, port := range ports {
|
||||
go func(channel, uuid, port string, i int, errorChan chan error, receiveFileName string) {
|
||||
go func(channel, uuid, port string, i int, errorChan chan error) {
|
||||
if i == 0 {
|
||||
log.Debug("dialing up")
|
||||
}
|
||||
|
@ -390,11 +374,12 @@ func (c *Croc) dialUp() (err error) {
|
|||
c.cs.Unlock()
|
||||
log.Debug("receive file")
|
||||
}()
|
||||
receiveFileName += "." + strconv.Itoa(i)
|
||||
receiveFileName := c.crocFileEncrypted + "." + strconv.Itoa(i)
|
||||
log.Debugf("receiving file into %s", receiveFileName)
|
||||
err = receiveFile(receiveFileName, i, connection)
|
||||
}
|
||||
errorChan <- err
|
||||
}(channel, uuid, port, i, errorChan, receiveFileName)
|
||||
}(channel, uuid, port, i, errorChan)
|
||||
}
|
||||
|
||||
// collect errors
|
||||
|
|
71
src/files.go
71
src/files.go
|
@ -7,6 +7,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
|
@ -148,3 +149,73 @@ func (c *Croc) getFilesReady() (err error) {
|
|||
}()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Croc) processReceivedFile() (err error) {
|
||||
// cat the file received
|
||||
c.cs.Lock()
|
||||
defer c.cs.Unlock()
|
||||
c.cs.channel.FileReceived = true
|
||||
defer func() {
|
||||
c.cs.channel.Update = true
|
||||
errWrite := c.cs.channel.ws.WriteJSON(c.cs.channel)
|
||||
if errWrite != nil {
|
||||
log.Error(errWrite)
|
||||
return
|
||||
}
|
||||
c.cs.channel.Update = false
|
||||
}()
|
||||
|
||||
filesToCat := make([]string, len(c.cs.channel.Ports))
|
||||
for i := range c.cs.channel.Ports {
|
||||
filesToCat[i] = c.crocFileEncrypted + "." + strconv.Itoa(i)
|
||||
log.Debugf("going to cat file %s", filesToCat[i])
|
||||
}
|
||||
|
||||
// defer os.Remove(c.crocFile)
|
||||
log.Debugf("catting file into %s", c.crocFile)
|
||||
err = catFiles(filesToCat, c.crocFileEncrypted, true)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// unencrypt
|
||||
c.crocFile = tempFileName("croc-unencrypted")
|
||||
var passphrase []byte
|
||||
passphrase, err = c.cs.channel.Pake.SessionKey()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
err = decryptFile(c.crocFileEncrypted, c.crocFile, passphrase)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
os.Remove(c.crocFileEncrypted)
|
||||
|
||||
// check hash
|
||||
log.Debug("checking hash")
|
||||
var hashString string
|
||||
hashString, err = hashFile(c.crocFile)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
if hashString == c.cs.channel.fileMetaData.Hash {
|
||||
log.Debug("hashes match")
|
||||
} else {
|
||||
err = errors.Errorf("hashes do not match, %s != %s", c.cs.channel.fileMetaData.Hash, hashString)
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// unzip file
|
||||
err = unzipFile(c.crocFile, ".")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
c.cs.channel.finishedHappy = true
|
||||
return
|
||||
}
|
||||
|
|
10
src/utils.go
10
src/utils.go
|
@ -5,6 +5,7 @@ import (
|
|||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"os"
|
||||
|
@ -202,3 +203,12 @@ func exists(name string) bool {
|
|||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func tempFileName(prefix string) string {
|
||||
var f *os.File
|
||||
f, _ = ioutil.TempFile(".", prefix)
|
||||
name := f.Name()
|
||||
f.Close()
|
||||
os.Remove(name)
|
||||
return name
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue