From f6973ba2573054fcc8d6c38d23bb1d566a36901f Mon Sep 17 00:00:00 2001 From: Nhung Ngo Date: Wed, 30 Aug 2023 11:53:07 +0700 Subject: [PATCH 1/3] add printing receive command as qrcode --- go.mod | 2 ++ go.sum | 2 ++ src/cli/cli.go | 2 ++ src/croc/croc.go | 13 +++++++++++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 5e9a79bc2..9640c182c 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,8 @@ require ( golang.org/x/time v0.3.0 ) +require github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect + require ( github.com/OneOfOne/xxhash v1.2.8 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect diff --git a/go.sum b/go.sum index afae9c75c..a8a7d3390 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/schollz/peerdiscovery v1.7.0/go.mod h1:BouGdURKIFl7OGez+2lyZJKD+2tBQ2 github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE= github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= diff --git a/src/cli/cli.go b/src/cli/cli.go index 64502fe40..cd39a0837 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -89,6 +89,7 @@ func Run() (err error) { } app.Flags = []cli.Flag{ &cli.BoolFlag{Name: "internal-dns", Usage: "use a built-in DNS stub resolver rather than the host operating system"}, + &cli.BoolFlag{Name: "qrcode", Usage: "show receive command as qrcode"}, &cli.BoolFlag{Name: "remember", Usage: "save these settings to reuse next time"}, &cli.BoolFlag{Name: "debug", Usage: "toggle debug mode"}, &cli.BoolFlag{Name: "yes", Usage: "automatically agree to all prompts"}, @@ -198,6 +199,7 @@ func send(c *cli.Context) (err error) { HashAlgorithm: c.String("hash"), ThrottleUpload: c.String("throttleUpload"), ZipFolder: c.Bool("zip"), + ShowQrCode: c.Bool("qrcode"), } if crocOptions.RelayAddress != models.DEFAULT_RELAY { crocOptions.RelayAddress6 = "" diff --git a/src/croc/croc.go b/src/croc/croc.go index 5ec7866a9..7904f4778 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -33,7 +33,7 @@ import ( "github.com/schollz/croc/v9/src/tcp" "github.com/schollz/croc/v9/src/utils" ) - +import qrcode "github.com/skip2/go-qrcode" var ( ipRequest = []byte("ips?") handshakeRequest = []byte("handshake") @@ -77,6 +77,7 @@ type Options struct { ThrottleUpload string ZipFolder bool TestFlag bool + ShowQrCode bool } // Client holds the state of the croc transfer @@ -529,6 +530,9 @@ func (c *Client) Send(filesInfo []FileInfo, emptyFoldersToTransfer []FileInfo, t flags.WriteString("--pass " + c.Options.RelayPassword + " ") } fmt.Fprintf(os.Stderr, "Code is: %[1]s\nOn the other computer run\n\ncroc %[2]s%[1]s\n", c.Options.SharedSecret, flags.String()) + if c.Options.ShowQrCode { + showReceiveCommandQrCode(fmt.Sprintf("croc %[2]s%[1]s", c.Options.SharedSecret, flags.String())) + } if c.Options.Ask { machid, _ := machineid.ID() fmt.Fprintf(os.Stderr, "\rYour machine ID is '%s'\n", machid) @@ -653,7 +657,12 @@ func (c *Client) Send(filesInfo []FileInfo, emptyFoldersToTransfer []FileInfo, t } return err } - +func showReceiveCommandQrCode(command string){ + qrCode, err := qrcode.New(command, qrcode.Medium) + if err == nil { + fmt.Println(qrCode.ToSmallString(false)) + } +} // Receive will receive a file func (c *Client) Receive() (err error) { fmt.Fprintf(os.Stderr, "connecting...") From 911458a2854bc1e47f9fafc5c5d87af2c4987f4d Mon Sep 17 00:00:00 2001 From: Nhung Ngo Date: Wed, 30 Aug 2023 12:00:35 +0700 Subject: [PATCH 2/3] merge import --- src/croc/croc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index 7904f4778..8a1009d79 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -32,8 +32,8 @@ import ( "github.com/schollz/croc/v9/src/models" "github.com/schollz/croc/v9/src/tcp" "github.com/schollz/croc/v9/src/utils" + qrcode "github.com/skip2/go-qrcode" ) -import qrcode "github.com/skip2/go-qrcode" var ( ipRequest = []byte("ips?") handshakeRequest = []byte("handshake") From d34c881411fb182b766db5f8f6e53464ce1a5d17 Mon Sep 17 00:00:00 2001 From: Nhung Ngo Date: Tue, 5 Sep 2023 10:05:57 +0700 Subject: [PATCH 3/3] move qr flag from application flags to send command flags and display only receive code as qr only --- src/cli/cli.go | 2 +- src/croc/croc.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index cd39a0837..ffb2ec055 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -65,6 +65,7 @@ func Run() (err error) { ArgsUsage: "[filename(s) or folder]", Flags: []cli.Flag{ &cli.BoolFlag{Name: "zip", Usage: "zip folder before sending"}, + &cli.BoolFlag{Name: "qrcode",Aliases: []string{"qr"}, Usage: "show receive code as a qrcode"}, &cli.StringFlag{Name: "code", Aliases: []string{"c"}, Usage: "codephrase used to connect to relay"}, &cli.StringFlag{Name: "hash", Value: "xxhash", Usage: "hash algorithm (xxhash, imohash, md5)"}, &cli.StringFlag{Name: "text", Aliases: []string{"t"}, Usage: "send some text"}, @@ -89,7 +90,6 @@ func Run() (err error) { } app.Flags = []cli.Flag{ &cli.BoolFlag{Name: "internal-dns", Usage: "use a built-in DNS stub resolver rather than the host operating system"}, - &cli.BoolFlag{Name: "qrcode", Usage: "show receive command as qrcode"}, &cli.BoolFlag{Name: "remember", Usage: "save these settings to reuse next time"}, &cli.BoolFlag{Name: "debug", Usage: "toggle debug mode"}, &cli.BoolFlag{Name: "yes", Usage: "automatically agree to all prompts"}, diff --git a/src/croc/croc.go b/src/croc/croc.go index 8a1009d79..c250d3191 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -531,7 +531,7 @@ func (c *Client) Send(filesInfo []FileInfo, emptyFoldersToTransfer []FileInfo, t } fmt.Fprintf(os.Stderr, "Code is: %[1]s\nOn the other computer run\n\ncroc %[2]s%[1]s\n", c.Options.SharedSecret, flags.String()) if c.Options.ShowQrCode { - showReceiveCommandQrCode(fmt.Sprintf("croc %[2]s%[1]s", c.Options.SharedSecret, flags.String())) + showReceiveCommandQrCode(fmt.Sprintf("%[1]s", c.Options.SharedSecret)) } if c.Options.Ask { machid, _ := machineid.ID()