mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
automatically determine if local
This commit is contained in:
parent
2bd8504a57
commit
90f1277ee5
1 changed files with 51 additions and 81 deletions
132
connect.go
132
connect.go
|
@ -74,13 +74,6 @@ func NewConnection(config *AppConfig) (*Connection, error) {
|
||||||
c.rate = config.Rate
|
c.rate = config.Rate
|
||||||
c.Local = config.Local
|
c.Local = config.Local
|
||||||
|
|
||||||
if c.Local {
|
|
||||||
c.DontEncrypt = true
|
|
||||||
if c.Server == "cowyo.com" {
|
|
||||||
c.Server = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stat, _ := os.Stdin.Stat()
|
stat, _ := os.Stdin.Stat()
|
||||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||||
config.File = "stdin"
|
config.File = "stdin"
|
||||||
|
@ -167,72 +160,7 @@ func (c *Connection) Run() error {
|
||||||
}()
|
}()
|
||||||
defer c.cleanup()
|
defer c.cleanup()
|
||||||
|
|
||||||
if c.Local {
|
// calculate number of threads
|
||||||
c.DontEncrypt = true
|
|
||||||
c.Yes = true
|
|
||||||
if c.Code == "" {
|
|
||||||
c.Code = strings.Split(GetRandomName(), "-")[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.IsSender {
|
|
||||||
if c.Code == "" {
|
|
||||||
c.Code = GetRandomName()
|
|
||||||
}
|
|
||||||
log.Debug("settings payload to ", c.Code)
|
|
||||||
p, _ := peerdiscovery.New(peerdiscovery.Settings{
|
|
||||||
Limit: 1,
|
|
||||||
TimeLimit: 600 * time.Second,
|
|
||||||
Delay: 50 * time.Millisecond,
|
|
||||||
Payload: []byte(c.Code),
|
|
||||||
})
|
|
||||||
go func() {
|
|
||||||
discovered, _ := p.Discover()
|
|
||||||
if len(discovered) > 0 {
|
|
||||||
log.Debugf("discovered by '%+v'", discovered[0])
|
|
||||||
c.runClient("localhost")
|
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
|
||||||
log.Debug("discovered by no one")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
} else {
|
|
||||||
p, _ := peerdiscovery.New(peerdiscovery.Settings{
|
|
||||||
Limit: 1,
|
|
||||||
TimeLimit: 1 * time.Second,
|
|
||||||
Delay: 50 * time.Millisecond,
|
|
||||||
Payload: []byte(c.Code),
|
|
||||||
})
|
|
||||||
fmt.Print("Finding local croc relay...")
|
|
||||||
discovered, _ := p.Discover()
|
|
||||||
if len(discovered) > 0 {
|
|
||||||
c.Server = discovered[0].Address
|
|
||||||
fmt.Println(discovered[0].Address)
|
|
||||||
c.Code = string(discovered[0].Payload)
|
|
||||||
log.Debugf("discovered code '%s'", c.Code)
|
|
||||||
time.Sleep(200 * time.Millisecond)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.IsSender {
|
|
||||||
log.Debug("starting relay in case local connections")
|
|
||||||
relay := NewRelay(&AppConfig{
|
|
||||||
Debug: c.Debug,
|
|
||||||
})
|
|
||||||
go relay.Run()
|
|
||||||
time.Sleep(200 * time.Millisecond)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug("checking code validity")
|
|
||||||
if len(c.Code) == 0 {
|
|
||||||
if c.IsSender {
|
|
||||||
c.Code = GetRandomName()
|
|
||||||
} else {
|
|
||||||
c.Code = getInput("Enter receive code: ")
|
|
||||||
}
|
|
||||||
log.Debug("changed code to ", c.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.NumberOfConnections = MAX_NUMBER_THREADS
|
c.NumberOfConnections = MAX_NUMBER_THREADS
|
||||||
if c.IsSender {
|
if c.IsSender {
|
||||||
fsize, err := FileSize(path.Join(c.File.Path, c.File.Name))
|
fsize, err := FileSize(path.Join(c.File.Path, c.File.Name))
|
||||||
|
@ -245,7 +173,19 @@ func (c *Connection) Run() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runClientError := make(chan error)
|
||||||
if c.IsSender {
|
if c.IsSender {
|
||||||
|
if c.Code == "" {
|
||||||
|
c.Code = GetRandomName()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("starting relay in case local connections")
|
||||||
|
relay := NewRelay(&AppConfig{
|
||||||
|
Debug: c.Debug,
|
||||||
|
})
|
||||||
|
go relay.Run()
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
if c.DontEncrypt {
|
if c.DontEncrypt {
|
||||||
// don't encrypt
|
// don't encrypt
|
||||||
CopyFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc")
|
CopyFile(path.Join(c.File.Path, c.File.Name), c.File.Name+".enc")
|
||||||
|
@ -293,16 +233,44 @@ func (c *Connection) Run() error {
|
||||||
fmt.Fprintf(os.Stderr, "Sending %s file named '%s'\n", humanize.Bytes(uint64(c.File.Size)), c.File.Name)
|
fmt.Fprintf(os.Stderr, "Sending %s file named '%s'\n", humanize.Bytes(uint64(c.File.Size)), c.File.Name)
|
||||||
|
|
||||||
}
|
}
|
||||||
if c.Local {
|
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
|
||||||
fmt.Fprintf(os.Stderr, "Receive with: croc --local\n")
|
|
||||||
fmt.Fprintf(os.Stderr, "or croc --local --server %s --code %s\n", GetLocalIP(), c.Code)
|
|
||||||
|
|
||||||
|
// broadcast local connection from sender
|
||||||
|
log.Debug("settings payload to ", c.Code)
|
||||||
|
go func() {
|
||||||
|
go peerdiscovery.Discover(peerdiscovery.Settings{
|
||||||
|
Limit: 1,
|
||||||
|
TimeLimit: 600 * time.Second,
|
||||||
|
Delay: 50 * time.Millisecond,
|
||||||
|
Payload: []byte(c.Code),
|
||||||
|
})
|
||||||
|
runClientError <- c.runClient("localhost")
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("checking code validity")
|
||||||
|
if len(c.Code) == 0 && !c.IsSender {
|
||||||
|
log.Debug("Finding local croc relay...")
|
||||||
|
discovered, _ := peerdiscovery.Discover(peerdiscovery.Settings{
|
||||||
|
Limit: 1,
|
||||||
|
TimeLimit: 1 * time.Second,
|
||||||
|
Delay: 50 * time.Millisecond,
|
||||||
|
Payload: []byte(c.Code),
|
||||||
|
})
|
||||||
|
if len(discovered) > 0 {
|
||||||
|
c.Server = discovered[0].Address
|
||||||
|
log.Debug(discovered[0].Address)
|
||||||
|
c.Code = string(discovered[0].Payload)
|
||||||
|
log.Debugf("discovered code '%s'", c.Code)
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
|
c.Code = getInput("Enter receive code: ")
|
||||||
|
log.Debug("changed code to ", c.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.runClient(c.Server)
|
go func() { runClientError <- c.runClient(c.Server) }()
|
||||||
|
return <-runClientError
|
||||||
}
|
}
|
||||||
|
|
||||||
// runClient spawns threads for parallel uplink/downlink via TCP
|
// runClient spawns threads for parallel uplink/downlink via TCP
|
||||||
|
@ -348,15 +316,15 @@ func (c *Connection) runClient(serverName string) error {
|
||||||
message := receiveMessage(connection)
|
message := receiveMessage(connection)
|
||||||
log.Debugf("relay says: %s", message)
|
log.Debugf("relay says: %s", message)
|
||||||
if c.IsSender {
|
if c.IsSender {
|
||||||
log.Debugf("telling relay: %s", "s."+c.Code)
|
log.Debugf("telling relay (%s): %s", c.Server, "s."+c.Code)
|
||||||
metaData, err := json.Marshal(c.File)
|
metaData, err := json.Marshal(c.File)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
encryptedMetaData, salt, iv := Encrypt(metaData, c.Code, c.DontEncrypt)
|
encryptedMetaData, salt, iv := Encrypt(metaData, c.Code)
|
||||||
sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
|
sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("telling relay: %s", "r."+c.Code)
|
log.Debugf("telling relay (%s): %s", c.Server, "r."+c.Code)
|
||||||
if c.Wait {
|
if c.Wait {
|
||||||
// tell server to wait for sender
|
// tell server to wait for sender
|
||||||
sendMessage("r."+c.HashedCode+".0.0.0", connection)
|
sendMessage("r."+c.HashedCode+".0.0.0", connection)
|
||||||
|
@ -420,6 +388,8 @@ func (c *Connection) runClient(serverName string) error {
|
||||||
responses.Unlock()
|
responses.Unlock()
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
return
|
return
|
||||||
|
} else if strings.Split(sendersAddress, ":")[0] == "127.0.0.1" {
|
||||||
|
sendersAddress = strings.Replace(sendersAddress, "127.0.0.1", c.Server, 1)
|
||||||
}
|
}
|
||||||
// have the main thread ask for the okay
|
// have the main thread ask for the okay
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue