dufs/src/server.rs

473 lines
16 KiB
Rust
Raw Normal View History

2022-05-26 11:17:55 +03:00
use crate::{Args, BoxResult};
2022-05-28 13:58:43 +03:00
use async_walkdir::WalkDir;
2022-05-30 09:22:35 +03:00
use async_zip::read::seek::ZipFileReader;
2022-05-28 13:58:43 +03:00
use async_zip::write::{EntryOptions, ZipFileWriter};
use async_zip::Compression;
use futures::stream::StreamExt;
2022-05-26 11:17:55 +03:00
use futures::TryStreamExt;
use headers::{
AccessControlAllowHeaders, AccessControlAllowOrigin, ContentType, ETag, HeaderMapExt,
IfModifiedSince, IfNoneMatch, LastModified,
};
2022-05-29 12:33:21 +03:00
use hyper::header::{HeaderValue, ACCEPT, CONTENT_TYPE, ORIGIN, RANGE, WWW_AUTHENTICATE};
2022-05-26 11:17:55 +03:00
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, StatusCode};
use percent_encoding::percent_decode;
use serde::Serialize;
use std::convert::Infallible;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;
2022-05-28 13:58:43 +03:00
use tokio::fs::File;
2022-05-29 01:57:16 +03:00
use tokio::io::AsyncWrite;
2022-05-26 11:17:55 +03:00
use tokio::{fs, io};
use tokio_util::codec::{BytesCodec, FramedRead};
2022-05-29 01:57:16 +03:00
use tokio_util::io::{ReaderStream, StreamReader};
2022-05-26 11:17:55 +03:00
type Request = hyper::Request<Body>;
type Response = hyper::Response<Body>;
macro_rules! status_code {
($status:expr) => {
hyper::Response::builder()
.status($status)
.body($status.canonical_reason().unwrap_or_default().into())
.unwrap()
};
}
2022-05-30 09:32:41 +03:00
const INDEX_HTML: &str = include_str!("assets/index.html");
const INDEX_CSS: &str = include_str!("assets/index.css");
const INDEX_JS: &str = include_str!("assets/index.js");
2022-05-28 17:27:28 +03:00
const BUF_SIZE: usize = 1024 * 16;
2022-05-26 11:17:55 +03:00
pub async fn serve(args: Args) -> BoxResult<()> {
let address = args.address()?;
let inner = Arc::new(InnerService::new(args));
let make_svc = make_service_fn(move |_| {
let inner = inner.clone();
async {
Ok::<_, Infallible>(service_fn(move |req| {
let inner = inner.clone();
2022-05-27 04:01:16 +03:00
inner.call(req)
2022-05-26 11:17:55 +03:00
}))
}
});
let server = hyper::Server::try_bind(&address)?.serve(make_svc);
let address = server.local_addr();
eprintln!("Files served on http://{}", address);
server.await?;
Ok(())
}
struct InnerService {
args: Args,
}
impl InnerService {
pub fn new(args: Args) -> Self {
Self { args }
}
2022-05-27 04:01:16 +03:00
pub async fn call(self: Arc<Self>, req: Request) -> Result<Response, hyper::Error> {
let method = req.method().clone();
let uri = req.uri().clone();
2022-05-29 12:33:21 +03:00
let cors = self.args.cors;
let mut res = self
2022-05-27 04:01:16 +03:00
.handle(req)
.await
.unwrap_or_else(|_| status_code!(StatusCode::INTERNAL_SERVER_ERROR));
info!(r#""{} {}" - {}"#, method, uri, res.status());
2022-05-29 12:33:21 +03:00
if cors {
add_cors(&mut res);
}
2022-05-27 04:01:16 +03:00
Ok(res)
}
pub async fn handle(self: Arc<Self>, req: Request) -> BoxResult<Response> {
2022-05-26 14:52:54 +03:00
if !self.auth_guard(&req).unwrap_or_default() {
let mut res = status_code!(StatusCode::UNAUTHORIZED);
2022-05-26 15:14:18 +03:00
res.headers_mut()
2022-05-29 12:33:21 +03:00
.insert(WWW_AUTHENTICATE, HeaderValue::from_static("Basic"));
2022-05-26 15:14:18 +03:00
return Ok(res);
2022-05-26 14:52:54 +03:00
}
2022-05-29 12:33:21 +03:00
match *req.method() {
Method::GET => self.handle_static(req).await,
Method::PUT => {
if self.args.readonly {
return Ok(status_code!(StatusCode::FORBIDDEN));
}
self.handle_upload(req).await
2022-05-26 13:06:52 +03:00
}
2022-05-29 12:33:21 +03:00
Method::OPTIONS => Ok(status_code!(StatusCode::NO_CONTENT)),
Method::DELETE => self.handle_delete(req).await,
_ => Ok(status_code!(StatusCode::NOT_FOUND)),
2022-05-27 04:01:16 +03:00
}
2022-05-26 11:17:55 +03:00
}
async fn handle_static(&self, req: Request) -> BoxResult<Response> {
let req_path = req.uri().path();
let path = match self.get_file_path(req_path)? {
2022-05-26 11:17:55 +03:00
Some(path) => path,
None => return Ok(status_code!(StatusCode::FORBIDDEN)),
};
match fs::metadata(&path).await {
Ok(meta) => {
if meta.is_dir() {
2022-05-29 07:43:40 +03:00
let req_query = req.uri().query().unwrap_or_default();
if req_query == "zip" {
return self.handle_send_dir_zip(path.as_path()).await;
2022-05-28 13:58:43 +03:00
}
2022-05-29 07:43:40 +03:00
if let Some(q) = req_query.strip_prefix("q=") {
return self.handle_query_dir(path.as_path(), q).await;
}
self.handle_ls_dir(path.as_path(), true).await
2022-05-26 11:17:55 +03:00
} else {
self.handle_send_file(&req, path.as_path()).await
2022-05-26 11:17:55 +03:00
}
}
Err(_) => {
if req_path.ends_with('/') {
2022-05-29 07:43:40 +03:00
self.handle_ls_dir(path.as_path(), false).await
} else {
Ok(status_code!(StatusCode::NOT_FOUND))
}
}
2022-05-26 11:17:55 +03:00
}
}
async fn handle_upload(&self, mut req: Request) -> BoxResult<Response> {
let forbidden = status_code!(StatusCode::FORBIDDEN);
2022-05-26 11:17:55 +03:00
let path = match self.get_file_path(req.uri().path())? {
Some(path) => path,
None => return Ok(forbidden),
2022-05-26 11:17:55 +03:00
};
match path.parent() {
Some(parent) => match fs::metadata(parent).await {
Ok(meta) => {
if !meta.is_dir() {
return Ok(forbidden);
}
}
Err(_) => fs::create_dir_all(parent).await?,
},
None => return Ok(forbidden),
2022-05-26 11:17:55 +03:00
}
2022-05-30 09:22:35 +03:00
let mut file = fs::File::create(&path).await?;
2022-05-26 11:17:55 +03:00
let body_with_io_error = req
.body_mut()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err));
let body_reader = StreamReader::new(body_with_io_error);
futures::pin_mut!(body_reader);
io::copy(&mut body_reader, &mut file).await?;
2022-05-30 09:22:35 +03:00
let req_query = req.uri().query().unwrap_or_default();
if req_query == "unzip" {
let root = path.parent().unwrap();
let mut zip = ZipFileReader::new(File::open(&path).await?).await?;
for i in 0..zip.entries().len() {
let entry = &zip.entries()[i];
let entry_name = entry.name();
let entry_path = root.join(entry_name);
if entry_name.ends_with('/') {
fs::create_dir_all(entry_path).await?;
} else {
if let Some(parent) = entry_path.parent() {
if fs::symlink_metadata(parent).await.is_err() {
fs::create_dir_all(&parent).await?;
}
}
let mut outfile = fs::File::create(&entry_path).await?;
let mut reader = zip.entry_reader(i).await?;
io::copy(&mut reader, &mut outfile).await?;
}
}
fs::remove_file(&path).await?;
}
2022-05-26 11:17:55 +03:00
return Ok(status_code!(StatusCode::OK));
}
2022-05-26 14:52:54 +03:00
async fn handle_delete(&self, req: Request) -> BoxResult<Response> {
let path = match self.get_file_path(req.uri().path())? {
Some(path) => path,
None => return Ok(status_code!(StatusCode::FORBIDDEN)),
};
let meta = fs::metadata(&path).await?;
if meta.is_file() {
fs::remove_file(path).await?;
} else {
fs::remove_dir_all(path).await?;
}
Ok(status_code!(StatusCode::OK))
}
2022-05-29 07:43:40 +03:00
async fn handle_ls_dir(&self, path: &Path, exist: bool) -> BoxResult<Response> {
2022-05-26 11:17:55 +03:00
let mut paths: Vec<PathItem> = vec![];
if exist {
let mut rd = fs::read_dir(path).await?;
while let Some(entry) = rd.next_entry().await? {
let entry_path = entry.path();
2022-05-29 07:43:40 +03:00
if let Ok(item) = get_path_item(entry_path, path.to_path_buf()).await {
paths.push(item);
}
}
2022-05-26 11:17:55 +03:00
}
2022-05-29 07:43:40 +03:00
self.send_index(path, paths)
}
2022-05-26 11:17:55 +03:00
2022-05-29 07:43:40 +03:00
async fn handle_query_dir(&self, path: &Path, q: &str) -> BoxResult<Response> {
let mut paths: Vec<PathItem> = vec![];
let mut walkdir = WalkDir::new(path);
while let Some(entry) = walkdir.next().await {
if let Ok(entry) = entry {
if !entry
.file_name()
.to_string_lossy()
.to_lowercase()
.contains(&q.to_lowercase())
{
continue;
}
if fs::symlink_metadata(entry.path()).await.is_err() {
continue;
}
if let Ok(item) = get_path_item(entry.path(), path.to_path_buf()).await {
paths.push(item);
}
}
}
self.send_index(path, paths)
2022-05-26 11:17:55 +03:00
}
2022-05-28 13:58:43 +03:00
async fn handle_send_dir_zip(&self, path: &Path) -> BoxResult<Response> {
2022-05-29 01:57:16 +03:00
let (mut writer, reader) = tokio::io::duplex(BUF_SIZE);
2022-05-28 17:27:28 +03:00
let path = path.to_owned();
tokio::spawn(async move {
if let Err(e) = dir_zip(&mut writer, &path).await {
error!("Fail to zip {}, {}", path.display(), e.to_string());
}
});
2022-05-29 01:57:16 +03:00
let stream = ReaderStream::new(reader);
let body = Body::wrap_stream(stream);
2022-05-28 13:58:43 +03:00
Ok(Response::new(body))
}
async fn handle_send_file(&self, req: &Request, path: &Path) -> BoxResult<Response> {
let (file, meta) = tokio::join!(fs::File::open(path), fs::metadata(path),);
let (file, meta) = (file?, meta?);
let mut res = Response::default();
if let Ok(mtime) = meta.modified() {
let mtime_value = get_timestamp(&mtime);
let size = meta.len();
let etag = format!(r#""{}-{}""#, mtime_value, size)
.parse::<ETag>()
.unwrap();
let last_modified = LastModified::from(mtime);
let fresh = {
// `If-None-Match` takes presedence over `If-Modified-Since`.
if let Some(if_none_match) = req.headers().typed_get::<IfNoneMatch>() {
!if_none_match.precondition_passes(&etag)
} else if let Some(if_modified_since) = req.headers().typed_get::<IfModifiedSince>()
{
!if_modified_since.is_modified(mtime)
} else {
false
}
};
res.headers_mut().typed_insert(last_modified);
res.headers_mut().typed_insert(etag);
if fresh {
*res.status_mut() = StatusCode::NOT_MODIFIED;
return Ok(res);
}
}
if let Some(mime) = mime_guess::from_path(&path).first() {
res.headers_mut().typed_insert(ContentType::from(mime));
}
2022-05-26 11:17:55 +03:00
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
*res.body_mut() = body;
Ok(res)
2022-05-26 11:17:55 +03:00
}
2022-05-29 07:43:40 +03:00
fn send_index(&self, path: &Path, mut paths: Vec<PathItem>) -> BoxResult<Response> {
paths.sort_unstable();
2022-05-30 06:47:57 +03:00
let rel_path = match self.args.path.parent() {
Some(p) => path.strip_prefix(p).unwrap(),
None => path,
};
2022-05-29 07:43:40 +03:00
let data = IndexData {
2022-05-30 06:47:57 +03:00
breadcrumb: normalize_path(rel_path),
2022-05-29 07:43:40 +03:00
paths,
readonly: self.args.readonly,
};
let data = serde_json::to_string(&data).unwrap();
2022-05-30 06:47:57 +03:00
let output = INDEX_HTML.replace(
"__SLOB__",
&format!(
r#"
<title>Files in {} - Duf/</title>
<style>{}</style>
<script>var DATA = {}; {}</script>
"#,
rel_path.display(),
INDEX_CSS,
data,
INDEX_JS
),
);
2022-05-29 07:43:40 +03:00
Ok(Response::new(output.into()))
2022-05-29 07:43:40 +03:00
}
2022-05-26 13:06:52 +03:00
fn auth_guard(&self, req: &Request) -> BoxResult<bool> {
if let Some(auth) = &self.args.auth {
if let Some(value) = req.headers().get("Authorization") {
let value = value.to_str()?;
let value = if value.contains("Basic ") {
&value[6..]
} else {
return Ok(false);
};
let value = base64::decode(value)?;
let value = std::str::from_utf8(&value)?;
return Ok(value == auth);
} else {
2022-05-30 07:40:57 +03:00
if self.args.no_auth_read && req.method() == Method::GET {
return Ok(true);
}
2022-05-26 13:06:52 +03:00
return Ok(false);
}
}
Ok(true)
}
2022-05-26 11:17:55 +03:00
fn get_file_path(&self, path: &str) -> BoxResult<Option<PathBuf>> {
let decoded_path = percent_decode(path[1..].as_bytes()).decode_utf8()?;
let slashes_switched = if cfg!(windows) {
decoded_path.replace('/', "\\")
} else {
decoded_path.into_owned()
};
let path = self.args.path.join(&slashes_switched);
if path.starts_with(&self.args.path) {
Ok(Some(path))
} else {
Ok(None)
}
}
}
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
2022-05-29 07:43:40 +03:00
struct IndexData {
2022-05-26 11:17:55 +03:00
breadcrumb: String,
paths: Vec<PathItem>,
2022-05-26 13:06:52 +03:00
readonly: bool,
2022-05-26 11:17:55 +03:00
}
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
struct PathItem {
path_type: PathType,
name: String,
mtime: u64,
2022-05-26 11:17:55 +03:00
size: Option<u64>,
}
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
enum PathType {
Dir,
SymlinkDir,
File,
SymlinkFile,
}
2022-05-29 07:43:40 +03:00
async fn get_path_item<P: AsRef<Path>>(path: P, base_path: P) -> BoxResult<PathItem> {
let path = path.as_ref();
let rel_path = path.strip_prefix(base_path).unwrap();
let meta = fs::metadata(&path).await?;
let s_meta = fs::symlink_metadata(&path).await?;
let is_dir = meta.is_dir();
let is_symlink = s_meta.file_type().is_symlink();
let path_type = match (is_symlink, is_dir) {
(true, true) => PathType::SymlinkDir,
(false, true) => PathType::Dir,
(true, false) => PathType::SymlinkFile,
(false, false) => PathType::File,
};
let mtime = get_timestamp(&meta.modified()?);
2022-05-29 07:43:40 +03:00
let size = match path_type {
PathType::Dir | PathType::SymlinkDir => None,
PathType::File | PathType::SymlinkFile => Some(meta.len()),
};
let name = normalize_path(rel_path);
Ok(PathItem {
path_type,
name,
mtime,
size,
})
}
fn get_timestamp(time: &SystemTime) -> u64 {
time.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis() as u64
}
2022-05-26 11:17:55 +03:00
fn normalize_path<P: AsRef<Path>>(path: P) -> String {
let path = path.as_ref().to_str().unwrap_or_default();
if cfg!(windows) {
path.replace('\\', "/")
} else {
path.to_string()
}
}
2022-05-28 13:58:43 +03:00
2022-05-29 12:33:21 +03:00
fn add_cors(res: &mut Response) {
res.headers_mut()
.typed_insert(AccessControlAllowOrigin::ANY);
res.headers_mut().typed_insert(
vec![RANGE, CONTENT_TYPE, ACCEPT, ORIGIN, WWW_AUTHENTICATE]
.into_iter()
.collect::<AccessControlAllowHeaders>(),
);
}
2022-05-28 13:58:43 +03:00
async fn dir_zip<W: AsyncWrite + Unpin>(writer: &mut W, dir: &Path) -> BoxResult<()> {
let mut writer = ZipFileWriter::new(writer);
let mut walkdir = WalkDir::new(dir);
while let Some(entry) = walkdir.next().await {
if let Ok(entry) = entry {
2022-05-29 07:43:40 +03:00
let meta = match fs::symlink_metadata(entry.path()).await {
Ok(meta) => meta,
Err(_) => continue,
};
2022-05-28 13:58:43 +03:00
if meta.is_file() {
let filepath = entry.path();
let filename = match filepath.strip_prefix(dir).ok().and_then(|v| v.to_str()) {
Some(v) => v,
None => continue,
};
let entry_options = EntryOptions::new(filename.to_owned(), Compression::Deflate);
let mut file = File::open(&filepath).await?;
let mut file_writer = writer.write_entry_stream(entry_options).await?;
io::copy(&mut file, &mut file_writer).await?;
file_writer.close().await?;
}
}
}
writer.close().await?;
Ok(())
}