23 次重大发布

0.41.0 2024 年 5 月 22 日
0.40.0 2024 年 2 月 13 日
0.39.0 2024 年 1 月 11 日
0.38.0 2023 年 11 月 29 日
0.27.0 2022 年 7 月 25 日

#25HTTP 服务器

Download history 37/week @ 2024-04-29 31/week @ 2024-05-06 45/week @ 2024-05-13 274/week @ 2024-05-20 64/week @ 2024-05-27 60/week @ 2024-06-03 55/week @ 2024-06-10 30/week @ 2024-06-17 26/week @ 2024-06-24 25/week @ 2024-07-01 37/week @ 2024-07-08 13/week @ 2024-07-15 17/week @ 2024-07-22 124/week @ 2024-07-29 29/week @ 2024-08-05 50/week @ 2024-08-12

222 每月下载次数

MIT/Apache

195KB
3.5K SLoC

杜夫斯

CI Crates Docker Pulls

杜夫斯是一个独特的实用文件服务器,支持静态服务、上传、搜索、访问控制、WebDAV...

demo

特性

  • 提供静态文件
  • 将文件夹下载为 zip 文件
  • 上传文件和文件夹(拖放)
  • 创建/编辑/搜索文件
  • 可续传/部分上传/下载
  • 访问控制
  • 支持 https
  • 支持 WebDAV
  • 易于使用 curl

安装

使用 cargo

cargo install dufs

使用 docker

docker run -v `pwd`:/data -p 5000:5000 --rm sigoden/dufs /data -A

使用 Homebrew

brew install dufs

在 macOS、Linux、Windows 上提供二进制文件

Github 发布 下载,解压并将 dufs 添加到您的 $PATH。

命令行界面

Dufs is a distinctive utility file server - https://github.com/sigoden/dufs

Usage: dufs [OPTIONS] [serve-path]

Arguments:
  [serve-path]  Specific path to serve [default: .]

Options:
  -c, --config <file>        Specify configuration file
  -b, --bind <addrs>         Specify bind address or unix socket
  -p, --port <port>          Specify port to listen on [default: 5000]
      --path-prefix <path>   Specify a path prefix
      --hidden <value>       Hide paths from directory listings, e.g. tmp,*.log,*.lock
  -a, --auth <rules>         Add auth roles, e.g. user:pass@/dir1:rw,/dir2
  -A, --allow-all            Allow all operations
      --allow-upload         Allow upload files/folders
      --allow-delete         Allow delete files/folders
      --allow-search         Allow search files/folders
      --allow-symlink        Allow symlink to files/folders outside root directory
      --allow-archive        Allow zip archive generation
      --enable-cors          Enable CORS, sets `Access-Control-Allow-Origin: *`
      --render-index         Serve index.html when requesting a directory, returns 404 if not found index.html
      --render-try-index     Serve index.html when requesting a directory, returns directory listing if not found index.html
      --render-spa           Serve SPA(Single Page Application)
      --assets <path>        Set the path to the assets directory for overriding the built-in assets
      --log-format <format>  Customize http log format
      --log-file <file>      Specify the file to save logs to, other than stdout/stderr
      --compress <level>     Set zip compress level [default: low] [possible values: none, low, medium, high]
      --completions <shell>  Print shell completion script for <shell> [possible values: bash, elvish, fish, powershell, zsh]
      --tls-cert <path>      Path to an SSL/TLS certificate to serve with HTTPS
      --tls-key <path>       Path to the SSL/TLS certificate's private key
  -h, --help                 Print help
  -V, --version              Print version

示例

以只读模式提供当前工作目录

dufs

允许所有操作,如上传/删除/搜索/创建/编辑...

dufs -A

仅允许上传操作

dufs --allow-upload

提供特定目录

dufs Downloads

提供单个文件

dufs linux-distro.iso

提供单页应用程序,如 react/vue

dufs --render-spa

提供具有 index.html 的静态网站

dufs --render-index

需要用户名/密码

dufs -a admin:123@/:rw

监听特定主机:ip

dufs -b 127.0.0.1 -p 80

监听 Unix 套接字

dufs -b /tmp/dufs.socket

使用 https

