cleanup after returning

This commit is contained in:
Zack Scholl 2018-07-01 14:50:40 -07:00
parent d255f3bba0
commit ebad469554
3 changed files with 18 additions and 7 deletions

View File

@ -25,7 +25,7 @@ func (c *Croc) Relay() error {
// Send will take an existing file or folder and send it through the croc relay // Send will take an existing file or folder and send it through the croc relay
func (c *Croc) Send(fname string, codePhrase string) (err error) { func (c *Croc) Send(fname string, codePhrase string) (err error) {
// prepare code phrase // prepare code phrase
defer c.cleanup()
c.cs.Lock() c.cs.Lock()
c.cs.channel.codePhrase = codePhrase c.cs.channel.codePhrase = codePhrase
if len(codePhrase) == 0 { if len(codePhrase) == 0 {
@ -125,6 +125,7 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) {
// Receive will receive something through the croc relay // Receive will receive something through the croc relay
func (c *Croc) Receive(codePhrase string) (err error) { func (c *Croc) Receive(codePhrase string) (err error) {
defer c.cleanup()
if !c.NoLocal { if !c.NoLocal {
// try to discovery codephrase and server through peer network // try to discovery codephrase and server through peer network
discovered, errDiscover := peerdiscovery.Discover(peerdiscovery.Settings{ discovered, errDiscover := peerdiscovery.Discover(peerdiscovery.Settings{

View File

@ -25,7 +25,6 @@ var isPrinted bool
func (c *Croc) client(role int, channel string) (err error) { func (c *Croc) client(role int, channel string) (err error) {
defer log.Flush() defer log.Flush()
defer c.cleanup()
// initialize the channel data for this client // initialize the channel data for this client
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
@ -64,7 +63,7 @@ func (c *Croc) client(role int, channel string) (err error) {
log.Debugf("sender read error:", err) log.Debugf("sender read error:", err)
return return
} }
log.Debugf("recv: %s", cd.String2()) // log.Debugf("recv: %s", cd.String2())
err = c.processState(cd) err = c.processState(cd)
if err != nil { if err != nil {
log.Warn(err) log.Warn(err)

View File

@ -15,6 +15,10 @@ import (
) )
func (c *Croc) processFile(src string) (err error) { func (c *Croc) processFile(src string) (err error) {
log.Debug("processing file")
defer func() {
log.Debug("finished processing file")
}()
fd := FileMetaData{} fd := FileMetaData{}
// pathToFile and filename are the files that should be used internally // pathToFile and filename are the files that should be used internally
@ -84,9 +88,12 @@ func (c *Croc) processFile(src string) (err error) {
} }
func (c *Croc) getFilesReady() (err error) { func (c *Croc) getFilesReady() (err error) {
log.Debug("getting files ready")
defer func() {
log.Debug("files ready")
}()
c.cs.Lock() c.cs.Lock()
defer c.cs.Unlock() defer c.cs.Unlock()
log.Debug("getting files ready")
c.cs.channel.notSentMetaData = true c.cs.channel.notSentMetaData = true
// send metadata // send metadata
@ -104,26 +111,31 @@ func (c *Croc) getFilesReady() (err error) {
var passphrase []byte var passphrase []byte
passphrase, err = c.cs.channel.Pake.SessionKey() passphrase, err = c.cs.channel.Pake.SessionKey()
if err != nil { if err != nil {
log.Error(err)
return return
} }
// encrypt file data // encrypt file data
c.crocFileEncrypted = tempFileName("croc-encrypted") c.crocFileEncrypted = tempFileName("croc-encrypted")
err = encryptFile(c.crocFile, c.crocFileEncrypted, passphrase) err = encryptFile(c.crocFile, c.crocFileEncrypted, passphrase)
if err != nil { if err != nil {
log.Error(err)
return return
} }
// remove the unencrypted versoin // remove the unencrypted versoin
if err = os.Remove(c.crocFile); err != nil { if err = os.Remove(c.crocFile); err != nil {
log.Error(err)
return return
} }
c.cs.channel.fileMetaData.IsEncrypted = true c.cs.channel.fileMetaData.IsEncrypted = true
// split into pieces to send // split into pieces to send
log.Debugf("splitting %s", c.crocFileEncrypted) log.Debugf("splitting %s", c.crocFileEncrypted)
if err = splitFile(c.crocFileEncrypted, len(c.cs.channel.Ports)); err != nil { if err = splitFile(c.crocFileEncrypted, len(c.cs.channel.Ports)); err != nil {
log.Error(err)
return return
} }
// remove the file now since we still have pieces // remove the file now since we still have pieces
if err = os.Remove(c.crocFileEncrypted); err != nil { if err = os.Remove(c.crocFileEncrypted); err != nil {
log.Error(err)
return return
} }
@ -131,20 +143,19 @@ func (c *Croc) getFilesReady() (err error) {
var metaDataBytes []byte var metaDataBytes []byte
metaDataBytes, err = json.Marshal(c.cs.channel.fileMetaData) metaDataBytes, err = json.Marshal(c.cs.channel.fileMetaData)
if err != nil { if err != nil {
log.Error(err)
return return
} }
c.cs.channel.EncryptedFileMetaData = encrypt(metaDataBytes, passphrase) c.cs.channel.EncryptedFileMetaData = encrypt(metaDataBytes, passphrase)
c.cs.channel.Update = true c.cs.channel.Update = true
log.Debugf("updating channel") log.Debugf("updating channel with file information")
errWrite := c.cs.channel.ws.WriteJSON(c.cs.channel) errWrite := c.cs.channel.ws.WriteJSON(c.cs.channel)
if errWrite != nil { if errWrite != nil {
log.Error(errWrite) log.Error(errWrite)
} }
c.cs.channel.Update = false c.cs.channel.Update = false
go func() { go func() {
// encrypt the files
// TODO
c.cs.Lock() c.cs.Lock()
c.cs.channel.fileReady = true c.cs.channel.fileReady = true
c.cs.Unlock() c.cs.Unlock()