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.
|
||||
|
||||
## [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
|
||||
|
||||
|
||||
573
Cargo.lock
generated
573
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "dufs"
|
||||
version = "0.30.0"
|
||||
version = "0.31.0"
|
||||
edition = "2021"
|
||||
authors = ["sigoden <sigoden@gmail.com>"]
|
||||
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"]
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "3", default-features = false, features = ["std", "wrap_help"] }
|
||||
clap_complete = "3"
|
||||
clap = { version = "4", features = ["wrap_help"] }
|
||||
clap_complete = "4"
|
||||
chrono = "0.4"
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
|
||||
tokio-util = { version = "0.7", features = ["io-util"] }
|
||||
|
||||
22
README.md
22
README.md
@@ -42,18 +42,17 @@ Download from [Github Releases](https://github.com/sigoden/dufs/releases), unzip
|
||||
```
|
||||
Dufs is a distinctive utility file server - https://github.com/sigoden/dufs
|
||||
|
||||
USAGE:
|
||||
dufs [OPTIONS] [--] [root]
|
||||
Usage: dufs [OPTIONS] [root]
|
||||
|
||||
ARGS:
|
||||
<root> Specific path to serve [default: .]
|
||||
Arguments:
|
||||
[root] Specific path to serve [default: .]
|
||||
|
||||
OPTIONS:
|
||||
-b, --bind <addr>... Specify bind address
|
||||
Options:
|
||||
-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, 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]
|
||||
-A, --allow-all Allow all operations
|
||||
--allow-upload Allow upload files/folders
|
||||
@@ -123,10 +122,15 @@ Require username/password
|
||||
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
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
<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>
|
||||
</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 />
|
||||
</form>
|
||||
</div>
|
||||
|
||||
110
src/args.rs
110
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};
|
||||
#[cfg(feature = "tls")]
|
||||
use rustls::{Certificate, PrivateKey};
|
||||
@@ -14,7 +15,7 @@ use crate::tls::{load_certs, load_private_key};
|
||||
use crate::utils::encode_uri;
|
||||
use crate::BoxResult;
|
||||
|
||||
pub fn build_cli() -> Command<'static> {
|
||||
pub fn build_cli() -> Command {
|
||||
let app = Command::new(env!("CARGO_CRATE_NAME"))
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.author(env!("CARGO_PKG_AUTHORS"))
|
||||
@@ -23,31 +24,30 @@ pub fn build_cli() -> Command<'static> {
|
||||
" - ",
|
||||
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::new("bind")
|
||||
.short('b')
|
||||
.long("bind")
|
||||
.help("Specify bind address")
|
||||
.multiple_values(true)
|
||||
.value_delimiter(',')
|
||||
.help("Specify bind address or unix socket")
|
||||
.action(ArgAction::Append)
|
||||
.value_name("addr"),
|
||||
.value_delimiter(',')
|
||||
.value_name("addrs"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("port")
|
||||
.short('p')
|
||||
.long("port")
|
||||
.default_value("5000")
|
||||
.value_parser(value_parser!(u16))
|
||||
.help("Specify port to listen on")
|
||||
.value_name("port"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("root")
|
||||
.default_value(".")
|
||||
.allow_invalid_utf8(true)
|
||||
.help("Specific path to serve"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("path-prefix")
|
||||
.long("path-prefix")
|
||||
@@ -66,15 +66,14 @@ pub fn build_cli() -> Command<'static> {
|
||||
.long("auth")
|
||||
.help("Add auth for path")
|
||||
.action(ArgAction::Append)
|
||||
.multiple_values(true)
|
||||
.value_delimiter(',')
|
||||
.value_name("rule"),
|
||||
.value_name("rules"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("auth-method")
|
||||
.long("auth-method")
|
||||
.help("Select auth method")
|
||||
.possible_values(["basic", "digest"])
|
||||
.value_parser(PossibleValuesParser::new(["basic", "digest"]))
|
||||
.default_value("digest")
|
||||
.value_name("value"),
|
||||
)
|
||||
@@ -82,53 +81,62 @@ pub fn build_cli() -> Command<'static> {
|
||||
Arg::new("allow-all")
|
||||
.short('A')
|
||||
.long("allow-all")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Allow all operations"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("allow-upload")
|
||||
.long("allow-upload")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Allow upload files/folders"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("allow-delete")
|
||||
.long("allow-delete")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Allow delete files/folders"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("allow-search")
|
||||
.long("allow-search")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Allow search files/folders"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("allow-symlink")
|
||||
.long("allow-symlink")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Allow symlink to files/folders outside root directory"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("enable-cors")
|
||||
.long("enable-cors")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Enable CORS, sets `Access-Control-Allow-Origin: *`"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("render-index")
|
||||
.long("render-index")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Serve index.html when requesting a directory, returns 404 if not found index.html"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("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"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("render-spa")
|
||||
.long("render-spa")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help("Serve SPA(Single Page Application)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("assets")
|
||||
.long("assets")
|
||||
.help("Use custom assets to override builtin assets")
|
||||
.allow_invalid_utf8(true)
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.value_name("path")
|
||||
);
|
||||
|
||||
@@ -138,12 +146,14 @@ pub fn build_cli() -> Command<'static> {
|
||||
Arg::new("tls-cert")
|
||||
.long("tls-cert")
|
||||
.value_name("path")
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.help("Path to an SSL/TLS certificate to serve with HTTPS"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tls-key")
|
||||
.long("tls-key")
|
||||
.value_name("path")
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.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)]
|
||||
pub struct Args {
|
||||
pub addrs: Vec<IpAddr>,
|
||||
pub addrs: Vec<BindAddr>,
|
||||
pub port: u16,
|
||||
pub path: PathBuf,
|
||||
pub path_is_file: bool,
|
||||
@@ -199,16 +209,16 @@ impl Args {
|
||||
/// If a parsing error ocurred, exit the process and print out informative
|
||||
/// error message to user.
|
||||
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
|
||||
.values_of("bind")
|
||||
.map(|v| v.collect())
|
||||
.get_many::<String>("bind")
|
||||
.map(|bind| bind.map(|v| v.as_str()).collect())
|
||||
.unwrap_or_else(|| vec!["0.0.0.0", "::"]);
|
||||
let addrs: Vec<IpAddr> = Args::parse_addrs(&addrs)?;
|
||||
let path = Args::parse_path(matches.value_of_os("root").unwrap_or_default())?;
|
||||
let addrs: Vec<BindAddr> = Args::parse_addrs(&addrs)?;
|
||||
let path = Args::parse_path(matches.get_one::<PathBuf>("root").unwrap())?;
|
||||
let path_is_file = path.metadata()?.is_file();
|
||||
let path_prefix = matches
|
||||
.value_of("path-prefix")
|
||||
.get_one::<String>("path-prefix")
|
||||
.map(|v| v.trim_matches('/').to_owned())
|
||||
.unwrap_or_default();
|
||||
let uri_prefix = if path_prefix.is_empty() {
|
||||
@@ -217,28 +227,31 @@ impl Args {
|
||||
format!("/{}/", &encode_uri(&path_prefix))
|
||||
};
|
||||
let hidden: Vec<String> = matches
|
||||
.value_of("hidden")
|
||||
.get_one::<String>("hidden")
|
||||
.map(|v| v.split(',').map(|x| x.to_string()).collect())
|
||||
.unwrap_or_default();
|
||||
let enable_cors = matches.is_present("enable-cors");
|
||||
let enable_cors = matches.get_flag("enable-cors");
|
||||
let auth: Vec<&str> = matches
|
||||
.values_of("auth")
|
||||
.map(|v| v.collect())
|
||||
.get_many::<String>("auth")
|
||||
.map(|auth| auth.map(|v| v.as_str()).collect())
|
||||
.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,
|
||||
_ => AuthMethod::Digest,
|
||||
};
|
||||
let auth = AccessControl::new(&auth, &uri_prefix)?;
|
||||
let allow_upload = matches.is_present("allow-all") || matches.is_present("allow-upload");
|
||||
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
|
||||
let allow_search = matches.is_present("allow-all") || matches.is_present("allow-search");
|
||||
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
|
||||
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 allow_upload = matches.get_flag("allow-all") || matches.get_flag("allow-upload");
|
||||
let allow_delete = matches.get_flag("allow-all") || matches.get_flag("allow-delete");
|
||||
let allow_search = matches.get_flag("allow-all") || matches.get_flag("allow-search");
|
||||
let allow_symlink = matches.get_flag("allow-all") || matches.get_flag("allow-symlink");
|
||||
let render_index = matches.get_flag("render-index");
|
||||
let render_try_index = matches.get_flag("render-try-index");
|
||||
let render_spa = matches.get_flag("render-spa");
|
||||
#[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)) => {
|
||||
let certs = load_certs(certs_file)?;
|
||||
let key = load_private_key(key_file)?;
|
||||
@@ -249,10 +262,11 @@ impl Args {
|
||||
#[cfg(not(feature = "tls"))]
|
||||
let tls = None;
|
||||
let log_http: LogHttp = matches
|
||||
.value_of("log-format")
|
||||
.get_one::<String>("log-format")
|
||||
.map(|v| v.as_str())
|
||||
.unwrap_or(DEFAULT_LOG_FORMAT)
|
||||
.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)?),
|
||||
None => None,
|
||||
};
|
||||
@@ -281,23 +295,27 @@ impl Args {
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_addrs(addrs: &[&str]) -> BoxResult<Vec<IpAddr>> {
|
||||
let mut ip_addrs = vec![];
|
||||
fn parse_addrs(addrs: &[&str]) -> BoxResult<Vec<BindAddr>> {
|
||||
let mut bind_addrs = vec![];
|
||||
let mut invalid_addrs = vec![];
|
||||
for addr in addrs {
|
||||
match addr.parse::<IpAddr>() {
|
||||
Ok(v) => {
|
||||
ip_addrs.push(v);
|
||||
bind_addrs.push(BindAddr::Address(v));
|
||||
}
|
||||
Err(_) => {
|
||||
if cfg!(unix) {
|
||||
bind_addrs.push(BindAddr::Path(PathBuf::from(addr)));
|
||||
} else {
|
||||
invalid_addrs.push(*addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !invalid_addrs.is_empty() {
|
||||
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> {
|
||||
@@ -322,3 +340,9 @@ impl Args {
|
||||
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 {
|
||||
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> {
|
||||
@@ -211,7 +216,7 @@ impl AuthMethod {
|
||||
let digest_vals = to_headermap(digest_value).ok()?;
|
||||
digest_vals
|
||||
.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())
|
||||
}
|
||||
}
|
||||
@@ -250,7 +255,7 @@ impl AuthMethod {
|
||||
if let (Some(username), Some(nonce), Some(user_response)) = (
|
||||
digest_vals
|
||||
.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"response".as_ref()),
|
||||
) {
|
||||
@@ -273,7 +278,7 @@ impl AuthMethod {
|
||||
if qop == &b"auth".as_ref() || qop == &b"auth-int".as_ref() {
|
||||
correct_response = Some({
|
||||
let mut c = Context::new();
|
||||
c.consume(&auth_pass);
|
||||
c.consume(auth_pass);
|
||||
c.consume(b":");
|
||||
c.consume(nonce);
|
||||
c.consume(b":");
|
||||
@@ -296,7 +301,7 @@ impl AuthMethod {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
let mut c = Context::new();
|
||||
c.consume(&auth_pass);
|
||||
c.consume(auth_pass);
|
||||
c.consume(b":");
|
||||
c.consume(nonce);
|
||||
c.consume(b":");
|
||||
|
||||
72
src/main.rs
72
src/main.rs
@@ -6,6 +6,8 @@ mod server;
|
||||
mod streamer;
|
||||
#[cfg(feature = "tls")]
|
||||
mod tls;
|
||||
#[cfg(unix)]
|
||||
mod unix;
|
||||
mod utils;
|
||||
|
||||
#[macro_use]
|
||||
@@ -20,6 +22,7 @@ use std::net::{IpAddr, SocketAddr, TcpListener as StdTcpListener};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use args::BindAddr;
|
||||
use clap_complete::Shell;
|
||||
use futures::future::join_all;
|
||||
use tokio::net::TcpListener;
|
||||
@@ -75,11 +78,9 @@ fn serve(
|
||||
let inner = Arc::new(Server::new(args.clone(), running));
|
||||
let mut handles = vec![];
|
||||
let port = args.port;
|
||||
for ip in args.addrs.iter() {
|
||||
for bind_addr in args.addrs.iter() {
|
||||
let inner = inner.clone();
|
||||
let incoming = create_addr_incoming(SocketAddr::new(*ip, port))
|
||||
.map_err(|e| format!("Failed to bind `{}:{}`, {}", ip, port, e))?;
|
||||
let serve_func = move |remote_addr: SocketAddr| {
|
||||
let serve_func = move |remote_addr: Option<SocketAddr>| {
|
||||
let inner = inner.clone();
|
||||
async move {
|
||||
Ok::<_, hyper::Error>(service_fn(move |req: Request| {
|
||||
@@ -88,6 +89,10 @@ fn serve(
|
||||
}))
|
||||
}
|
||||
};
|
||||
match bind_addr {
|
||||
BindAddr::Address(ip) => {
|
||||
let incoming = create_addr_incoming(SocketAddr::new(*ip, port))
|
||||
.map_err(|e| format!("Failed to bind `{}:{}`, {}", ip, port, e))?;
|
||||
match args.tls.as_ref() {
|
||||
#[cfg(feature = "tls")]
|
||||
Some((certs, key)) => {
|
||||
@@ -99,9 +104,10 @@ fn serve(
|
||||
let accepter = TlsAcceptor::new(config.clone(), incoming);
|
||||
let new_service = make_service_fn(move |socket: &TlsStream| {
|
||||
let remote_addr = socket.remote_addr();
|
||||
serve_func(remote_addr)
|
||||
serve_func(Some(remote_addr))
|
||||
});
|
||||
let server = tokio::spawn(hyper::Server::builder(accepter).serve(new_service));
|
||||
let server =
|
||||
tokio::spawn(hyper::Server::builder(accepter).serve(new_service));
|
||||
handles.push(server);
|
||||
}
|
||||
#[cfg(not(feature = "tls"))]
|
||||
@@ -111,13 +117,30 @@ fn serve(
|
||||
None => {
|
||||
let new_service = make_service_fn(move |socket: &AddrStream| {
|
||||
let remote_addr = socket.remote_addr();
|
||||
serve_func(remote_addr)
|
||||
serve_func(Some(remote_addr))
|
||||
});
|
||||
let server = tokio::spawn(hyper::Server::builder(incoming).serve(new_service));
|
||||
let server =
|
||||
tokio::spawn(hyper::Server::builder(incoming).serve(new_service));
|
||||
handles.push(server);
|
||||
}
|
||||
};
|
||||
}
|
||||
BindAddr::Path(path) => {
|
||||
if path.exists() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(handles)
|
||||
}
|
||||
|
||||
@@ -137,9 +160,11 @@ fn create_addr_incoming(addr: SocketAddr) -> BoxResult<AddrIncoming> {
|
||||
}
|
||||
|
||||
fn print_listening(args: Arc<Args>) -> BoxResult<()> {
|
||||
let mut addrs = vec![];
|
||||
let mut bind_addrs = vec![];
|
||||
let (mut ipv4, mut ipv6) = (false, false);
|
||||
for ip in args.addrs.iter() {
|
||||
for bind_addr in args.addrs.iter() {
|
||||
match bind_addr {
|
||||
BindAddr::Address(ip) => {
|
||||
if ip.is_unspecified() {
|
||||
if ip.is_ipv6() {
|
||||
ipv6 = true;
|
||||
@@ -147,7 +172,10 @@ fn print_listening(args: Arc<Args>) -> BoxResult<()> {
|
||||
ipv4 = true;
|
||||
}
|
||||
} else {
|
||||
addrs.push(*ip);
|
||||
bind_addrs.push(bind_addr.clone());
|
||||
}
|
||||
}
|
||||
_ => bind_addrs.push(bind_addr.clone()),
|
||||
}
|
||||
}
|
||||
if ipv4 || ipv6 {
|
||||
@@ -156,25 +184,27 @@ fn print_listening(args: Arc<Args>) -> BoxResult<()> {
|
||||
for iface in ifaces.into_iter() {
|
||||
let local_ip = iface.ip();
|
||||
if ipv4 && local_ip.is_ipv4() {
|
||||
addrs.push(local_ip)
|
||||
bind_addrs.push(BindAddr::Address(local_ip))
|
||||
}
|
||||
if ipv6 && local_ip.is_ipv6() {
|
||||
addrs.push(local_ip)
|
||||
bind_addrs.push(BindAddr::Address(local_ip))
|
||||
}
|
||||
}
|
||||
}
|
||||
addrs.sort_unstable();
|
||||
let urls = addrs
|
||||
bind_addrs.sort_unstable();
|
||||
let urls = bind_addrs
|
||||
.into_iter()
|
||||
.map(|addr| match addr {
|
||||
.map(|bind_addr| match bind_addr {
|
||||
BindAddr::Address(addr) => {
|
||||
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<_>>();
|
||||
|
||||
if urls.len() == 1 {
|
||||
|
||||
@@ -84,13 +84,15 @@ impl Server {
|
||||
pub async fn call(
|
||||
self: Arc<Self>,
|
||||
req: Request,
|
||||
addr: SocketAddr,
|
||||
addr: Option<SocketAddr>,
|
||||
) -> Result<Response, hyper::Error> {
|
||||
let uri = req.uri().clone();
|
||||
let assets_prefix = self.assets_prefix.clone();
|
||||
let enable_cors = self.args.enable_cors;
|
||||
let mut http_log_data = self.args.log_http.data(&req, &self.args);
|
||||
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 {
|
||||
Ok(res) => {
|
||||
@@ -270,8 +272,11 @@ impl Server {
|
||||
}
|
||||
}
|
||||
"MKCOL" => {
|
||||
if !allow_upload || !is_miss {
|
||||
if !allow_upload {
|
||||
status_forbid(&mut res);
|
||||
} else if !is_miss {
|
||||
*res.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
|
||||
*res.body_mut() = Body::from("Already exists");
|
||||
} else {
|
||||
self.handle_mkcol(path, &mut res).await?;
|
||||
}
|
||||
@@ -386,11 +391,12 @@ impl Server {
|
||||
res: &mut Response,
|
||||
) -> BoxResult<()> {
|
||||
let mut paths: Vec<PathItem> = vec![];
|
||||
let search = query_params.get("q").unwrap().to_lowercase();
|
||||
if !search.is_empty() {
|
||||
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_paths = tokio::task::spawn_blocking(move || {
|
||||
let mut it = WalkDir::new(&path_buf).into_iter();
|
||||
let mut paths: Vec<PathBuf> = vec![];
|
||||
@@ -423,6 +429,7 @@ impl Server {
|
||||
paths.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.send_index(path, paths, true, query_params, head_only, res)
|
||||
}
|
||||
|
||||
@@ -593,7 +600,7 @@ impl Server {
|
||||
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));
|
||||
} else {
|
||||
res.headers_mut().insert(
|
||||
@@ -892,7 +899,11 @@ impl Server {
|
||||
}
|
||||
|
||||
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) {
|
||||
decoded_path.replace('/', "\\")
|
||||
} else {
|
||||
@@ -902,7 +913,7 @@ impl Server {
|
||||
Some(path) => path,
|
||||
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> {
|
||||
|
||||
17
src/tls.rs
17
src/tls.rs
@@ -5,6 +5,7 @@ use hyper::server::conn::{AddrIncoming, AddrStream};
|
||||
use rustls::{Certificate, PrivateKey};
|
||||
use std::future::Future;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, io};
|
||||
@@ -123,10 +124,12 @@ impl Accept for TlsAcceptor {
|
||||
}
|
||||
|
||||
// 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.
|
||||
let cert_file = fs::File::open(&filename)
|
||||
.map_err(|e| format!("Failed to access `{}`, {}", &filename, e))?;
|
||||
let cert_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(cert_file);
|
||||
|
||||
// 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.
|
||||
pub fn load_private_key(filename: &str) -> Result<PrivateKey, Box<dyn std::error::Error>> {
|
||||
let key_file = fs::File::open(&filename)
|
||||
.map_err(|e| format!("Failed to access `{}`, {}", &filename, e))?;
|
||||
pub fn load_private_key<T: AsRef<Path>>(
|
||||
filename: T,
|
||||
) -> 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);
|
||||
|
||||
// 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);
|
||||
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(())
|
||||
}
|
||||
|
||||
#[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]
|
||||
fn get_file(server: TestServer) -> Result<(), Error> {
|
||||
let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?;
|
||||
|
||||
@@ -34,7 +34,7 @@ fn tls_works(#[case] server: TestServer) -> Result<(), Error> {
|
||||
#[rstest]
|
||||
fn wrong_path_cert() -> Result<(), Error> {
|
||||
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()
|
||||
.failure()
|
||||
.stderr(contains("error: Failed to access `wrong`"));
|
||||
@@ -46,7 +46,7 @@ fn wrong_path_cert() -> Result<(), Error> {
|
||||
#[rstest]
|
||||
fn wrong_path_key() -> Result<(), Error> {
|
||||
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()
|
||||
.failure()
|
||||
.stderr(contains("error: Failed to access `wrong`"));
|
||||
|
||||
@@ -47,6 +47,13 @@ fn propfind_404(server: TestServer) -> Result<(), Error> {
|
||||
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]
|
||||
fn propfind_file(server: TestServer) -> Result<(), Error> {
|
||||
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(())
|
||||
}
|
||||
|
||||
#[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]
|
||||
fn copy_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||
let new_url = format!("{}test2.html", server.url());
|
||||
|
||||
Reference in New Issue
Block a user