dufs --tls-cert my.crt --tls-key my.key

API

上传文件

curl -T path-to-file http://127.0.0.1:5000/new-path/path-to-file

下载文件

curl http://127.0.0.1:5000/path-to-file           # download the file
curl http://127.0.0.1:5000/path-to-file?hash      # retrieve the sha256 hash of the file

将文件夹作为 zip 文件下载

curl -o path-to-folder.zip http://127.0.0.1:5000/path-to-folder?zip

删除文件/文件夹

curl -X DELETE http://127.0.0.1:5000/path-to-file-or-folder

创建目录

curl -X MKCOL http://127.0.0.1:5000/path-to-folder

将文件/文件夹移动到新路径

curl -X MOVE http://127.0.0.1:5000/path -H "Destination: http://127.0.0.1:5000/new-path"

列出/搜索目录内容

curl http://127.0.0.1:5000?q=Dockerfile           # search for files, similar to `find -name Dockerfile`
curl http://127.0.0.1:5000?simple                 # output names only, similar to `ls -1`
curl http://127.0.0.1:5000?json                   # output paths in json format

具有授权(基本或摘要认证都有效)

curl http://127.0.0.1:5000/file --user user:pass                 # basic auth
curl http://127.0.0.1:5000/file --user user:pass --digest        # digest auth

可续传下载

curl -C- -o file http://127.0.0.1:5000/file

可续传上传

