mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 17:13:02 +03:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d35cea4c36 | ||
|
|
1329e42b9a | ||
|
|
6ebf619430 | ||
|
|
8b4727c3a4 | ||
|
|
604ccc6556 | ||
|
|
1a9990f04e | ||
|
|
bd07783cde | ||
|
|
dbf2de9cb9 |
15
CHANGELOG.md
15
CHANGELOG.md
@@ -2,7 +2,20 @@
|
|||||||
|
|
||||||
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.30.0] - 2022-09-05
|
## [0.31.0] - 2022-11-11
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Auth not works with --path-prefix ([#138](https://github.com/sigoden/dufs/issues/138))
|
||||||
|
- Don't search on empty query string ([#140](https://github.com/sigoden/dufs/issues/140))
|
||||||
|
- Status code for MKCOL on existing resource ([#142](https://github.com/sigoden/dufs/issues/142))
|
||||||
|
- Panic on PROPFIND // ([#144](https://github.com/sigoden/dufs/issues/144))
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Support unix sockets ([#145](https://github.com/sigoden/dufs/issues/145))
|
||||||
|
|
||||||
|
## [0.30.0] - 2022-09-09
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
|
|||||||
573
Cargo.lock
generated
573
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dufs"
|
name = "dufs"
|
||||||
version = "0.30.0"
|
version = "0.31.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["sigoden <sigoden@gmail.com>"]
|
authors = ["sigoden <sigoden@gmail.com>"]
|
||||||
description = "Dufs is a distinctive utility file server"
|
description = "Dufs is a distinctive utility file server"
|
||||||
@@ -11,8 +11,8 @@ categories = ["command-line-utilities", "web-programming::http-server"]
|
|||||||
keywords = ["static", "file", "server", "webdav", "cli"]
|
keywords = ["static", "file", "server", "webdav", "cli"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "3", default-features = false, features = ["std", "wrap_help"] }
|
clap = { version = "4", features = ["wrap_help"] }
|
||||||
clap_complete = "3"
|
clap_complete = "4"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
|
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
|
||||||
tokio-util = { version = "0.7", features = ["io-util"] }
|
tokio-util = { version = "0.7", features = ["io-util"] }
|
||||||
|
|||||||
62
README.md
62
README.md
@@ -42,35 +42,34 @@ Download from [Github Releases](https://github.com/sigoden/dufs/releases), unzip
|
|||||||
```
|
```
|
||||||
Dufs is a distinctive utility file server - https://github.com/sigoden/dufs
|
Dufs is a distinctive utility file server - https://github.com/sigoden/dufs
|
||||||
|
|
||||||
USAGE:
|
Usage: dufs [OPTIONS] [root]
|
||||||
dufs [OPTIONS] [--] [root]
|
|
||||||
|
|
||||||
ARGS:
|
Arguments:
|
||||||
<root> Specific path to serve [default: .]
|
[root] Specific path to serve [default: .]
|
||||||
|
|
||||||
OPTIONS:
|
Options:
|
||||||
-b, --bind <addr>... Specify bind address
|
-b, --bind <addrs> Specify bind address or unix socket
|
||||||
-p, --port <port> Specify port to listen on [default: 5000]
|
-p, --port <port> Specify port to listen on [default: 5000]
|
||||||
--path-prefix <path> Specify a path prefix
|
--path-prefix <path> Specify a path prefix
|
||||||
--hidden <value> Hide paths from directory listings, separated by `,`
|
--hidden <value> Hide paths from directory listings, separated by `,`
|
||||||
-a, --auth <rule>... Add auth for path
|
-a, --auth <rules> Add auth for path
|
||||||
--auth-method <value> Select auth method [default: digest] [possible values: basic, digest]
|
--auth-method <value> Select auth method [default: digest] [possible values: basic, digest]
|
||||||
-A, --allow-all Allow all operations
|
-A, --allow-all Allow all operations
|
||||||
--allow-upload Allow upload files/folders
|
--allow-upload Allow upload files/folders
|
||||||
--allow-delete Allow delete files/folders
|
--allow-delete Allow delete files/folders
|
||||||
--allow-search Allow search files/folders
|
--allow-search Allow search files/folders
|
||||||
--allow-symlink Allow symlink to files/folders outside root directory
|
--allow-symlink Allow symlink to files/folders outside root directory
|
||||||
--enable-cors Enable CORS, sets `Access-Control-Allow-Origin: *`
|
--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-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-try-index Serve index.html when requesting a directory, returns directory listing if not found index.html
|
||||||
--render-spa Serve SPA(Single Page Application)
|
--render-spa Serve SPA(Single Page Application)
|
||||||
--assets <path> Use custom assets to override builtin assets
|
--assets <path> Use custom assets to override builtin assets
|
||||||
--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
|
||||||
--log-format <format> Customize http log format
|
--log-format <format> Customize http log format
|
||||||
--completions <shell> Print shell completion script for <shell> [possible values: bash, elvish, fish, powershell, zsh]
|
--completions <shell> Print shell completion script for <shell> [possible values: bash, elvish, fish, powershell, zsh]
|
||||||
-h, --help Print help information
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
@@ -123,10 +122,15 @@ Require username/password
|
|||||||
dufs -a /@admin:123
|
dufs -a /@admin:123
|
||||||
```
|
```
|
||||||
|
|
||||||
Listen on a specific port
|
Listen on specific host:ip
|
||||||
|
|
||||||
```
|
```
|
||||||
dufs -p 80
|
dufs -b 127.0.0.1 -p 80
|
||||||
|
```
|
||||||
|
|
||||||
|
Listen on unix socket
|
||||||
|
```
|
||||||
|
dufs -b /tmp/dufs.socket
|
||||||
```
|
```
|
||||||
|
|
||||||
Use https
|
Use https
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
<div class="icon">
|
<div class="icon">
|
||||||
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/></svg>
|
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/></svg>
|
||||||
</div>
|
</div>
|
||||||
<input id="search" name="q" type="text" maxlength="128" autocomplete="off" tabindex="1">
|
<input id="search" name="q" type="text" maxlength="128" autocomplete="off" tabindex="1" required>
|
||||||
<input type="submit" hidden />
|
<input type="submit" hidden />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
112
src/args.rs
112
src/args.rs
@@ -1,4 +1,5 @@
|
|||||||
use clap::{value_parser, AppSettings, Arg, ArgAction, ArgMatches, Command};
|
use clap::builder::PossibleValuesParser;
|
||||||
|
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
||||||
use clap_complete::{generate, Generator, Shell};
|
use clap_complete::{generate, Generator, Shell};
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
use rustls::{Certificate, PrivateKey};
|
use rustls::{Certificate, PrivateKey};
|
||||||
@@ -14,7 +15,7 @@ use crate::tls::{load_certs, load_private_key};
|
|||||||
use crate::utils::encode_uri;
|
use crate::utils::encode_uri;
|
||||||
use crate::BoxResult;
|
use crate::BoxResult;
|
||||||
|
|
||||||
pub fn build_cli() -> Command<'static> {
|
pub fn build_cli() -> Command {
|
||||||
let app = Command::new(env!("CARGO_CRATE_NAME"))
|
let app = Command::new(env!("CARGO_CRATE_NAME"))
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.author(env!("CARGO_PKG_AUTHORS"))
|
.author(env!("CARGO_PKG_AUTHORS"))
|
||||||
@@ -23,31 +24,30 @@ pub fn build_cli() -> Command<'static> {
|
|||||||
" - ",
|
" - ",
|
||||||
env!("CARGO_PKG_REPOSITORY")
|
env!("CARGO_PKG_REPOSITORY")
|
||||||
))
|
))
|
||||||
.global_setting(AppSettings::DeriveDisplayOrder)
|
.arg(
|
||||||
|
Arg::new("root")
|
||||||
|
.default_value(".")
|
||||||
|
.value_parser(value_parser!(PathBuf))
|
||||||
|
.help("Specific path to serve"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("bind")
|
Arg::new("bind")
|
||||||
.short('b')
|
.short('b')
|
||||||
.long("bind")
|
.long("bind")
|
||||||
.help("Specify bind address")
|
.help("Specify bind address or unix socket")
|
||||||
.multiple_values(true)
|
|
||||||
.value_delimiter(',')
|
|
||||||
.action(ArgAction::Append)
|
.action(ArgAction::Append)
|
||||||
.value_name("addr"),
|
.value_delimiter(',')
|
||||||
|
.value_name("addrs"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("port")
|
Arg::new("port")
|
||||||
.short('p')
|
.short('p')
|
||||||
.long("port")
|
.long("port")
|
||||||
.default_value("5000")
|
.default_value("5000")
|
||||||
|
.value_parser(value_parser!(u16))
|
||||||
.help("Specify port to listen on")
|
.help("Specify port to listen on")
|
||||||
.value_name("port"),
|
.value_name("port"),
|
||||||
)
|
)
|
||||||
.arg(
|
|
||||||
Arg::new("root")
|
|
||||||
.default_value(".")
|
|
||||||
.allow_invalid_utf8(true)
|
|
||||||
.help("Specific path to serve"),
|
|
||||||
)
|
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("path-prefix")
|
Arg::new("path-prefix")
|
||||||
.long("path-prefix")
|
.long("path-prefix")
|
||||||
@@ -66,15 +66,14 @@ pub fn build_cli() -> Command<'static> {
|
|||||||
.long("auth")
|
.long("auth")
|
||||||
.help("Add auth for path")
|
.help("Add auth for path")
|
||||||
.action(ArgAction::Append)
|
.action(ArgAction::Append)
|
||||||
.multiple_values(true)
|
|
||||||
.value_delimiter(',')
|
.value_delimiter(',')
|
||||||
.value_name("rule"),
|
.value_name("rules"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("auth-method")
|
Arg::new("auth-method")
|
||||||
.long("auth-method")
|
.long("auth-method")
|
||||||
.help("Select auth method")
|
.help("Select auth method")
|
||||||
.possible_values(["basic", "digest"])
|
.value_parser(PossibleValuesParser::new(["basic", "digest"]))
|
||||||
.default_value("digest")
|
.default_value("digest")
|
||||||
.value_name("value"),
|
.value_name("value"),
|
||||||
)
|
)
|
||||||
@@ -82,53 +81,62 @@ pub fn build_cli() -> Command<'static> {
|
|||||||
Arg::new("allow-all")
|
Arg::new("allow-all")
|
||||||
.short('A')
|
.short('A')
|
||||||
.long("allow-all")
|
.long("allow-all")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Allow all operations"),
|
.help("Allow all operations"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("allow-upload")
|
Arg::new("allow-upload")
|
||||||
.long("allow-upload")
|
.long("allow-upload")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Allow upload files/folders"),
|
.help("Allow upload files/folders"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("allow-delete")
|
Arg::new("allow-delete")
|
||||||
.long("allow-delete")
|
.long("allow-delete")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Allow delete files/folders"),
|
.help("Allow delete files/folders"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("allow-search")
|
Arg::new("allow-search")
|
||||||
.long("allow-search")
|
.long("allow-search")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Allow search files/folders"),
|
.help("Allow search files/folders"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("allow-symlink")
|
Arg::new("allow-symlink")
|
||||||
.long("allow-symlink")
|
.long("allow-symlink")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Allow symlink to files/folders outside root directory"),
|
.help("Allow symlink to files/folders outside root directory"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("enable-cors")
|
Arg::new("enable-cors")
|
||||||
.long("enable-cors")
|
.long("enable-cors")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Enable CORS, sets `Access-Control-Allow-Origin: *`"),
|
.help("Enable CORS, sets `Access-Control-Allow-Origin: *`"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("render-index")
|
Arg::new("render-index")
|
||||||
.long("render-index")
|
.long("render-index")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Serve index.html when requesting a directory, returns 404 if not found index.html"),
|
.help("Serve index.html when requesting a directory, returns 404 if not found index.html"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("render-try-index")
|
Arg::new("render-try-index")
|
||||||
.long("render-try-index")
|
.long("render-try-index")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Serve index.html when requesting a directory, returns directory listing if not found index.html"),
|
.help("Serve index.html when requesting a directory, returns directory listing if not found index.html"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("render-spa")
|
Arg::new("render-spa")
|
||||||
.long("render-spa")
|
.long("render-spa")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
.help("Serve SPA(Single Page Application)"),
|
.help("Serve SPA(Single Page Application)"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("assets")
|
Arg::new("assets")
|
||||||
.long("assets")
|
.long("assets")
|
||||||
.help("Use custom assets to override builtin assets")
|
.help("Use custom assets to override builtin assets")
|
||||||
.allow_invalid_utf8(true)
|
.value_parser(value_parser!(PathBuf))
|
||||||
.value_name("path")
|
.value_name("path")
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -138,12 +146,14 @@ pub fn build_cli() -> Command<'static> {
|
|||||||
Arg::new("tls-cert")
|
Arg::new("tls-cert")
|
||||||
.long("tls-cert")
|
.long("tls-cert")
|
||||||
.value_name("path")
|
.value_name("path")
|
||||||
|
.value_parser(value_parser!(PathBuf))
|
||||||
.help("Path to an SSL/TLS certificate to serve with HTTPS"),
|
.help("Path to an SSL/TLS certificate to serve with HTTPS"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("tls-key")
|
Arg::new("tls-key")
|
||||||
.long("tls-key")
|
.long("tls-key")
|
||||||
.value_name("path")
|
.value_name("path")
|
||||||
|
.value_parser(value_parser!(PathBuf))
|
||||||
.help("Path to the SSL/TLS certificate's private key"),
|
.help("Path to the SSL/TLS certificate's private key"),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -168,7 +178,7 @@ pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
pub addrs: Vec<IpAddr>,
|
pub addrs: Vec<BindAddr>,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub path_is_file: bool,
|
pub path_is_file: bool,
|
||||||
@@ -199,16 +209,16 @@ impl Args {
|
|||||||
/// If a parsing error ocurred, exit the process and print out informative
|
/// If a parsing error ocurred, exit the process and print out informative
|
||||||
/// error message to user.
|
/// error message to user.
|
||||||
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.get_one::<u16>("port").unwrap();
|
||||||
let addrs = matches
|
let addrs = matches
|
||||||
.values_of("bind")
|
.get_many::<String>("bind")
|
||||||
.map(|v| v.collect())
|
.map(|bind| bind.map(|v| v.as_str()).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<BindAddr> = Args::parse_addrs(&addrs)?;
|
||||||
let path = Args::parse_path(matches.value_of_os("root").unwrap_or_default())?;
|
let path = Args::parse_path(matches.get_one::<PathBuf>("root").unwrap())?;
|
||||||
let path_is_file = path.metadata()?.is_file();
|
let path_is_file = path.metadata()?.is_file();
|
||||||
let path_prefix = matches
|
let path_prefix = matches
|
||||||
.value_of("path-prefix")
|
.get_one::<String>("path-prefix")
|
||||||
.map(|v| v.trim_matches('/').to_owned())
|
.map(|v| v.trim_matches('/').to_owned())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let uri_prefix = if path_prefix.is_empty() {
|
let uri_prefix = if path_prefix.is_empty() {
|
||||||
@@ -217,28 +227,31 @@ impl Args {
|
|||||||
format!("/{}/", &encode_uri(&path_prefix))
|
format!("/{}/", &encode_uri(&path_prefix))
|
||||||
};
|
};
|
||||||
let hidden: Vec<String> = matches
|
let hidden: Vec<String> = matches
|
||||||
.value_of("hidden")
|
.get_one::<String>("hidden")
|
||||||
.map(|v| v.split(',').map(|x| x.to_string()).collect())
|
.map(|v| v.split(',').map(|x| x.to_string()).collect())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let enable_cors = matches.is_present("enable-cors");
|
let enable_cors = matches.get_flag("enable-cors");
|
||||||
let auth: Vec<&str> = matches
|
let auth: Vec<&str> = matches
|
||||||
.values_of("auth")
|
.get_many::<String>("auth")
|
||||||
.map(|v| v.collect())
|
.map(|auth| auth.map(|v| v.as_str()).collect())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let auth_method = match matches.value_of("auth-method").unwrap() {
|
let auth_method = match matches.get_one::<String>("auth-method").unwrap().as_str() {
|
||||||
"basic" => AuthMethod::Basic,
|
"basic" => AuthMethod::Basic,
|
||||||
_ => AuthMethod::Digest,
|
_ => AuthMethod::Digest,
|
||||||
};
|
};
|
||||||
let auth = AccessControl::new(&auth, &uri_prefix)?;
|
let auth = AccessControl::new(&auth, &uri_prefix)?;
|
||||||
let allow_upload = matches.is_present("allow-all") || matches.is_present("allow-upload");
|
let allow_upload = matches.get_flag("allow-all") || matches.get_flag("allow-upload");
|
||||||
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
|
let allow_delete = matches.get_flag("allow-all") || matches.get_flag("allow-delete");
|
||||||
let allow_search = matches.is_present("allow-all") || matches.is_present("allow-search");
|
let allow_search = matches.get_flag("allow-all") || matches.get_flag("allow-search");
|
||||||
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
|
let allow_symlink = matches.get_flag("allow-all") || matches.get_flag("allow-symlink");
|
||||||
let render_index = matches.is_present("render-index");
|
let render_index = matches.get_flag("render-index");
|
||||||
let render_try_index = matches.is_present("render-try-index");
|
let render_try_index = matches.get_flag("render-try-index");
|
||||||
let render_spa = matches.is_present("render-spa");
|
let render_spa = matches.get_flag("render-spa");
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) {
|
let tls = match (
|
||||||
|
matches.get_one::<PathBuf>("tls-cert"),
|
||||||
|
matches.get_one::<PathBuf>("tls-key"),
|
||||||
|
) {
|
||||||
(Some(certs_file), Some(key_file)) => {
|
(Some(certs_file), Some(key_file)) => {
|
||||||
let certs = load_certs(certs_file)?;
|
let certs = load_certs(certs_file)?;
|
||||||
let key = load_private_key(key_file)?;
|
let key = load_private_key(key_file)?;
|
||||||
@@ -249,10 +262,11 @@ impl Args {
|
|||||||
#[cfg(not(feature = "tls"))]
|
#[cfg(not(feature = "tls"))]
|
||||||
let tls = None;
|
let tls = None;
|
||||||
let log_http: LogHttp = matches
|
let log_http: LogHttp = matches
|
||||||
.value_of("log-format")
|
.get_one::<String>("log-format")
|
||||||
|
.map(|v| v.as_str())
|
||||||
.unwrap_or(DEFAULT_LOG_FORMAT)
|
.unwrap_or(DEFAULT_LOG_FORMAT)
|
||||||
.parse()?;
|
.parse()?;
|
||||||
let assets_path = match matches.value_of_os("assets") {
|
let assets_path = match matches.get_one::<PathBuf>("assets") {
|
||||||
Some(v) => Some(Args::parse_assets_path(v)?),
|
Some(v) => Some(Args::parse_assets_path(v)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
@@ -281,23 +295,27 @@ impl Args {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_addrs(addrs: &[&str]) -> BoxResult<Vec<IpAddr>> {
|
fn parse_addrs(addrs: &[&str]) -> BoxResult<Vec<BindAddr>> {
|
||||||
let mut ip_addrs = vec![];
|
let mut bind_addrs = vec![];
|
||||||
let mut invalid_addrs = vec![];
|
let mut invalid_addrs = vec![];
|
||||||
for addr in addrs {
|
for addr in addrs {
|
||||||
match addr.parse::<IpAddr>() {
|
match addr.parse::<IpAddr>() {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
ip_addrs.push(v);
|
bind_addrs.push(BindAddr::Address(v));
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
invalid_addrs.push(*addr);
|
if cfg!(unix) {
|
||||||
|
bind_addrs.push(BindAddr::Path(PathBuf::from(addr)));
|
||||||
|
} else {
|
||||||
|
invalid_addrs.push(*addr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !invalid_addrs.is_empty() {
|
if !invalid_addrs.is_empty() {
|
||||||
return Err(format!("Invalid bind address `{}`", invalid_addrs.join(",")).into());
|
return Err(format!("Invalid bind address `{}`", invalid_addrs.join(",")).into());
|
||||||
}
|
}
|
||||||
Ok(ip_addrs)
|
Ok(bind_addrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_path<P: AsRef<Path>>(path: P) -> BoxResult<PathBuf> {
|
fn parse_path<P: AsRef<Path>>(path: P) -> BoxResult<PathBuf> {
|
||||||
@@ -322,3 +340,9 @@ impl Args {
|
|||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum BindAddr {
|
||||||
|
Address(IpAddr),
|
||||||
|
Path(PathBuf),
|
||||||
|
}
|
||||||
|
|||||||
15
src/auth.rs
15
src/auth.rs
@@ -132,7 +132,12 @@ impl GuardType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn sanitize_path(path: &str, uri_prefix: &str) -> String {
|
fn sanitize_path(path: &str, uri_prefix: &str) -> String {
|
||||||
encode_uri(&format!("{}{}", uri_prefix, path.trim_matches('/')))
|
let new_path = match (uri_prefix, path) {
|
||||||
|
("/", "/") => "/".into(),
|
||||||
|
(_, "/") => uri_prefix.trim_end_matches('/').into(),
|
||||||
|
_ => format!("{}{}", uri_prefix, path.trim_matches('/')),
|
||||||
|
};
|
||||||
|
encode_uri(&new_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn walk_path(path: &str) -> impl Iterator<Item = &str> {
|
fn walk_path(path: &str) -> impl Iterator<Item = &str> {
|
||||||
@@ -211,7 +216,7 @@ impl AuthMethod {
|
|||||||
let digest_vals = to_headermap(digest_value).ok()?;
|
let digest_vals = to_headermap(digest_value).ok()?;
|
||||||
digest_vals
|
digest_vals
|
||||||
.get(b"username".as_ref())
|
.get(b"username".as_ref())
|
||||||
.and_then(|b| std::str::from_utf8(*b).ok())
|
.and_then(|b| std::str::from_utf8(b).ok())
|
||||||
.map(|v| v.to_string())
|
.map(|v| v.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,7 +255,7 @@ impl AuthMethod {
|
|||||||
if let (Some(username), Some(nonce), Some(user_response)) = (
|
if let (Some(username), Some(nonce), Some(user_response)) = (
|
||||||
digest_vals
|
digest_vals
|
||||||
.get(b"username".as_ref())
|
.get(b"username".as_ref())
|
||||||
.and_then(|b| std::str::from_utf8(*b).ok()),
|
.and_then(|b| std::str::from_utf8(b).ok()),
|
||||||
digest_vals.get(b"nonce".as_ref()),
|
digest_vals.get(b"nonce".as_ref()),
|
||||||
digest_vals.get(b"response".as_ref()),
|
digest_vals.get(b"response".as_ref()),
|
||||||
) {
|
) {
|
||||||
@@ -273,7 +278,7 @@ impl AuthMethod {
|
|||||||
if qop == &b"auth".as_ref() || qop == &b"auth-int".as_ref() {
|
if qop == &b"auth".as_ref() || qop == &b"auth-int".as_ref() {
|
||||||
correct_response = Some({
|
correct_response = Some({
|
||||||
let mut c = Context::new();
|
let mut c = Context::new();
|
||||||
c.consume(&auth_pass);
|
c.consume(auth_pass);
|
||||||
c.consume(b":");
|
c.consume(b":");
|
||||||
c.consume(nonce);
|
c.consume(nonce);
|
||||||
c.consume(b":");
|
c.consume(b":");
|
||||||
@@ -296,7 +301,7 @@ impl AuthMethod {
|
|||||||
Some(r) => r,
|
Some(r) => r,
|
||||||
None => {
|
None => {
|
||||||
let mut c = Context::new();
|
let mut c = Context::new();
|
||||||
c.consume(&auth_pass);
|
c.consume(auth_pass);
|
||||||
c.consume(b":");
|
c.consume(b":");
|
||||||
c.consume(nonce);
|
c.consume(nonce);
|
||||||
c.consume(b":");
|
c.consume(b":");
|
||||||
|
|||||||
134
src/main.rs
134
src/main.rs
@@ -6,6 +6,8 @@ mod server;
|
|||||||
mod streamer;
|
mod streamer;
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
mod tls;
|
mod tls;
|
||||||
|
#[cfg(unix)]
|
||||||
|
mod unix;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@@ -20,6 +22,7 @@ use std::net::{IpAddr, SocketAddr, TcpListener as StdTcpListener};
|
|||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use args::BindAddr;
|
||||||
use clap_complete::Shell;
|
use clap_complete::Shell;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
@@ -75,11 +78,9 @@ fn serve(
|
|||||||
let inner = Arc::new(Server::new(args.clone(), running));
|
let inner = Arc::new(Server::new(args.clone(), running));
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
let port = args.port;
|
let port = args.port;
|
||||||
for ip in args.addrs.iter() {
|
for bind_addr in args.addrs.iter() {
|
||||||
let inner = inner.clone();
|
let inner = inner.clone();
|
||||||
let incoming = create_addr_incoming(SocketAddr::new(*ip, port))
|
let serve_func = move |remote_addr: Option<SocketAddr>| {
|
||||||
.map_err(|e| format!("Failed to bind `{}:{}`, {}", ip, port, e))?;
|
|
||||||
let serve_func = move |remote_addr: SocketAddr| {
|
|
||||||
let inner = inner.clone();
|
let inner = inner.clone();
|
||||||
async move {
|
async move {
|
||||||
Ok::<_, hyper::Error>(service_fn(move |req: Request| {
|
Ok::<_, hyper::Error>(service_fn(move |req: Request| {
|
||||||
@@ -88,35 +89,57 @@ fn serve(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match args.tls.as_ref() {
|
match bind_addr {
|
||||||
#[cfg(feature = "tls")]
|
BindAddr::Address(ip) => {
|
||||||
Some((certs, key)) => {
|
let incoming = create_addr_incoming(SocketAddr::new(*ip, port))
|
||||||
let config = ServerConfig::builder()
|
.map_err(|e| format!("Failed to bind `{}:{}`, {}", ip, port, e))?;
|
||||||
.with_safe_defaults()
|
match args.tls.as_ref() {
|
||||||
.with_no_client_auth()
|
#[cfg(feature = "tls")]
|
||||||
.with_single_cert(certs.clone(), key.clone())?;
|
Some((certs, key)) => {
|
||||||
let config = Arc::new(config);
|
let config = ServerConfig::builder()
|
||||||
let accepter = TlsAcceptor::new(config.clone(), incoming);
|
.with_safe_defaults()
|
||||||
let new_service = make_service_fn(move |socket: &TlsStream| {
|
.with_no_client_auth()
|
||||||
let remote_addr = socket.remote_addr();
|
.with_single_cert(certs.clone(), key.clone())?;
|
||||||
serve_func(remote_addr)
|
let config = Arc::new(config);
|
||||||
});
|
let accepter = TlsAcceptor::new(config.clone(), incoming);
|
||||||
let server = tokio::spawn(hyper::Server::builder(accepter).serve(new_service));
|
let new_service = make_service_fn(move |socket: &TlsStream| {
|
||||||
handles.push(server);
|
let remote_addr = socket.remote_addr();
|
||||||
|
serve_func(Some(remote_addr))
|
||||||
|
});
|
||||||
|
let server =
|
||||||
|
tokio::spawn(hyper::Server::builder(accepter).serve(new_service));
|
||||||
|
handles.push(server);
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "tls"))]
|
||||||
|
Some(_) => {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let new_service = make_service_fn(move |socket: &AddrStream| {
|
||||||
|
let remote_addr = socket.remote_addr();
|
||||||
|
serve_func(Some(remote_addr))
|
||||||
|
});
|
||||||
|
let server =
|
||||||
|
tokio::spawn(hyper::Server::builder(incoming).serve(new_service));
|
||||||
|
handles.push(server);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "tls"))]
|
BindAddr::Path(path) => {
|
||||||
Some(_) => {
|
if path.exists() {
|
||||||
unreachable!()
|
std::fs::remove_file(path)?;
|
||||||
|
}
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
let listener = tokio::net::UnixListener::bind(path)
|
||||||
|
.map_err(|e| format!("Failed to bind `{}`, {}", path.display(), e))?;
|
||||||
|
let acceptor = unix::UnixAcceptor::from_listener(listener);
|
||||||
|
let new_service = make_service_fn(move |_| serve_func(None));
|
||||||
|
let server = tokio::spawn(hyper::Server::builder(acceptor).serve(new_service));
|
||||||
|
handles.push(server);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
}
|
||||||
let new_service = make_service_fn(move |socket: &AddrStream| {
|
|
||||||
let remote_addr = socket.remote_addr();
|
|
||||||
serve_func(remote_addr)
|
|
||||||
});
|
|
||||||
let server = tokio::spawn(hyper::Server::builder(incoming).serve(new_service));
|
|
||||||
handles.push(server);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Ok(handles)
|
Ok(handles)
|
||||||
}
|
}
|
||||||
@@ -137,17 +160,22 @@ fn create_addr_incoming(addr: SocketAddr) -> BoxResult<AddrIncoming> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_listening(args: Arc<Args>) -> BoxResult<()> {
|
fn print_listening(args: Arc<Args>) -> BoxResult<()> {
|
||||||
let mut addrs = vec![];
|
let mut bind_addrs = vec![];
|
||||||
let (mut ipv4, mut ipv6) = (false, false);
|
let (mut ipv4, mut ipv6) = (false, false);
|
||||||
for ip in args.addrs.iter() {
|
for bind_addr in args.addrs.iter() {
|
||||||
if ip.is_unspecified() {
|
match bind_addr {
|
||||||
if ip.is_ipv6() {
|
BindAddr::Address(ip) => {
|
||||||
ipv6 = true;
|
if ip.is_unspecified() {
|
||||||
} else {
|
if ip.is_ipv6() {
|
||||||
ipv4 = true;
|
ipv6 = true;
|
||||||
|
} else {
|
||||||
|
ipv4 = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bind_addrs.push(bind_addr.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
_ => bind_addrs.push(bind_addr.clone()),
|
||||||
addrs.push(*ip);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ipv4 || ipv6 {
|
if ipv4 || ipv6 {
|
||||||
@@ -156,25 +184,27 @@ fn print_listening(args: Arc<Args>) -> BoxResult<()> {
|
|||||||
for iface in ifaces.into_iter() {
|
for iface in ifaces.into_iter() {
|
||||||
let local_ip = iface.ip();
|
let local_ip = iface.ip();
|
||||||
if ipv4 && local_ip.is_ipv4() {
|
if ipv4 && local_ip.is_ipv4() {
|
||||||
addrs.push(local_ip)
|
bind_addrs.push(BindAddr::Address(local_ip))
|
||||||
}
|
}
|
||||||
if ipv6 && local_ip.is_ipv6() {
|
if ipv6 && local_ip.is_ipv6() {
|
||||||
addrs.push(local_ip)
|
bind_addrs.push(BindAddr::Address(local_ip))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addrs.sort_unstable();
|
bind_addrs.sort_unstable();
|
||||||
let urls = addrs
|
let urls = bind_addrs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|addr| match addr {
|
.map(|bind_addr| match bind_addr {
|
||||||
IpAddr::V4(_) => format!("{}:{}", addr, args.port),
|
BindAddr::Address(addr) => {
|
||||||
IpAddr::V6(_) => format!("[{}]:{}", addr, args.port),
|
let addr = match addr {
|
||||||
|
IpAddr::V4(_) => format!("{}:{}", addr, args.port),
|
||||||
|
IpAddr::V6(_) => format!("[{}]:{}", addr, args.port),
|
||||||
|
};
|
||||||
|
let protocol = if args.tls.is_some() { "https" } else { "http" };
|
||||||
|
format!("{}://{}{}", protocol, addr, args.uri_prefix)
|
||||||
|
}
|
||||||
|
BindAddr::Path(path) => path.display().to_string(),
|
||||||
})
|
})
|
||||||
.map(|addr| match &args.tls {
|
|
||||||
Some(_) => format!("https://{}", addr),
|
|
||||||
None => format!("http://{}", addr),
|
|
||||||
})
|
|
||||||
.map(|url| format!("{}{}", url, args.uri_prefix))
|
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if urls.len() == 1 {
|
if urls.len() == 1 {
|
||||||
|
|||||||
@@ -84,13 +84,15 @@ impl Server {
|
|||||||
pub async fn call(
|
pub async fn call(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
req: Request,
|
req: Request,
|
||||||
addr: SocketAddr,
|
addr: Option<SocketAddr>,
|
||||||
) -> Result<Response, hyper::Error> {
|
) -> Result<Response, hyper::Error> {
|
||||||
let uri = req.uri().clone();
|
let uri = req.uri().clone();
|
||||||
let assets_prefix = self.assets_prefix.clone();
|
let assets_prefix = self.assets_prefix.clone();
|
||||||
let enable_cors = self.args.enable_cors;
|
let enable_cors = self.args.enable_cors;
|
||||||
let mut http_log_data = self.args.log_http.data(&req, &self.args);
|
let mut http_log_data = self.args.log_http.data(&req, &self.args);
|
||||||
http_log_data.insert("remote_addr".to_string(), addr.ip().to_string());
|
if let Some(addr) = addr {
|
||||||
|
http_log_data.insert("remote_addr".to_string(), addr.ip().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
let mut res = match self.clone().handle(req).await {
|
let mut res = match self.clone().handle(req).await {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
@@ -270,8 +272,11 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"MKCOL" => {
|
"MKCOL" => {
|
||||||
if !allow_upload || !is_miss {
|
if !allow_upload {
|
||||||
status_forbid(&mut res);
|
status_forbid(&mut res);
|
||||||
|
} else if !is_miss {
|
||||||
|
*res.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
|
||||||
|
*res.body_mut() = Body::from("Already exists");
|
||||||
} else {
|
} else {
|
||||||
self.handle_mkcol(path, &mut res).await?;
|
self.handle_mkcol(path, &mut res).await?;
|
||||||
}
|
}
|
||||||
@@ -386,41 +391,43 @@ impl Server {
|
|||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
let mut paths: Vec<PathItem> = vec![];
|
let mut paths: Vec<PathItem> = vec![];
|
||||||
let path_buf = path.to_path_buf();
|
|
||||||
let hidden = Arc::new(self.args.hidden.to_vec());
|
|
||||||
let hidden = hidden.clone();
|
|
||||||
let running = self.running.clone();
|
|
||||||
let search = query_params.get("q").unwrap().to_lowercase();
|
let search = query_params.get("q").unwrap().to_lowercase();
|
||||||
let search_paths = tokio::task::spawn_blocking(move || {
|
if !search.is_empty() {
|
||||||
let mut it = WalkDir::new(&path_buf).into_iter();
|
let path_buf = path.to_path_buf();
|
||||||
let mut paths: Vec<PathBuf> = vec![];
|
let hidden = Arc::new(self.args.hidden.to_vec());
|
||||||
while let Some(Ok(entry)) = it.next() {
|
let hidden = hidden.clone();
|
||||||
if !running.load(Ordering::SeqCst) {
|
let running = self.running.clone();
|
||||||
break;
|
let search_paths = tokio::task::spawn_blocking(move || {
|
||||||
}
|
let mut it = WalkDir::new(&path_buf).into_iter();
|
||||||
let entry_path = entry.path();
|
let mut paths: Vec<PathBuf> = vec![];
|
||||||
let base_name = get_file_name(entry_path);
|
while let Some(Ok(entry)) = it.next() {
|
||||||
let file_type = entry.file_type();
|
if !running.load(Ordering::SeqCst) {
|
||||||
if is_hidden(&hidden, base_name) {
|
break;
|
||||||
if file_type.is_dir() {
|
|
||||||
it.skip_current_dir();
|
|
||||||
}
|
}
|
||||||
continue;
|
let entry_path = entry.path();
|
||||||
|
let base_name = get_file_name(entry_path);
|
||||||
|
let file_type = entry.file_type();
|
||||||
|
if is_hidden(&hidden, base_name) {
|
||||||
|
if file_type.is_dir() {
|
||||||
|
it.skip_current_dir();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if !base_name.to_lowercase().contains(&search) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if entry.path().symlink_metadata().is_err() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
paths.push(entry_path.to_path_buf());
|
||||||
}
|
}
|
||||||
if !base_name.to_lowercase().contains(&search) {
|
paths
|
||||||
continue;
|
})
|
||||||
|
.await?;
|
||||||
|
for search_path in search_paths.into_iter() {
|
||||||
|
if let Ok(Some(item)) = self.to_pathitem(search_path, path.to_path_buf()).await {
|
||||||
|
paths.push(item);
|
||||||
}
|
}
|
||||||
if entry.path().symlink_metadata().is_err() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
paths.push(entry_path.to_path_buf());
|
|
||||||
}
|
|
||||||
paths
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
for search_path in search_paths.into_iter() {
|
|
||||||
if let Ok(Some(item)) = self.to_pathitem(search_path, path.to_path_buf()).await {
|
|
||||||
paths.push(item);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.send_index(path, paths, true, query_params, head_only, res)
|
self.send_index(path, paths, true, query_params, head_only, res)
|
||||||
@@ -593,7 +600,7 @@ impl Server {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(mime) = mime_guess::from_path(&path).first() {
|
if let Some(mime) = mime_guess::from_path(path).first() {
|
||||||
res.headers_mut().typed_insert(ContentType::from(mime));
|
res.headers_mut().typed_insert(ContentType::from(mime));
|
||||||
} else {
|
} else {
|
||||||
res.headers_mut().insert(
|
res.headers_mut().insert(
|
||||||
@@ -892,7 +899,11 @@ impl Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn extract_path(&self, path: &str) -> Option<PathBuf> {
|
fn extract_path(&self, path: &str) -> Option<PathBuf> {
|
||||||
let decoded_path = decode_uri(&path[1..])?;
|
let mut slash_stripped_path = path;
|
||||||
|
while let Some(p) = slash_stripped_path.strip_prefix('/') {
|
||||||
|
slash_stripped_path = p
|
||||||
|
}
|
||||||
|
let decoded_path = decode_uri(slash_stripped_path)?;
|
||||||
let slashes_switched = if cfg!(windows) {
|
let slashes_switched = if cfg!(windows) {
|
||||||
decoded_path.replace('/', "\\")
|
decoded_path.replace('/', "\\")
|
||||||
} else {
|
} else {
|
||||||
@@ -902,7 +913,7 @@ impl Server {
|
|||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
Some(self.args.path.join(&stripped_path))
|
Some(self.args.path.join(stripped_path))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn strip_path_prefix<'a, P: AsRef<Path>>(&self, path: &'a P) -> Option<&'a Path> {
|
fn strip_path_prefix<'a, P: AsRef<Path>>(&self, path: &'a P) -> Option<&'a Path> {
|
||||||
|
|||||||
17
src/tls.rs
17
src/tls.rs
@@ -5,6 +5,7 @@ use hyper::server::conn::{AddrIncoming, AddrStream};
|
|||||||
use rustls::{Certificate, PrivateKey};
|
use rustls::{Certificate, PrivateKey};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
use std::path::Path;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{fs, io};
|
use std::{fs, io};
|
||||||
@@ -123,10 +124,12 @@ impl Accept for TlsAcceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load public certificate from file.
|
// Load public certificate from file.
|
||||||
pub fn load_certs(filename: &str) -> Result<Vec<Certificate>, Box<dyn std::error::Error>> {
|
pub fn load_certs<T: AsRef<Path>>(
|
||||||
|
filename: T,
|
||||||
|
) -> Result<Vec<Certificate>, Box<dyn std::error::Error>> {
|
||||||
// Open certificate file.
|
// Open certificate file.
|
||||||
let cert_file = fs::File::open(&filename)
|
let cert_file = fs::File::open(filename.as_ref())
|
||||||
.map_err(|e| format!("Failed to access `{}`, {}", &filename, e))?;
|
.map_err(|e| format!("Failed to access `{}`, {}", filename.as_ref().display(), e))?;
|
||||||
let mut reader = io::BufReader::new(cert_file);
|
let mut reader = io::BufReader::new(cert_file);
|
||||||
|
|
||||||
// Load and return certificate.
|
// Load and return certificate.
|
||||||
@@ -138,9 +141,11 @@ pub fn load_certs(filename: &str) -> Result<Vec<Certificate>, Box<dyn std::error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load private key from file.
|
// Load private key from file.
|
||||||
pub fn load_private_key(filename: &str) -> Result<PrivateKey, Box<dyn std::error::Error>> {
|
pub fn load_private_key<T: AsRef<Path>>(
|
||||||
let key_file = fs::File::open(&filename)
|
filename: T,
|
||||||
.map_err(|e| format!("Failed to access `{}`, {}", &filename, e))?;
|
) -> Result<PrivateKey, Box<dyn std::error::Error>> {
|
||||||
|
let key_file = fs::File::open(filename.as_ref())
|
||||||
|
.map_err(|e| format!("Failed to access `{}`, {}", filename.as_ref().display(), e))?;
|
||||||
let mut reader = io::BufReader::new(key_file);
|
let mut reader = io::BufReader::new(key_file);
|
||||||
|
|
||||||
// Load and return a single private key.
|
// Load and return a single private key.
|
||||||
|
|||||||
31
src/unix.rs
Normal file
31
src/unix.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use hyper::server::accept::Accept;
|
||||||
|
use tokio::net::UnixListener;
|
||||||
|
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
pub struct UnixAcceptor {
|
||||||
|
inner: UnixListener,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UnixAcceptor {
|
||||||
|
pub fn from_listener(listener: UnixListener) -> Self {
|
||||||
|
Self { inner: listener }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Accept for UnixAcceptor {
|
||||||
|
type Conn = tokio::net::UnixStream;
|
||||||
|
type Error = std::io::Error;
|
||||||
|
|
||||||
|
fn poll_accept(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||||
|
match self.inner.poll_accept(cx) {
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
|
Poll::Ready(Ok((socket, _addr))) => Poll::Ready(Some(Ok(socket))),
|
||||||
|
Poll::Ready(Err(err)) => Poll::Ready(Some(Err(err))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -121,3 +121,15 @@ fn auth_webdav_copy(
|
|||||||
assert_eq!(resp.status(), 403);
|
assert_eq!(resp.status(), 403);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn auth_path_prefix(
|
||||||
|
#[with(&["--auth", "/@user:pass", "--path-prefix", "xyz", "-A"])] server: TestServer,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let url = format!("{}xyz/index.html", server.url());
|
||||||
|
let resp = fetch!(b"GET", &url).send()?;
|
||||||
|
assert_eq!(resp.status(), 401);
|
||||||
|
let resp = fetch!(b"GET", &url).send_with_digest_auth("user", "pass")?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -98,6 +98,15 @@ fn head_dir_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn empty_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}?q=", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
let paths = utils::retrieve_index_paths(&resp.text()?);
|
||||||
|
assert!(paths.is_empty());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn get_file(server: TestServer) -> Result<(), Error> {
|
fn get_file(server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?;
|
let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?;
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ fn tls_works(#[case] server: TestServer) -> Result<(), Error> {
|
|||||||
#[rstest]
|
#[rstest]
|
||||||
fn wrong_path_cert() -> Result<(), Error> {
|
fn wrong_path_cert() -> Result<(), Error> {
|
||||||
Command::cargo_bin("dufs")?
|
Command::cargo_bin("dufs")?
|
||||||
.args(&["--tls-cert", "wrong", "--tls-key", "tests/data/key.pem"])
|
.args(["--tls-cert", "wrong", "--tls-key", "tests/data/key.pem"])
|
||||||
.assert()
|
.assert()
|
||||||
.failure()
|
.failure()
|
||||||
.stderr(contains("error: Failed to access `wrong`"));
|
.stderr(contains("error: Failed to access `wrong`"));
|
||||||
@@ -46,7 +46,7 @@ fn wrong_path_cert() -> Result<(), Error> {
|
|||||||
#[rstest]
|
#[rstest]
|
||||||
fn wrong_path_key() -> Result<(), Error> {
|
fn wrong_path_key() -> Result<(), Error> {
|
||||||
Command::cargo_bin("dufs")?
|
Command::cargo_bin("dufs")?
|
||||||
.args(&["--tls-cert", "tests/data/cert.pem", "--tls-key", "wrong"])
|
.args(["--tls-cert", "tests/data/cert.pem", "--tls-key", "wrong"])
|
||||||
.assert()
|
.assert()
|
||||||
.failure()
|
.failure()
|
||||||
.stderr(contains("error: Failed to access `wrong`"));
|
.stderr(contains("error: Failed to access `wrong`"));
|
||||||
|
|||||||
@@ -47,6 +47,13 @@ fn propfind_404(server: TestServer) -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn propfind_double_slash(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPFIND", format!("{}/", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 207);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn propfind_file(server: TestServer) -> Result<(), Error> {
|
fn propfind_file(server: TestServer) -> Result<(), Error> {
|
||||||
let resp = fetch!(b"PROPFIND", format!("{}test.html", server.url())).send()?;
|
let resp = fetch!(b"PROPFIND", format!("{}test.html", server.url())).send()?;
|
||||||
@@ -93,6 +100,13 @@ fn mkcol_not_allow_upload(server: TestServer) -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn mkcol_already_exists(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"MKCOL", format!("{}dira", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 405);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn copy_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
fn copy_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
let new_url = format!("{}test2.html", server.url());
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
|||||||
Reference in New Issue
Block a user