ITADN
orhun/rustypaste
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

GitHub Release Crate Release Coverage Continuous Integration Continuous Deployment Docker Builds Documentation

Rustypaste 是一个极简的文件上传/粘贴板服务。

$ 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(只能查看一次)
    • 支持受密码保护的文件
      • 自动生成密码
      • Argon2id 哈希
    • 猜测 MIME 类型
      • 支持覆盖和黑名单
      • 支持通过 ?download=true 强制下载
    • 无重复上传(可选)
    • 列出/删除文件
    • 自定义落地页
  • 单一二进制文件
  • 简单配置
    • 支持热重载
  • 易于部署
  • 无数据库
    • 使用文件系统
  • 自托管
    • 中心化是坏的!
  • 使用 Rust 编写
    • 极速!

安装

打包状态

Packaging status

来自 crates.io

cargo install rustypaste

Arch Linux

pacman -S rustypaste

Alpine Linux

rustypaste 可用于 Alpine Edge。启用 community repository 后,可通过 apk 进行安装。

apk add rustypaste

FreeBSD

pkg install rustypaste

二进制发布

请参阅 releases 页面] 上可用的二进制文件。

从源码构建

git clone https://github.com/orhun/rustypaste.git
cd rustypaste/
cargo build --release

功能标志

  • openssl:使用发行版 OpenSSL(在 release 模式下二进制大小减少约 20%)
  • rustls:使用 rustls(默认启用)

要为构建启用某个功能,请将 --features 标志传递给 cargo build 命令。

例如,要复用发行版中已存在的 OpenSSL:

cargo build --release --no-default-features --features openssl

测试

单元测试
cargo test -- --test-threads 1
测试夹具
./fixtures/test-fixtures.sh

用法

独立命令行工具(rpaste)可在此处获取。

CLI

function rpaste() {
  curl -F "file=@$1" -H "Authorization: <auth_token>" "<server_address>"
}

* 考虑从文件中读取授权头。 (e.g. -H @rpaste_auth)

# upload a file
$ rpaste x.txt

# paste from stdin
$ rpaste -

过期

$ curl -F "file=@x.txt" -H "expire:10min" "<server_address>"

支持的单位:

  • nsec, ns
  • usec, us
  • msec, ms
  • seconds, second, sec, s
  • minutes, minute, min, m
  • hours, hour, hr, h
  • days, day, d
  • weeks, week, w
  • months, month, M
  • years, year, y

一次性文件

$ curl -F "oneshot=@x.txt" "<server_address>"

一次性 URL

$ curl -F "oneshot_url=https://example.com" "<server_address>"

受密码保护的文件

上传一个带有自动生成密码的文件:

$ curl -F "protected=@secret.txt" "<server_address>"
https://paste.site.com/secret.txt
Password: aBcD1234EfGh5678IjKl9012

使用 Bearer token 下载:

$ curl -H "Authorization: Bearer aBcD1234EfGh5678IjKl9012" https://paste.site.com/secret.txt

或者使用 Basic Auth:

$ curl -u "user:aBcD1234EfGh5678IjKl9012" https://paste.site.com/secret.txt

注意:受保护文件不能与其他粘贴类型(oneshot、URL)组合使用。密码与文件永久绑定,无法更改。如果密码丢失,文件将无法访问。当主文件被删除或过期时,密码文件会自动删除。

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 端点,并使用一个(或多个)认证令牌对其进行保护。

$ curl -H "Authorization: <auth_token>" -X DELETE "<server_address>/file.txt"

DELETE 端点将不会公开,如果未设置 delete_tokens,则返回 404 错误。

覆盖文件名

当使用 random_url 配置选项,或从远程 URL 粘贴文件](#paste-file-from-remote-url)时,rustypaste 会自动选择一个文件名。

可以通过发送名为 filename 的标头来覆盖此行为:

curl -F "file=@x.txt" -H "filename: <file_name>" "<server_address>"
curl -F "remote=https://example.com/file.png" -H "filename: <file_name>" "<server_address>"

Server

启动服务器:

$ rustypaste

如果当前目录中未找到配置文件,请通过 CONFIG 环境变量指定:

$ CONFIG="$HOME/.rustypaste.toml" rustypaste

身份验证

要启用基本 HTTP 身份验证,请设置 AUTH_TOKEN 环境变量(通过 .env):

$ echo "AUTH_TOKEN=$(openssl rand -base64 16)" > .env
$ rustypaste

设置多个认证令牌有 2 种选项:

  • 通过 config.toml 中的数组字段 [server].auth_tokens
  • 或者将换行分隔的列表写入文件,并分别通过 AUTH_TOKENS_FILEDELETE_TOKENS_FILE 将其路径传递给 rustypaste。

如果未设置 AUTH_TOKENAUTH_TOKENS_FILE[server].auth_tokens,服务器将不要求任何身份验证。

例外是 DELETE 端点,它要求至少设置一个令牌。有关更多信息,请参阅 从服务器删除文件

有关配置选项,请参阅 config.toml

MIME 处理

rustypaste 根据文件扩展名(可带可选覆盖)确定文件的 MIME 类型,并将文本类类型作为 text/plain; charset=utf-8 提供,以避免脚本执行。

  • [paste].mime_override 允许通过文件名正则表达式覆盖 MIME 类型。
  • [paste].mime_blacklist 阻止上传特定 MIME 类型的文件。
  • [paste].text_mime_overrides 强制将额外检测/猜测的 MIME 类型渲染为纯文本(与通过文件名正则表达式匹配的 mime_override 不同,此选项匹配内容的实际 MIME 类型)。

列表端点

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 字段,或将 [landing_page].file 指向您的 html 文件。

示例请参见 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 Request Entity Too Large 错误,请在 nginx.conf 中设置最大请求体大小:

http {
    # ...
    client_max_body_size 100M;
}

第三方客户端

贡献

欢迎提交 Pull requests!

建议先通过 issues 提交您的想法,并查看 现有 issues

许可证

所有代码均依据 The MIT License 授权。