upload_offset=$(curl -I -s http://127.0.0.1:5000/file | tr -d '\r' | sed -n 's/content-length: //p')
dd skip=$upload_offset if=file status=none ibs=1 | \
  curl -X PATCH -H "X-Update-Range: append" --data-binary @- http://127.0.0.1:5000/file

高级主题

访问控制

杜夫斯支持基于账户的访问控制。您可以使用 --auth/-a 控制哪个账户可以在哪个路径上执行什么操作。

dufs -a admin:admin@/:rw -a guest:guest@/
dufs -a user:pass@/:rw,/dir1 -a @/
  1. 使用 @ 来分隔账户和路径。没有账户表示匿名用户。
  2. 使用 : 来分隔账户的用户名和密码。
  3. 使用 , 分隔路径。
  4. 使用路径后缀 :rw/:ro 设置权限:read-write/read-only:ro 可以省略。
  • -a admin:admin@/:rwadmin 对所有路径拥有完全权限。
  • -a guest:guest@/guest 对所有路径拥有只读权限。
  • -a user:pass@/:rw,/dir1user/* 拥有读写权限,对 /dir1/* 拥有只读权限。
  • -a @/:所有路径公开可访问,任何人都可以查看/下载。

密码中使用 ':' 和 '@' 字符没有限制。例如,user:pa:ss@1@/:rw 是有效的,密码是 pa:ss@1

哈希密码

DUFS 支持使用 sha-512 哈希密码。

创建哈希密码

$ mkpasswd  -m sha-512 -s
Password: 123456 
$6$tWMB51u6Kb2ui3wd$5gVHP92V9kZcMwQeKTjyTRgySsYJu471Jb1I6iHQ8iZ6s07GgCIO69KcPBRuwPE5tDq05xMAzye0NxVKuJdYs/

使用哈希密码

dufs -a 'admin:$6$tWMB51u6Kb2ui3wd$5gVHP92V9kZcMwQeKTjyTRgySsYJu471Jb1I6iHQ8iZ6s07GgCIO69KcPBRuwPE5tDq05xMAzye0NxVKuJdYs/@/:rw'

关于哈希密码的两大重要事项

  1. Dufs 只支持 sha-512 哈希密码,所以请确保密码字符串始终以 $6$ 开头。
  2. 摘要认证与哈希密码不兼容。

隐藏路径

Dufs 支持通过选项 --hidden <glob>,... 从目录列表中隐藏路径。

dufs --hidden .git,.DS_Store,tmp

在 --hidden 中使用的 glob 仅匹配文件和目录名,而不是路径。所以 --hidden dir1/file 是无效的。

dufs --hidden '.*'                          # hidden dotfiles
dufs --hidden '*/'                          # hidden all folders
dufs --hidden '*.log,*.lock'                # hidden by exts
dufs --hidden '*.log' --hidden '*.lock'

日志格式

Dufs 支持使用选项 --log-format 自定义 http 日志格式。

日志格式可以使用以下变量。

变量 描述
$remote_addr 客户端地址
$remote_user 提供认证的用户名
$request 完整的原始请求行
$status 响应状态
$http_ 任意请求头字段。例如:$http_user_agent, $http_referer

默认日志格式是 '$remote_addr "$request" $status'

2022-08-06T06:59:31+08:00 INFO - 127.0.0.1 "GET /" 200

禁用 http 日志

dufs --log-format=''

记录用户代理

dufs --log-format '$remote_addr "$request" $status $http_user_agent'
2022-08-06T06:53:55+08:00 INFO - 127.0.0.1 "GET /" 200 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

记录远程用户

dufs --log-format '$remote_addr $remote_user "$request" $status' -a /@admin:admin -a /folder1@user1:pass1
2022-08-06T07:04:37+08:00 INFO - 127.0.0.1 admin "GET /" 200

环境变量

所有选项都可以使用以 DUFS_ 为前缀的环境变量设置。

[serve-path]                DUFS_SERVE_PATH="."
    --config <file>         DUFS_CONFIG=config.yaml
-b, --bind <addrs>          DUFS_BIND=0.0.0.0
-p, --port <port>           DUFS_PORT=5000
    --path-prefix <path>    DUFS_PATH_PREFIX=/dufs
    --hidden <value>        DUFS_HIDDEN=tmp,*.log,*.lock
-a, --auth <rules>          DUFS_AUTH="admin:admin@/:rw|@/" 
-A, --allow-all             DUFS_ALLOW_ALL=true
    --allow-upload          DUFS_ALLOW_UPLOAD=true
    --allow-delete          DUFS_ALLOW_DELETE=true
    --allow-search          DUFS_ALLOW_SEARCH=true
    --allow-symlink         DUFS_ALLOW_SYMLINK=true
    --allow-archive         DUFS_ALLOW_ARCHIVE=true
    --enable-cors           DUFS_ENABLE_CORS=true
    --render-index          DUFS_RENDER_INDEX=true
    --render-try-index      DUFS_RENDER_TRY_INDEX=true
    --render-spa            DUFS_RENDER_SPA=true
    --assets <path>         DUFS_ASSETS=./assets
    --log-format <format>   DUFS_LOG_FORMAT=""
    --log-file <file>       DUFS_LOG_FILE=./dufs.log
    --compress <compress>   DUFS_COMPRESS=low
    --tls-cert <path>       DUFS_TLS_CERT=cert.pem
    --tls-key <path>        DUFS_TLS_KEY=key.pem

配置文件

您可以通过选择选项 --config <path-to-config.yaml> 来指定并使用配置文件。

以下是一些配置项

serve-path: '.'
bind: 0.0.0.0
port: 5000
path-prefix: /dufs
hidden:
  - tmp
  - '*.log'
  - '*.lock'
auth:
  - admin:admin@/:rw
  - user:pass@/src:rw,/share
  - '@/'  # According to the YAML spec, quoting is required.
allow-all: false
allow-upload: true
allow-delete: true
allow-search: true
allow-symlink: true
allow-archive: true
enable-cors: true
render-index: true
render-try-index: true
render-spa: true
assets: ./assets/
log-format: '$remote_addr "$request" $status $http_user_agent'
log-file: ./dufs.log
compress: low
tls-cert: tests/data/cert.pem
tls-key: tests/data/key_pkcs1.pem

自定义 UI

Dufs 允许用户使用自己的资源自定义 UI。

dufs --assets my-assets-dir/

您的资源文件夹必须包含一个 index.html 文件。

index.html 可以使用以下占位符变量来检索内部数据。

  • __INDEX_DATA__:目录列表数据
  • __ASSETS_PREFIX__:资源 URL 前缀

许可

版权所有 (c) 2022-2024 dufs-developers。

dufs 可在 MIT 许可证或 Apache 许可证 2.0 的条款下提供,您可选其一。

请参阅 LICENSE-APACHE 和 LICENSE-MIT 文件以获取许可证详情。

依赖项

~19–36MB
~604K SLoC