mirror of https://github.com/schollz/croc.git
multiple tcp ports
This commit is contained in:
parent
a7b8488040
commit
667edd0373
9
main.go
9
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
humanize "github.com/dustin/go-humanize"
|
humanize "github.com/dustin/go-humanize"
|
||||||
|
@ -63,7 +64,7 @@ func main() {
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
cli.StringFlag{Name: "addr", Value: "198.199.67.130", Usage: "address of the public relay"},
|
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-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: "no-local", Usage: "disable local mode"},
|
||||||
cli.BoolFlag{Name: "local", Usage: "use only local mode"},
|
cli.BoolFlag{Name: "local", Usage: "use only local mode"},
|
||||||
cli.BoolFlag{Name: "debug", Usage: "increase verbosity (a lot)"},
|
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-tcp", Usage: "force TCP"},
|
||||||
cli.BoolFlag{Name: "force-web", Usage: "force websockets"},
|
cli.BoolFlag{Name: "force-web", Usage: "force websockets"},
|
||||||
cli.StringFlag{Name: "port", Value: "8153", Usage: "port that the websocket listens on"},
|
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)"},
|
cli.StringFlag{Name: "curve", Value: "siec", Usage: "specify elliptic curve to use (p224, p256, p384, p521, siec)"},
|
||||||
}
|
}
|
||||||
app.EnableBashCompletion = true
|
app.EnableBashCompletion = true
|
||||||
|
@ -88,7 +89,7 @@ func main() {
|
||||||
cr = croc.Init(c.GlobalBool("debug"))
|
cr = croc.Init(c.GlobalBool("debug"))
|
||||||
cr.AllowLocalDiscovery = true
|
cr.AllowLocalDiscovery = true
|
||||||
cr.Address = c.GlobalString("addr")
|
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.AddressWebsocketPort = c.GlobalString("addr-ws")
|
||||||
cr.NoRecipientPrompt = c.GlobalBool("yes")
|
cr.NoRecipientPrompt = c.GlobalBool("yes")
|
||||||
cr.Stdout = c.GlobalBool("stdout")
|
cr.Stdout = c.GlobalBool("stdout")
|
||||||
|
@ -96,7 +97,7 @@ func main() {
|
||||||
cr.NoLocal = c.GlobalBool("no-local")
|
cr.NoLocal = c.GlobalBool("no-local")
|
||||||
cr.ShowText = true
|
cr.ShowText = true
|
||||||
cr.RelayWebsocketPort = c.String("port")
|
cr.RelayWebsocketPort = c.String("port")
|
||||||
cr.RelayTCPPort = c.String("tcp-port")
|
cr.RelayTCPPorts = strings.Split(c.String("tcp-port"), ",")
|
||||||
cr.CurveType = c.String("curve")
|
cr.CurveType = c.String("curve")
|
||||||
if c.GlobalBool("force-tcp") {
|
if c.GlobalBool("force-tcp") {
|
||||||
cr.ForceSend = 2
|
cr.ForceSend = 2
|
||||||
|
|
|
@ -24,12 +24,12 @@ type Croc struct {
|
||||||
|
|
||||||
// Options for relay
|
// Options for relay
|
||||||
RelayWebsocketPort string
|
RelayWebsocketPort string
|
||||||
RelayTCPPort string
|
RelayTCPPorts []string
|
||||||
CurveType string
|
CurveType string
|
||||||
|
|
||||||
// Options for connecting to server
|
// Options for connecting to server
|
||||||
Address string
|
Address string
|
||||||
AddressTCPPort string
|
AddressTCPPorts []string
|
||||||
AddressWebsocketPort string
|
AddressWebsocketPort string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
LocalOnly bool
|
LocalOnly bool
|
||||||
|
@ -61,11 +61,11 @@ func Init(debug bool) (c *Croc) {
|
||||||
c.UseEncryption = true
|
c.UseEncryption = true
|
||||||
c.AllowLocalDiscovery = true
|
c.AllowLocalDiscovery = true
|
||||||
c.RelayWebsocketPort = "8153"
|
c.RelayWebsocketPort = "8153"
|
||||||
c.RelayTCPPort = "8154"
|
c.RelayTCPPorts = []string{"8154", "8155", "8156", "8156"}
|
||||||
c.CurveType = "siec"
|
c.CurveType = "siec"
|
||||||
c.Address = "198.199.67.130"
|
c.Address = "198.199.67.130"
|
||||||
c.AddressWebsocketPort = "8153"
|
c.AddressWebsocketPort = "8153"
|
||||||
c.AddressTCPPort = "8154"
|
c.AddressTCPPorts = []string{"8154", "8155", "8156", "8156"}
|
||||||
c.NoRecipientPrompt = true
|
c.NoRecipientPrompt = true
|
||||||
debugLevel := "info"
|
debugLevel := "info"
|
||||||
if debug {
|
if debug {
|
||||||
|
|
|
@ -40,7 +40,7 @@ func (c *Croc) Send(fname, codephrase string) (err error) {
|
||||||
if !c.NoLocal {
|
if !c.NoLocal {
|
||||||
go func() {
|
go func() {
|
||||||
// start own relay and connect to it
|
// 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 :(
|
time.Sleep(250 * time.Millisecond) // race condition here, but this should work most of the time :(
|
||||||
|
|
||||||
// broadcast for peer discovery
|
// broadcast for peer discovery
|
||||||
|
@ -50,12 +50,12 @@ func (c *Croc) Send(fname, codephrase string) (err error) {
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
TimeLimit: 600 * time.Second,
|
TimeLimit: 600 * time.Second,
|
||||||
Delay: 50 * time.Millisecond,
|
Delay: 50 * time.Millisecond,
|
||||||
Payload: []byte(c.RelayWebsocketPort + "- " + c.RelayTCPPort),
|
Payload: []byte(c.RelayWebsocketPort + "- " + strings.Join(c.RelayTCPPorts, ",")),
|
||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// connect to own relay
|
// 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 {
|
} else {
|
||||||
waitingFor = 1
|
waitingFor = 1
|
||||||
|
@ -106,7 +106,7 @@ func (c *Croc) Receive(codephrase string) (err error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode == http.StatusOK {
|
||||||
// we connected, so use this
|
// 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 {
|
} else {
|
||||||
log.Debugf("could not connect: %s", err.Error())
|
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")
|
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()
|
defer log.Flush()
|
||||||
if len(codephrase) < 4 {
|
if len(codephrase) < 4 {
|
||||||
return fmt.Errorf("codephrase is too short")
|
return fmt.Errorf("codephrase is too short")
|
||||||
|
@ -157,9 +157,9 @@ func (c *Croc) sendReceive(address, websocketPort, tcpPort, fname, codephrase st
|
||||||
}
|
}
|
||||||
|
|
||||||
if isSender {
|
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 {
|
} 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 {
|
for {
|
||||||
|
@ -192,5 +192,5 @@ func (c *Croc) sendReceive(address, websocketPort, tcpPort, fname, codephrase st
|
||||||
|
|
||||||
// Relay will start a relay on the specified port
|
// Relay will start a relay on the specified port
|
||||||
func (c *Croc) Relay() (err error) {
|
func (c *Croc) Relay() (err error) {
|
||||||
return relay.Run(c.RelayWebsocketPort, c.RelayTCPPort)
|
return relay.Run(c.RelayWebsocketPort, c.RelayTCPPorts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,11 +12,13 @@ import (
|
||||||
var DebugLevel string
|
var DebugLevel string
|
||||||
|
|
||||||
// Run is the async operation for running a server
|
// 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)
|
logger.SetLogLevel(DebugLevel)
|
||||||
|
|
||||||
if tcpPort != "" {
|
if len(tcpPorts) > 0 {
|
||||||
go tcp.Run(DebugLevel, tcpPort)
|
for _, tcpPort := range tcpPorts {
|
||||||
|
go tcp.Run(DebugLevel, tcpPort)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go h.run()
|
go h.run()
|
||||||
|
|
Loading…
Reference in New Issue