1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-24 08:02:33 +03:00
This commit is contained in:
Zack Scholl 2017-10-17 18:39:50 -06:00
parent 0bcd272f4e
commit d1eb0de12d
3 changed files with 114 additions and 64 deletions

View File

@ -11,9 +11,12 @@ import (
"sync" "sync"
"time" "time"
"github.com/gosuri/uiprogress"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var bars []*uiprogress.Bar
// runClient spawns threads for parallel uplink/downlink via TCP // runClient spawns threads for parallel uplink/downlink via TCP
func runClient(connectionType string, codePhrase string) { func runClient(connectionType string, codePhrase string) {
logger := log.WithFields(log.Fields{ logger := log.WithFields(log.Fields{
@ -22,6 +25,10 @@ func runClient(connectionType string, codePhrase string) {
}) })
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(numberConnections) wg.Add(numberConnections)
uiprogress.Start()
bars = make([]*uiprogress.Bar, numberConnections)
fileNameToReceive := ""
for id := 0; id < numberConnections; id++ { for id := 0; id < numberConnections; id++ {
go func(id int) { go func(id int) {
defer wg.Done() defer wg.Done()
@ -33,66 +40,108 @@ func runClient(connectionType string, codePhrase string) {
defer connection.Close() defer connection.Close()
message := receiveMessage(connection) message := receiveMessage(connection)
logger.Infof("message: %s", message) logger.Debugf("relay says: %s", message)
logger.Debugf("telling relay: %s", connectionType+"."+codePhrase)
sendMessage(connectionType+"."+codePhrase, connection) sendMessage(connectionType+"."+codePhrase, connection)
if connectionType == "s" { // this is a sender if connectionType == "s" { // this is a sender
logger.Info("waiting for ok from relay") logger.Debug("waiting for ok from relay")
message = receiveMessage(connection) message = receiveMessage(connection)
logger.Info("got ok from relay") logger.Debug("got ok from relay")
// wait for pipe to be made // wait for pipe to be made
time.Sleep(1 * time.Second) time.Sleep(100 * time.Millisecond)
// Write data from file // Write data from file
sendFileToClient(id, connection) logger.Debug("send file")
sendFile(id, connection)
// TODO: Release from connection pool
// POST /release
} else { // this is a receiver } else { // this is a receiver
// receive file // receive file
receiveFile(id, connection) logger.Debug("receive file")
fileNameToReceive = receiveFile(id, connection)
// TODO: Release from connection pool
// POST /release
} }
}(id) }(id)
} }
wg.Wait() wg.Wait()
if connectionType == "r" {
catFile(fileNameToReceive)
}
} }
func receiveFile(id int, connection net.Conn) { func catFile(fileNameToReceive string) {
// cat the file
os.Remove(fileNameToReceive)
finished, err := os.Create(fileNameToReceive)
defer finished.Close()
if err != nil {
log.Fatal(err)
}
for id := 0; id < numberConnections; id++ {
fh, err := os.Open(fileNameToReceive + "." + strconv.Itoa(id))
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(finished, fh)
if err != nil {
log.Fatal(err)
}
fh.Close()
os.Remove(fileNameToReceive + "." + strconv.Itoa(id))
}
fmt.Println("\n\n\nDownloaded " + fileNameToReceive + "!")
}
func receiveFile(id int, connection net.Conn) string {
logger := log.WithFields(log.Fields{
"function": "receiveFile #" + strconv.Itoa(id),
})
bufferFileName := make([]byte, 64) bufferFileName := make([]byte, 64)
bufferFileSize := make([]byte, 10) bufferFileSize := make([]byte, 10)
logger.Debug("waiting for file size")
connection.Read(bufferFileSize) connection.Read(bufferFileSize)
fileSize, _ := strconv.ParseInt(strings.Trim(string(bufferFileSize), ":"), 10, 64) fileSize, _ := strconv.ParseInt(strings.Trim(string(bufferFileSize), ":"), 10, 64)
logger.Debugf("filesize: %d", fileSize)
connection.Read(bufferFileName) connection.Read(bufferFileName)
fileName = strings.Trim(string(bufferFileName), ":") fileNameToReceive := strings.Trim(string(bufferFileName), ":")
os.Remove(fileName + "." + strconv.Itoa(id)) logger.Debugf("fileName: %v", fileNameToReceive)
newFile, err := os.Create(fileName + "." + strconv.Itoa(id)) os.Remove(fileNameToReceive + "." + strconv.Itoa(id))
newFile, err := os.Create(fileNameToReceive + "." + strconv.Itoa(id))
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer newFile.Close() defer newFile.Close()
bars[id] = uiprogress.AddBar(int(fileSize)/1024 + 1).AppendCompleted().PrependElapsed()
logger.Debug("waiting for file")
var receivedBytes int64 var receivedBytes int64
for { for {
bars[id].Incr()
if (fileSize - receivedBytes) < BUFFERSIZE { if (fileSize - receivedBytes) < BUFFERSIZE {
logger.Debug("at the end")
io.CopyN(newFile, connection, (fileSize - receivedBytes)) io.CopyN(newFile, connection, (fileSize - receivedBytes))
// Empty the remaining bytes that we don't need from the network buffer // Empty the remaining bytes that we don't need from the network buffer
if (receivedBytes+BUFFERSIZE)-fileSize < BUFFERSIZE {
logger.Debug("empty remaining bytes from network buffer")
connection.Read(make([]byte, (receivedBytes+BUFFERSIZE)-fileSize)) connection.Read(make([]byte, (receivedBytes+BUFFERSIZE)-fileSize))
}
break break
} }
io.CopyN(newFile, connection, BUFFERSIZE) io.CopyN(newFile, connection, BUFFERSIZE)
//Increment the counter //Increment the counter
receivedBytes += BUFFERSIZE receivedBytes += BUFFERSIZE
} }
logger.Debug("received file")
return fileNameToReceive
} }
func sendFileToClient(id int, connection net.Conn) { func sendFile(id int, connection net.Conn) {
logger := log.WithFields(log.Fields{ logger := log.WithFields(log.Fields{
"function": "sendFileToClient #" + strconv.Itoa(id), "function": "sendFile #" + strconv.Itoa(id),
}) })
defer connection.Close() defer connection.Close()
//Open the file that needs to be send to the client //Open the file that needs to be send to the client
@ -118,18 +167,18 @@ func sendFileToClient(id int, connection net.Conn) {
} }
fileSize := fillString(strconv.FormatInt(int64(bytesPerConnection), 10), 10) fileSize := fillString(strconv.FormatInt(int64(bytesPerConnection), 10), 10)
fileName := fillString(fileInfo.Name(), 64) fileNameToSend := fillString(fileInfo.Name(), 64)
if id == 0 || id == numberConnections-1 { if id == 0 || id == numberConnections-1 {
logger.Infof("numChunks: %v", numChunks) logger.Debugf("numChunks: %v", numChunks)
logger.Infof("chunksPerWorker: %v", chunksPerWorker) logger.Debugf("chunksPerWorker: %v", chunksPerWorker)
logger.Infof("bytesPerConnection: %v", bytesPerConnection) logger.Debugf("bytesPerConnection: %v", bytesPerConnection)
logger.Infof("fileName: %v", fileInfo.Name()) logger.Debugf("fileNameToSend: %v", fileInfo.Name())
} }
logger.Info("sending") logger.Debugf("sending %s", fileSize)
connection.Write([]byte(fileSize)) connection.Write([]byte(fileSize))
connection.Write([]byte(fileName)) connection.Write([]byte(fileNameToSend))
sendBuffer := make([]byte, BUFFERSIZE) sendBuffer := make([]byte, BUFFERSIZE)
chunkI := 0 chunkI := 0
@ -137,7 +186,7 @@ func sendFileToClient(id int, connection net.Conn) {
_, err = file.Read(sendBuffer) _, err = file.Read(sendBuffer)
if err == io.EOF { if err == io.EOF {
//End of file reached, break out of for loop //End of file reached, break out of for loop
logger.Info("EOF") logger.Debug("EOF")
break break
} }
if (chunkI >= chunksPerWorker*id && chunkI < chunksPerWorker*id+chunksPerWorker) || (id == numberConnections-1 && chunkI >= chunksPerWorker*id) { if (chunkI >= chunksPerWorker*id && chunkI < chunksPerWorker*id+chunksPerWorker) || (id == numberConnections-1 && chunkI >= chunksPerWorker*id) {
@ -145,6 +194,6 @@ func sendFileToClient(id int, connection net.Conn) {
} }
chunkI++ chunkI++
} }
fmt.Println("File has been sent, closing connection!") logger.Debug("file is sent")
return return
} }

View File

@ -3,22 +3,24 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
const BUFFERSIZE = 1024 const BUFFERSIZE = 1024
const numberConnections = 1 const numberConnections = 4
// Build flags // Build flags
var server, file string var server, file string
// Global varaibles // Global varaibles
var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string var serverAddress, fileName, codePhraseFlag, connectionTypeFlag string
var runAsRelay bool var runAsRelay, debugFlag bool
func main() { func main() {
flag.BoolVar(&runAsRelay, "relay", false, "run as relay") flag.BoolVar(&runAsRelay, "relay", false, "run as relay")
flag.BoolVar(&debugFlag, "debug", false, "debug mode")
flag.StringVar(&serverAddress, "server", "", "(run as client) server address to connect to") flag.StringVar(&serverAddress, "server", "", "(run as client) server address to connect to")
flag.StringVar(&fileName, "file", "", "(run as server) file to serve") flag.StringVar(&fileName, "file", "", "(run as server) file to serve")
flag.StringVar(&codePhraseFlag, "code", "", "(run as server) file to serve") flag.StringVar(&codePhraseFlag, "code", "", "(run as server) file to serve")
@ -31,6 +33,22 @@ func main() {
if file != "" { if file != "" {
fileName = file fileName = file
} }
if len(fileName) > 0 {
_, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
return
}
}
log.SetFormatter(&log.TextFormatter{})
if debugFlag {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.WarnLevel)
}
if runAsRelay { if runAsRelay {
runServer() runServer()
} else if len(serverAddress) != 0 { } else if len(serverAddress) != 0 {
@ -39,16 +57,3 @@ func main() {
fmt.Println("You must specify either -file (for running as a server) or -server (for running as a client)") fmt.Println("You must specify either -file (for running as a server) or -server (for running as a client)")
} }
} }
func init() {
// Log as JSON instead of the default ASCII formatter.
// log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
// log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.DebugLevel)
}

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"net" "net"
"strconv" "strconv"
"strings" "strings"
@ -31,7 +30,7 @@ func runServer() {
logger := log.WithFields(log.Fields{ logger := log.WithFields(log.Fields{
"function": "main", "function": "main",
}) })
logger.Info("Initializing") logger.Debug("Initializing")
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(numberConnections) wg.Add(numberConnections)
for id := 0; id < numberConnections; id++ { for id := 0; id < numberConnections; id++ {
@ -62,62 +61,60 @@ func listener(id int) (err error) {
return errors.Wrap(err, "Error listening on "+serverAddress+":"+port) return errors.Wrap(err, "Error listening on "+serverAddress+":"+port)
} }
defer server.Close() defer server.Close()
logger.Info("waiting for connections") logger.Debug("waiting for connections")
//Spawn a new goroutine whenever a client connects //Spawn a new goroutine whenever a client connects
for { for {
connection, err := server.Accept() connection, err := server.Accept()
if err != nil { if err != nil {
return errors.Wrap(err, "problem accepting connection") return errors.Wrap(err, "problem accepting connection")
} }
logger.Infof("Client %s connected", connection.RemoteAddr().String()) logger.Debugf("Client %s connected", connection.RemoteAddr().String())
go clientCommuncation(id, connection) go clientCommuncation(id, connection)
} }
} }
func clientCommuncation(id int, connection net.Conn) { func clientCommuncation(id int, connection net.Conn) {
logger := log.WithFields(log.Fields{
"id": id,
"connection": connection.RemoteAddr().String(),
})
logger.Info("Asking who?")
sendMessage("who?", connection) sendMessage("who?", connection)
message := receiveMessage(connection) message := receiveMessage(connection)
connectionType := strings.Split(message, ".")[0] connectionType := strings.Split(message, ".")[0]
codePhrase := strings.Split(message, ".")[1] codePhrase := strings.Split(message, ".")[1] + "-" + strconv.Itoa(id)
logger := log.WithFields(log.Fields{
"id": id,
"codePhrase": codePhrase,
})
if connectionType == "s" { if connectionType == "s" {
logger.Info("got sender") logger.Debug("got sender")
connections.Lock() connections.Lock()
connections.sender[codePhrase] = connection connections.sender[codePhrase] = connection
connections.Unlock() connections.Unlock()
for { for {
logger.Info("waiting for reciever")
connections.RLock() connections.RLock()
if _, ok := connections.reciever[codePhrase]; ok { if _, ok := connections.reciever[codePhrase]; ok {
logger.Info("got reciever") logger.Debug("got reciever")
connections.RUnlock() connections.RUnlock()
break break
} }
connections.RUnlock() connections.RUnlock()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
logger.Info("telling sender ok") logger.Debug("telling sender ok")
sendMessage("ok", connection) sendMessage("ok", connection)
logger.Info("preparing pipe") logger.Debug("preparing pipe")
connections.Lock() connections.Lock()
con1 := connections.sender[codePhrase] con1 := connections.sender[codePhrase]
con2 := connections.reciever[codePhrase] con2 := connections.reciever[codePhrase]
connections.Unlock() connections.Unlock()
fmt.Println("piping connections") logger.Debug("piping connections")
Pipe(con1, con2) Pipe(con1, con2)
fmt.Println("done piping") logger.Debug("done piping")
connections.Lock() connections.Lock()
delete(connections.sender, codePhrase) delete(connections.sender, codePhrase)
delete(connections.reciever, codePhrase) delete(connections.reciever, codePhrase)
connections.Unlock() connections.Unlock()
fmt.Println("deleted " + codePhrase) logger.Debug("deleted sender and receiver")
} else { } else {
fmt.Println("Got reciever") logger.Debug("got reciever")
connections.Lock() connections.Lock()
connections.reciever[codePhrase] = connection connections.reciever[codePhrase] = connection
connections.Unlock() connections.Unlock()
@ -132,8 +129,7 @@ func sendMessage(message string, connection net.Conn) {
func receiveMessage(connection net.Conn) string { func receiveMessage(connection net.Conn) string {
messageByte := make([]byte, BUFFERSIZE) messageByte := make([]byte, BUFFERSIZE)
n, err := connection.Read(messageByte) connection.Read(messageByte)
fmt.Println(n, err)
return strings.Replace(string(messageByte), ":", "", -1) return strings.Replace(string(messageByte), ":", "", -1)
} }