mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Better code input
This commit is contained in:
parent
a9551f7a9c
commit
a8cfbe25d1
2 changed files with 58 additions and 28 deletions
69
connect.go
69
connect.go
|
@ -53,18 +53,6 @@ func NewConnection(flags *Flags) *Connection {
|
||||||
} else {
|
} else {
|
||||||
c.IsSender = false
|
c.IsSender = false
|
||||||
}
|
}
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Connection) Run() {
|
|
||||||
if len(c.Code) == 0 {
|
|
||||||
if !c.IsSender {
|
|
||||||
c.Code = getInput("Enter receive code: ")
|
|
||||||
}
|
|
||||||
if len(c.Code) < 5 {
|
|
||||||
c.Code = GetRandomName()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.SetFormatter(&log.TextFormatter{})
|
log.SetFormatter(&log.TextFormatter{})
|
||||||
if c.Debug {
|
if c.Debug {
|
||||||
|
@ -73,6 +61,41 @@ func (c *Connection) Run() {
|
||||||
log.SetLevel(log.WarnLevel)
|
log.SetLevel(log.WarnLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Run() {
|
||||||
|
log.Debug("checking code validity")
|
||||||
|
for {
|
||||||
|
// check code
|
||||||
|
goodCode := true
|
||||||
|
m := strings.Split(c.Code, "-")
|
||||||
|
numThreads, errParse := strconv.Atoi(m[0])
|
||||||
|
if len(m) < 2 {
|
||||||
|
goodCode = false
|
||||||
|
} else if numThreads > MAX_NUMBER_THREADS || numThreads < 1 {
|
||||||
|
c.NumberOfConnections = MAX_NUMBER_THREADS
|
||||||
|
goodCode = false
|
||||||
|
} else if errParse != nil {
|
||||||
|
goodCode = false
|
||||||
|
}
|
||||||
|
log.Debug(m)
|
||||||
|
if !goodCode {
|
||||||
|
if c.IsSender {
|
||||||
|
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.IsSender {
|
||||||
// encrypt the file
|
// encrypt the file
|
||||||
log.Debug("encrypting...")
|
log.Debug("encrypting...")
|
||||||
|
@ -112,6 +135,7 @@ func (c *Connection) runClient() {
|
||||||
c.bars = make([]*uiprogress.Bar, c.NumberOfConnections)
|
c.bars = make([]*uiprogress.Bar, c.NumberOfConnections)
|
||||||
}
|
}
|
||||||
gotOK := false
|
gotOK := false
|
||||||
|
gotResponse := 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()
|
||||||
|
@ -168,32 +192,35 @@ func (c *Connection) runClient() {
|
||||||
// have the main thread ask for the okay
|
// have the main thread ask for the okay
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
fmt.Printf("Receiving file (%d bytes) into: %s\n", c.File.Size, c.File.Name)
|
fmt.Printf("Receiving file (%d bytes) into: %s\n", c.File.Size, c.File.Name)
|
||||||
getOk := getInput("ok? (y/n): ")
|
getOK := getInput("ok? (y/n): ")
|
||||||
if getOk == "y" {
|
if getOK == "y" {
|
||||||
gotOK = true
|
gotOK = true
|
||||||
} else {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
gotResponse = true
|
||||||
}
|
}
|
||||||
// wait for the main thread to get the okay
|
// wait for the main thread to get the okay
|
||||||
for limit := 0; limit < 1000; limit++ {
|
for limit := 0; limit < 1000; limit++ {
|
||||||
if gotOK {
|
if gotResponse {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
}
|
}
|
||||||
if !gotOK {
|
if !gotOK {
|
||||||
return
|
sendMessage("not ok", connection)
|
||||||
|
} else {
|
||||||
|
sendMessage("ok", connection)
|
||||||
|
logger.Debug("receive file")
|
||||||
|
c.receiveFile(id, connection)
|
||||||
}
|
}
|
||||||
sendMessage("ok", connection)
|
|
||||||
logger.Debug("receive file")
|
|
||||||
c.receiveFile(id, connection)
|
|
||||||
}
|
}
|
||||||
}(id)
|
}(id)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
if !c.IsSender {
|
if !c.IsSender {
|
||||||
|
if !gotOK {
|
||||||
|
return
|
||||||
|
}
|
||||||
c.catFile(c.File.Name)
|
c.catFile(c.File.Name)
|
||||||
encrypted, err := ioutil.ReadFile(c.File.Name + ".encrypted")
|
encrypted, err := ioutil.ReadFile(c.File.Name + ".encrypted")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
17
relay.go
17
relay.go
|
@ -11,6 +11,8 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MAX_NUMBER_THREADS = 8
|
||||||
|
|
||||||
type connectionMap struct {
|
type connectionMap struct {
|
||||||
reciever map[string]net.Conn
|
reciever map[string]net.Conn
|
||||||
sender map[string]net.Conn
|
sender map[string]net.Conn
|
||||||
|
@ -27,7 +29,7 @@ type Relay struct {
|
||||||
func NewRelay(flags *Flags) *Relay {
|
func NewRelay(flags *Flags) *Relay {
|
||||||
r := new(Relay)
|
r := new(Relay)
|
||||||
r.Debug = flags.Debug
|
r.Debug = flags.Debug
|
||||||
r.NumberOfConnections = flags.NumberOfConnections
|
r.NumberOfConnections = MAX_NUMBER_THREADS
|
||||||
log.SetFormatter(&log.TextFormatter{})
|
log.SetFormatter(&log.TextFormatter{})
|
||||||
if r.Debug {
|
if r.Debug {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
|
@ -149,7 +151,6 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
}
|
}
|
||||||
r.connections.RUnlock()
|
r.connections.RUnlock()
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
logger.Debug("waiting for metadata")
|
|
||||||
}
|
}
|
||||||
// send meta data
|
// send meta data
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
|
@ -157,11 +158,13 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
r.connections.RUnlock()
|
r.connections.RUnlock()
|
||||||
// check for receiver's consent
|
// check for receiver's consent
|
||||||
consent := receiveMessage(connection)
|
consent := receiveMessage(connection)
|
||||||
logger.Debug("consent: %s", consent)
|
logger.Debugf("consent: %s", consent)
|
||||||
logger.Debug("got reciever")
|
if consent == "ok" {
|
||||||
r.connections.Lock()
|
logger.Debug("got consent")
|
||||||
r.connections.reciever[key] = connection
|
r.connections.Lock()
|
||||||
r.connections.Unlock()
|
r.connections.reciever[key] = connection
|
||||||
|
r.connections.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue