Compare commits

..

16 Commits

Author SHA1 Message Date
sigoden
f103e15e15 chore(release): version v0.11.0 2022-06-03 11:19:57 +08:00
sigoden
9dda55b7c8 feat: listen 0.0.0.0 by default 2022-06-03 11:19:16 +08:00
sigoden
c3dd0f0ec5 feat: support gracefully shutdown server 2022-06-03 11:00:12 +08:00
sigoden
4167e5c07e chore(ci): publish to docker
* ci: publish to docker

* update release.yaml

* update Dockerfile
2022-06-03 10:36:06 +08:00
sigoden
f66e129985 chore(release): version v0.10.1 2022-06-03 07:21:15 +08:00
sigoden
7c3970480e chore: add type comments to assets/js 2022-06-03 07:18:12 +08:00
sigoden
34bc8d411a fix: panic when bind already used port 2022-06-03 07:15:41 +08:00
sigoden
51cedf2f8a chore(release): version v0.10.0 2022-06-03 06:58:10 +08:00
sigoden
48c3c7ded6 fix: broken ui 2022-06-03 06:57:20 +08:00
sigoden
4491a74b34 docs: refactor readme 2022-06-03 06:51:50 +08:00
sigoden
7c2449cb1a fix: rename --no-auth-read to --no-auth-access 2022-06-03 06:51:03 +08:00
sigoden
0a3d9c391f feat: improve ui 2022-06-03 06:49:55 +08:00
sigoden
07f4e7d0f2 fix: remove unzip file even failed to unzip 2022-06-02 19:43:43 +08:00
sigoden
c50f97925c feat: change auth logic/options 2022-06-02 19:36:04 +08:00
sigoden
ecb3984edc chore(readme): insert cli output 2022-06-02 17:10:15 +08:00
sigoden
24f885164a refactor: small improvement 2022-06-02 17:06:22 +08:00
13 changed files with 336 additions and 191 deletions

13
.dockerignore Normal file
View File

@@ -0,0 +1,13 @@
# Directories
/.git/
/.github/
/target/
/examples/
/docs/
/benches/
/tmp/
# Files
.gitignore
*.md
LICENSE*

View File

@@ -6,8 +6,10 @@ on:
- v[0-9]+.[0-9]+.[0-9]+* - v[0-9]+.[0-9]+.[0-9]+*
jobs: jobs:
all: release:
name: All name: Publish to Github Reelases
outputs:
rc: ${{ steps.check-tag.outputs.rc }}
strategy: strategy:
matrix: matrix:
@@ -124,3 +126,40 @@ jobs:
prerelease: ${{ steps.check-tag.outputs.rc == 'true' }} prerelease: ${{ steps.check-tag.outputs.rc == 'true' }}
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
docker:
name: Publish to Docker Hub
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
needs: release
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
push: ${{ needs.release.outputs.rc == 'false' }}
tags: sigoden/duf:latest, sigoden/duf:${{ github.ref_name }}
publish-crate:
name: Publish to crates.io
if: ${{ needs.release.outputs.rc == 'false' }}
runs-on: ubuntu-latest
needs: release
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
- name: Publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_API_TOKEN }}
run: cargo publish

View File

@@ -2,6 +2,40 @@
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.11.0] - 2022-06-03
### Features
- Support gracefully shutdown server
- Listen 0.0.0.0 by default
## [0.10.1] - 2022-06-02
### Bug Fixes
- Panic when bind already used port
## [0.10.0] - 2022-06-02
### Bug Fixes
- Remove unzip file even failed to unzip
- Rename --no-auth-read to --no-auth-access
- Broken ui
### Documentation
- Refactor readme
### Features
- Change auth logic/options
- Improve ui
### Refactor
- Small improvement
## [0.9.0] - 2022-06-02 ## [0.9.0] - 2022-06-02
### Documentation ### Documentation
@@ -27,12 +61,6 @@ All notable changes to this project will be documented in this file.
- Add some headers to res - Add some headers to res
- Support render-index/render-spa - Support render-index/render-spa
### Miscellaneous Tasks
- Move src/assets out of src
- Update description
- Upgrade version
## [0.7.0] - 2022-05-31 ## [0.7.0] - 2022-05-31
### Bug Fixes ### Bug Fixes
@@ -46,10 +74,6 @@ All notable changes to this project will be documented in this file.
- Drag and drop uploads, upload folder - Drag and drop uploads, upload folder
### Miscellaneous Tasks
- Upgrade version
## [0.6.0] - 2022-05-31 ## [0.6.0] - 2022-05-31
### Features ### Features
@@ -58,10 +82,6 @@ All notable changes to this project will be documented in this file.
- Distinct upload and delete operation - Distinct upload and delete operation
- Support range requests - Support range requests
### Miscellaneous Tasks
- Upgrade version
### Refactor ### Refactor
- Improve code quality - Improve code quality
@@ -74,12 +94,6 @@ All notable changes to this project will be documented in this file.
- Add no-auth-read options - Add no-auth-read options
- Unzip zip file when unload - Unzip zip file when unload
### Miscellaneous Tasks
- Reorganize web static files
- Rename src/static to src/assets
- Upgrade version
## [0.4.0] - 2022-05-29 ## [0.4.0] - 2022-05-29
### Features ### Features
@@ -87,10 +101,6 @@ All notable changes to this project will be documented in this file.
- Replace --static option to --no-edit - Replace --static option to --no-edit
- Add cors - Add cors
### Miscellaneous Tasks
- Upgrade version
## [0.3.0] - 2022-05-29 ## [0.3.0] - 2022-05-29
### Documentation ### Documentation
@@ -137,10 +147,6 @@ All notable changes to this project will be documented in this file.
- Add logger - Add logger
- Download folder as zip file - Download folder as zip file
### Miscellaneous Tasks
- Update cargo metadata
## [0.1.0] - 2022-05-26 ## [0.1.0] - 2022-05-26
### Bug Fixes ### Bug Fixes
@@ -158,11 +164,6 @@ All notable changes to this project will be documented in this file.
- Support delete operation - Support delete operation
- Remove parent path - Remove parent path
### Miscellaneous Tasks
- Add readme and license
- Update cargo metadata
### Styling ### Styling
- Cargo fmt - Cargo fmt

12
Cargo.lock generated
View File

