mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 09:09:03 +03:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9384cc8587 | ||
|
|
df48021757 | ||
|
|
af866aaaf4 | ||
|
|
68d238d34d | ||
|
|
a10150f2f8 | ||
|
|
5b11bb75dd |
@@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [0.18.0] - 2022-06-18
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Add option --render-try-index ([#47](https://github.com/sigoden/duf/issues/47))
|
||||||
|
- Add slash to end of dir href
|
||||||
|
|
||||||
## [0.17.1] - 2022-06-16
|
## [0.17.1] - 2022-06-16
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|||||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -571,7 +571,7 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "duf"
|
name = "duf"
|
||||||
version = "0.17.1"
|
version = "0.18.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assert_cmd",
|
"assert_cmd",
|
||||||
"assert_fs",
|
"assert_fs",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "duf"
|
name = "duf"
|
||||||
version = "0.17.1"
|
version = "0.18.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["sigoden <sigoden@gmail.com>"]
|
authors = ["sigoden <sigoden@gmail.com>"]
|
||||||
description = "Duf is a simple file server."
|
description = "Duf is a simple file server."
|
||||||
|
|||||||
39
README.md
39
README.md
@@ -30,7 +30,7 @@ cargo install duf
|
|||||||
### With docker
|
### With docker
|
||||||
|
|
||||||
```
|
```
|
||||||
docker run -v /tmp:/tmp -p 5000:5000 --rm -it docker.io/sigoden/duf /tmp
|
docker run -v `pwd`:/data -p 5000:5000 --rm -it sigoden/duf /data
|
||||||
```
|
```
|
||||||
|
|
||||||
### Binaries on macOS, Linux, Windows
|
### Binaries on macOS, Linux, Windows
|
||||||
@@ -49,51 +49,52 @@ ARGS:
|
|||||||
<path> Path to a root directory for serving files [default: .]
|
<path> Path to a root directory for serving files [default: .]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
|
-b, --bind <addr>... Specify bind address
|
||||||
|
-p, --port <port> Specify port to listen on [default: 5000]
|
||||||
|
--path-prefix <path> Specify an url path prefix
|
||||||
-a, --auth <user:pass> Use HTTP authentication
|
-a, --auth <user:pass> Use HTTP authentication
|
||||||
--no-auth-access Not required auth when access static files
|
--no-auth-access Not required auth when access static files
|
||||||
-A, --allow-all Allow all operations
|
-A, --allow-all Allow all operations
|
||||||
|
--allow-upload Allow upload files/folders
|
||||||
--allow-delete Allow delete files/folders
|
--allow-delete Allow delete files/folders
|
||||||
--allow-symlink Allow symlink to files/folders outside root directory
|
--allow-symlink Allow symlink to files/folders outside root directory
|
||||||
--allow-upload Allow upload files/folders
|
|
||||||
-b, --bind <address>... Specify bind address
|
|
||||||
--cors Enable CORS, sets `Access-Control-Allow-Origin: *`
|
|
||||||
-h, --help Print help information
|
|
||||||
-p, --port <port> Specify port to listen on [default: 5000]
|
|
||||||
--path-prefix <path> Specify an url path prefix
|
|
||||||
--render-index Render index.html when requesting a directory
|
--render-index Render index.html when requesting a directory
|
||||||
|
--render-try-index Render index.html if it exists when requesting a directory
|
||||||
--render-spa Render for single-page application
|
--render-spa Render for single-page application
|
||||||
|
--cors Enable CORS, sets `Access-Control-Allow-Origin: *`
|
||||||
--tls-cert <path> Path to an SSL/TLS certificate to serve with HTTPS
|
--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
|
--tls-key <path> Path to the SSL/TLS certificate's private key
|
||||||
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
You can run this command to start serving your current working directory on 127.0.0.1:5000 by default.
|
Serve current working directory, no upload/delete
|
||||||
|
|
||||||
```
|
```
|
||||||
duf
|
duf
|
||||||
```
|
```
|
||||||
|
|
||||||
...or specify which folder you want to serve.
|
Allow upload/delete
|
||||||
|
|
||||||
```
|
```
|
||||||
duf folder_name
|
duf -A
|
||||||
```
|
```
|
||||||
|
|
||||||
Allow all operations such as upload, delete
|
Listen on a specific port
|
||||||
|
|
||||||
```sh
|
|
||||||
duf --allow-all
|
|
||||||
```
|
|
||||||
|
|
||||||
Only allow upload operation
|
|
||||||
|
|
||||||
```
|
```
|
||||||
duf --allow-upload
|
duf -p 80
|
||||||
```
|
```
|
||||||
|
|
||||||
Serve a single page application (SPA)
|
Protect with authentication
|
||||||
|
|
||||||
|
```
|
||||||
|
duf -a admin:admin
|
||||||
|
```
|
||||||
|
|
||||||
|
For a single page application (SPA)
|
||||||
|
|
||||||
```
|
```
|
||||||
duf --render-spa
|
duf --render-spa
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @typedef {object} PathItem
|
* @typedef {object} PathItem
|
||||||
* @property {"Dir"|"SymlinkDir"|"File"|"SymlinkFile"} path_type
|
* @property {"Dir"|"SymlinkDir"|"File"|"SymlinkFile"} path_type
|
||||||
* @property {boolean} is_symlink
|
|
||||||
* @property {string} name
|
* @property {string} name
|
||||||
* @property {number} mtime
|
* @property {number} mtime
|
||||||
* @property {number} size
|
* @property {number} size
|
||||||
@@ -153,10 +152,11 @@ function addBreadcrumb(value) {
|
|||||||
* @param {number} index
|
* @param {number} index
|
||||||
*/
|
*/
|
||||||
function addPath(file, index) {
|
function addPath(file, index) {
|
||||||
const url = getUrl(file.name)
|
let url = getUrl(file.name)
|
||||||
let actionDelete = "";
|
let actionDelete = "";
|
||||||
let actionDownload = "";
|
let actionDownload = "";
|
||||||
if (file.path_type.endsWith("Dir")) {
|
if (file.path_type.endsWith("Dir")) {
|
||||||
|
url += "/";
|
||||||
actionDownload = `
|
actionDownload = `
|
||||||
<div class="action-btn">
|
<div class="action-btn">
|
||||||
<a href="${url}?zip" title="Download folder as a .zip file">
|
<a href="${url}?zip" title="Download folder as a .zip file">
|
||||||
|
|||||||
43
src/args.rs
43
src/args.rs
@@ -1,4 +1,4 @@
|
|||||||
use clap::{Arg, ArgMatches, Command};
|
use clap::{AppSettings, Arg, ArgMatches, Command};
|
||||||
use rustls::{Certificate, PrivateKey};
|
use rustls::{Certificate, PrivateKey};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
@@ -17,14 +17,15 @@ fn app() -> Command<'static> {
|
|||||||
" - ",
|
" - ",
|
||||||
env!("CARGO_PKG_REPOSITORY")
|
env!("CARGO_PKG_REPOSITORY")
|
||||||
))
|
))
|
||||||
|
.global_setting(AppSettings::DeriveDisplayOrder)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("address")
|
Arg::new("bind")
|
||||||
.short('b')
|
.short('b')
|
||||||
.long("bind")
|
.long("bind")
|
||||||
.help("Specify bind address")
|
.help("Specify bind address")
|
||||||
.multiple_values(true)
|
.multiple_values(true)
|
||||||
.multiple_occurrences(true)
|
.multiple_occurrences(true)
|
||||||
.value_name("address"),
|
.value_name("addr"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("port")
|
Arg::new("port")
|
||||||
@@ -46,6 +47,18 @@ fn app() -> Command<'static> {
|
|||||||
.value_name("path")
|
.value_name("path")
|
||||||
.help("Specify an url path prefix"),
|
.help("Specify an url path prefix"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("auth")
|
||||||
|
.short('a')
|
||||||
|
.long("auth")
|
||||||
|
.help("Use HTTP authentication")
|
||||||
|
.value_name("user:pass"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("no-auth-access")
|
||||||
|
.long("no-auth-access")
|
||||||
|
.help("Not required auth when access static files"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("allow-all")
|
Arg::new("allow-all")
|
||||||
.short('A')
|
.short('A')
|
||||||
@@ -72,25 +85,16 @@ fn app() -> Command<'static> {
|
|||||||
.long("render-index")
|
.long("render-index")
|
||||||
.help("Render index.html when requesting a directory"),
|
.help("Render index.html when requesting a directory"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("render-try-index")
|
||||||
|
.long("render-try-index")
|
||||||
|
.help("Render index.html if it exists when requesting a directory"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("render-spa")
|
Arg::new("render-spa")
|
||||||
.long("render-spa")
|
.long("render-spa")
|
||||||
.help("Render for single-page application"),
|
.help("Render for single-page application"),
|
||||||
)
|
)
|
||||||
.arg(
|
|
||||||
Arg::new("auth")
|
|
||||||
.short('a')
|
|
||||||
.display_order(1)
|
|
||||||
.long("auth")
|
|
||||||
.help("Use HTTP authentication")
|
|
||||||
.value_name("user:pass"),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::new("no-auth-access")
|
|
||||||
.display_order(1)
|
|
||||||
.long("no-auth-access")
|
|
||||||
.help("Not required auth when access static files"),
|
|
||||||
)
|
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("cors")
|
Arg::new("cors")
|
||||||
.long("cors")
|
.long("cors")
|
||||||
@@ -128,6 +132,7 @@ pub struct Args {
|
|||||||
pub allow_symlink: bool,
|
pub allow_symlink: bool,
|
||||||
pub render_index: bool,
|
pub render_index: bool,
|
||||||
pub render_spa: bool,
|
pub render_spa: bool,
|
||||||
|
pub render_try_index: bool,
|
||||||
pub cors: bool,
|
pub cors: bool,
|
||||||
pub tls: Option<(Vec<Certificate>, PrivateKey)>,
|
pub tls: Option<(Vec<Certificate>, PrivateKey)>,
|
||||||
}
|
}
|
||||||
@@ -140,7 +145,7 @@ impl Args {
|
|||||||
pub fn parse(matches: ArgMatches) -> BoxResult<Args> {
|
pub fn parse(matches: ArgMatches) -> BoxResult<Args> {
|
||||||
let port = matches.value_of_t::<u16>("port")?;
|
let port = matches.value_of_t::<u16>("port")?;
|
||||||
let addrs = matches
|
let addrs = matches
|
||||||
.values_of("address")
|
.values_of("bind")
|
||||||
.map(|v| v.collect())
|
.map(|v| v.collect())
|
||||||
.unwrap_or_else(|| vec!["0.0.0.0", "::"]);
|
.unwrap_or_else(|| vec!["0.0.0.0", "::"]);
|
||||||
let addrs: Vec<IpAddr> = Args::parse_addrs(&addrs)?;
|
let addrs: Vec<IpAddr> = Args::parse_addrs(&addrs)?;
|
||||||
@@ -164,6 +169,7 @@ impl Args {
|
|||||||
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
|
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
|
||||||
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
|
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
|
||||||
let render_index = matches.is_present("render-index");
|
let render_index = matches.is_present("render-index");
|
||||||
|
let render_try_index = matches.is_present("render-try-index");
|
||||||
let render_spa = matches.is_present("render-spa");
|
let render_spa = matches.is_present("render-spa");
|
||||||
let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) {
|
let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) {
|
||||||
(Some(certs_file), Some(key_file)) => {
|
(Some(certs_file), Some(key_file)) => {
|
||||||
@@ -187,6 +193,7 @@ impl Args {
|
|||||||
allow_upload,
|
allow_upload,
|
||||||
allow_symlink,
|
allow_symlink,
|
||||||
render_index,
|
render_index,
|
||||||
|
render_try_index,
|
||||||
render_spa,
|
render_spa,
|
||||||
tls,
|
tls,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ impl Server {
|
|||||||
let allow_delete = self.args.allow_delete;
|
let allow_delete = self.args.allow_delete;
|
||||||
let render_index = self.args.render_index;
|
let render_index = self.args.render_index;
|
||||||
let render_spa = self.args.render_spa;
|
let render_spa = self.args.render_spa;
|
||||||
|
let render_try_index = self.args.render_try_index;
|
||||||
|
|
||||||
if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await {
|
if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await {
|
||||||
status_not_found(&mut res);
|
status_not_found(&mut res);
|
||||||
@@ -129,7 +130,9 @@ impl Server {
|
|||||||
Method::GET | Method::HEAD => {
|
Method::GET | Method::HEAD => {
|
||||||
let head_only = method == Method::HEAD;
|
let head_only = method == Method::HEAD;
|
||||||
if is_dir {
|
if is_dir {
|
||||||
if render_index || render_spa {
|
if render_try_index && query == "zip" {
|
||||||
|
self.handle_zip_dir(path, head_only, &mut res).await?;
|
||||||
|
} else if render_index || render_spa || render_try_index {
|
||||||
self.handle_render_index(path, headers, head_only, &mut res)
|
self.handle_render_index(path, headers, head_only, &mut res)
|
||||||
.await?;
|
.await?;
|
||||||
} else if query == "zip" {
|
} else if query == "zip" {
|
||||||
@@ -366,15 +369,17 @@ impl Server {
|
|||||||
head_only: bool,
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
let path = path.join(INDEX_NAME);
|
let index_path = path.join(INDEX_NAME);
|
||||||
if fs::metadata(&path)
|
if fs::metadata(&index_path)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
.map(|v| v.is_file())
|
.map(|v| v.is_file())
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
{
|
{
|
||||||
self.handle_send_file(&path, headers, head_only, res)
|
self.handle_send_file(&index_path, headers, head_only, res)
|
||||||
.await?;
|
.await?;
|
||||||
|
} else if self.args.render_try_index {
|
||||||
|
self.handle_ls_dir(path, true, head_only, res).await?;
|
||||||
} else {
|
} else {
|
||||||
status_not_found(res)
|
status_not_found(res)
|
||||||
}
|
}
|
||||||
@@ -1029,6 +1034,7 @@ fn status_forbid(res: &mut Response) {
|
|||||||
|
|
||||||
fn status_not_found(res: &mut Response) {
|
fn status_not_found(res: &mut Response) {
|
||||||
*res.status_mut() = StatusCode::NOT_FOUND;
|
*res.status_mut() = StatusCode::NOT_FOUND;
|
||||||
|
*res.body_mut() = Body::from("Not Found");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn status_no_content(res: &mut Response) {
|
fn status_no_content(res: &mut Response) {
|
||||||
|
|||||||
@@ -29,7 +29,11 @@ pub static FILES: &[&str] = &[
|
|||||||
"foo\\bar.test",
|
"foo\\bar.test",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Directory names for testing purpose
|
/// Directory names for testing diretory don't exist
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub static DIR_NO_FOUND: &str = "dir-no-found/";
|
||||||
|
|
||||||
|
/// Directory names for testing diretory don't have index.html
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub static DIR_NO_INDEX: &str = "dir-no-index/";
|
pub static DIR_NO_INDEX: &str = "dir-no-index/";
|
||||||
|
|
||||||
@@ -55,7 +59,7 @@ pub fn tmpdir() -> TempDir {
|
|||||||
}
|
}
|
||||||
for directory in DIRECTORIES {
|
for directory in DIRECTORIES {
|
||||||
for file in FILES {
|
for file in FILES {
|
||||||
if *directory == DIR_NO_INDEX {
|
if *directory == DIR_NO_INDEX && *file == "index.html" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
tmpdir
|
tmpdir
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
mod fixtures;
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
use fixtures::{server, Error, TestServer, DIR_NO_INDEX};
|
use fixtures::{server, Error, TestServer, DIR_NO_FOUND, DIR_NO_INDEX};
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@@ -12,12 +13,43 @@ fn render_index(#[with(&["--render-index"])] server: TestServer) -> Result<(), E
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn render_index_404(#[with(&["--render-index"])] server: TestServer) -> Result<(), Error> {
|
fn render_index2(#[with(&["--render-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(format!("{}/{}", server.url(), DIR_NO_INDEX))?;
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), DIR_NO_INDEX))?;
|
||||||
assert_eq!(resp.status(), 404);
|
assert_eq!(resp.status(), 404);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_try_index(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
let text = resp.text()?;
|
||||||
|
assert_eq!(text, "This is index.html");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_try_index2(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), DIR_NO_INDEX))?;
|
||||||
|
let files: Vec<&str> = self::fixtures::FILES
|
||||||
|
.iter()
|
||||||
|
.filter(|v| **v != "index.html")
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
assert_index_resp!(resp, files);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_try_index3(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}?zip", server.url(), DIR_NO_INDEX))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-type").unwrap(),
|
||||||
|
"application/zip"
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(server.url())?;
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
@@ -27,8 +59,8 @@ fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn render_spa_no_404(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
fn render_spa2(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(format!("{}/{}", server.url(), DIR_NO_INDEX))?;
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), DIR_NO_FOUND))?;
|
||||||
let text = resp.text()?;
|
let text = resp.text()?;
|
||||||
assert_eq!(text, "This is index.html");
|
assert_eq!(text, "This is index.html");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user