From 9aa327a0a4bdf2e40c5fc30a1bb634f70fcbc0fa Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Wed, 23 Oct 2019 14:59:47 -0700 Subject: [PATCH] use machine ID --- go.mod | 3 ++- go.sum | 4 ++++ src/croc/croc.go | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 9f2a4b4..b716fb8 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/OneOfOne/xxhash v1.2.5 // indirect github.com/cespare/xxhash v1.1.0 github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect + github.com/denisbrodbeck/machineid v1.0.1 github.com/fatih/color v1.7.0 // indirect github.com/kalafut/imohash v1.0.0 github.com/kr/pretty v0.1.0 // indirect @@ -23,7 +24,7 @@ require ( github.com/urfave/cli v1.22.1 golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc golang.org/x/net v0.0.0-20191003171128-d98b1b443823 // indirect - golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed // indirect + golang.org/x/sys v0.0.0-20191023151326-f89234f9a2c2 // indirect golang.org/x/text v0.3.2 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index 42b802e..2dccc0e 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ= +github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/kalafut/imohash v1.0.0 h1:LgCJ+p/BwM2HKpOxFopkeddpzVCfm15EtXMroXD1SYE= @@ -83,6 +85,8 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed h1:5TJcLJn2a55mJjzYk0yOoqN8X1OdvBDUnaZaKKyQtkY= golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191023151326-f89234f9a2c2 h1:I7efaDQAsIQmkTF+WSdcydwVWzK07Yuz8IFF8rNkDe0= +golang.org/x/sys v0.0.0-20191023151326-f89234f9a2c2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= diff --git a/src/croc/croc.go b/src/croc/croc.go index ed570a1..b84a579 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -17,6 +17,7 @@ import ( "sync" "time" + "github.com/denisbrodbeck/machineid" "github.com/pkg/errors" "github.com/schollz/croc/v6/src/comm" "github.com/schollz/croc/v6/src/compress" @@ -124,11 +125,13 @@ type FileInfo struct { type RemoteFileRequest struct { CurrentFileChunkRanges []int64 FilesToTransferCurrentNum int + MachineID string } // SenderInfo lists the files to be transferred type SenderInfo struct { FilesToTransfer []FileInfo + MachineID string } // New establishes a new connection for transferring files between two instances. @@ -304,6 +307,10 @@ func (c *Client) Send(options TransferOptions) (err error) { otherRelay = "--relay " + c.Options.RelayAddress + " " } fmt.Fprintf(os.Stderr, "Code is: %s\nOn the other computer run\n\ncroc %s%s\n", c.Options.SharedSecret, otherRelay, c.Options.SharedSecret) + if c.Options.Ask { + machid, _ := machineid.ID() + fmt.Fprintf(os.Stderr, "\nYour machine ID is '%s'\n", machid) + } // // c.spinner.Suffix = " waiting for recipient..." // c.spinner.Start() // create channel for quitting @@ -521,7 +528,12 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error } // c.spinner.Stop() if !c.Options.NoPrompt || c.Options.Ask { - fmt.Fprintf(os.Stderr, "\rAccept %s (%s)? (y/n) ", fname, utils.ByteCountDecimal(totalSize)) + if c.Options.Ask && senderInfo.MachineID != "" { + machID, _ := machineid.ID() + fmt.Fprintf(os.Stderr, "\rYour machine id is '%s'.\nAccept %s (%s) from '%s'? (y/n) ", machID, fname, utils.ByteCountDecimal(totalSize), senderInfo.MachineID) + } else { + fmt.Fprintf(os.Stderr, "\rAccept %s (%s)? (y/n) ", fname, utils.ByteCountDecimal(totalSize)) + } if strings.ToLower(strings.TrimSpace(utils.GetInput(""))) != "y" { err = message.Send(c.conn[0], c.Key, message.Message{ Type: "error", @@ -667,6 +679,19 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) { c.chunkMap[uint64(chunk)] = struct{}{} } c.Step3RecipientRequestFile = true + + if c.Options.Ask { + fmt.Fprintf(os.Stderr, "\rSend to machine '%s'? (y/n) ", remoteFile.MachineID) + if strings.ToLower(strings.TrimSpace(utils.GetInput(""))) != "y" { + err = message.Send(c.conn[0], c.Key, message.Message{ + Type: "error", + Message: "refusing files", + }) + done = true + err = fmt.Errorf("refused files") + return + } + } case "close-sender": c.bar.Finish() log.Debug("close-sender received...") @@ -695,8 +720,10 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) { func (c *Client) updateIfSenderChannelSecured() (err error) { if c.Options.IsSender && c.Step1ChannelSecured && !c.Step2FileInfoTransfered { var b []byte + machID, _ := machineid.ID() b, err = json.Marshal(SenderInfo{ FilesToTransfer: c.FilesToTransfer, + MachineID: machID, }) if err != nil { log.Error(err) @@ -785,9 +812,11 @@ func (c *Client) recipientGetFileReady(finished bool) (err error) { } c.TotalSent = 0 + machID, _ := machineid.ID() bRequest, _ := json.Marshal(RemoteFileRequest{ CurrentFileChunkRanges: c.CurrentFileChunkRanges, FilesToTransferCurrentNum: c.FilesToTransferCurrentNum, + MachineID: machID, }) log.Debug("converting to chunk range") c.CurrentFileChunks = utils.ChunkRangesToChunks(c.CurrentFileChunkRanges) @@ -901,17 +930,6 @@ func (c *Client) updateState() (err error) { } if c.Options.IsSender && c.Step3RecipientRequestFile && !c.Step4FileTransfer { - if c.Options.Ask { - fmt.Fprintf(os.Stderr, "\rSend to X? (y/n) ") - if strings.ToLower(strings.TrimSpace(utils.GetInput(""))) != "y" { - err = message.Send(c.conn[0], c.Key, message.Message{ - Type: "error", - Message: "refusing files", - }) - return fmt.Errorf("refused files") - } - } - log.Debug("start sending data!") if !c.firstSend {