mirror of https://github.com/schollz/croc.git
commit
35106d4dbe
|
@ -198,6 +198,7 @@ func send(c *cli.Context) (err error) {
|
||||||
NoCompress: c.Bool("no-compress"),
|
NoCompress: c.Bool("no-compress"),
|
||||||
Overwrite: c.Bool("overwrite"),
|
Overwrite: c.Bool("overwrite"),
|
||||||
Curve: c.String("curve"),
|
Curve: c.String("curve"),
|
||||||
|
HashAlgorithm: "xxhash",
|
||||||
}
|
}
|
||||||
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
|
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
|
||||||
crocOptions.RelayAddress6 = ""
|
crocOptions.RelayAddress6 = ""
|
||||||
|
@ -395,6 +396,7 @@ func receive(c *cli.Context) (err error) {
|
||||||
IP: c.String("ip"),
|
IP: c.String("ip"),
|
||||||
Overwrite: c.Bool("overwrite"),
|
Overwrite: c.Bool("overwrite"),
|
||||||
Curve: c.String("curve"),
|
Curve: c.String("curve"),
|
||||||
|
HashAlgorithm: "xxhash",
|
||||||
}
|
}
|
||||||
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
|
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
|
||||||
crocOptions.RelayAddress6 = ""
|
crocOptions.RelayAddress6 = ""
|
||||||
|
|
|
@ -67,6 +67,7 @@ type Options struct {
|
||||||
IP string
|
IP string
|
||||||
Overwrite bool
|
Overwrite bool
|
||||||
Curve string
|
Curve string
|
||||||
|
HashAlgorithm string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client holds the state of the croc transfer
|
// Client holds the state of the croc transfer
|
||||||
|
@ -149,6 +150,7 @@ type SenderInfo struct {
|
||||||
Ask bool
|
Ask bool
|
||||||
SendingText bool
|
SendingText bool
|
||||||
NoCompress bool
|
NoCompress bool
|
||||||
|
HashAlgorithm string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New establishes a new connection for transferring files between two instances.
|
// New establishes a new connection for transferring files between two instances.
|
||||||
|
@ -222,8 +224,11 @@ func (c *Client) sendCollectFiles(options TransferOptions) (err error) {
|
||||||
}
|
}
|
||||||
log.Debugf("%+v", c.FilesToTransfer[i])
|
log.Debugf("%+v", c.FilesToTransfer[i])
|
||||||
}
|
}
|
||||||
|
if c.Options.HashAlgorithm == "" {
|
||||||
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath)
|
c.Options.HashAlgorithm = "xxhash"
|
||||||
|
}
|
||||||
|
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm)
|
||||||
|
log.Debugf("hashed %s to %x using %s", fullPath, c.FilesToTransfer[i].Hash, c.Options.HashAlgorithm)
|
||||||
totalFilesSize += fstats.Size()
|
totalFilesSize += fstats.Size()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -769,6 +774,11 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
||||||
}
|
}
|
||||||
c.Options.SendingText = senderInfo.SendingText
|
c.Options.SendingText = senderInfo.SendingText
|
||||||
c.Options.NoCompress = senderInfo.NoCompress
|
c.Options.NoCompress = senderInfo.NoCompress
|
||||||
|
c.Options.HashAlgorithm = senderInfo.HashAlgorithm
|
||||||
|
if c.Options.HashAlgorithm == "" {
|
||||||
|
c.Options.HashAlgorithm = "imohash"
|
||||||
|
}
|
||||||
|
log.Debugf("using hash algorithm: %s", c.Options.HashAlgorithm)
|
||||||
if c.Options.NoCompress {
|
if c.Options.NoCompress {
|
||||||
log.Debug("disabling compression")
|
log.Debug("disabling compression")
|
||||||
}
|
}
|
||||||
|
@ -1047,6 +1057,7 @@ func (c *Client) updateIfSenderChannelSecured() (err error) {
|
||||||
Ask: c.Options.Ask,
|
Ask: c.Options.Ask,
|
||||||
SendingText: c.Options.SendingText,
|
SendingText: c.Options.SendingText,
|
||||||
NoCompress: c.Options.NoCompress,
|
NoCompress: c.Options.NoCompress,
|
||||||
|
HashAlgorithm: c.Options.HashAlgorithm,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
@ -1232,11 +1243,17 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
||||||
if _, ok := c.FilesHasFinished[i]; ok {
|
if _, ok := c.FilesHasFinished[i]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Debugf("checking %+v", fileInfo)
|
|
||||||
if i < c.FilesToTransferCurrentNum {
|
if i < c.FilesToTransferCurrentNum {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fileHash, errHash := utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name))
|
log.Debugf("checking %+v", fileInfo)
|
||||||
|
recipientFileInfo, errRecipientFile := os.Lstat(path.Join(fileInfo.FolderRemote, fileInfo.Name))
|
||||||
|
var errHash error
|
||||||
|
var fileHash []byte
|
||||||
|
if errRecipientFile == nil && recipientFileInfo.Size() == fileInfo.Size {
|
||||||
|
// the file exists, but is same size, so hash it
|
||||||
|
fileHash, errHash = utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name), c.Options.HashAlgorithm)
|
||||||
|
}
|
||||||
if fileInfo.Size == 0 || fileInfo.Symlink != "" {
|
if fileInfo.Size == 0 || fileInfo.Symlink != "" {
|
||||||
err = c.createEmptyFileAndFinish(fileInfo, i)
|
err = c.createEmptyFileAndFinish(fileInfo, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1248,8 +1265,9 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
||||||
}
|
}
|
||||||
log.Debugf("%s %+x %+x %+v", fileInfo.Name, fileHash, fileInfo.Hash, errHash)
|
log.Debugf("%s %+x %+x %+v", fileInfo.Name, fileHash, fileInfo.Hash, errHash)
|
||||||
if !bytes.Equal(fileHash, fileInfo.Hash) {
|
if !bytes.Equal(fileHash, fileInfo.Hash) {
|
||||||
|
log.Debugf("hashed %s to %x using %s", fileInfo.Name, fileHash, c.Options.HashAlgorithm)
|
||||||
log.Debugf("hashes are not equal %x != %x", fileHash, fileInfo.Hash)
|
log.Debugf("hashes are not equal %x != %x", fileHash, fileInfo.Hash)
|
||||||
if errHash == nil && !c.Options.Overwrite {
|
if errHash == nil && !c.Options.Overwrite && errRecipientFile == nil {
|
||||||
log.Debug("asking to overwrite")
|
log.Debug("asking to overwrite")
|
||||||
ans := utils.GetInput(fmt.Sprintf("\nOverwrite '%s'? (y/n) ", path.Join(fileInfo.FolderRemote, fileInfo.Name)))
|
ans := utils.GetInput(fmt.Sprintf("\nOverwrite '%s'? (y/n) ", path.Join(fileInfo.FolderRemote, fileInfo.Name)))
|
||||||
if strings.TrimSpace(strings.ToLower(ans)) != "y" {
|
if strings.TrimSpace(strings.ToLower(ans)) != "y" {
|
||||||
|
@ -1275,7 +1293,6 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
||||||
c.LastFolder = newFolder
|
c.LastFolder = newFolder
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// TODO: print out something about this file already existing
|
|
||||||
}
|
}
|
||||||
err = c.recipientGetFileReady(finished)
|
err = c.recipientGetFileReady(finished)
|
||||||
return
|
return
|
||||||
|
|
|
@ -43,9 +43,8 @@ func GetInput(prompt string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// HashFile returns the hash of a file or, in case of a symlink, the
|
// HashFile returns the hash of a file or, in case of a symlink, the
|
||||||
// SHA256 hash of its target
|
// SHA256 hash of its target. Takes an argument to specify the algorithm to use.
|
||||||
// HashFile returns the hash of a file
|
func HashFile(fname string, algorithm string) (hash256 []byte, err error) {
|
||||||
func HashFile(fname string) (hash256 []byte, err error) {
|
|
||||||
var fstats os.FileInfo
|
var fstats os.FileInfo
|
||||||
fstats, err = os.Lstat(fname)
|
fstats, err = os.Lstat(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,7 +58,16 @@ func HashFile(fname string) (hash256 []byte, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return IMOHashFile(fname)
|
switch algorithm {
|
||||||
|
case "imohash":
|
||||||
|
return IMOHashFile(fname)
|
||||||
|
case "md5":
|
||||||
|
return MD5HashFile(fname)
|
||||||
|
case "xxhash":
|
||||||
|
return XXHashFile(fname)
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("unspecified algorithm")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MD5HashFile returns MD5 hash
|
// MD5HashFile returns MD5 hash
|
||||||
|
|
|
@ -175,9 +175,9 @@ func TestHashFile(t *testing.T) {
|
||||||
if err := tmpfile.Close(); err != nil {
|
if err := tmpfile.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
hashed, err := HashFile(tmpfile.Name())
|
hashed, err := HashFile(tmpfile.Name(), "xxhash")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "18c9673a4bb8325d323e7f24fda9ae1e", fmt.Sprintf("%x", hashed))
|
assert.Equal(t, "e66c561610ad51e2", fmt.Sprintf("%x", hashed))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPublicIP(t *testing.T) {
|
func TestPublicIP(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue