37 个版本
| 0.15.1 | 2024 年 7 月 29 日 |
|---|---|
| 0.15.0 | 2024 年 3 月 27 日 |
| 0.14.4 | 2023 年 12 月 20 日 |
| 0.14.1 | 2023 年 11 月 2 日 |
| 0.1.3 | 2021 年 7 月 27 日 |
51 in HTTP 服务器
每月 211 次下载
130KB
2.5K SLoC
锈粘贴 是一个最小的文件上传/粘贴服务。
$ echo "some text" > awesome.txt
$ curl -F "file=@awesome.txt" https://paste.site.com
https://paste.site.com/safe-toad.txt
$ curl https://paste.site.com/safe-toad.txt
some text
目录
特性
- 文件上传 & URL 缩短 & 从 URL 上传
- 支持基本 HTTP 身份验证
- 随机文件名(可选)
- 宠物名(例如
capital-mosquito.txt) - 字母数字字符串(例如
yB84D2Dv.txt) - 随机后缀(例如
file.MRV5as.tar.gz)
- 宠物名(例如
- 支持过期链接
- 文件自动过期(可选)
- 自动删除过期的文件(可选)
- 支持一次性链接/URL(只能查看一次)
- 猜测 MIME 类型
- 支持覆盖和黑名单
- 支持通过
?download=true强制下载
- 无重复上传(可选)
- 列出/删除文件
- 自定义落地页面
- 单个二进制文件
- 简单的配置
- 支持热重载
- 易于部署
- 无数据库
- 使用文件系统
- 自托管
- 集中化是坏事!
- 用 Rust 编写
- 闪电般快速!
安装
从 crates.io
cargo install rustypaste
Arch Linux
pacman -S rustypaste
Alpine Linux
rustypaste 可在 Alpine Edge 上使用。通过启用 社区仓库 后,可以使用 apk 进行安装。
apk add rustypaste
FreeBSD
pkg install rustypaste
二进制发布
请在 发布页面 上查看可用的二进制文件。
从源代码构建
git clone https://github.com/orhun/rustypaste.git
cd rustypaste/
cargo build --release
特性标志
shuttle:为在 Shuttle 上部署提供入口点openssl:使用发行版的 OpenSSL(在发布模式下二进制大小减少约 20%)rustls:使用 rustls(默认启用)
要启用构建功能,请向 cargo build 命令传递 --features 标志。
例如,要重复使用发行版上已存在的 OpenSSL
cargo build --release --no-default-features --features openssl
测试
单元测试
cargo test -- --test-threads 1
测试固定装置
./fixtures/test-fixtures.sh
使用
独立的命令行工具(rpaste)可在 此处 获取。
命令行界面
function rpaste() {
curl -F "file=@$1" -H "Authorization: <auth_token>" "<server_address>"
}
* 考虑从文件中读取授权头。(例如 -H @rpaste_auth)
# upload a file
$ rpaste x.txt
# paste from stdin
$ rpaste -
过期
$ curl -F "file=@x.txt" -H "expire:10min" "<server_address>"
支持的单位
nsec、nsusec、usmsec、msseconds、second、sec、sminutes、minute、min、mhours、hour、hr、hdays、day、dweeks、week、wmonths、month、Myears、year、y
一次性文件
$ curl -F "oneshot=@x.txt" "<server_address>"
一次性 URL
$ curl -F "oneshot_url=https://example.com" "<server_address>"
URL 缩短
$ curl -F "url=https://example.com/some/long/url" "<server_address>"
从远程 URL 粘贴文件
$ curl -F "remote=https://example.com/file.png" "<server_address>"
清理过期的文件
配置 [paste].delete_expired_files 以设置自动删除过期文件的间隔。
另一方面,以下脚本可以用作 cron 来手动清理过期文件
#!/bin/env sh
now=$(date +%s)
find upload/ -maxdepth 2 -type f -iname "*.[0-9]*" |
while read -r filename; do
[ "$(( ${filename##*.} / 1000 - "${now}" ))" -lt 0 ] && rm -v "${filename}"
done
从服务器删除文件
在 config.toml 中设置 delete_tokens 数组以激活 DELETE 端点并使用一个(或多个)授权令牌(auth token)来保护它。
$ curl -H "Authorization: <auth_token>" -X DELETE "<server_address>/file.txt"
如果没有设置
delete_tokens,则不会公开DELETE端点,并将返回404错误。
使用 random_url 时覆盖文件名
可以通过发送名为 filename 的头信息来覆盖随机文件名的生成。
curl -F "file=@x.txt" -H "filename: <file_name>" "<server_address>"
服务器
要启动服务器
$ rustypaste
如果配置文件不在当前目录中,请通过 CONFIG 环境变量指定它
$ CONFIG="$HOME/.rustypaste.toml" rustypaste
要启用基本 HTTP 身份验证,请设置 AUTH_TOKEN 环境变量(通过 .env)
$ echo "AUTH_TOKEN=$(openssl rand -base64 16)" > .env
$ rustypaste
您也可以通过 config.toml 中的 [server].auth_tokens 数组字段设置多个授权令牌。
如果没有设置
AUTH_TOKEN或[server].auth_tokens,则服务器将不需要任何身份验证。异常是
DELETE端点,它需要至少设置一个令牌。有关更多信息,请参阅从服务器删除文件。
有关配置选项,请参阅config.toml。
列表端点
在 config.toml 中将 expose_list 设置为 true 以检索上传目录中文件的 JSON 格式列表。这不会包括单次文件、单次 URL 或 URL。
$ curl "http://<server_address>/list"
[{"file_name":"accepted-cicada.txt","file_size":241,"expires_at_utc":null}]
如果设置了,此路由将需要 AUTH_TOKEN。
HTML 表单
可以使用 HTML 表单上传文件。为此,您需要更新 config.toml 中的两个字段。
- 将
[landing_page].content_type设置为text/html; charset=utf-8。 - 使用您的 HTML 表单更新
[landing_page].text字段或指向您的 html 文件的[landing_page].file。
有关示例,请参阅 examples/html_form.toml
Docker
以下命令可以用于运行由本存储库中的 Dockerfile 构建的容器
$ docker run --rm -d \
-v "$(pwd)/upload/":/app/upload \
-v "$(pwd)/config.toml":/app/config.toml \
--env-file "$(pwd)/.env" \
-e "RUST_LOG=debug" \
-p 8000:8000 \
--name rustypaste \
orhunp/rustypaste
- 上传的文件放入
./upload(在主机机器上) - 通过
-e或--env-file设置AUTH_TOKEN以启用身份验证
您可以使用 docker build -t rustypaste . 命令构建此映像。
如果您想使用 docker compose 运行映像,只需运行 docker-compose up -d。 (见 docker-compose.yml)
Nginx
带有反向代理的服务器配置示例
server {
listen 80;
location / {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options "sameorigin";
add_header X-Content-Type-Options "nosniff";
}
}
如果在上传过程中遇到 413 请求实体过大 错误,请设置 nginx.conf 中的最大正文大小。
http {
# ...
client_max_body_size 100M;
}
贡献
欢迎提交拉取请求!
许可证
所有代码均根据 MIT 许可证 许可。依赖项
~30–46MB
~1M SLoC
