mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
reduce complexity
This commit is contained in:
parent
cfbd65be31
commit
7ac7e5d56d
1 changed files with 59 additions and 54 deletions
|
@ -228,44 +228,24 @@ func (c *Client) sendCollectFiles(options TransferOptions) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// Send will send the specified file
|
||||
func (c *Client) Send(options TransferOptions) (err error) {
|
||||
err = c.sendCollectFiles(options)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
otherRelay := ""
|
||||
if c.Options.RelayAddress != models.DEFAULT_RELAY {
|
||||
otherRelay = "--relay " + c.Options.RelayAddress + " "
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Code is: %s\nOn the other computer run\n\ncroc %s%s\n", c.Options.SharedSecret, otherRelay, c.Options.SharedSecret)
|
||||
// // c.spinner.Suffix = " waiting for recipient..."
|
||||
// c.spinner.Start()
|
||||
// create channel for quitting
|
||||
// connect to the relay for messaging
|
||||
errchan := make(chan error, 1)
|
||||
|
||||
if !c.Options.DisableLocal {
|
||||
// add two things to the error channel
|
||||
errchan = make(chan error, 2)
|
||||
func (c *Client) setupLocalRelay() {
|
||||
// setup the relay locally
|
||||
for _, port := range c.Options.RelayPorts {
|
||||
|
||||
go func(portStr string) {
|
||||
debugString := "warn"
|
||||
if c.Options.Debug {
|
||||
debugString = "debug"
|
||||
}
|
||||
err = tcp.Run(debugString, portStr, strings.Join(c.Options.RelayPorts[1:], ","))
|
||||
err := tcp.Run(debugString, portStr, strings.Join(c.Options.RelayPorts[1:], ","))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(port)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) broadcastOnLocalNetwork() {
|
||||
// look for peers first
|
||||
go func() {
|
||||
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
|
||||
Limit: -1,
|
||||
Payload: []byte(c.Options.RelayPorts[0]),
|
||||
|
@ -277,9 +257,9 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||
if err == nil && len(discoveries) > 0 {
|
||||
log.Debug("using local server")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
go func() {
|
||||
func (c *Client) transferOverLocalRelay(options TransferOptions, errchan chan<- error) {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
log.Debug("establishing connection")
|
||||
var banner string
|
||||
|
@ -302,7 +282,32 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||
c.ExternalIP = ipaddr
|
||||
errchan <- c.transfer(options)
|
||||
}()
|
||||
}
|
||||
|
||||
// Send will send the specified file
|
||||
func (c *Client) Send(options TransferOptions) (err error) {
|
||||
err = c.sendCollectFiles(options)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
otherRelay := ""
|
||||
if c.Options.RelayAddress != models.DEFAULT_RELAY {
|
||||
otherRelay = "--relay " + c.Options.RelayAddress + " "
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Code is: %s\nOn the other computer run\n\ncroc %s%s\n", c.Options.SharedSecret, otherRelay, c.Options.SharedSecret)
|
||||
// // c.spinner.Suffix = " waiting for recipient..."
|
||||
// c.spinner.Start()
|
||||
// create channel for quitting
|
||||
// connect to the relay for messaging
|
||||
errchan := make(chan error, 1)
|
||||
|
||||
if !c.Options.DisableLocal {
|
||||
// add two things to the error channel
|
||||
errchan = make(chan error, 2)
|
||||
c.setupLocalRelay()
|
||||
go c.broadcastOnLocalNetwork()
|
||||
go c.transferOverLocalRelay(options, errchan)
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue