2 Commits

3 changed files with 312 additions and 343 deletions

485
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,16 +11,16 @@ actix-http = "2.2"
actix-web = {version = "3.3", features = ["rustls"]}
actix-web-httpauth = "0.5"
chrono = "0.4"
clap = {version = "3.0.0-beta.4", features = ["wrap_help", "color"]}
clap = {version = "3.0.0", features = ["wrap_help", "color", "cargo"]}
env_logger = "0.9"
lazy_static = "1.4"
log = "0.4"
mime_guess = "2"
mime_guess = "2.0"
regex = "1.5"
rustls = "0.18"
serde = "1"
serde = "1.0"
sha2 = "0.9"
tera = "1"
tera = "1.13"
toml = "0.5"
urlencoding = "2.1"

View File

@ -12,6 +12,7 @@ use actix_web::{
dev::{self, Service, ServiceResponse},
http, middleware, App, HttpResponse, HttpServer,
};
use clap::Arg;
use env_logger::fmt::Color;
use log::{error, info};
use sha2::Digest;
@ -342,102 +343,71 @@ async fn validator(
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let matches = clap_app!((crate_name!()) =>
(version: crate_version!())
(author: crate_authors!())
(about: crate_description!())
(@arg noindex: --noindex "Disable automatic index page generation")
(@arg compress: -c --compress "Enable streaming compression (Content-length/segment download will be disabled)")
// (@arg upload: -u --upload "Enable file upload")
(@arg nocache: --nocache "Disable HTTP cache")
(@arg nocolor: --nocolor "Disable cli colors")
(@arg cors: --cors [VALUE] min_values(0) max_values(1) "Enable CORS")
(@arg spa: --spa "Enable Single-Page Application mode (always serve /index.html when the file is not found)")
(@arg dotfiles: -d --dotfiles "Show dotfiles")
(@arg open: -o --open "Open the page in the default browser")
(@arg quiet: -q --quiet "Disable access log output")
(@arg quietall: --quietall "Disable all output")
(@arg ROOT: default_value["."] {
|path| match std::fs::metadata(path) {
Ok(meta) => {
if meta.is_dir() {
Ok(())
} else {
Err("Parameter is not a directory".to_owned())
}
}
Err(e) => Err(e.to_string()),
let check_does_dir_exits = |path: &str| match std::fs::metadata(path) {
Ok(meta) => {
if meta.is_dir() {
Ok(())
} else {
Err("Parameter is not a directory".to_owned())
}
} "Root directory")
(@arg address: -a --address +takes_value default_value["0.0.0.0"] {
|s| match IpAddr::from_str(s) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
Err(e) => Err(e.to_string()),
};
let check_does_file_exits = |path: &str| match std::fs::metadata(path) {
Ok(metadata) => {
if metadata.is_file() {
Ok(())
} else {
Err("Parameter is not a file".to_owned())
}
} "IP address to serve on")
(@arg port: -p --port +takes_value default_value["8000"] {
|s| match s.parse::<u16>() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} "Port to serve on")
(@arg auth: --auth +takes_value {
|s| {
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
if parts.len() < 2 || parts.len() >= 2 && parts[1].is_empty() {
Err("Password not found".to_owned())
} else if parts[0].is_empty() {
Err("Username not found".to_owned())
} else {
Ok(())
}
}
} "HTTP Auth (username:password)")
(@arg cert: --cert +takes_value {
|s| match std::fs::metadata(s) {
Ok(metadata) => {
if metadata.is_file() {
Ok(())
} else {
Err("Parameter is not a file".to_owned())
}
}
Err(e) => Err(e.to_string()),
}
} "Path of TLS/SSL public key (certificate)")
(@arg key: --key +takes_value {
|s| match std::fs::metadata(s) {
Ok(metadata) => {
if metadata.is_file() {
Ok(())
} else {
Err("Parameter is not a file".to_owned())
}
}
Err(e) => Err(e.to_string()),
}
} "Path of TLS/SSL private key")
(@subcommand doc =>
(about: "Open cargo doc via local server (Need cargo installation)")
(@arg nocolor: --nocolor "Disable cli colors")
(@arg noopen: -no --noopen "Do not open the page in the default browser")
(@arg log: --log "Enable access log output [default: false]")
(@arg quietall: --quietall "Disable all output")
(@arg address: -a --address +takes_value default_value["0.0.0.0"] {
|s| match IpAddr::from_str(s) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} "IP address to serve on")
(@arg port: -p --port +takes_value default_value["8000"] {
|s| match s.parse::<u16>() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} "Port to serve on")
}
Err(e) => Err(e.to_string()),
};
let check_is_ip_addr = |s: &str| match IpAddr::from_str(s) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
};
let check_is_port_num = |s: &str| match s.parse::<u16>() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
};
let check_is_auth = |s: &str| {
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
if parts.len() < 2 || parts.len() >= 2 && parts[1].is_empty() {
Err("Password not found".to_owned())
} else if parts[0].is_empty() {
Err("Username not found".to_owned())
} else {
Ok(())
}
};
let matches = app_from_crate!()
.arg(Arg::new("noindex").long("noindex").help("Disable automatic index page generation"))
.arg(Arg::new("compress").short('c').long("compress").help("Enable streaming compression (Content-length/segment download will be disabled)"))
.arg(Arg::new("nocache").long("nocache").help("Disable HTTP cache"))
.arg(Arg::new("nocolor").long("nocolor").help("Disable cli colors"))
.arg(Arg::new("cors").long("cors").takes_value(true).min_values(0).max_values(1).help("Enable CORS [with custom value]"))
.arg(Arg::new("spa").long("spa").help("Enable Single-Page Application mode (always serve /index.html when the file is not found)"))
.arg(Arg::new("dotfiles").short('d').long("dotfiles").help("Show dotfiles"))
.arg(Arg::new("open").short('o').long("open").help("Open the page in the default browser"))
.arg(Arg::new("quiet").short('q').long("quiet").help("Disable access log output"))
.arg(Arg::new("quietall").long("quietall").help("Disable all output"))
.arg(Arg::new("ROOT").default_value(".").validator(check_does_dir_exits).help("Root directory"))
.arg(Arg::new("address").short('a').long("address").default_value("0.0.0.0").takes_value(true).validator(check_is_ip_addr).help("IP address to serve on"))
.arg(Arg::new("port").short('p').long("port").default_value("8000").takes_value(true).validator(check_is_port_num).help("Port to serve on"))
.arg(Arg::new("auth").long("auth").takes_value(true).validator(check_is_auth).help("HTTP Auth (username:password)"))
.arg(Arg::new("cert").long("cert").takes_value(true).validator(check_does_file_exits).help("Path of TLS/SSL public key (certificate)"))
.arg(Arg::new("key").long("key").takes_value(true).validator(check_does_file_exits).help("Path of TLS/SSL private key"))
.subcommand(clap::App::new("doc")
.about("Open cargo doc via local server (Need cargo installation)")
.arg(Arg::new("nocolor").long("nocolor").help("Disable cli colors"))
.arg(Arg::new("noopen").long("noopen").help("Do not open the page in the default browser"))
.arg(Arg::new("log").long("log").help("Enable access log output [default: disabled]"))
.arg(Arg::new("quietall").long("quietall").help("Disable all output"))
.arg(Arg::new("address").short('a').long("address").default_value("0.0.0.0").takes_value(true).validator(check_is_ip_addr).help("IP address to serve on"))
.arg(Arg::new("port").short('p').long("port").default_value("8000").takes_value(true).validator(check_is_port_num).help("Port to serve on"))
)
)
.get_matches();
.get_matches();
set_var(
"ROOT",
@ -608,11 +578,11 @@ async fn main() -> std::io::Result<()> {
.format(|buf, record| {
let data = record.args().to_string();
let mut style = buf.style();
let blue = style.set_color(Color::Rgb(52, 152, 219));
let blue = style.set_color(Color::Cyan);
let mut style = buf.style();
let red = style.set_color(Color::Rgb(231, 76, 60));
let red = style.set_color(Color::Red);
let mut style = buf.style();
let green = style.set_color(Color::Rgb(76, 175, 80));
let green = style.set_color(Color::Green);
if record.target() == "actix_web::middleware::logger" {
let data: Vec<&str> = data.splitn(5, '^').collect();
let time = blue.value(