golang跨计算机安全传输文件和文件夹插件库croc的使用
Golang跨计算机安全传输文件和文件夹插件库croc的使用
关于croc
croc
是一个工具,它允许任何两台计算机简单且安全地传输文件和文件夹。据我所知,croc是唯一一个CLI文件传输工具,它实现了以下所有功能:
- 允许任意两台计算机传输数据(使用中继)
- 提供端到端加密(使用PAKE)
- 实现简单的跨平台传输(Windows、Linux、Mac)
- 允许多文件传输
- 允许恢复中断的传输
- 不需要本地服务器或端口转发
- IPv6优先,IPv4作为备用
- 可以使用代理,如Tor
安装
通用安装方法
curl https://getcroc.schollz.com | bash
在macOS上
使用Homebrew:
brew install croc
使用MacPorts:
sudo port selfupdate
sudo port install croc
在Windows上
使用Scoop、Chocolatey或Winget:
scoop install croc
或
choco install croc
或
winget install schollz.croc
从源代码构建
需要Go 1.22+:
go install github.com/schollz/croc/v10@latest
使用示例
基本文件传输
发送文件:
$ croc send [file(s)-or-folder]
Sending 'file-or-folder' (X MB)
Code is: code-phrase
接收文件:
croc code-phrase
使用自定义代码短语
croc send --code [code-phrase] [file(s)-or-folder]
发送文本
croc send --text "hello world"
使用管道
发送:
cat [filename] | croc send
接收:
croc --yes [code-phrase] > out
完整的Go示例代码
package main
import (
"fmt"
"os/exec"
)
func main() {
// 示例1:发送文件
sendCmd := exec.Command("croc", "send", "example.txt")
output, err := sendCmd.CombinedOutput()
if err != nil {
fmt.Printf("发送文件失败: %v\n", err)
return
}
fmt.Printf("发送成功: %s\n", output)
// 示例2:接收文件(需要手动输入代码短语)
// 在实际应用中,你可能需要从用户输入或其他方式获取代码短语
receiveCmd := exec.Command("croc", "123-456-789")
output, err = receiveCmd.CombinedOutput()
if err != nil {
fmt.Printf("接收文件失败: %v\n", err)
return
}
fmt.Printf("接收成功: %s\n", output)
// 示例3:发送文本
sendTextCmd := exec.Command("croc", "send", "--text", "Hello from Go!")
output, err = sendTextCmd.CombinedOutput()
if err != nil {
fmt.Printf("发送文本失败: %v\n", err)
return
}
fmt.Printf("文本发送成功: %s\n", output)
}
高级功能
使用代理
croc --socks5 "127.0.0.1:9050" send SOMEFILE
自托管中继服务器
运行中继:
croc relay
使用自定义中继发送文件:
croc --relay "myrelay.example.com:9009" send [filename]
Docker运行中继
docker run -d -p 9009-9013:9009-9013 -e CROC_PASS='YOURPASSWORD' schollz/croc
注意事项
在Linux和macOS上,为了安全考虑,接收文件时应使用环境变量传递密码:
CROC_SECRET=*** croc
croc是一个功能强大且安全的文件传输工具,特别适合在不同计算机之间快速共享文件,同时保证数据传输的安全性。
更多关于golang跨计算机安全传输文件和文件夹插件库croc的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复