mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 21:30:16 +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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -276,17 +275,7 @@ func (c *Croc) spawnConnections(role int) (err error) {
|
||||||
err = c.dialUp()
|
err = c.dialUp()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if role == 1 {
|
if role == 1 {
|
||||||
c.cs.Lock()
|
err = c.processReceivedFile()
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
@ -303,18 +292,13 @@ func (c *Croc) dialUp() (err error) {
|
||||||
c.cs.Unlock()
|
c.cs.Unlock()
|
||||||
errorChan := make(chan error, len(ports))
|
errorChan := make(chan error, len(ports))
|
||||||
|
|
||||||
// generate a receive filename
|
if role == 1 {
|
||||||
var f *os.File
|
// generate a receive filename
|
||||||
f, err = ioutil.TempFile(".", "croc-received")
|
c.crocFileEncrypted = tempFileName("croc-received")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
receiveFileName := f.Name()
|
|
||||||
f.Close()
|
|
||||||
os.Remove(receiveFileName)
|
|
||||||
|
|
||||||
for i, port := range ports {
|
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 {
|
if i == 0 {
|
||||||
log.Debug("dialing up")
|
log.Debug("dialing up")
|
||||||
}
|
}
|
||||||
|
@ -390,11 +374,12 @@ func (c *Croc) dialUp() (err error) {
|
||||||
c.cs.Unlock()
|
c.cs.Unlock()
|
||||||
log.Debug("receive file")
|
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)
|
err = receiveFile(receiveFileName, i, connection)
|
||||||
}
|
}
|
||||||
errorChan <- err
|
errorChan <- err
|
||||||
}(channel, uuid, port, i, errorChan, receiveFileName)
|
}(channel, uuid, port, i, errorChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect errors
|
// collect errors
|
||||||
|
|
71
src/files.go
71
src/files.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/cihub/seelog"
|
log "github.com/cihub/seelog"
|
||||||
|
@ -148,3 +149,73 @@ func (c *Croc) getFilesReady() (err error) {
|
||||||
}()
|
}()
|
||||||
return
|
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"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -202,3 +203,12 @@ func exists(name string) bool {
|
||||||
}
|
}
|
||||||
return true
|
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