2017-10-20 23:51:03 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
2017-10-21 21:45:44 +03:00
|
|
|
"path"
|
2017-10-20 23:51:03 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gosuri/uiprogress"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
Server string
|
|
|
|
File FileMetaData
|
|
|
|
NumberOfConnections int
|
|
|
|
Code string
|
|
|
|
HashedCode string
|
|
|
|
IsSender bool
|
|
|
|
Debug bool
|
|
|
|
DontEncrypt bool
|
2017-10-21 01:18:06 +03:00
|
|
|
Wait bool
|
2017-10-20 23:51:03 +03:00
|
|
|
bars []*uiprogress.Bar
|
|
|
|
rate int
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileMetaData struct {
|
|
|
|
Name string
|
|
|
|
Size int
|
|
|
|
Hash string
|
2017-10-21 21:45:44 +03:00
|
|
|
Path string
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnection(flags *Flags) *Connection {
|
|
|
|
c := new(Connection)
|
|
|
|
c.Debug = flags.Debug
|
|
|
|
c.DontEncrypt = flags.DontEncrypt
|
2017-10-21 01:18:06 +03:00
|
|
|
c.Wait = flags.Wait
|
2017-10-20 23:51:03 +03:00
|
|
|
c.Server = flags.Server
|
|
|
|
c.Code = flags.Code
|
|
|
|
c.NumberOfConnections = flags.NumberOfConnections
|
|
|
|
c.rate = flags.Rate
|
|
|
|
if len(flags.File) > 0 {
|
2017-10-21 21:45:44 +03:00
|
|
|
c.File.Name = path.Base(flags.File)
|
|
|
|
c.File.Path = path.Dir(flags.File)
|
2017-10-20 23:51:03 +03:00
|
|
|
c.IsSender = true
|
|
|
|
} else {
|
|
|
|
c.IsSender = false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetFormatter(&log.TextFormatter{})
|
|
|
|
if c.Debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.WarnLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Run() error {
|
|
|
|
forceSingleThreaded := false
|
|
|
|
if c.IsSender {
|
2017-10-21 21:45:44 +03:00
|
|
|
fsize, err := FileSize(path.Join(c.File.Path, c.File.Name))
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fsize < MAX_NUMBER_THREADS*BUFFERSIZE {
|
|
|
|
forceSingleThreaded = true
|
|
|
|
log.Debug("forcing single thread")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Debug("checking code validity")
|
|
|
|
for {
|
|
|
|
// check code
|
|
|
|
goodCode := true
|
|
|
|
m := strings.Split(c.Code, "-")
|
2017-10-21 01:18:06 +03:00
|
|
|
log.Debug(m)
|
2017-10-20 23:51:03 +03:00
|
|
|
numThreads, errParse := strconv.Atoi(m[0])
|
|
|
|
if len(m) < 2 {
|
|
|
|
goodCode = false
|
2017-10-21 01:18:06 +03:00
|
|
|
log.Debug("code too short")
|
2017-10-20 23:51:03 +03:00
|
|
|
} else if numThreads > MAX_NUMBER_THREADS || numThreads < 1 || (forceSingleThreaded && numThreads != 1) {
|
|
|
|
c.NumberOfConnections = MAX_NUMBER_THREADS
|
|
|
|
goodCode = false
|
2017-10-21 01:18:06 +03:00
|
|
|
log.Debug("incorrect number of threads")
|
2017-10-20 23:51:03 +03:00
|
|
|
} else if errParse != nil {
|
|
|
|
goodCode = false
|
2017-10-21 01:18:06 +03:00
|
|
|
log.Debug("problem parsing threads")
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
log.Debug(m)
|
2017-10-21 01:18:06 +03:00
|
|
|
log.Debug(goodCode)
|
2017-10-20 23:51:03 +03:00
|
|
|
if !goodCode {
|
|
|
|
if c.IsSender {
|
|
|
|
if forceSingleThreaded {
|
|
|
|
c.NumberOfConnections = 1
|
|
|
|
}
|
|
|
|
c.Code = strconv.Itoa(c.NumberOfConnections) + "-" + GetRandomName()
|
|
|
|
} else {
|
|
|
|
if len(c.Code) != 0 {
|
|
|
|
fmt.Println("Code must begin with number of threads (e.g. 3-some-code)")
|
|
|
|
}
|
|
|
|
c.Code = getInput("Enter receive code: ")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// assign number of connections
|
|
|
|
c.NumberOfConnections, _ = strconv.Atoi(strings.Split(c.Code, "-")[0])
|
|
|
|
|
|
|
|
if c.IsSender {
|
|
|
|
if c.DontEncrypt {
|
|
|
|
// don't encrypt
|
2017-10-21 21:45:44 +03:00
|
|
|
CopyFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc")
|
2017-10-20 23:51:03 +03:00
|
|
|
} else {
|
|
|
|
// encrypt
|
|
|
|
log.Debug("encrypting...")
|
2017-10-21 21:45:44 +03:00
|
|
|
if err := EncryptFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc", c.Code); err != nil {
|
2017-10-20 23:51:03 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
if err := SplitFile(c.File.Name+".enc", c.NumberOfConnections); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
c.File.Size, err = FileSize(c.File.Name + ".enc")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
// remove the file now since we still have pieces
|
|
|
|
if err := os.Remove(c.File.Name + ".enc"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-21 01:18:06 +03:00
|
|
|
fmt.Printf("Sending %d byte file named '%s'\n", c.File.Size, c.File.Name)
|
|
|
|
fmt.Printf("Code is: %s\n", c.Code)
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.runClient()
|
|
|
|
}
|
|
|
|
|
|
|
|
// runClient spawns threads for parallel uplink/downlink via TCP
|
|
|
|
func (c *Connection) runClient() error {
|
|
|
|
logger := log.WithFields(log.Fields{
|
|
|
|
"code": c.Code,
|
|
|
|
"sender?": c.IsSender,
|
|
|
|
})
|
|
|
|
|
|
|
|
c.HashedCode = Hash(c.Code)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(c.NumberOfConnections)
|
|
|
|
|
|
|
|
uiprogress.Start()
|
|
|
|
if !c.Debug {
|
|
|
|
c.bars = make([]*uiprogress.Bar, c.NumberOfConnections)
|
|
|
|
}
|
2017-10-21 19:02:26 +03:00
|
|
|
gotTimeout := false
|
2017-10-20 23:51:03 +03:00
|
|
|
gotOK := false
|
|
|
|
gotResponse := false
|
|
|
|
gotConnectionInUse := false
|
2017-10-21 01:18:06 +03:00
|
|
|
notPresent := 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)
|
|
|
|
connection, err := net.Dial("tcp", c.Server+":"+port)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
message := receiveMessage(connection)
|
|
|
|
logger.Debugf("relay says: %s", message)
|
|
|
|
if c.IsSender {
|
|
|
|
logger.Debugf("telling relay: %s", "s."+c.Code)
|
|
|
|
metaData, err := json.Marshal(c.File)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
encryptedMetaData, salt, iv := Encrypt(metaData, c.Code)
|
|
|
|
sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
|
|
|
|
} else {
|
|
|
|
logger.Debugf("telling relay: %s", "r."+c.Code)
|
2017-10-21 01:18:06 +03:00
|
|
|
if c.Wait {
|
|
|
|
sendMessage("r."+c.HashedCode+".0.0.0", connection)
|
|
|
|
} else {
|
|
|
|
sendMessage("c."+c.HashedCode+".0.0.0", connection)
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
if c.IsSender { // this is a sender
|
|
|
|
logger.Debug("waiting for ok from relay")
|
|
|
|
message = receiveMessage(connection)
|
2017-10-21 19:02:26 +03:00
|
|
|
if message == "timeout" {
|
|
|
|
gotTimeout = true
|
|
|
|
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-20 23:51:03 +03:00
|
|
|
gotConnectionInUse = true
|
|
|
|
} else {
|
|
|
|
logger.Debug("got ok from relay")
|
|
|
|
if id == 0 {
|
|
|
|
fmt.Printf("\nSending (->%s)..\n", message)
|
|
|
|
}
|
|
|
|
// wait for pipe to be made
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// Write data from file
|
|
|
|
logger.Debug("send file")
|
2017-10-21 04:09:40 +03:00
|
|
|
if err := c.sendFile(id, connection); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
} else { // this is a receiver
|
|
|
|
logger.Debug("waiting for meta data from sender")
|
|
|
|
message = receiveMessage(connection)
|
|
|
|
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-20 23:51:03 +03:00
|
|
|
gotConnectionInUse = true
|
|
|
|
} 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" {
|
|
|
|
notPresent = true
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return
|
2017-10-21 01:18:06 +03:00
|
|
|
}
|
2017-10-20 23:51:03 +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)
|
|
|
|
// have the main thread ask for the okay
|
|
|
|
if id == 0 {
|
|
|
|
fmt.Printf("Receiving file (%d bytes) into: %s\n", c.File.Size, c.File.Name)
|
|
|
|
var sentFileNames []string
|
|
|
|
|
|
|
|
if fileAlreadyExists(sentFileNames, c.File.Name) {
|
|
|
|
fmt.Printf("Will not overwrite file!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
getOK := getInput("ok? (y/n): ")
|
|
|
|
if getOK == "y" {
|
|
|
|
gotOK = true
|
|
|
|
sentFileNames = append(sentFileNames, c.File.Name)
|
|
|
|
}
|
|
|
|
gotResponse = true
|
|
|
|
}
|
|
|
|
// wait for the main thread to get the okay
|
|
|
|
for limit := 0; limit < 1000; limit++ {
|
|
|
|
if gotResponse {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
if !gotOK {
|
|
|
|
sendMessage("not ok", connection)
|
|
|
|
} else {
|
|
|
|
sendMessage("ok", connection)
|
|
|
|
logger.Debug("receive file")
|
2017-10-21 01:44:03 +03:00
|
|
|
if id == 0 {
|
|
|
|
fmt.Printf("\n\nReceiving (<-%s)..\n", sendersAddress)
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
c.receiveFile(id, connection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(id)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if gotConnectionInUse {
|
|
|
|
return nil // connection was in use, just quit cleanly
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSender {
|
2017-10-21 19:02:26 +03:00
|
|
|
if gotTimeout {
|
|
|
|
fmt.Println("Timeout waiting for receiver")
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
fmt.Println("\nFile sent.")
|
2017-10-20 23:51:03 +03:00
|
|
|
} else { // Is a Receiver
|
2017-10-21 01:18:06 +03:00
|
|
|
if notPresent {
|
|
|
|
fmt.Println("Sender/Code not present")
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if !gotOK {
|
|
|
|
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 {
|
|
|
|
if err := CopyFile(c.File.Name+".enc", c.File.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := DecryptFile(c.File.Name+".enc", c.File.Name, c.Code); err != nil {
|
|
|
|
return errors.Wrap(err, "Problem decrypting file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !c.Debug {
|
|
|
|
os.Remove(c.File.Name + ".enc")
|
|
|
|
}
|
|
|
|
|
|
|
|
fileHash, err := HashFile(c.File.Name)
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
|
|
|
|
} else {
|
2017-10-21 21:45:44 +03:00
|
|
|
fmt.Printf("\nReceived file written to %s\n", c.File.Name)
|
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)
|
|
|
|
for id := range files {
|
2017-10-21 04:09:40 +03:00
|
|
|
files[id] = c.File.Name + ".enc." + strconv.Itoa(id)
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2017-10-21 03:49:19 +03:00
|
|
|
toRemove := true
|
|
|
|
if c.Debug {
|
|
|
|
toRemove = false
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
2017-10-21 03:49:19 +03:00
|
|
|
return CatFiles(files, c.File.Name+".enc", toRemove)
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) receiveFile(id int, connection net.Conn) error {
|
|
|
|
logger := log.WithFields(log.Fields{
|
|
|
|
"function": "receiveFile #" + strconv.Itoa(id),
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.Debug("waiting for chunk size from sender")
|
|
|
|
fileSizeBuffer := make([]byte, 10)
|
|
|
|
connection.Read(fileSizeBuffer)
|
|
|
|
fileDataString := strings.Trim(string(fileSizeBuffer), ":")
|
|
|
|
fileSizeInt, _ := strconv.Atoi(fileDataString)
|
|
|
|
chunkSize := int64(fileSizeInt)
|
|
|
|
logger.Debugf("chunk size: %d", chunkSize)
|
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
os.Remove(c.File.Name + ".enc." + strconv.Itoa(id))
|
|
|
|
newFile, err := os.Create(c.File.Name + ".enc." + strconv.Itoa(id))
|
2017-10-20 23:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer newFile.Close()
|
|
|
|
|
|
|
|
if !c.Debug {
|
|
|
|
c.bars[id] = uiprogress.AddBar(int(chunkSize)/1024 + 1).AppendCompleted().PrependElapsed()
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("waiting for file")
|
|
|
|
var receivedBytes int64
|
2017-10-21 01:18:06 +03:00
|
|
|
receivedFirstBytes := false
|
2017-10-20 23:51:03 +03:00
|
|
|
for {
|
|
|
|
if !c.Debug {
|
|
|
|
c.bars[id].Incr()
|
|
|
|
}
|
|
|
|
if (chunkSize - receivedBytes) < BUFFERSIZE {
|
|
|
|
logger.Debug("at the end")
|
|
|
|
io.CopyN(newFile, connection, (chunkSize - receivedBytes))
|
|
|
|
// Empty the remaining bytes that we don't need from the network buffer
|
|
|
|
if (receivedBytes+BUFFERSIZE)-chunkSize < BUFFERSIZE {
|
|
|
|
logger.Debug("empty remaining bytes from network buffer")
|
|
|
|
connection.Read(make([]byte, (receivedBytes+BUFFERSIZE)-chunkSize))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
io.CopyN(newFile, connection, BUFFERSIZE)
|
|
|
|
receivedBytes += BUFFERSIZE
|
2017-10-21 01:18:06 +03:00
|
|
|
if !receivedFirstBytes {
|
|
|
|
receivedFirstBytes = true
|
|
|
|
logger.Debug("Receieved first bytes!")
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
logger.Debug("received file")
|
|
|
|
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
|
|
|
logger := log.WithFields(log.Fields{
|
|
|
|
"function": "sendFile #" + strconv.Itoa(id),
|
|
|
|
})
|
|
|
|
defer connection.Close()
|
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
// open encrypted file chunk
|
|
|
|
logger.Debug("opening encrypted file chunk: " + c.File.Name + ".enc." + strconv.Itoa(id))
|
|
|
|
file, err := os.Open(c.File.Name + ".enc." + strconv.Itoa(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
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
|
|
|
}
|
2017-10-21 04:09:40 +03:00
|
|
|
logger.Debugf("sending chunk size: %d", fi.Size())
|
|
|
|
connection.Write([]byte(fillString(strconv.FormatInt(int64(fi.Size()), 10), 10)))
|
2017-10-20 23:51:03 +03:00
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
// show the progress
|
2017-10-20 23:51:03 +03:00
|
|
|
if !c.Debug {
|
2017-10-21 04:09:40 +03:00
|
|
|
logger.Debug("going to show progress")
|
|
|
|
c.bars[id] = uiprogress.AddBar(int(fi.Size())).AppendCompleted().PrependElapsed()
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|
|
|
|
|
2017-10-21 04:09:40 +03:00
|
|
|
// rate limit the bandwidth
|
|
|
|
logger.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))
|
|
|
|
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 {
|
2017-10-21 04:09:40 +03:00
|
|
|
n, err := file.Read(sendBuffer)
|
|
|
|
connection.Write(sendBuffer)
|
|
|
|
totalBytesSent += n
|
|
|
|
if !c.Debug {
|
|
|
|
c.bars[id].Set(totalBytesSent)
|
|
|
|
}
|
2017-10-20 23:51:03 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
//End of file reached, break out of for loop
|
|
|
|
logger.Debug("EOF")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Debug("file is sent")
|
2017-10-21 04:09:40 +03:00
|
|
|
logger.Debug("removing piece")
|
|
|
|
if !c.Debug {
|
|
|
|
file.Close()
|
|
|
|
err = os.Remove(c.File.Name + ".enc." + strconv.Itoa(id))
|
|
|
|
}
|
|
|
|
return err
|
2017-10-20 23:51:03 +03:00
|
|
|
}
|