mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Merge branch 'master' of git://github.com/Lunsford94/croc into Lunsford94-master
This commit is contained in:
commit
da16d6f4d7
3 changed files with 42 additions and 4 deletions
38
connect.go
38
connect.go
|
@ -27,6 +27,7 @@ type Connection struct {
|
||||||
IsSender bool
|
IsSender bool
|
||||||
Debug bool
|
Debug bool
|
||||||
DontEncrypt bool
|
DontEncrypt bool
|
||||||
|
Wait bool
|
||||||
bars []*uiprogress.Bar
|
bars []*uiprogress.Bar
|
||||||
rate int
|
rate int
|
||||||
}
|
}
|
||||||
|
@ -41,6 +42,7 @@ func NewConnection(flags *Flags) *Connection {
|
||||||
c := new(Connection)
|
c := new(Connection)
|
||||||
c.Debug = flags.Debug
|
c.Debug = flags.Debug
|
||||||
c.DontEncrypt = flags.DontEncrypt
|
c.DontEncrypt = flags.DontEncrypt
|
||||||
|
c.Wait = flags.Wait
|
||||||
c.Server = flags.Server
|
c.Server = flags.Server
|
||||||
c.Code = flags.Code
|
c.Code = flags.Code
|
||||||
c.NumberOfConnections = flags.NumberOfConnections
|
c.NumberOfConnections = flags.NumberOfConnections
|
||||||
|
@ -79,16 +81,21 @@ func (c *Connection) Run() error {
|
||||||
// check code
|
// check code
|
||||||
goodCode := true
|
goodCode := true
|
||||||
m := strings.Split(c.Code, "-")
|
m := strings.Split(c.Code, "-")
|
||||||
|
log.Debug(m)
|
||||||
numThreads, errParse := strconv.Atoi(m[0])
|
numThreads, errParse := strconv.Atoi(m[0])
|
||||||
if len(m) < 2 {
|
if len(m) < 2 {
|
||||||
goodCode = false
|
goodCode = false
|
||||||
|
log.Debug("code too short")
|
||||||
} else if numThreads > MAX_NUMBER_THREADS || numThreads < 1 || (forceSingleThreaded && numThreads != 1) {
|
} else if numThreads > MAX_NUMBER_THREADS || numThreads < 1 || (forceSingleThreaded && numThreads != 1) {
|
||||||
c.NumberOfConnections = MAX_NUMBER_THREADS
|
c.NumberOfConnections = MAX_NUMBER_THREADS
|
||||||
goodCode = false
|
goodCode = false
|
||||||
|
log.Debug("incorrect number of threads")
|
||||||
} else if errParse != nil {
|
} else if errParse != nil {
|
||||||
goodCode = false
|
goodCode = false
|
||||||
|
log.Debug("problem parsing threads")
|
||||||
}
|
}
|
||||||
log.Debug(m)
|
log.Debug(m)
|
||||||
|
log.Debug(goodCode)
|
||||||
if !goodCode {
|
if !goodCode {
|
||||||
if c.IsSender {
|
if c.IsSender {
|
||||||
if forceSingleThreaded {
|
if forceSingleThreaded {
|
||||||
|
@ -130,6 +137,8 @@ func (c *Connection) Run() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fmt.Printf("Sending %d byte file named '%s'\n", c.File.Size, c.File.Name)
|
||||||
|
fmt.Printf("Code is: %s\n", c.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.runClient()
|
return c.runClient()
|
||||||
|
@ -154,6 +163,7 @@ func (c *Connection) runClient() error {
|
||||||
gotOK := false
|
gotOK := false
|
||||||
gotResponse := false
|
gotResponse := false
|
||||||
gotConnectionInUse := false
|
gotConnectionInUse := false
|
||||||
|
notPresent := false
|
||||||
for id := 0; id < c.NumberOfConnections; id++ {
|
for id := 0; id < c.NumberOfConnections; id++ {
|
||||||
go func(id int) {
|
go func(id int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
@ -176,7 +186,11 @@ func (c *Connection) runClient() error {
|
||||||
sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
|
sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
|
||||||
} else {
|
} else {
|
||||||
logger.Debugf("telling relay: %s", "r."+c.Code)
|
logger.Debugf("telling relay: %s", "r."+c.Code)
|
||||||
sendMessage("r."+c.HashedCode+".0.0.0", connection)
|
if c.Wait {
|
||||||
|
sendMessage("r."+c.HashedCode+".0.0.0", connection)
|
||||||
|
} else {
|
||||||
|
sendMessage("c."+c.HashedCode+".0.0.0", connection)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if c.IsSender { // this is a sender
|
if c.IsSender { // this is a sender
|
||||||
logger.Debug("waiting for ok from relay")
|
logger.Debug("waiting for ok from relay")
|
||||||
|
@ -205,6 +219,11 @@ func (c *Connection) runClient() error {
|
||||||
} else {
|
} else {
|
||||||
m := strings.Split(message, "-")
|
m := strings.Split(message, "-")
|
||||||
encryptedData, salt, iv, sendersAddress := m[0], m[1], m[2], m[3]
|
encryptedData, salt, iv, sendersAddress := m[0], m[1], m[2], m[3]
|
||||||
|
if sendersAddress == "0.0.0.0" {
|
||||||
|
notPresent = true
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
return
|
||||||
|
}
|
||||||
encryptedBytes, err := hex.DecodeString(encryptedData)
|
encryptedBytes, err := hex.DecodeString(encryptedData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
@ -245,7 +264,9 @@ func (c *Connection) runClient() error {
|
||||||
} else {
|
} else {
|
||||||
sendMessage("ok", connection)
|
sendMessage("ok", connection)
|
||||||
logger.Debug("receive file")
|
logger.Debug("receive file")
|
||||||
fmt.Printf("\n\nReceiving (<-%s)..\n", sendersAddress)
|
if id == 0 {
|
||||||
|
fmt.Printf("\n\nReceiving (<-%s)..\n", sendersAddress)
|
||||||
|
}
|
||||||
c.receiveFile(id, connection)
|
c.receiveFile(id, connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,6 +282,10 @@ func (c *Connection) runClient() error {
|
||||||
if c.IsSender {
|
if c.IsSender {
|
||||||
// TODO: Add confirmation
|
// TODO: Add confirmation
|
||||||
} else { // Is a Receiver
|
} else { // Is a Receiver
|
||||||
|
if notPresent {
|
||||||
|
fmt.Println("Sender/Code not present")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if !gotOK {
|
if !gotOK {
|
||||||
return errors.New("Transfer interrupted")
|
return errors.New("Transfer interrupted")
|
||||||
}
|
}
|
||||||
|
@ -291,6 +316,10 @@ func (c *Connection) runClient() error {
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\nReceived file written to %s", c.File.Name)
|
fmt.Printf("\nReceived file written to %s", c.File.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fmt.Println("File sent.")
|
||||||
|
// TODO: Add confirmation
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -354,6 +383,7 @@ func (c *Connection) receiveFile(id int, connection net.Conn) error {
|
||||||
|
|
||||||
logger.Debug("waiting for file")
|
logger.Debug("waiting for file")
|
||||||
var receivedBytes int64
|
var receivedBytes int64
|
||||||
|
receivedFirstBytes := false
|
||||||
for {
|
for {
|
||||||
if !c.Debug {
|
if !c.Debug {
|
||||||
c.bars[id].Incr()
|
c.bars[id].Incr()
|
||||||
|
@ -370,6 +400,10 @@ func (c *Connection) receiveFile(id int, connection net.Conn) error {
|
||||||
}
|
}
|
||||||
io.CopyN(newFile, connection, BUFFERSIZE)
|
io.CopyN(newFile, connection, BUFFERSIZE)
|
||||||
receivedBytes += BUFFERSIZE
|
receivedBytes += BUFFERSIZE
|
||||||
|
if !receivedFirstBytes {
|
||||||
|
receivedFirstBytes = true
|
||||||
|
logger.Debug("Receieved first bytes!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logger.Debug("received file")
|
logger.Debug("received file")
|
||||||
return nil
|
return nil
|
||||||
|
|
2
main.go
2
main.go
|
@ -15,6 +15,7 @@ var oneGigabytePerSecond = 1000000 // expressed as kbps
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
Relay bool
|
Relay bool
|
||||||
Debug bool
|
Debug bool
|
||||||
|
Wait bool
|
||||||
DontEncrypt bool
|
DontEncrypt bool
|
||||||
Server string
|
Server string
|
||||||
File string
|
File string
|
||||||
|
@ -37,6 +38,7 @@ croc version ` + version + `
|
||||||
flags := new(Flags)
|
flags := new(Flags)
|
||||||
flag.BoolVar(&flags.Relay, "relay", false, "run as relay")
|
flag.BoolVar(&flags.Relay, "relay", false, "run as relay")
|
||||||
flag.BoolVar(&flags.Debug, "debug", false, "debug mode")
|
flag.BoolVar(&flags.Debug, "debug", false, "debug mode")
|
||||||
|
flag.BoolVar(&flags.Wait, "wait", false, "wait for code to be sent")
|
||||||
flag.StringVar(&flags.Server, "server", "cowyo.com", "address of relay server")
|
flag.StringVar(&flags.Server, "server", "cowyo.com", "address of relay server")
|
||||||
flag.StringVar(&flags.File, "send", "", "file to send")
|
flag.StringVar(&flags.File, "send", "", "file to send")
|
||||||
flag.StringVar(&flags.Code, "code", "", "use your own code phrase")
|
flag.StringVar(&flags.Code, "code", "", "use your own code phrase")
|
||||||
|
|
6
relay.go
6
relay.go
|
@ -128,7 +128,6 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
sendMessage("no", connection)
|
sendMessage("no", connection)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("got sender")
|
logger.Debug("got sender")
|
||||||
r.connections.Lock()
|
r.connections.Lock()
|
||||||
r.connections.metadata[key] = metaData
|
r.connections.metadata[key] = metaData
|
||||||
|
@ -174,7 +173,6 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
r.connections.Lock()
|
r.connections.Lock()
|
||||||
r.connections.potentialReceivers[key] = struct{}{}
|
r.connections.potentialReceivers[key] = struct{}{}
|
||||||
r.connections.Unlock()
|
r.connections.Unlock()
|
||||||
|
|
||||||
// wait for sender's metadata
|
// wait for sender's metadata
|
||||||
sendersAddress := ""
|
sendersAddress := ""
|
||||||
for {
|
for {
|
||||||
|
@ -188,6 +186,10 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.connections.RUnlock()
|
r.connections.RUnlock()
|
||||||
|
if connectionType == "c" {
|
||||||
|
sendMessage("0-0-0-0.0.0.0", connection)
|
||||||
|
return
|
||||||
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
// send meta data
|
// send meta data
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue