diff --git a/src/cli/cli.go b/src/cli/cli.go index 36fcaf3a..748ea66c 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -85,6 +85,7 @@ func Run() (err error) { &cli.BoolFlag{Name: "no-compress", Usage: "disable compression"}, &cli.BoolFlag{Name: "ask", Usage: "make sure sender and recipient are prompted"}, &cli.BoolFlag{Name: "local", Usage: "force to use only local connections"}, + &cli.StringFlag{Name: "ip", Value: "", Usage: "set sender ip if known e.g. 10.0.0.1:9009, [::1]:9009"}, &cli.StringFlag{Name: "relay", Value: models.DEFAULT_RELAY, Usage: "address of the relay", EnvVars: []string{"CROC_RELAY"}}, &cli.StringFlag{Name: "relay6", Value: models.DEFAULT_RELAY6, Usage: "ipv6 address of the relay", EnvVars: []string{"CROC_RELAY6"}}, &cli.StringFlag{Name: "out", Value: ".", Usage: "specify an output folder to receive the file"}, @@ -376,6 +377,7 @@ func receive(c *cli.Context) (err error) { Ask: c.Bool("ask"), RelayPassword: determinePass(c), OnlyLocal: c.Bool("local"), + IP: c.String("ip"), } if crocOptions.RelayAddress != models.DEFAULT_RELAY { crocOptions.RelayAddress6 = "" diff --git a/src/croc/croc.go b/src/croc/croc.go index 7073f404..03f4783c 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -64,6 +64,7 @@ type Options struct { Ask bool SendingText bool NoCompress bool + IP string } // Client holds the state of the croc transfer @@ -487,11 +488,27 @@ func (c *Client) Receive() (err error) { // recipient will look for peers first // and continue if it doesn't find any within 100 ms usingLocal := false - if c.Options.OnlyLocal { + isIPset := false + + if c.Options.OnlyLocal || c.Options.IP != "" { c.Options.RelayAddress = "" c.Options.RelayAddress6 = "" } - if !c.Options.DisableLocal { + + if c.Options.IP != "" { + // check ip version + if strings.Count(c.Options.IP, ":") >= 2 { + log.Debug("assume ipv6") + c.Options.RelayAddress6 = c.Options.IP + } + if strings.Contains(c.Options.IP, ".") { + log.Debug("assume ipv4") + c.Options.RelayAddress = c.Options.IP + } + isIPset = true + } + + if !c.Options.DisableLocal && !isIPset { log.Debug("attempt to discover peers") var discoveries []peerdiscovery.Discovered var wgDiscovery sync.WaitGroup @@ -589,7 +606,7 @@ func (c *Client) Receive() (err error) { log.Debugf("receiver connection established: %+v", c.conn[0]) log.Debugf("banner: %s", banner) - if !usingLocal && !c.Options.DisableLocal { + if !usingLocal && !c.Options.DisableLocal && !isIPset { // ask the sender for their local ips and port // and try to connect to them log.Debug("sending ips?") diff --git a/src/utils/utils.go b/src/utils/utils.go index 9e4bc49d..cc74bf6b 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -136,7 +136,7 @@ func GetRandomName() string { // ByteCountDecimal converts bytes to human readable byte string func ByteCountDecimal(b int64) string { - const unit = 1000 + const unit = 1024 if b < unit { return fmt.Sprintf("%d B", b) } diff --git a/src/utils/utils_test.go b/src/utils/utils_test.go index 101162e5..8e3acaa1 100644 --- a/src/utils/utils_test.go +++ b/src/utils/utils_test.go @@ -88,9 +88,9 @@ func TestSHA256(t *testing.T) { } func TestByteCountDecimal(t *testing.T) { - assert.Equal(t, "10.0 kB", ByteCountDecimal(10000)) + assert.Equal(t, "10.0 kB", ByteCountDecimal(10240)) assert.Equal(t, "50 B", ByteCountDecimal(50)) - assert.Equal(t, "12.4 MB", ByteCountDecimal(12378517)) + assert.Equal(t, "12.4 MB", ByteCountDecimal(13002343)) } func TestMissingChunks(t *testing.T) {