@@ -286,7 +286,7 @@ dependencies = [
[[package]] [[package]]
name = "duf" name = "duf"
version = "0.9.0" version = "0.11.0"
dependencies = [ dependencies = [
"async-walkdir", "async-walkdir",
"async_zip", "async_zip",
@@ -882,6 +882,15 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.6" version = "0.4.6"
@@ -965,6 +974,7 @@ dependencies = [
"num_cpus", "num_cpus",
"once_cell", "once_cell",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"winapi 0.3.9", "winapi 0.3.9",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "duf" name = "duf"
version = "0.9.0" version = "0.11.0"
edition = "2021" edition = "2021"
authors = ["sigoden <sigoden@gmail.com>"] authors = ["sigoden <sigoden@gmail.com>"]
description = "Duf is a fully functional file server." description = "Duf is a fully functional file server."
@@ -14,7 +14,7 @@ keywords = ["static", "file", "server", "http", "cli"]
[dependencies] [dependencies]
clap = { version = "3", default-features = false, features = ["std", "cargo"] } clap = { version = "3", default-features = false, features = ["std", "cargo"] }
chrono = "0.4" chrono = "0.4"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util"]} tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
tokio-rustls = "0.23" tokio-rustls = "0.23"
tokio-stream = { version = "0.1", features = ["net"] } tokio-stream = { version = "0.1", features = ["net"] }
tokio-util = { version = "0.7", features = ["codec", "io-util"] } tokio-util = { version = "0.7", features = ["codec", "io-util"] }

10
Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM rust:1.61 as builder
RUN rustup target add x86_64-unknown-linux-musl
RUN apt-get update && apt-get install --no-install-recommends -y musl-tools
WORKDIR /app
COPY . .
RUN cargo build --target x86_64-unknown-linux-musl --release
FROM alpine
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/duf /bin/
ENTRYPOINT ["/bin/duf"]

View File

@@ -16,7 +16,8 @@ Duf is a fully functional file server.
- Delete files - Delete files
- Basic authentication - Basic authentication
- Upload zip file then unzip - Upload zip file then unzip
- Serve through https - Partial responses (Parallel/Resume download)
- Support https/tls
- Easy to use with curl - Easy to use with curl
## Install ## Install
@@ -27,11 +28,47 @@ Duf is a fully functional file server.
cargo install duf cargo install duf
``` ```
### With docker
```
docker run -v /tmp:/tmp -p 5000:5000 --rm -it docker.io/sigoden/duf /tmp
```
### Binaries on macOS, Linux, Windows ### Binaries on macOS, Linux, Windows
Download from [Github Releases](https://github.com/sigoden/duf/releases), unzip and add duf to your $PATH. Download from [Github Releases](https://github.com/sigoden/duf/releases), unzip and add duf to your $PATH.
## Usage ## CLI
```
Duf is a fully functional file server.
USAGE:
duf [OPTIONS] [path]
ARGS:
<path> Path to a root directory for serving files [default: .]
OPTIONS:
-a, --auth <user:pass> Use HTTP authentication
--no-auth-access Not required auth when access static files
-A, --allow-all Allow all operations
--allow-delete Allow delete files/folders
--allow-symlink Allow symlink to files/folders outside root directory
--allow-upload Allow upload files/folders
-b, --bind <address> Specify bind address [default: 0.0.0.0]
--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-spa Render for single-page application
--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
-V, --version Print version information
```
## Examples
You can run this command to start serving your current working directory on 127.0.0.1:5000 by default. You can run this command to start serving your current working directory on 127.0.0.1:5000 by default.
@@ -45,18 +82,10 @@ duf
duf folder_name duf folder_name
``` ```
Listen on all Interfaces and port 3000
```
duf -b 0.0.0.0 -p 3000
```
Allow all operations such as upload, delete Allow all operations such as upload, delete
```sh ```sh
duf --allow-all duf --allow-all
# or
duf -A
``` ```
Only allow upload operation Only allow upload operation
@@ -71,13 +100,13 @@ Serve a single page application (SPA)
duf --render-spa duf --render-spa
``` ```
Serve https Use https
``` ```
duf --tls-cert my.crt --tls-key my.key duf --tls-cert my.crt --tls-key my.key
``` ```
### Api ## API
Download a file Download a file
``` ```

View File

@@ -97,38 +97,46 @@ body {
padding: 0 1em; padding: 0 1em;
} }
.main th { .uploaders-table th,
.paths-table th {
text-align: left; text-align: left;
font-weight: unset; font-weight: unset;
color: #5c5c5c; color: #5c5c5c;
white-space: nowrap; white-space: nowrap;
} }
.main td { .uploaders-table td,
.paths-table td {
white-space: nowrap; white-space: nowrap;
} }
.main .cell-name { .uploaders-table .cell-name,
width: 400px; .paths-table .cell-name {
width: 500px;
} }
.main .cell-mtime { .uploaders-table .cell-status {
width: 80px;
padding-left: 0.6em;
}
.paths-table .cell-actions {
width: 60px;
display: flex;
padding-left: 0.6em;
}
.paths-table .cell-mtime {
width: 120px; width: 120px;
padding-left: 0.6em; padding-left: 0.6em;
} }
.main .cell-size { .paths-table .cell-size {
text-align: right; text-align: right;
width: 70px; width: 70px;
padding-left: 0.6em; padding-left: 0.6em;
} }
.main .cell-actions {
width: 60px;
display: flex;
padding-left: 0.6em;
}
.path svg { .path svg {
height: 100%; height: 100%;
@@ -158,7 +166,7 @@ body {
padding-left: 0.4em; padding-left: 0.4em;
} }
.uploaders { .uploaders-table {
padding: 0.5em 0; padding: 0.5em 0;
} }

View File

@@ -31,9 +31,15 @@
</form> </form>
</div> </div>
<div class="main"> <div class="main">
<div class="uploaders"> <table class="uploaders-table hidden">
</div> <thead>
<table> <tr>
<th class="cell-name">Name</th>
<th class="cell-status">Status</th>
</tr>
</thead>
</table>
<table class="paths-table hidden">
<thead> <thead>
<tr> <tr>
<th class="cell-name">Name</th> <th class="cell-name">Name</th>

View File

@@ -1,11 +1,17 @@
let $tbody, $uploaders; /**
* @type Element
*/
let $pathsTable, $pathsTableBody, $uploadersTable;
/**
* @type string
*/
let baseDir; let baseDir;
class Uploader { class Uploader {
idx; idx;
file; file;
name; name;
$elem; $uploadStatus;
static globalIdx = 0; static globalIdx = 0;
constructor(file, dirs) { constructor(file, dirs) {
this.name = [...dirs, file.name].join("/"); this.name = [...dirs, file.name].join("/");
@@ -19,12 +25,16 @@ class Uploader {
if (file.name == baseDir + ".zip") { if (file.name == baseDir + ".zip") {
url += "?unzip"; url += "?unzip";
} }
$uploaders.insertAdjacentHTML("beforeend", ` $uploadersTable.insertAdjacentHTML("beforeend", `
<div class="uploader path"> <tr id="upload${idx}" class="uploader">
<div><svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></div> <td class="path cell-name">
<a href="${url}" id="file${idx}">${name} (0%)</a> <div>${getSvg("File")}</div>
</div>`); <a href="${url}">${name}</a>
this.$elem = document.getElementById(`file${idx}`); </td>
<td class="cell-status" id="uploadStatus${idx}"></td>
</tr>`);
$uploadersTable.classList.remove("hidden");
this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
const ajax = new XMLHttpRequest(); const ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", e => this.progress(e), false); ajax.upload.addEventListener("progress", e => this.progress(e), false);
@@ -45,15 +55,15 @@ class Uploader {
progress(event) { progress(event) {
const percent = (event.loaded / event.total) * 100; const percent = (event.loaded / event.total) * 100;
this.$elem.innerHTML = `${this.name} (${percent.toFixed(2)}%)`; this.$uploadStatus.innerHTML = `${percent.toFixed(2)}%`;
} }
complete() { complete() {
this.$elem.innerHTML = `${this.name}`; this.$uploadStatus.innerHTML = ``;
} }
fail() { fail() {
this.$elem.innerHTML = `<strike>${this.name}</strike>`; this.$uploadStatus.innerHTML = ``;
} }
} }
@@ -110,15 +120,15 @@ function addPath(file, index) {
${actionDelete} ${actionDelete}
</td>` </td>`
$tbody.insertAdjacentHTML("beforeend", ` $pathsTableBody.insertAdjacentHTML("beforeend", `
<tr id="addPath${index}"> <tr id="addPath${index}">
<td class="path cell-name"> <td class="path cell-name">
<div>${getSvg(file.path_type)}</div> <div>${getSvg(file.path_type)}</div>
<a href="${url}" title="${file.name}">${file.name}</a> <a href="${url}" title="${file.name}">${file.name}</a>
</td> </td>
<td class="cell-mtime">${formatMtime(file.mtime)}</td> <td class="cell-mtime">${formatMtime(file.mtime)}</td>
<td class="cell-size">${formatSize(file.size)}</td> <td class="cell-size">${formatSize(file.size)}</td>
${actionCell} ${actionCell}
</tr>`) </tr>`)
} }
@@ -134,6 +144,10 @@ async function deletePath(index) {
}); });
if (res.status === 200) { if (res.status === 200) {
document.getElementById(`addPath${index}`).remove(); document.getElementById(`addPath${index}`).remove();
DATA.paths[index] = null;
if (!DATA.paths.find(v => !!v)) {
$pathsTable.classList.add("hidden");
}
} else { } else {
throw new Error(await res.text()) throw new Error(await res.text())
} }
@@ -224,19 +238,23 @@ function formatSize(size) {
} }
function ready() { function ready() {
$tbody = document.querySelector(".main tbody"); $pathsTable = document.querySelector(".paths-table")
$uploaders = document.querySelector(".uploaders"); $pathsTableBody = document.querySelector(".paths-table tbody");
$uploadersTable = document.querySelector(".uploaders-table");
addBreadcrumb(DATA.breadcrumb); addBreadcrumb(DATA.breadcrumb);
if (Array.isArray(DATA.paths)) { if (Array.isArray(DATA.paths)) {
const len = DATA.paths.length; const len = DATA.paths.length;
if (len > 0) {
$pathsTable.classList.remove("hidden");
}
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
addPath(DATA.paths[i], i); addPath(DATA.paths[i], i);
} }
} }
if (DATA.allow_upload) { if (DATA.allow_upload) {
dropzone(); dropzone();
document.querySelector(".upload-control").classList.remove(["hidden"]); document.querySelector(".upload-control").classList.remove("hidden");
document.getElementById("file").addEventListener("change", e => { document.getElementById("file").addEventListener("change", e => {
const files = e.target.files; const files = e.target.files;
for (let file of files) { for (let file of files) {

View File

@@ -16,7 +16,7 @@ fn app() -> clap::Command<'static> {
Arg::new("address") Arg::new("address")
.short('b') .short('b')
.long("bind") .long("bind")
.default_value("127.0.0.1") .default_value("0.0.0.0")
.help("Specify bind address") .help("Specify bind address")
.value_name("address"), .value_name("address"),
) )
@@ -49,39 +49,41 @@ fn app() -> clap::Command<'static> {
.arg( .arg(
Arg::new("allow-upload") Arg::new("allow-upload")
.long("allow-upload") .long("allow-upload")
.help("Allow upload operation"), .help("Allow upload files/folders"),
) )
.arg( .arg(
Arg::new("allow-delete") Arg::new("allow-delete")
.long("allow-delete") .long("allow-delete")
.help("Allow delete operation"), .help("Allow delete files/folders"),
) )
.arg( .arg(
Arg::new("allow-symlink") Arg::new("allow-symlink")
.long("allow-symlink") .long("allow-symlink")
.help("Allow symlink to directories/files outside root directory"), .help("Allow symlink to files/folders outside root directory"),
) )
.arg( .arg(
Arg::new("render-index") Arg::new("render-index")
.long("render-index") .long("render-index")
.help("Render existing index.html when requesting a directory"), .help("Render index.html when requesting a directory"),
) )
.arg( .arg(
Arg::new("render-spa") Arg::new("render-spa")
.long("render-spa") .long("render-spa")
.help("Render spa, rewrite all not-found requests to `index.html"), .help("Render for single-page application"),
) )
.arg( .arg(
Arg::new("auth") Arg::new("auth")
.short('a') .short('a')
.display_order(1)
.long("auth") .long("auth")
.help("Use HTTP authentication for all operations") .help("Use HTTP authentication")
.value_name("user:pass"), .value_name("user:pass"),
) )
.arg( .arg(
Arg::new("no-auth-read") Arg::new("no-auth-access")
.long("no-auth-read") .display_order(1)
.help("Do not authenticate read operations like static serving"), .long("no-auth-access")
.help("Not required auth when access static files"),
) )
.arg( .arg(
Arg::new("cors") Arg::new("cors")
@@ -113,7 +115,7 @@ pub struct Args {
pub path: PathBuf, pub path: PathBuf,
pub path_prefix: Option<String>, pub path_prefix: Option<String>,
pub auth: Option<String>, pub auth: Option<String>,
pub no_auth_read: bool, pub no_auth_access: bool,
pub allow_upload: bool, pub allow_upload: bool,
pub allow_delete: bool, pub allow_delete: bool,
pub allow_symlink: bool, pub allow_symlink: bool,
@@ -135,7 +137,7 @@ impl Args {
let path_prefix = matches.value_of("path-prefix").map(|v| v.to_owned()); let path_prefix = matches.value_of("path-prefix").map(|v| v.to_owned());
let cors = matches.is_present("cors"); let cors = matches.is_present("cors");
let auth = matches.value_of("auth").map(|v| v.to_owned()); let auth = matches.value_of("auth").map(|v| v.to_owned());
let no_auth_read = matches.is_present("no-auth-read"); let no_auth_access = matches.is_present("no-auth-access");
let allow_upload = matches.is_present("allow-all") || matches.is_present("allow-upload"); 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_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");
@@ -156,7 +158,7 @@ impl Args {
path, path,
path_prefix, path_prefix,
auth, auth,
no_auth_read, no_auth_access,
cors, cors,
allow_delete, allow_delete,
allow_upload, allow_upload,
@@ -171,7 +173,7 @@ impl Args {
fn parse_path<P: AsRef<Path>>(path: P) -> BoxResult<PathBuf> { fn parse_path<P: AsRef<Path>>(path: P) -> BoxResult<PathBuf> {
let path = path.as_ref(); let path = path.as_ref();
if !path.exists() { if !path.exists() {
bail!("error: path \"{}\" doesn't exist", path.display()); return Err(format!("Path `{}` doesn't exist", path.display()).into());
} }
env::current_dir() env::current_dir()
@@ -179,27 +181,14 @@ impl Args {
p.push(path); // If path is absolute, it replaces the current path. p.push(path); // If path is absolute, it replaces the current path.
std::fs::canonicalize(p) std::fs::canonicalize(p)
}) })
.or_else(|err| { .map_err(|err| format!("Failed to access path `{}`: {}", path.display(), err,).into())
bail!(
"error: failed to access path \"{}\": {}",
path.display(),
err,
)
})
} }
/// Construct socket address from arguments. /// Construct socket address from arguments.
pub fn address(&self) -> BoxResult<SocketAddr> { pub fn address(&self) -> BoxResult<SocketAddr> {
format!("{}:{}", self.address, self.port) format!("{}:{}", self.address, self.port)
.parse() .parse()
.or_else(|err| { .map_err(|_| format!("Invalid bind address `{}:{}`", self.address, self.port).into())
bail!(
"error: invalid address {}:{} : {}",
self.address,
self.port,
err,
)
})
} }
} }

View File

@@ -1,9 +1,3 @@
macro_rules! bail {
($($tt:tt)*) => {
return Err(From::from(format!($($tt)*)))
}
}
mod args; mod args;
mod server; mod server;
@@ -23,6 +17,6 @@ async fn run() -> BoxResult<()> {
} }
fn handle_err<T>(err: Box<dyn std::error::Error>) -> T { fn handle_err<T>(err: Box<dyn std::error::Error>) -> T {
eprintln!("Server error: {}", err); eprintln!("error: {}", err);
std::process::exit(1); std::process::exit(1);
} }

View File

@@ -53,17 +53,24 @@ macro_rules! status {
} }
pub async fn serve(args: Args) -> BoxResult<()> { pub async fn serve(args: Args) -> BoxResult<()> {
match args.tls.as_ref() {
Some(_) => serve_https(args).await,
None => serve_http(args).await,
}
}
pub async fn serve_https(args: Args) -> BoxResult<()> {
let args = Arc::new(args); let args = Arc::new(args);
let socket_addr = args.address()?; let socket_addr = args.address()?;
let (certs, key) = args.tls.clone().unwrap();
let inner = Arc::new(InnerService::new(args.clone())); let inner = Arc::new(InnerService::new(args.clone()));
if let Some((certs, key)) = args.tls.as_ref() {
let config = ServerConfig::builder() let config = ServerConfig::builder()
.with_safe_defaults() .with_safe_defaults()
.with_no_client_auth() .with_no_client_auth()
.with_single_cert(certs.clone(), key.clone())?; .with_single_cert(certs, key)?;
let tls_acceptor = TlsAcceptor::from(Arc::new(config)); let tls_acceptor = TlsAcceptor::from(Arc::new(config));
let arc_acceptor = Arc::new(tls_acceptor); let arc_acceptor = Arc::new(tls_acceptor);
let listener = TcpListener::bind(&socket_addr).await.unwrap(); let listener = TcpListener::bind(&socket_addr).await?;
let incoming = tokio_stream::wrappers::TcpListenerStream::new(listener); let incoming = tokio_stream::wrappers::TcpListenerStream::new(listener);
let incoming = hyper::server::accept::from_stream(incoming.filter_map(|socket| async { let incoming = hyper::server::accept::from_stream(incoming.filter_map(|socket| async {
match socket { match socket {
@@ -84,9 +91,16 @@ pub async fn serve(args: Args) -> BoxResult<()> {
} }
})); }));
print_listening(args.address.as_str(), args.port, true); print_listening(args.address.as_str(), args.port, true);
server.await?; let graceful = server.with_graceful_shutdown(shutdown_signal());
} else { graceful.await?;
let server = hyper::Server::bind(&socket_addr).serve(make_service_fn(move |_| { Ok(())
}
pub async fn serve_http(args: Args) -> BoxResult<()> {
let args = Arc::new(args);
let socket_addr = args.address()?;
let inner = Arc::new(InnerService::new(args.clone()));
let server = hyper::Server::try_bind(&socket_addr)?.serve(make_service_fn(move |_| {
let inner = inner.clone(); let inner = inner.clone();
async move { async move {
Ok::<_, Infallible>(service_fn(move |req| { Ok::<_, Infallible>(service_fn(move |req| {
@@ -96,9 +110,8 @@ pub async fn serve(args: Args) -> BoxResult<()> {
} }
})); }));
print_listening(args.address.as_str(), args.port, false); print_listening(args.address.as_str(), args.port, false);
server.await?; let graceful = server.with_graceful_shutdown(shutdown_signal());
} graceful.await?;
Ok(()) Ok(())
} }
@@ -260,27 +273,9 @@ impl InnerService {
let query = req.uri().query().unwrap_or_default(); let query = req.uri().query().unwrap_or_default();
if query == "unzip" { if query == "unzip" {
let root = path.parent().unwrap(); if let Err(e) = self.unzip_file(path).await {
let mut zip = ZipFileReader::new(File::open(&path).await?).await?; eprintln!("Failed to unzip {}, {}", path.display(), e);
for i in 0..zip.entries().len() { status!(res, StatusCode::BAD_REQUEST);
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 !self.args.allow_delete && fs::metadata(&entry_path).await.is_ok() {
continue;
}
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?; fs::remove_file(&path).await?;
} }
@@ -519,7 +514,7 @@ impl InnerService {
.unwrap_or_default(), .unwrap_or_default(),
_ => false, _ => false,
}, },
None => self.args.no_auth_read && req.method() == Method::GET, None => self.args.no_auth_access && req.method() == Method::GET,
}, },
} }
}; };
@@ -539,6 +534,32 @@ impl InnerService {
.unwrap_or_default() .unwrap_or_default()
} }
async fn unzip_file(&self, path: &Path) -> BoxResult<()> {
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 !self.args.allow_delete && fs::metadata(&entry_path).await.is_ok() {
continue;
}
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?;
}
}
Ok(())
}
fn extract_path(&self, path: &str) -> Option<PathBuf> { fn extract_path(&self, path: &str) -> Option<PathBuf> {
let decoded_path = percent_decode(path[1..].as_bytes()).decode_utf8().ok()?; let decoded_path = percent_decode(path[1..].as_bytes()).decode_utf8().ok()?;
let slashes_switched = if cfg!(windows) { let slashes_switched = if cfg!(windows) {
@@ -725,6 +746,7 @@ fn print_listening(address: &str, port: u16, tls: bool) {
for addr in addrs { for addr in addrs {
eprintln!(" {}://{}:{}", protocol, addr, port); eprintln!(" {}://{}:{}", protocol, addr, port);
} }
eprintln!();
} }
} }
@@ -742,3 +764,9 @@ fn retrive_listening_addrs(address: &str) -> Vec<String> {
} }
vec![address.to_owned()] vec![address.to_owned()]
} }
async fn shutdown_signal() {
tokio::signal::ctrl_c()
.await
.expect("Failed to install CTRL+C signal handler")
}