2017-10-20 23:51:03 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-23 19:18:35 +03:00
|
|
|
"encoding/base64"
|
2017-10-20 23:51:03 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-04-13 19:31:43 +03:00
|
|
|
"io/ioutil"
|
2017-10-20 23:51:03 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
2017-12-15 02:32:15 +03:00
|
|
|
"os/signal"
|
2017-10-21 21:45:44 +03:00
|
|
|
"path"
|
2018-04-24 09:45:40 +03:00
|
|
|
"path/filepath"
|
2017-10-20 23:51:03 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-12-15 02:32:15 +03:00
|
|
|
"syscall"
|
2017-10-20 23:51:03 +03:00
|
|
|
"time"
|
|
|
|
|
2018-06-26 17:43:22 +03:00
|
|
|
"github.com/briandowns/spinner"
|
2018-02-07 11:24:44 +03:00
|
|
|
"github.com/dustin/go-humanize"
|
2018-06-26 19:09:25 +03:00
|
|
|
"github.com/fatih/color"
|
2018-06-25 05:21:56 +03:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/schollz/croc/keypair"
|
|
|
|
"github.com/schollz/croc/randomstring"
|
2018-04-23 07:42:05 +03:00
|
|
|
"github.com/schollz/peerdiscovery"
|
2017-10-26 21:23:50 +03:00
|
|
|
"github.com/schollz/progressbar"
|
2017-12-15 02:32:15 +03:00
|
|
|
tarinator "github.com/schollz/tarinator-go"
|
2017-10-22 19:24:55 +03:00
|
|
|
|
2018-04-23 07:42:05 +03:00
|
|
|
log "github.com/cihub/seelog"
|
2017-10-20 23:51:03 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
Server string
|
|
|
|
File FileMetaData
|
|
|
|
NumberOfConnections int
|
|
|
|
Code string
|
|
|
|
HashedCode string
|
2017-10-22 07:05:08 +03:00
|
|
|
Path string
|
2017-10-20 23:51:03 +03:00
|
|
|
IsSender bool
|
2017-10-22 07:05:08 +03:00
|
|
|
AskPath bool
|
2018-06-26 18:55:30 +03:00
|
|
|
NoLocal bool
|
2017-10-20 23:51:03 +03:00
|
|
|
Debug bool
|
|
|
|
DontEncrypt bool
|
2018-04-14 01:31:03 +03:00
|
|
|
Yes bool
|
2018-04-22 15:16:16 +03:00
|
|
|
Local bool
|
2018-04-14 00:57:34 +03:00
|
|
|
UseStdout bool
|
2017-10-21 01:18:06 +03:00
|
|
|
Wait bool
|
2017-10-26 21:23:50 +03:00
|
|
|
bar *progressbar.ProgressBar
|
2017-10-20 23:51:03 +03:00
|
|
|
rate int
|
2018-06-23 19:18:35 +03:00
|
|
|
keypair keypair.KeyPair
|
|
|
|
encryptedPassword string
|
2018-06-26 17:43:22 +03:00
|
|
|
spinner *spinner.Spinner
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileMetaData struct {
|
2018-04-13 19:31:43 +03:00
|
|
|
Name string
|
|
|
|
Size int
|
|
|
|
Hash string
|
|
|
|
Path string
|
|
|
|
IsDir bool
|
|
|
|
IsEncrypted bool
|
|
|
|
DeleteAfterSending bool
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
2017-10-22 01:28:06 +03:00
|
|
|
const (
|
2017-10-22 13:14:46 +03:00
|
|
|
crocReceiveDir = "croc_received"
|
2017-10-22 01:28:06 +03:00
|
|
|
tmpTarGzFileName = "to_send.tmp.tar.gz"
|
|
|
|
)
|
|
|
|
|
2018-02-09 05:17:25 +03:00
|
|
|
func NewConnection(config *AppConfig) (*Connection, error) {
|
2018-04-23 07:42:05 +03:00
|
|
|
defer log.Flush()
|
2017-10-20 23:51:03 +03:00
|
|
|
c := new(Connection)
|
2018-02-09 05:17:25 +03:00
|
|
|
c.Debug = config.Debug
|
|
|
|
c.DontEncrypt = config.DontEncrypt
|
|
|
|
c.Wait = config.Wait
|
|
|
|
c.Server = config.Server
|
|
|
|
c.Code = config.Code
|
|
|
|
c.NumberOfConnections = config.NumberOfConnections
|
2018-04-14 00:57:34 +03:00
|
|
|
c.UseStdout = config.UseStdout
|
2018-04-14 01:31:03 +03:00
|
|
|
c.Yes = config.Yes
|
2018-02-09 05:17:25 +03:00
|
|
|
c.rate = config.Rate
|
2018-04-25 10:20:09 +03:00
|
|
|
c.Local = config.Local
|
2018-06-26 18:55:30 +03:00
|
|
|
c.NoLocal = config.Local
|
2018-06-25 05:21:56 +03:00
|
|
|
|
|
|
|
// make or load keypair
|
|
|
|
homedir, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
pathToCrocConfig := path.Join(homedir, ".config", "croc", "keys")
|
|
|
|
if _, errExists := os.Stat(pathToCrocConfig); os.IsNotExist(errExists) {
|
|
|
|
// path/to/whatever does not exist
|
|
|
|
os.MkdirAll(path.Join(homedir, ".config", "croc"), 0700)
|
|
|
|
keys, _ := keypair.New()
|
|
|
|
err = ioutil.WriteFile(pathToCrocConfig, []byte(keys.String()), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keypairBytes, err := ioutil.ReadFile(pathToCrocConfig)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
c.keypair, err = keypair.Load(string(keypairBytes))
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2018-06-24 17:32:32 +03:00
|
|
|
if c.Debug {
|
|
|
|
SetLogLevel("debug")
|
|
|
|
} else {
|
|
|
|
SetLogLevel("error")
|
|
|
|
}
|
2018-04-25 10:20:09 +03:00
|
|
|
if c.Local {
|
|
|
|
c.Yes = true
|
|
|
|
c.DontEncrypt = true
|
|
|
|
}
|
2018-04-14 11:29:39 +03:00
|
|
|
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
config.File = "stdin"
|
|
|
|
}
|
2018-02-09 05:17:25 +03:00
|
|
|
if len(config.File) > 0 {
|
2018-04-24 09:45:40 +03:00
|
|
|
config.File = filepath.Clean(config.File)
|
2018-04-13 19:31:43 +03:00
|
|
|
if config.File == "stdin" {
|
2018-06-24 17:44:25 +03:00
|
|
|
c.Yes = true
|
2018-04-14 11:29:39 +03:00
|
|
|
f, err := ioutil.TempFile(".", "croc-stdin-")
|
2018-04-13 19:31:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
config.File = f.Name()
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
c.File.DeleteAfterSending = true
|
|
|
|
}
|
2017-10-22 00:52:03 +03:00
|
|
|
// check wether the file is a dir
|
2018-02-09 05:17:25 +03:00
|
|
|
info, err := os.Stat(config.File)
|
2017-10-22 00:52:03 +03:00
|
|
|
if err != nil {
|
2018-06-26 17:50:14 +03:00
|
|
|
log.Error(err)
|
2017-10-22 19:24:55 +03:00
|
|
|
return c, err
|
2017-10-22 00:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsDir() { // if our file is a dir
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner = spinner.New(spinner.CharSets[24], 100*time.Millisecond) // Build our new spinner
|
|
|
|
c.spinner.Suffix = " Compressing folder.."
|
|
|
|
c.spinner.Start()
|
2017-10-22 00:52:03 +03:00
|
|
|
|
|
|
|
// we "tarify" the file
|
2018-06-24 17:30:35 +03:00
|
|
|
log.Debugf("compressing %s to %s", config.File, path.Base(config.File)+".tar")
|
2018-02-09 05:17:25 +03:00
|
|
|
err = tarinator.Tarinate([]string{config.File}, path.Base(config.File)+".tar")
|
2017-10-22 00:52:03 +03:00
|
|
|
if err != nil {
|
2017-10-22 19:24:55 +03:00
|
|
|
return c, err
|
2017-10-22 00:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// now, we change the target file name to match the new archive created
|
2018-02-09 05:17:25 +03:00
|
|
|
config.File = path.Base(config.File) + ".tar"
|
2017-10-22 01:55:34 +03:00
|
|
|
// we set the value IsDir to true
|
|
|
|
c.File.IsDir = true
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner.Stop()
|
2017-10-22 00:52:03 +03:00
|
|
|
}
|
2018-06-26 17:48:11 +03:00
|
|
|
c.File.Path, c.File.Name = path.Split(filepath.ToSlash(config.File))
|
2018-04-25 08:58:35 +03:00
|
|
|
c.File.Size, _ = FileSize(config.File)
|
2017-10-20 23:51:03 +03:00
|
|
|
c.IsSender = true
|
|
|
|
} else {
|
|
|
|
c.IsSender = false
|
2018-02-09 05:17:25 +03:00
|
|
|
c.AskPath = config.PathSpec
|
|
|
|
c.Path = config.Path
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2018-06-23 20:02:46 +03:00
|
|
|
c.File.IsEncrypted = true
|
|
|
|
if c.DontEncrypt {
|
|
|
|
c.File.IsEncrypted = false
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
|
2017-10-22 19:24:55 +03:00
|
|
|
return c, nil
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:32:15 +03:00
|
|
|
func (c *Connection) cleanup() {
|
|
|
|
log.Debug("cleaning")
|
2018-04-23 07:42:05 +03:00
|
|
|
if c.Debug {
|
|
|
|
return
|
|
|
|
}
|
2018-04-14 01:21:15 +03:00
|
|
|
for id := 0; id <= 8; id++ {
|
|
|
|
err := os.Remove(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
|
|
|
|
if err == nil {
|
|
|
|
log.Debugf("removed %s", path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
|
|
|
|
}
|
2017-12-15 02:32:15 +03:00
|
|
|
}
|
|
|
|
os.Remove(path.Join(c.Path, c.File.Name+".enc"))
|
|
|
|
}
|
|
|
|
|
2017-10-20 23:51:03 +03:00
|
|
|
func (c *Connection) Run() error {
|
2017-12-15 02:32:15 +03:00
|
|
|
// catch the Ctl+C
|
|
|
|
catchCtlC := make(chan os.Signal, 2)
|
|
|
|
signal.Notify(catchCtlC, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-catchCtlC
|
|
|
|
c.cleanup()
|
|
|
|
fmt.Println("\nExiting")
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
defer c.cleanup()
|
|
|
|
|
2018-04-25 06:54:50 +03:00
|
|
|
// calculate number of threads
|
|
|
|
c.NumberOfConnections = MAX_NUMBER_THREADS
|
|
|
|
if c.IsSender {
|
|
|
|
fsize, err := FileSize(path.Join(c.File.Path, c.File.Name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fsize < MAX_NUMBER_THREADS*BUFFERSIZE {
|
|
|
|
c.NumberOfConnections = 1
|
|
|
|
log.Debug("forcing single thread")
|
2018-04-23 10:22:59 +03:00
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 06:54:50 +03:00
|
|
|
runClientError := make(chan error)
|
2018-04-24 17:46:07 +03:00
|
|
|
if c.IsSender {
|
|
|
|
if c.Code == "" {
|
|
|
|
c.Code = GetRandomName()
|
|
|
|
}
|
2018-04-25 08:58:35 +03:00
|
|
|
if c.File.IsDir {
|
|
|
|
fmt.Fprintf(os.Stderr, "Sending %s folder named '%s'\n", humanize.Bytes(uint64(c.File.Size)), c.File.Name[:len(c.File.Name)-4])
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "Sending %s file named '%s'\n", humanize.Bytes(uint64(c.File.Size)), c.File.Name)
|
|
|
|
}
|
2018-06-26 19:09:25 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Code is: ")
|
|
|
|
color.New(color.FgHiGreen, color.Bold).Fprint(os.Stderr, c.Code)
|
|
|
|
fmt.Fprintf(os.Stderr, "\n\n")
|
|
|
|
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner = spinner.New(spinner.CharSets[24], 100*time.Millisecond) // Build our new spinner
|
|
|
|
c.spinner.Suffix = " Waiting for recipient.."
|
|
|
|
c.spinner.Start()
|
2018-04-23 07:42:05 +03:00
|
|
|
|
2018-04-24 17:46:07 +03:00
|
|
|
log.Debug("starting relay in case local connections")
|
2018-04-23 07:42:05 +03:00
|
|
|
relay := NewRelay(&AppConfig{
|
|
|
|
Debug: c.Debug,
|
|
|
|
})
|
|
|
|
go relay.Run()
|
2018-04-24 17:46:07 +03:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2018-04-23 07:42:05 +03:00
|
|
|
|
2017-10-20 23:51:03 +03:00
|
|
|
// get file hash
|
|
|
|
var err error
|
2017-10-21 21:45:44 +03:00
|
|
|
c.File.Hash, err = HashFile(path.Join(c.File.Path, c.File.Name))
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// get file size
|
2018-06-23 19:18:35 +03:00
|
|
|
c.File.Size, err = FileSize(c.File.Name)
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-22 01:28:06 +03:00
|
|
|
|
2018-06-26 18:55:30 +03:00
|
|
|
if c.Server != "localhost" && !c.NoLocal {
|
2018-06-24 16:55:43 +03:00
|
|
|
// broadcast local connection from sender
|
|
|
|
log.Debug("settings payload to ", c.Code)
|
|
|
|
go func() {
|
|
|
|
log.Debug("listening for local croc relay...")
|
|
|
|
go peerdiscovery.Discover(peerdiscovery.Settings{
|
|
|
|
Limit: 1,
|
|
|
|
TimeLimit: 600 * time.Second,
|
|
|
|
Delay: 50 * time.Millisecond,
|
|
|
|
Payload: []byte(c.Code),
|
|
|
|
})
|
|
|
|
runClientError <- c.runClient("localhost")
|
|
|
|
}()
|
|
|
|
}
|
2018-04-25 06:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("checking code validity")
|
|
|
|
if len(c.Code) == 0 && !c.IsSender {
|
|
|
|
log.Debug("Finding local croc relay...")
|
2018-06-23 21:08:02 +03:00
|
|
|
discovered, errDiscover := peerdiscovery.Discover(peerdiscovery.Settings{
|
2018-04-25 06:54:50 +03:00
|
|
|
Limit: 1,
|
|
|
|
TimeLimit: 1 * time.Second,
|
|
|
|
Delay: 50 * time.Millisecond,
|
|
|
|
Payload: []byte(c.Code),
|
|
|
|
})
|
2018-06-23 21:08:02 +03:00
|
|
|
if errDiscover != nil {
|
|
|
|
log.Debug(errDiscover)
|
|
|
|
}
|
2018-04-25 06:54:50 +03:00
|
|
|
if len(discovered) > 0 {
|
2018-06-25 18:05:56 +03:00
|
|
|
log.Debugf("discovered %s on %s", discovered[0].Payload, discovered[0].Address)
|
|
|
|
_, connectTimeout := net.DialTimeout("tcp", discovered[0].Address+":27001", 1*time.Second)
|
|
|
|
if connectTimeout == nil {
|
|
|
|
log.Debug("connected")
|
|
|
|
c.Server = discovered[0].Address
|
|
|
|
log.Debug(discovered[0].Address)
|
|
|
|
c.Code = string(discovered[0].Payload)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
} else {
|
|
|
|
log.Debug("could not connect")
|
|
|
|
c.Code = getInput("Enter receive code: ")
|
|
|
|
}
|
2018-04-22 15:16:16 +03:00
|
|
|
} else {
|
2018-04-25 06:54:50 +03:00
|
|
|
c.Code = getInput("Enter receive code: ")
|
|
|
|
log.Debug("changed code to ", c.Code)
|
2018-04-22 15:16:16 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 10:20:09 +03:00
|
|
|
if !c.Local {
|
|
|
|
go func() { runClientError <- c.runClient(c.Server) }()
|
|
|
|
}
|
2018-04-25 06:54:50 +03:00
|
|
|
return <-runClientError
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// runClient spawns threads for parallel uplink/downlink via TCP
|
2018-04-24 17:46:07 +03:00
|
|
|
func (c *Connection) runClient(serverName string) error {
|
2018-06-23 20:02:46 +03:00
|
|
|
|
2017-10-20 23:51:03 +03:00
|
|
|
c.HashedCode = Hash(c.Code)
|
2018-04-23 07:42:05 +03:00
|
|
|
c.NumberOfConnections = MAX_NUMBER_THREADS
|
2017-10-20 23:51:03 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(c.NumberOfConnections)
|
|
|
|
|
|
|
|
if !c.Debug {
|
2018-06-24 16:14:07 +03:00
|
|
|
c.bar = progressbar.NewOptions(c.File.Size, progressbar.OptionSetWriter(os.Stderr))
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2017-10-24 17:43:35 +03:00
|
|
|
type responsesStruct struct {
|
|
|
|
gotTimeout bool
|
|
|
|
gotOK bool
|
|
|
|
gotResponse bool
|
|
|
|
gotConnectionInUse bool
|
|
|
|
notPresent bool
|
2017-11-05 17:07:35 +03:00
|
|
|
startTime time.Time
|
2017-10-24 17:43:35 +03:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
responses := new(responsesStruct)
|
2017-11-05 17:07:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.startTime = time.Now()
|
|
|
|
responses.Unlock()
|
2018-06-23 20:57:25 +03:00
|
|
|
var okToContinue bool
|
|
|
|
fileTransfered := false
|
2017-10-20 23:51:03 +03:00
|
|
|
for id := 0; id < c.NumberOfConnections; id++ {
|
|
|
|
go func(id int) {
|
|
|
|
defer wg.Done()
|
|
|
|
port := strconv.Itoa(27001 + id)
|
2018-06-25 16:47:37 +03:00
|
|
|
log.Debugf("connecting to %s", serverName+":"+port)
|
2018-04-24 17:46:07 +03:00
|
|
|
connection, err := net.Dial("tcp", serverName+":"+port)
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
2018-04-24 17:46:07 +03:00
|
|
|
if serverName == "cowyo.com" {
|
2018-04-25 10:20:09 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\nCheck http://bit.ly/croc-relay to see if the public server is down or contact the webmaster: @yakczar")
|
2017-10-21 22:51:19 +03:00
|
|
|
} else {
|
2018-04-24 17:46:07 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\nCould not connect to relay %s\n", serverName)
|
2017-10-21 22:51:19 +03:00
|
|
|
}
|
2018-04-25 10:20:09 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Use --local to run locally")
|
2017-10-21 22:51:19 +03:00
|
|
|
os.Exit(1)
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
defer connection.Close()
|
2018-06-23 20:02:46 +03:00
|
|
|
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)
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
|
|
|
|
message := receiveMessage(connection)
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debugf("relay says: %s", message)
|
2017-10-20 23:51:03 +03:00
|
|
|
if c.IsSender {
|
2018-04-25 06:54:50 +03:00
|
|
|
log.Debugf("telling relay (%s): %s", c.Server, "s."+c.Code)
|
2017-10-20 23:51:03 +03:00
|
|
|
metaData, err := json.Marshal(c.File)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-04-25 06:54:50 +03:00
|
|
|
encryptedMetaData, salt, iv := Encrypt(metaData, c.Code)
|
2018-06-23 19:18:35 +03:00
|
|
|
sendMessage("s."+c.keypair.Public+"."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
|
2017-10-20 23:51:03 +03:00
|
|
|
} else {
|
2018-04-25 06:54:50 +03:00
|
|
|
log.Debugf("telling relay (%s): %s", c.Server, "r."+c.Code)
|
2017-10-21 01:18:06 +03:00
|
|
|
if c.Wait {
|
2017-10-22 19:36:44 +03:00
|
|
|
// tell server to wait for sender
|
2018-06-23 19:18:35 +03:00
|
|
|
sendMessage("r."+c.keypair.Public+"."+c.HashedCode+".0.0.0", connection)
|
2017-10-21 01:18:06 +03:00
|
|
|
} else {
|
2017-10-22 19:36:44 +03:00
|
|
|
// tell server to cancel if sender doesn't exist
|
2018-06-23 19:18:35 +03:00
|
|
|
sendMessage("c."+c.keypair.Public+"."+c.HashedCode+".0.0.0", connection)
|
2017-10-21 01:18:06 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
if c.IsSender { // this is a sender
|
2018-06-23 19:18:35 +03:00
|
|
|
log.Debugf("[%d] waiting for ok from relay", id)
|
2017-10-20 23:51:03 +03:00
|
|
|
message = receiveMessage(connection)
|
2017-10-21 19:02:26 +03:00
|
|
|
if message == "timeout" {
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.gotTimeout = true
|
|
|
|
responses.Unlock()
|
2017-10-21 19:02:26 +03:00
|
|
|
fmt.Println("You've just exceeded limit waiting time.")
|
|
|
|
return
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if message == "no" {
|
2017-10-21 19:02:26 +03:00
|
|
|
if id == 0 {
|
2017-10-21 04:51:02 +03:00
|
|
|
fmt.Println("The specifed code is already in use by a sender.")
|
|
|
|
}
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.gotConnectionInUse = true
|
|
|
|
responses.Unlock()
|
2017-10-20 23:51:03 +03:00
|
|
|
} else {
|
2018-06-23 19:18:35 +03:00
|
|
|
// message is IP address, lets check next message
|
|
|
|
log.Debugf("[%d] got ok from relay: %s", id, message)
|
|
|
|
publicKeyRecipient := receiveMessage(connection)
|
2018-06-23 20:02:46 +03:00
|
|
|
// check if okay again
|
2018-06-23 20:57:25 +03:00
|
|
|
if id == 0 {
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner.Stop()
|
2018-06-26 19:09:25 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Your public key: ")
|
|
|
|
color.New(color.FgHiRed).Fprintln(os.Stderr, c.keypair.Public)
|
|
|
|
fmt.Fprintf(os.Stderr, "Recipient public key: ")
|
|
|
|
color.New(color.FgHiYellow).Fprintln(os.Stderr, publicKeyRecipient)
|
2018-06-23 20:57:25 +03:00
|
|
|
getOK := "y"
|
|
|
|
if !c.Yes {
|
|
|
|
getOK = getInput("ok? (y/n): ")
|
|
|
|
}
|
|
|
|
responses.Lock()
|
|
|
|
responses.gotOK = true
|
|
|
|
responses.Unlock()
|
|
|
|
if getOK == "y" {
|
|
|
|
okToContinue = true
|
|
|
|
} else {
|
|
|
|
okToContinue = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
responses.RLock()
|
|
|
|
ok := responses.gotOK
|
|
|
|
responses.RUnlock()
|
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
if okToContinue {
|
|
|
|
sendMessage("ok", connection)
|
|
|
|
} else {
|
|
|
|
sendMessage("no", connection)
|
|
|
|
return
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if id == 0 {
|
2018-06-25 05:21:56 +03:00
|
|
|
passphraseString, err := randomstring.GenerateRandomString(30)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Debugf("passphrase: [%s]", passphraseString)
|
|
|
|
encryptedPassword, err := c.keypair.Encrypt([]byte(passphraseString), publicKeyRecipient)
|
2018-06-23 19:18:35 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-23 20:02:46 +03:00
|
|
|
|
2018-06-23 19:18:35 +03:00
|
|
|
// encrypt files
|
2018-06-23 20:02:46 +03:00
|
|
|
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
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner = spinner.New(spinner.CharSets[24], 100*time.Millisecond) // Build our new spinner
|
|
|
|
c.spinner.Suffix = " Encrypting..."
|
|
|
|
c.spinner.Start()
|
2018-06-24 17:30:35 +03:00
|
|
|
log.Debugf("encrypting %s with passphrase [%s]", path.Join(c.File.Path, c.File.Name), passphraseString)
|
2018-06-23 20:02:46 +03:00
|
|
|
if err := EncryptFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc", passphraseString); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c.File.IsEncrypted = true
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner.Stop()
|
2018-06-23 20:02:46 +03:00
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
}
|
2018-06-23 19:18:35 +03:00
|
|
|
|
2018-06-24 17:30:35 +03:00
|
|
|
// remove compressed archive
|
|
|
|
if c.File.IsDir {
|
|
|
|
log.Debug("removing archive: " + c.File.Name)
|
|
|
|
if err := os.Remove(c.File.Name); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2018-06-23 19:18:35 +03:00
|
|
|
c.encryptedPassword = base64.StdEncoding.EncodeToString(encryptedPassword)
|
|
|
|
}
|
|
|
|
log.Debugf("[%d] waiting for 0 thread to encrypt", id)
|
|
|
|
for {
|
|
|
|
if c.encryptedPassword != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2018-06-23 19:18:35 +03:00
|
|
|
log.Debugf("sending encrypted passphrase: %s", c.encryptedPassword)
|
|
|
|
sendMessage(c.encryptedPassword, connection)
|
|
|
|
// wait for relay go
|
|
|
|
receiveMessage(connection)
|
2018-06-23 20:02:46 +03:00
|
|
|
if id == 0 {
|
2018-06-24 20:31:04 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\nSending (->@%s)..\n", message)
|
2018-06-23 20:02:46 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
// wait for pipe to be made
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// Write data from file
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("send file")
|
2017-11-05 17:07:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.startTime = time.Now()
|
|
|
|
responses.Unlock()
|
2018-04-14 01:21:15 +03:00
|
|
|
if !c.Debug {
|
|
|
|
c.bar.Reset()
|
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
if err := c.sendFile(id, connection); err != nil {
|
2018-06-23 20:57:25 +03:00
|
|
|
log.Warn(err)
|
|
|
|
} else {
|
|
|
|
fileTransfered = true
|
2017-10-21 04:09:40 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
} else { // this is a receiver
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("waiting for meta data from sender")
|
2017-10-20 23:51:03 +03:00
|
|
|
message = receiveMessage(connection)
|
2018-06-24 16:55:43 +03:00
|
|
|
log.Debugf("message from server: %s", message)
|
2017-10-20 23:51:03 +03:00
|
|
|
if message == "no" {
|
2017-10-21 19:02:26 +03:00
|
|
|
if id == 0 {
|
2017-10-21 04:51:02 +03:00
|
|
|
fmt.Println("The specifed code is already in use by a sender.")
|
|
|
|
}
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.gotConnectionInUse = true
|
|
|
|
responses.Unlock()
|
2017-10-20 23:51:03 +03:00
|
|
|
} else {
|
|
|
|
m := strings.Split(message, "-")
|
|
|
|
encryptedData, salt, iv, sendersAddress := m[0], m[1], m[2], m[3]
|
2017-10-21 01:44:03 +03:00
|
|
|
if sendersAddress == "0.0.0.0" {
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.notPresent = true
|
|
|
|
responses.Unlock()
|
2017-10-21 01:44:03 +03:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return
|
2018-04-25 06:54:50 +03:00
|
|
|
} else if strings.Split(sendersAddress, ":")[0] == "127.0.0.1" {
|
|
|
|
sendersAddress = strings.Replace(sendersAddress, "127.0.0.1", c.Server, 1)
|
2017-10-21 01:18:06 +03:00
|
|
|
}
|
2018-06-23 19:18:35 +03:00
|
|
|
// now get public key
|
|
|
|
publicKeySender := receiveMessage(connection)
|
|
|
|
|
2017-10-20 23:51:03 +03:00
|
|
|
// have the main thread ask for the okay
|
|
|
|
if id == 0 {
|
2017-10-24 17:43:35 +03:00
|
|
|
encryptedBytes, err := hex.DecodeString(encryptedData)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
decryptedBytes, _ := Decrypt(encryptedBytes, c.Code, salt, iv, c.DontEncrypt)
|
|
|
|
err = json.Unmarshal(decryptedBytes, &c.File)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debugf("meta data received: %v", c.File)
|
2017-10-22 20:26:53 +03:00
|
|
|
fType := "file"
|
2018-06-26 17:43:22 +03:00
|
|
|
c.Path = "."
|
2017-10-22 20:26:53 +03:00
|
|
|
fName := path.Join(c.Path, c.File.Name)
|
|
|
|
if c.File.IsDir {
|
|
|
|
fType = "folder"
|
|
|
|
fName = fName[:len(fName)-4]
|
|
|
|
}
|
2017-10-22 19:44:18 +03:00
|
|
|
if _, err := os.Stat(path.Join(c.Path, c.File.Name)); os.IsNotExist(err) {
|
2018-06-26 17:54:05 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Receiving %s (%s) into: '%s'\n\n", fType, humanize.Bytes(uint64(c.File.Size)), fName)
|
2017-10-22 19:44:18 +03:00
|
|
|
} else {
|
2018-06-26 17:54:05 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Overwriting %s '%s' (%s)\n\n", fType, fName, humanize.Bytes(uint64(c.File.Size)))
|
2017-10-22 19:44:18 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
var sentFileNames []string
|
|
|
|
|
2017-10-22 07:05:08 +03:00
|
|
|
if c.AskPath {
|
|
|
|
getPath := getInput("path: ")
|
|
|
|
if len(getPath) > 0 {
|
|
|
|
c.Path = path.Clean(getPath)
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if fileAlreadyExists(sentFileNames, c.File.Name) {
|
2018-04-14 00:57:34 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Will not overwrite file!")
|
2017-10-20 23:51:03 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-06-26 17:43:22 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Your public key: %s\n", c.keypair.Public)
|
|
|
|
fmt.Fprintf(os.Stderr, "Sender public key: %s\n", publicKeySender)
|
2018-04-14 01:31:03 +03:00
|
|
|
getOK := "y"
|
|
|
|
if !c.Yes {
|
|
|
|
getOK = getInput("ok? (y/n): ")
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if getOK == "y" {
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.gotOK = true
|
|
|
|
responses.Unlock()
|
2017-10-20 23:51:03 +03:00
|
|
|
sentFileNames = append(sentFileNames, c.File.Name)
|
|
|
|
}
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.gotResponse = true
|
|
|
|
responses.Unlock()
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
// wait for the main thread to get the okay
|
|
|
|
for limit := 0; limit < 1000; limit++ {
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
gotResponse := responses.gotResponse
|
|
|
|
responses.Unlock()
|
2017-10-20 23:51:03 +03:00
|
|
|
if gotResponse {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.RLock()
|
|
|
|
gotOK := responses.gotOK
|
|
|
|
responses.RUnlock()
|
2017-10-20 23:51:03 +03:00
|
|
|
if !gotOK {
|
|
|
|
sendMessage("not ok", connection)
|
|
|
|
} else {
|
2018-06-23 19:18:35 +03:00
|
|
|
sendMessage("ok", connection)
|
2018-06-23 20:02:46 +03:00
|
|
|
encryptedPassword := receiveMessage(connection)
|
|
|
|
log.Debugf("[%d] got encrypted passphrase: %s", id, encryptedPassword)
|
2018-06-23 20:57:25 +03:00
|
|
|
if encryptedPassword == "" {
|
|
|
|
return
|
|
|
|
}
|
2018-06-23 20:02:46 +03:00
|
|
|
encryptedPasswordBytes, err := base64.StdEncoding.DecodeString(encryptedPassword)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-23 20:57:25 +03:00
|
|
|
if publicKeySender == "" {
|
|
|
|
return
|
|
|
|
}
|
2018-06-23 20:02:46 +03:00
|
|
|
decryptedPassphrase, err := c.keypair.Decrypt(encryptedPasswordBytes, publicKeySender)
|
2018-06-23 20:57:25 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
return
|
|
|
|
}
|
2018-06-23 20:02:46 +03:00
|
|
|
c.encryptedPassword = string(decryptedPassphrase)
|
|
|
|
log.Debugf("decrypted password to: %s", c.encryptedPassword)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
sendMessage("ok", connection)
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("receive file")
|
2017-10-21 01:44:03 +03:00
|
|
|
if id == 0 {
|
2018-06-24 20:31:04 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\nReceiving (<-@%s)..\n", sendersAddress)
|
2017-10-21 01:44:03 +03:00
|
|
|
}
|
2017-11-05 17:07:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
responses.startTime = time.Now()
|
|
|
|
responses.Unlock()
|
2018-04-22 15:49:27 +03:00
|
|
|
if !c.Debug && id == 0 {
|
2018-06-24 17:11:42 +03:00
|
|
|
c.bar = progressbar.NewOptions(c.File.Size, progressbar.OptionSetWriter(os.Stderr))
|
2018-04-22 15:49:27 +03:00
|
|
|
} else {
|
|
|
|
// try to let the first thread start first
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-04-22 15:16:16 +03:00
|
|
|
}
|
2017-10-21 23:29:48 +03:00
|
|
|
if err := c.receiveFile(id, connection); err != nil {
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(errors.Wrap(err, "no file to recieve"))
|
2018-06-23 20:57:25 +03:00
|
|
|
} else {
|
|
|
|
fileTransfered = true
|
2017-10-21 23:29:48 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(id)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debugf("moving on")
|
2017-10-20 23:51:03 +03:00
|
|
|
|
2017-10-24 17:43:35 +03:00
|
|
|
responses.Lock()
|
|
|
|
defer responses.Unlock()
|
|
|
|
if responses.gotConnectionInUse {
|
2017-10-20 23:51:03 +03:00
|
|
|
return nil // connection was in use, just quit cleanly
|
|
|
|
}
|
|
|
|
|
2018-04-23 09:21:40 +03:00
|
|
|
timeSinceStart := time.Since(responses.startTime).Nanoseconds()
|
2017-11-05 17:07:35 +03:00
|
|
|
|
2018-06-24 16:55:43 +03:00
|
|
|
if c.IsSender {
|
2017-10-24 17:43:35 +03:00
|
|
|
if responses.gotTimeout {
|
2017-10-21 19:02:26 +03:00
|
|
|
fmt.Println("Timeout waiting for receiver")
|
|
|
|
return nil
|
2018-06-24 16:55:43 +03:00
|
|
|
} else if !fileTransfered {
|
|
|
|
fmt.Fprintf(os.Stderr, "\nNo mutual consent")
|
|
|
|
return nil
|
2018-06-24 20:13:58 +03:00
|
|
|
} else {
|
|
|
|
c.bar.Finish()
|
2017-10-21 19:02:26 +03:00
|
|
|
}
|
2018-04-23 11:38:57 +03:00
|
|
|
fileOrFolder := "File"
|
|
|
|
if c.File.IsDir {
|
|
|
|
fileOrFolder = "Folder"
|
|
|
|
}
|
2018-06-23 20:57:25 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\n%s sent", fileOrFolder)
|
2017-10-20 23:51:03 +03:00
|
|
|
} else { // Is a Receiver
|
2017-10-24 17:43:35 +03:00
|
|
|
if responses.notPresent {
|
2018-04-23 11:38:57 +03:00
|
|
|
fmt.Println("Either code is incorrect or sender is not ready. Use -wait to wait until sender connects.")
|
2017-10-21 01:18:06 +03:00
|
|
|
return nil
|
2018-06-24 16:55:43 +03:00
|
|
|
} else if !fileTransfered {
|
|
|
|
fmt.Fprintf(os.Stderr, "\nNo mutual consent")
|
|
|
|
return nil
|
2018-06-24 20:13:58 +03:00
|
|
|
} else {
|
|
|
|
c.bar.Finish()
|
2017-10-21 01:18:06 +03:00
|
|
|
}
|
2017-10-24 17:43:35 +03:00
|
|
|
if !responses.gotOK {
|
2017-10-20 23:51:03 +03:00
|
|
|
return errors.New("Transfer interrupted")
|
|
|
|
}
|
2017-10-21 03:49:19 +03:00
|
|
|
if err := c.catFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
log.Debugf("Code: [%s]", c.Code)
|
|
|
|
if c.DontEncrypt {
|
2017-10-22 07:05:08 +03:00
|
|
|
if err := CopyFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name)); err != nil {
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Error(err)
|
2017-10-20 23:51:03 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Debugf("is encrypted: %+v", c.File.IsEncrypted)
|
2017-10-22 19:28:31 +03:00
|
|
|
if c.File.IsEncrypted {
|
2018-06-26 17:43:22 +03:00
|
|
|
c.spinner = spinner.New(spinner.CharSets[24], 100*time.Millisecond) // Build our new spinner
|
|
|
|
c.spinner.Suffix = " Decrypting file.."
|
|
|
|
c.spinner.Start()
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Debugf("decrypting file with [%s]", c.encryptedPassword)
|
2018-06-26 17:43:22 +03:00
|
|
|
err := DecryptFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name), c.encryptedPassword)
|
|
|
|
c.spinner.Stop()
|
|
|
|
if err != nil {
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Error(err)
|
2017-10-22 19:28:31 +03:00
|
|
|
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 {
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Error(err)
|
2017-10-22 19:28:31 +03:00
|
|
|
return errors.Wrap(err, "Problem copying file")
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !c.Debug {
|
2017-10-22 07:05:08 +03:00
|
|
|
os.Remove(path.Join(c.Path, c.File.Name+".enc"))
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
2017-10-22 07:05:08 +03:00
|
|
|
fileHash, err := HashFile(path.Join(c.Path, c.File.Name))
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
log.Debugf("\n\n\ndownloaded hash: [%s]", fileHash)
|
|
|
|
log.Debugf("\n\n\nrelayed hash: [%s]", c.File.Hash)
|
|
|
|
|
|
|
|
if c.File.Hash != fileHash {
|
2018-06-23 20:02:46 +03:00
|
|
|
log.Flush()
|
2017-10-20 23:51:03 +03:00
|
|
|
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
|
2017-10-24 13:02:55 +03:00
|
|
|
}
|
|
|
|
if c.File.IsDir { // if the file was originally a dir
|
2018-04-23 11:38:57 +03:00
|
|
|
fmt.Print("\nDecompressing folder...")
|
2017-10-24 13:02:55 +03:00
|
|
|
log.Debug("untarring " + c.File.Name)
|
|
|
|
err := tarinator.UnTarinate(c.Path, path.Join(c.Path, c.File.Name))
|
|
|
|
if err != nil {
|
2018-04-22 16:02:42 +03:00
|
|
|
log.Debug("problem untarring: " + err.Error())
|
2017-10-24 13:02:55 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-04-22 16:02:42 +03:00
|
|
|
// we remove the old tar.gz filels
|
|
|
|
log.Debug("removing old tar file: " + c.File.Name)
|
2017-10-24 13:02:55 +03:00
|
|
|
err = os.Remove(path.Join(c.Path, c.File.Name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-10-22 13:14:46 +03:00
|
|
|
}
|
2018-06-26 17:54:05 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\nReceived folder written to '%s'", path.Join(c.Path, c.File.Name[:len(c.File.Name)-4]))
|
2017-10-24 13:02:55 +03:00
|
|
|
} else {
|
2018-04-14 00:57:34 +03:00
|
|
|
outputStream := path.Join(c.Path, c.File.Name)
|
|
|
|
if c.UseStdout {
|
|
|
|
outputStream = "stdout"
|
|
|
|
}
|
2018-06-26 17:54:05 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "\nReceived file written to '%s'", outputStream)
|
2018-04-14 00:57:34 +03:00
|
|
|
if c.UseStdout {
|
|
|
|
defer os.Remove(path.Join(c.Path, c.File.Name))
|
|
|
|
b, _ := ioutil.ReadFile(path.Join(c.Path, c.File.Name))
|
|
|
|
fmt.Printf("%s", b)
|
|
|
|
}
|
2017-10-22 13:14:46 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2018-04-23 09:21:40 +03:00
|
|
|
fmt.Fprintf(os.Stderr, " (%s/s)\n", humanize.Bytes(uint64(float64(1000000000)*float64(c.File.Size)/float64(timeSinceStart))))
|
2017-10-20 23:51:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileAlreadyExists(s []string, f string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == f {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-21 03:49:19 +03:00
|
|
|
func (c *Connection) catFile() error {
|
2017-10-20 23:51:03 +03:00
|
|
|
// cat the file
|
2017-10-21 03:49:19 +03:00
|
|
|
files := make([]string, c.NumberOfConnections)
|
2018-04-23 07:42:05 +03:00
|
|
|
i := 0
|
|
|
|
for id := 0; id < len(files); id++ {
|
|
|
|
files[i] = path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id))
|
|
|
|
if _, err := os.Stat(files[id]); os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Debug(files[i])
|
|
|
|
i++
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
files = files[:i]
|
|
|
|
log.Debug(files)
|
2017-10-24 12:13:04 +03:00
|
|
|
toRemove := !c.Debug
|
2017-10-22 07:05:08 +03:00
|
|
|
return CatFiles(files, path.Join(c.Path, c.File.Name+".enc"), toRemove)
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) receiveFile(id int, connection net.Conn) error {
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("waiting for chunk size from sender")
|
2017-10-20 23:51:03 +03:00
|
|
|
fileSizeBuffer := make([]byte, 10)
|
|
|
|
connection.Read(fileSizeBuffer)
|
|
|
|
fileDataString := strings.Trim(string(fileSizeBuffer), ":")
|
|
|
|
fileSizeInt, _ := strconv.Atoi(fileDataString)
|
|
|
|
chunkSize := int64(fileSizeInt)
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debugf("chunk size: %d", chunkSize)
|
2017-10-22 01:07:44 +03:00
|
|
|
if chunkSize == 0 {
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(fileSizeBuffer)
|
2017-10-22 01:07:44 +03:00
|
|
|
return errors.New("chunk size is empty!")
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
|
2017-10-22 07:05:08 +03:00
|
|
|
os.Remove(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
|
2017-10-21 23:38:27 +03:00
|
|
|
log.Debug("Making " + c.File.Name + ".enc." + strconv.Itoa(id))
|
2017-10-22 07:05:08 +03:00
|
|
|
newFile, err := os.Create(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer newFile.Close()
|
|
|
|
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(id, "waiting for file")
|
2017-10-20 23:51:03 +03:00
|
|
|
var receivedBytes int64
|
2017-10-21 01:18:06 +03:00
|
|
|
receivedFirstBytes := false
|
2017-10-20 23:51:03 +03:00
|
|
|
for {
|
|
|
|
if (chunkSize - receivedBytes) < BUFFERSIZE {
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debugf("%d at the end: %d < %d", id, (chunkSize - receivedBytes), BUFFERSIZE)
|
2017-10-20 23:51:03 +03:00
|
|
|
io.CopyN(newFile, connection, (chunkSize - receivedBytes))
|
|
|
|
// Empty the remaining bytes that we don't need from the network buffer
|
|
|
|
if (receivedBytes+BUFFERSIZE)-chunkSize < BUFFERSIZE {
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(id, "empty remaining bytes from network buffer")
|
2017-10-20 23:51:03 +03:00
|
|
|
connection.Read(make([]byte, (receivedBytes+BUFFERSIZE)-chunkSize))
|
|
|
|
}
|
2017-10-26 21:23:50 +03:00
|
|
|
if !c.Debug {
|
|
|
|
c.bar.Add(int((chunkSize - receivedBytes)))
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
break
|
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
written, _ := io.CopyN(newFile, connection, BUFFERSIZE)
|
|
|
|
receivedBytes += written
|
2017-10-21 01:18:06 +03:00
|
|
|
if !receivedFirstBytes {
|
|
|
|
receivedFirstBytes = true
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(id, "Receieved first bytes!")
|
2017-10-21 01:18:06 +03:00
|
|
|
}
|
2017-10-26 21:23:50 +03:00
|
|
|
if !c.Debug {
|
2018-04-23 07:42:05 +03:00
|
|
|
c.bar.Add(int(written))
|
2017-10-26 21:23:50 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(id, "received file")
|
2017-10-20 23:51:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
func (c *Connection) sendFile(id int, connection net.Conn) error {
|
2017-10-20 23:51:03 +03:00
|
|
|
defer connection.Close()
|
|
|
|
|
2018-04-23 07:42:05 +03:00
|
|
|
// open encrypted file chunk, if it exists
|
|
|
|
log.Debug("opening encrypted file chunk: " + c.File.Name + ".enc." + strconv.Itoa(id))
|
2017-10-21 04:09:40 +03:00
|
|
|
file, err := os.Open(c.File.Name + ".enc." + strconv.Itoa(id))
|
|
|
|
if err != nil {
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug(err)
|
|
|
|
return nil
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
defer file.Close()
|
2017-10-20 23:51:03 +03:00
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
// determine and send the file size to client
|
|
|
|
fi, err := file.Stat()
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
2017-10-21 04:09:40 +03:00
|
|
|
return err
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debugf("sending chunk size: %d", fi.Size())
|
2017-10-22 01:07:44 +03:00
|
|
|
_, err = connection.Write([]byte(fillString(strconv.FormatInt(int64(fi.Size()), 10), 10)))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Problem sending chunk data: ")
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
// rate limit the bandwidth
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("determining rate limiting")
|
2017-10-20 23:51:03 +03:00
|
|
|
bufferSizeInKilobytes := BUFFERSIZE / 1024
|
|
|
|
rate := float64(c.rate) / float64(c.NumberOfConnections*bufferSizeInKilobytes)
|
|
|
|
throttle := time.NewTicker(time.Second / time.Duration(rate))
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debugf("rate: %+v", rate)
|
2017-10-20 23:51:03 +03:00
|
|
|
defer throttle.Stop()
|
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
// send the file
|
|
|
|
sendBuffer := make([]byte, BUFFERSIZE)
|
|
|
|
totalBytesSent := 0
|
2017-10-20 23:51:03 +03:00
|
|
|
for range throttle.C {
|
2018-04-23 07:42:05 +03:00
|
|
|
_, err := file.Read(sendBuffer)
|
|
|
|
written, errWrite := connection.Write(sendBuffer)
|
|
|
|
totalBytesSent += written
|
2017-10-21 04:09:40 +03:00
|
|
|
if !c.Debug {
|
2018-04-23 07:42:05 +03:00
|
|
|
c.bar.Add(int(written))
|
|
|
|
}
|
|
|
|
if errWrite != nil {
|
2018-06-23 20:57:25 +03:00
|
|
|
return errWrite
|
2017-10-21 04:09:40 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
//End of file reached, break out of for loop
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("EOF")
|
2017-10-20 23:51:03 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
log.Debug("file is sent")
|
|
|
|
log.Debug("removing piece")
|
2017-10-21 04:09:40 +03:00
|
|
|
if !c.Debug {
|
|
|
|
file.Close()
|
|
|
|
err = os.Remove(c.File.Name + ".enc." + strconv.Itoa(id))
|
|
|
|
}
|
2018-04-13 19:31:43 +03:00
|
|
|
if err != nil && c.File.DeleteAfterSending {
|
|
|
|
err = os.Remove(path.Join(c.File.Path, c.File.Name))
|
|
|
|
}
|
2018-04-23 07:42:05 +03:00
|
|
|
|
|
|
|
// wait until client breaks connection
|
|
|
|
for range throttle.C {
|
|
|
|
_, errWrite := connection.Write([]byte("."))
|
|
|
|
if errWrite != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
return err
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|