From 667edd037379cb88fe9e5ef82d2b4e1cdbcb3122 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 25 Sep 2018 15:14:58 -0700 Subject: [PATCH] multiple tcp ports --- main.go | 9 +++++---- src/croc/croc.go | 8 ++++---- src/croc/sending.go | 16 ++++++++-------- src/relay/relay.go | 8 +++++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 1188d9d9..362338de 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "path/filepath" + "strings" "time" humanize "github.com/dustin/go-humanize" @@ -63,7 +64,7 @@ func main() { app.Flags = []cli.Flag{ cli.StringFlag{Name: "addr", Value: "198.199.67.130", Usage: "address of the public relay"}, cli.StringFlag{Name: "addr-ws", Value: "8153", Usage: "port of the public relay websocket server to connect"}, - cli.StringFlag{Name: "addr-tcp", Value: "8154", Usage: "tcp port of the public relay serer to connect"}, + cli.StringFlag{Name: "addr-tcp", Value: "8154,8155,8156,8157", Usage: "tcp ports of the public relay serer to connect"}, cli.BoolFlag{Name: "no-local", Usage: "disable local mode"}, cli.BoolFlag{Name: "local", Usage: "use only local mode"}, cli.BoolFlag{Name: "debug", Usage: "increase verbosity (a lot)"}, @@ -72,7 +73,7 @@ func main() { cli.BoolFlag{Name: "force-tcp", Usage: "force TCP"}, cli.BoolFlag{Name: "force-web", Usage: "force websockets"}, cli.StringFlag{Name: "port", Value: "8153", Usage: "port that the websocket listens on"}, - cli.StringFlag{Name: "tcp-port", Value: "8154", Usage: "port that the tcp server listens on"}, + cli.StringFlag{Name: "tcp-port", Value: "8154,8155,8156,8157", Usage: "ports that the tcp server listens on"}, cli.StringFlag{Name: "curve", Value: "siec", Usage: "specify elliptic curve to use (p224, p256, p384, p521, siec)"}, } app.EnableBashCompletion = true @@ -88,7 +89,7 @@ func main() { cr = croc.Init(c.GlobalBool("debug")) cr.AllowLocalDiscovery = true cr.Address = c.GlobalString("addr") - cr.AddressTCPPort = c.GlobalString("addr-tcp") + cr.AddressTCPPorts = strings.Split(c.GlobalString("addr-tcp"), ",") cr.AddressWebsocketPort = c.GlobalString("addr-ws") cr.NoRecipientPrompt = c.GlobalBool("yes") cr.Stdout = c.GlobalBool("stdout") @@ -96,7 +97,7 @@ func main() { cr.NoLocal = c.GlobalBool("no-local") cr.ShowText = true cr.RelayWebsocketPort = c.String("port") - cr.RelayTCPPort = c.String("tcp-port") + cr.RelayTCPPorts = strings.Split(c.String("tcp-port"), ",") cr.CurveType = c.String("curve") if c.GlobalBool("force-tcp") { cr.ForceSend = 2 diff --git a/src/croc/croc.go b/src/croc/croc.go index 9f833465..233702ca 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -24,12 +24,12 @@ type Croc struct { // Options for relay RelayWebsocketPort string - RelayTCPPort string + RelayTCPPorts []string CurveType string // Options for connecting to server Address string - AddressTCPPort string + AddressTCPPorts []string AddressWebsocketPort string Timeout time.Duration LocalOnly bool @@ -61,11 +61,11 @@ func Init(debug bool) (c *Croc) { c.UseEncryption = true c.AllowLocalDiscovery = true c.RelayWebsocketPort = "8153" - c.RelayTCPPort = "8154" + c.RelayTCPPorts = []string{"8154", "8155", "8156", "8156"} c.CurveType = "siec" c.Address = "198.199.67.130" c.AddressWebsocketPort = "8153" - c.AddressTCPPort = "8154" + c.AddressTCPPorts = []string{"8154", "8155", "8156", "8156"} c.NoRecipientPrompt = true debugLevel := "info" if debug { diff --git a/src/croc/sending.go b/src/croc/sending.go index b55092e9..1edf3b9d 100644 --- a/src/croc/sending.go +++ b/src/croc/sending.go @@ -40,7 +40,7 @@ func (c *Croc) Send(fname, codephrase string) (err error) { if !c.NoLocal { go func() { // start own relay and connect to it - go relay.Run(c.RelayWebsocketPort, c.RelayTCPPort) + go relay.Run(c.RelayWebsocketPort, c.RelayTCPPorts) time.Sleep(250 * time.Millisecond) // race condition here, but this should work most of the time :( // broadcast for peer discovery @@ -50,12 +50,12 @@ func (c *Croc) Send(fname, codephrase string) (err error) { Limit: 1, TimeLimit: 600 * time.Second, Delay: 50 * time.Millisecond, - Payload: []byte(c.RelayWebsocketPort + "- " + c.RelayTCPPort), + Payload: []byte(c.RelayWebsocketPort + "- " + strings.Join(c.RelayTCPPorts, ",")), }) }() // connect to own relay - errChan <- c.sendReceive("localhost", c.RelayWebsocketPort, c.RelayTCPPort, fname, codephrase, true, true) + errChan <- c.sendReceive("localhost", c.RelayWebsocketPort, c.RelayTCPPorts, fname, codephrase, true, true) }() } else { waitingFor = 1 @@ -106,7 +106,7 @@ func (c *Croc) Receive(codephrase string) (err error) { if err == nil { if resp.StatusCode == http.StatusOK { // we connected, so use this - return c.sendReceive(discovered[0].Address, strings.TrimSpace(ports[0]), strings.TrimSpace(ports[1]), "", codephrase, false, true) + return c.sendReceive(discovered[0].Address, strings.TrimSpace(ports[0]), strings.TrimSpace(strings.Split(ports[1], ",")), "", codephrase, false, true) } } else { log.Debugf("could not connect: %s", err.Error()) @@ -125,7 +125,7 @@ func (c *Croc) Receive(codephrase string) (err error) { return errors.New("must use local or public relay") } -func (c *Croc) sendReceive(address, websocketPort, tcpPort, fname, codephrase string, isSender bool, isLocal bool) (err error) { +func (c *Croc) sendReceive(address, websocketPort string, tcpPorts []string, fname string, codephrase string, isSender bool, isLocal bool) (err error) { defer log.Flush() if len(codephrase) < 4 { return fmt.Errorf("codephrase is too short") @@ -157,9 +157,9 @@ func (c *Croc) sendReceive(address, websocketPort, tcpPort, fname, codephrase st } if isSender { - go sender.Send(c.ForceSend, address, tcpPort, isLocal, done, sock, fname, codephrase, c.UseCompression, c.UseEncryption) + go sender.Send(c.ForceSend, address, tcpPorts, isLocal, done, sock, fname, codephrase, c.UseCompression, c.UseEncryption) } else { - go recipient.Receive(c.ForceSend, address, tcpPort, isLocal, done, sock, codephrase, c.NoRecipientPrompt, c.Stdout) + go recipient.Receive(c.ForceSend, address, tcpPorts, isLocal, done, sock, codephrase, c.NoRecipientPrompt, c.Stdout) } for { @@ -192,5 +192,5 @@ func (c *Croc) sendReceive(address, websocketPort, tcpPort, fname, codephrase st // Relay will start a relay on the specified port func (c *Croc) Relay() (err error) { - return relay.Run(c.RelayWebsocketPort, c.RelayTCPPort) + return relay.Run(c.RelayWebsocketPort, c.RelayTCPPorts) } diff --git a/src/relay/relay.go b/src/relay/relay.go index 42aa5966..e713e446 100644 --- a/src/relay/relay.go +++ b/src/relay/relay.go @@ -12,11 +12,13 @@ import ( var DebugLevel string // Run is the async operation for running a server -func Run(port string, tcpPort string) (err error) { +func Run(port string, tcpPorts []string) (err error) { logger.SetLogLevel(DebugLevel) - if tcpPort != "" { - go tcp.Run(DebugLevel, tcpPort) + if len(tcpPorts) > 0 { + for _, tcpPort := range tcpPorts { + go tcp.Run(DebugLevel, tcpPort) + } } go h.run()