mirror of
https://github.com/Tim-Paik/srv.git
synced 2024-10-13 00:29:43 +00:00
0.6.5-beta Rewrite based on actix_web
This commit is contained in:
parent
48f3c062cf
commit
45d5d12487
1760
Cargo.lock
generated
1760
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
21
Cargo.toml
21
Cargo.toml
@ -3,13 +3,24 @@ authors = ["Tim_Paik <timpaikc@outlook.com>"]
|
|||||||
description = "simple http server written in rust"
|
description = "simple http server written in rust"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
name = "web"
|
name = "web"
|
||||||
version = "0.2.1-beta"
|
version = "0.6.5-beta"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
actix-files = "0.5"
|
||||||
|
actix-web = {version = "3.3", features = ["openssl"]}
|
||||||
|
actix-web-httpauth = "0.5"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
clap = {version = "3.0.0-beta.2", features = ["wrap_help", "color"]}
|
clap = {version = "3.0.0-beta.2", features = ["wrap_help", "color"]}
|
||||||
colored = "2"
|
env_logger = "0.9"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4"
|
||||||
|
log = "0.4"
|
||||||
mime_guess = "2.0.3"
|
mime_guess = "2.0.3"
|
||||||
rocket = {version = "0.5.0-rc.1", features = ["tls"]}
|
openssl = {version = "0.10"}
|
||||||
rocket_dyn_templates = {version = "0.1.0-rc.1", features = ["tera"]}
|
regex = "1.5"
|
||||||
|
serde = "1"
|
||||||
|
sha2 = "0.9"
|
||||||
|
tera = "1"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level = "z"
|
||||||
|
lto = true
|
768
src/main.rs
768
src/main.rs
@ -1,46 +1,163 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate lazy_static;
|
||||||
|
|
||||||
use colored::*;
|
use actix_files as fs;
|
||||||
use rocket::fairing::{Fairing, Info, Kind};
|
use actix_web::{
|
||||||
use rocket::figment::providers::{Env, Format, Toml};
|
dev::{Service, ServiceResponse},
|
||||||
use rocket::response::Redirect;
|
http, middleware, App, HttpResponse, HttpServer,
|
||||||
use rocket::{config::TlsConfig, fs::NamedFile};
|
};
|
||||||
use rocket_dyn_templates::Template;
|
use env_logger::fmt::Color;
|
||||||
use std::hash::{Hash, Hasher};
|
use log::error;
|
||||||
use std::net::IpAddr;
|
use sha2::Digest;
|
||||||
use std::path::Path;
|
use std::{
|
||||||
use std::str::FromStr;
|
env::{set_var, var},
|
||||||
|
fs::read_dir,
|
||||||
|
io::{Error, ErrorKind, Write},
|
||||||
|
net::IpAddr,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref TEMPLATE: tera::Tera = {
|
||||||
|
let mut tera = tera::Tera::default();
|
||||||
|
tera.add_raw_template("index", include_str!("../templates/index.html.tera.embed"))
|
||||||
|
.unwrap();
|
||||||
|
tera
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn url_decode(data: &str) -> String {
|
fn get_file_type(from: &Path) -> String {
|
||||||
match rocket::http::RawStr::url_decode_lossy(rocket::http::RawStr::new(data)) {
|
match from.extension() {
|
||||||
std::borrow::Cow::Borrowed(data) => data.to_string(),
|
Some(os_str) => match os_str.to_str().unwrap_or("") {
|
||||||
std::borrow::Cow::Owned(data) => data.to_string(),
|
"7z" => "archive",
|
||||||
|
"bz" => "archive",
|
||||||
|
"bz2" => "archive",
|
||||||
|
"cab" => "archive",
|
||||||
|
"gz" => "archive",
|
||||||
|
"iso" => "archive",
|
||||||
|
"rar" => "archive",
|
||||||
|
"xz" => "archive",
|
||||||
|
"zip" => "archive",
|
||||||
|
"zst" => "archive",
|
||||||
|
"zstd" => "archive",
|
||||||
|
"doc" => "word",
|
||||||
|
"docx" => "word",
|
||||||
|
"ppt" => "powerpoint",
|
||||||
|
"pptx" => "powerpoint",
|
||||||
|
"xls" => "excel",
|
||||||
|
"xlsx" => "excel",
|
||||||
|
"heic" => "image",
|
||||||
|
"pdf" => "pdf",
|
||||||
|
// JavaScript / TypeScript
|
||||||
|
"js" => "code",
|
||||||
|
"cjs" => "code",
|
||||||
|
"mjs" => "code",
|
||||||
|
"jsx" => "code",
|
||||||
|
"ts" => "code",
|
||||||
|
"tsx" => "code",
|
||||||
|
"json" => "code",
|
||||||
|
"coffee" => "code",
|
||||||
|
// HTML / CSS
|
||||||
|
"html" => "code",
|
||||||
|
"htm" => "code",
|
||||||
|
"xml" => "code",
|
||||||
|
"xhtml" => "code",
|
||||||
|
"vue" => "code",
|
||||||
|
"ejs" => "code",
|
||||||
|
"template" => "code",
|
||||||
|
"tmpl" => "code",
|
||||||
|
"pug" => "code",
|
||||||
|
"art" => "code",
|
||||||
|
"hbs" => "code",
|
||||||
|
"tera" => "code",
|
||||||
|
"css" => "code",
|
||||||
|
"scss" => "code",
|
||||||
|
"sass" => "code",
|
||||||
|
"less" => "code",
|
||||||
|
// Python
|
||||||
|
"py" => "code",
|
||||||
|
"pyc" => "code",
|
||||||
|
// JVM
|
||||||
|
"java" => "code",
|
||||||
|
"kt" => "code",
|
||||||
|
"kts" => "code",
|
||||||
|
"gradle" => "code",
|
||||||
|
"groovy" => "code",
|
||||||
|
"scala" => "code",
|
||||||
|
"jsp" => "code",
|
||||||
|
// Shell
|
||||||
|
"sh" => "code",
|
||||||
|
// Php
|
||||||
|
"php" => "code",
|
||||||
|
// C / C++
|
||||||
|
"c" => "code",
|
||||||
|
"cc" => "code",
|
||||||
|
"cpp" => "code",
|
||||||
|
"h" => "code",
|
||||||
|
"cmake" => "code",
|
||||||
|
// C#
|
||||||
|
"cs" => "code",
|
||||||
|
"xaml" => "code",
|
||||||
|
"sln" => "code",
|
||||||
|
"csproj" => "code",
|
||||||
|
// Golang
|
||||||
|
"go" => "code",
|
||||||
|
"mod" => "code",
|
||||||
|
"sum" => "code",
|
||||||
|
// Swift
|
||||||
|
"swift" => "code",
|
||||||
|
"plist" => "code",
|
||||||
|
"xib" => "code",
|
||||||
|
"xcconfig" => "code",
|
||||||
|
"entitlements" => "code",
|
||||||
|
"xcworkspacedata" => "code",
|
||||||
|
"pbxproj" => "code",
|
||||||
|
// Ruby
|
||||||
|
"rb" => "code",
|
||||||
|
// Rust
|
||||||
|
"rs" => "code",
|
||||||
|
// Objective-C
|
||||||
|
"m" => "code",
|
||||||
|
// Dart
|
||||||
|
"dart" => "code",
|
||||||
|
// Microsoft
|
||||||
|
"manifest" => "code",
|
||||||
|
"rc" => "code",
|
||||||
|
"cmd" => "code",
|
||||||
|
"bat" => "code",
|
||||||
|
"ps1" => "code",
|
||||||
|
// Config
|
||||||
|
"ini" => "code",
|
||||||
|
"yaml" => "code",
|
||||||
|
"toml" => "code",
|
||||||
|
"conf" => "code",
|
||||||
|
"properties" => "code",
|
||||||
|
"lock" => "alt",
|
||||||
|
_ => match mime_guess::from_path(from).first_or_octet_stream().type_() {
|
||||||
|
mime_guess::mime::AUDIO => "audio",
|
||||||
|
mime_guess::mime::IMAGE => "image",
|
||||||
|
mime_guess::mime::PDF => "pdf",
|
||||||
|
mime_guess::mime::VIDEO => "video",
|
||||||
|
mime_guess::mime::TEXT => "alt",
|
||||||
|
_ => "file",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
None => "file",
|
||||||
}
|
}
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<path..>")]
|
#[derive(Eq, Ord, PartialEq, PartialOrd, serde::Serialize)]
|
||||||
async fn file_server(path: std::path::PathBuf) -> Option<NamedFile> {
|
|
||||||
let mut path = Path::new(&std::env::var("ROOT").unwrap_or(".".to_string()))
|
|
||||||
.join(url_decode(path.to_str().unwrap_or("")));
|
|
||||||
if path.is_dir() {
|
|
||||||
path.push("index.html")
|
|
||||||
}
|
|
||||||
NamedFile::open(path).await.ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Eq, Ord, PartialEq, PartialOrd, rocket::serde::Serialize)]
|
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
struct Dir {
|
struct Dir {
|
||||||
name: String,
|
name: String,
|
||||||
modified: String,
|
modified: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, Ord, PartialEq, PartialOrd, rocket::serde::Serialize)]
|
#[derive(Eq, Ord, PartialEq, PartialOrd, serde::Serialize)]
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
struct File {
|
struct File {
|
||||||
name: String,
|
name: String,
|
||||||
size: u64,
|
size: u64,
|
||||||
@ -48,8 +165,7 @@ struct File {
|
|||||||
modified: String,
|
modified: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(rocket::serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
#[serde(crate = "rocket::serde")]
|
|
||||||
struct IndexContext<'r> {
|
struct IndexContext<'r> {
|
||||||
title: &'r str,
|
title: &'r str,
|
||||||
paths: Vec<&'r str>,
|
paths: Vec<&'r str>,
|
||||||
@ -57,70 +173,58 @@ struct IndexContext<'r> {
|
|||||||
files: Vec<File>,
|
files: Vec<File>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Responder)]
|
fn render_index(
|
||||||
enum Resp {
|
dir: &actix_files::Directory,
|
||||||
#[response(status = 200, content_type = "text/html; charset=utf-8")]
|
req: &actix_web::HttpRequest,
|
||||||
Index(Template),
|
) -> Result<ServiceResponse, std::io::Error> {
|
||||||
#[response(status = 404)]
|
let mut index = dir.path.clone();
|
||||||
NotFound(&'static str),
|
index.push("index.html");
|
||||||
#[response(status = 200)]
|
if index.exists() && index.is_file() {
|
||||||
File(Option<NamedFile>),
|
let res = match actix_files::NamedFile::open(index)?
|
||||||
#[response(status = 302)]
|
.set_content_type(mime_guess::mime::TEXT_HTML_UTF_8)
|
||||||
Redirect(Redirect),
|
.into_response(req)
|
||||||
}
|
{
|
||||||
|
Ok(res) => res,
|
||||||
#[catch(404)]
|
Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())),
|
||||||
async fn not_found(request: &rocket::Request<'_>) -> Resp {
|
};
|
||||||
let path = url_decode(request.uri().path().as_str());
|
return Ok(ServiceResponse::new(req.clone(), res));
|
||||||
let root = std::env::var("ROOT").unwrap_or(".".to_string());
|
|
||||||
let root = Path::new(&root);
|
|
||||||
let localpath = path[1..path.len()].to_string();
|
|
||||||
// Remove the / in front of the path, if the path with / is spliced, the previous path will be ignored
|
|
||||||
let localpath = &root.join(localpath);
|
|
||||||
// Show dotfiles, std::path::PathBuf does not match the url beginning with the dot
|
|
||||||
let show_dot_files = std::env::var("DOTFILES").unwrap_or("false".to_string()) == "true";
|
|
||||||
if localpath.is_file() && show_dot_files {
|
|
||||||
return Resp::File(NamedFile::open(localpath).await.ok());
|
|
||||||
}
|
}
|
||||||
// Single-Page Application support
|
if var("NOINDEX").unwrap_or("false".to_string()) == "true" {
|
||||||
if root.join("index.html").is_file()
|
return Ok(ServiceResponse::new(
|
||||||
&& std::env::var("SPA").unwrap_or("false".to_string()) == "true"
|
req.clone(),
|
||||||
{
|
HttpResponse::NotFound().body(""),
|
||||||
return Resp::File(NamedFile::open(&root.join("index.html")).await.ok());
|
));
|
||||||
}
|
|
||||||
if !localpath.is_dir() || std::env::var("NOINDEX").unwrap_or("false".to_string()) == "true" {
|
|
||||||
return Resp::NotFound("");
|
|
||||||
}
|
|
||||||
if !path.ends_with("/") {
|
|
||||||
return Resp::Redirect(Redirect::to(path.to_string() + "/"));
|
|
||||||
}
|
}
|
||||||
|
let show_dot_files = var("DOTFILES").unwrap_or("false".to_string()) == "true";
|
||||||
let mut context = IndexContext {
|
let mut context = IndexContext {
|
||||||
title: "",
|
title: "",
|
||||||
paths: vec![],
|
paths: vec![],
|
||||||
dirs: vec![],
|
dirs: vec![],
|
||||||
files: vec![],
|
files: vec![],
|
||||||
};
|
};
|
||||||
for path in path.split('/') {
|
for path in req.path().split('/') {
|
||||||
if path == "" {
|
if path == "" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
context.paths.push(path);
|
context.paths.push(path);
|
||||||
}
|
}
|
||||||
match std::fs::read_dir(localpath) {
|
match read_dir(&dir.path) {
|
||||||
Err(e) => println!("{} {}", "Error".bright_red(), e.to_string()),
|
Err(e) => {
|
||||||
|
error!(target: "read_dir", "[ERROR] Read dir error: {}", e.to_string());
|
||||||
|
}
|
||||||
Ok(paths) => {
|
Ok(paths) => {
|
||||||
for path in paths {
|
for path in paths {
|
||||||
let path = match path {
|
let path = match path {
|
||||||
Ok(a) => a,
|
Ok(path) => path,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{} {}", "Error".bright_red(), e.to_string());
|
error!(target: "read_dir", "[ERROR] Read path error: {}", e.to_string());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let name = match path.file_name().to_str() {
|
let name = match path.file_name().to_str() {
|
||||||
Some(str) => str.to_string(),
|
Some(str) => str.to_string(),
|
||||||
None => {
|
None => {
|
||||||
println!("{} {}", "Error".bright_red(), "Read filename error");
|
error!(target: "read_dir", "[ERROR] Read filename error");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -130,16 +234,16 @@ async fn not_found(request: &rocket::Request<'_>) -> Resp {
|
|||||||
let metadata = match path.metadata() {
|
let metadata = match path.metadata() {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{} {}", "Error".bright_red(), e.to_string());
|
error!(target: "read_dir", "[ERROR] Read metadata error: {}", e.to_string());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let modified = match metadata.modified() {
|
let modified = match metadata.modified() {
|
||||||
Ok(time) => chrono::DateTime::<chrono::Local>::from(time)
|
Ok(time) => chrono::DateTime::<chrono::Local>::from(time)
|
||||||
.format("%Y-%m-%d %H:%M:%S")
|
.format("%Y/%m/%d %H:%M:%S")
|
||||||
.to_string(),
|
.to_string(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{} {}", "Error".bright_red(), e.to_string());
|
error!(target: "read_dir", "[ERROR] Read modified time error: {}", e.to_string());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -147,129 +251,7 @@ async fn not_found(request: &rocket::Request<'_>) -> Resp {
|
|||||||
context.dirs.push(Dir { name, modified });
|
context.dirs.push(Dir { name, modified });
|
||||||
} else if metadata.is_file() {
|
} else if metadata.is_file() {
|
||||||
let size = metadata.len();
|
let size = metadata.len();
|
||||||
let filetype = match path.path().extension() {
|
let filetype = get_file_type(&path.path());
|
||||||
Some(os_str) => match os_str.to_str().unwrap_or("") {
|
|
||||||
"7z" => "archive",
|
|
||||||
"bz" => "archive",
|
|
||||||
"bz2" => "archive",
|
|
||||||
"cab" => "archive",
|
|
||||||
"gz" => "archive",
|
|
||||||
"iso" => "archive",
|
|
||||||
"rar" => "archive",
|
|
||||||
"xz" => "archive",
|
|
||||||
"zip" => "archive",
|
|
||||||
"zst" => "archive",
|
|
||||||
"zstd" => "archive",
|
|
||||||
"doc" => "word",
|
|
||||||
"docx" => "word",
|
|
||||||
"ppt" => "powerpoint",
|
|
||||||
"pptx" => "powerpoint",
|
|
||||||
"xls" => "excel",
|
|
||||||
"xlsx" => "excel",
|
|
||||||
"heic" => "image",
|
|
||||||
"pdf" => "pdf",
|
|
||||||
// JavaScript / TypeScript
|
|
||||||
"js" => "code",
|
|
||||||
"cjs" => "code",
|
|
||||||
"mjs" => "code",
|
|
||||||
"jsx" => "code",
|
|
||||||
"ts" => "code",
|
|
||||||
"tsx" => "code",
|
|
||||||
"json" => "code",
|
|
||||||
"coffee" => "code",
|
|
||||||
// HTML / CSS
|
|
||||||
"html" => "code",
|
|
||||||
"htm" => "code",
|
|
||||||
"xml" => "code",
|
|
||||||
"xhtml" => "code",
|
|
||||||
"vue" => "code",
|
|
||||||
"ejs" => "code",
|
|
||||||
"template" => "code",
|
|
||||||
"tmpl" => "code",
|
|
||||||
"pug" => "code",
|
|
||||||
"art" => "code",
|
|
||||||
"hbs" => "code",
|
|
||||||
"tera" => "code",
|
|
||||||
"css" => "code",
|
|
||||||
"scss" => "code",
|
|
||||||
"sass" => "code",
|
|
||||||
"less" => "code",
|
|
||||||
// Python
|
|
||||||
"py" => "code",
|
|
||||||
"pyc" => "code",
|
|
||||||
// JVM
|
|
||||||
"java" => "code",
|
|
||||||
"kt" => "code",
|
|
||||||
"kts" => "code",
|
|
||||||
"gradle" => "code",
|
|
||||||
"groovy" => "code",
|
|
||||||
"scala" => "code",
|
|
||||||
"jsp" => "code",
|
|
||||||
// Shell
|
|
||||||
"sh" => "code",
|
|
||||||
// Php
|
|
||||||
"php" => "code",
|
|
||||||
// C / C++
|
|
||||||
"c" => "code",
|
|
||||||
"cc" => "code",
|
|
||||||
"cpp" => "code",
|
|
||||||
"h" => "code",
|
|
||||||
"cmake" => "code",
|
|
||||||
// C#
|
|
||||||
"cs" => "code",
|
|
||||||
"xaml" => "code",
|
|
||||||
"sln" => "code",
|
|
||||||
"csproj" => "code",
|
|
||||||
// Golang
|
|
||||||
"go" => "code",
|
|
||||||
"mod" => "code",
|
|
||||||
"sum" => "code",
|
|
||||||
// Swift
|
|
||||||
"swift" => "code",
|
|
||||||
"plist" => "code",
|
|
||||||
"xib" => "code",
|
|
||||||
"xcconfig" => "code",
|
|
||||||
"entitlements" => "code",
|
|
||||||
"xcworkspacedata" => "code",
|
|
||||||
"pbxproj" => "code",
|
|
||||||
// Ruby
|
|
||||||
"rb" => "code",
|
|
||||||
// Rust
|
|
||||||
"rs" => "code",
|
|
||||||
// Objective-C
|
|
||||||
"m" => "code",
|
|
||||||
// Dart
|
|
||||||
"dart" => "code",
|
|
||||||
// Microsoft
|
|
||||||
"manifest" => "code",
|
|
||||||
"rc" => "code",
|
|
||||||
"cmd" => "code",
|
|
||||||
"bat" => "code",
|
|
||||||
"ps1" => "code",
|
|
||||||
// Config
|
|
||||||
"ini" => "code",
|
|
||||||
"yaml" => "code",
|
|
||||||
"toml" => "code",
|
|
||||||
"conf" => "code",
|
|
||||||
"properties" => "code",
|
|
||||||
"lock" => "alt",
|
|
||||||
_ => {
|
|
||||||
match mime_guess::from_path(path.path())
|
|
||||||
.first_or_octet_stream()
|
|
||||||
.type_()
|
|
||||||
{
|
|
||||||
mime_guess::mime::AUDIO => "audio",
|
|
||||||
mime_guess::mime::IMAGE => "image",
|
|
||||||
mime_guess::mime::PDF => "pdf",
|
|
||||||
mime_guess::mime::VIDEO => "video",
|
|
||||||
mime_guess::mime::TEXT => "alt",
|
|
||||||
_ => "file",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => "file",
|
|
||||||
}
|
|
||||||
.to_string();
|
|
||||||
context.files.push(File {
|
context.files.push(File {
|
||||||
name,
|
name,
|
||||||
size,
|
size,
|
||||||
@ -283,93 +265,25 @@ async fn not_found(request: &rocket::Request<'_>) -> Resp {
|
|||||||
context.title = context.paths.last().unwrap_or(&"/");
|
context.title = context.paths.last().unwrap_or(&"/");
|
||||||
context.dirs.sort();
|
context.dirs.sort();
|
||||||
context.files.sort();
|
context.files.sort();
|
||||||
Resp::Index(Template::render("index", &context))
|
let content = tera::Context::from_serialize(&context);
|
||||||
}
|
let content = match content {
|
||||||
|
Ok(ctx) => ctx,
|
||||||
#[catch(500)]
|
Err(e) => {
|
||||||
fn internal_server_error() {}
|
error!(target: "tera::Context::from_serialize", "[ERROR] Read modified time error: {}", e.to_string());
|
||||||
|
return Err(Error::new(ErrorKind::Other, e.to_string()));
|
||||||
struct Logger {}
|
|
||||||
|
|
||||||
#[rocket::async_trait]
|
|
||||||
impl Fairing for Logger {
|
|
||||||
fn info(&self) -> Info {
|
|
||||||
Info {
|
|
||||||
name: "Logger",
|
|
||||||
kind: Kind::Liftoff | Kind::Response,
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
async fn on_liftoff(&self, rocket: &rocket::Rocket<rocket::Orbit>) {
|
let index = TEMPLATE
|
||||||
println!(
|
.render("index", &content)
|
||||||
"{}",
|
.unwrap_or("TEMPLATE RENDER ERROR".to_string());
|
||||||
format!(
|
let res = HttpResponse::Ok()
|
||||||
"Serving {} on {}{}:{}",
|
.content_type("text/html; charset=utf-8")
|
||||||
std::env::var("ROOT").unwrap_or("[Get Path Error]".to_string()),
|
.body(index);
|
||||||
if rocket.config().tls_enabled() {
|
Ok(ServiceResponse::new(req.clone(), res))
|
||||||
"https://"
|
|
||||||
} else {
|
|
||||||
"http://"
|
|
||||||
},
|
|
||||||
rocket.config().address.to_string(),
|
|
||||||
rocket.config().port.to_string()
|
|
||||||
)
|
|
||||||
.bright_green()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
async fn on_response<'r>(
|
|
||||||
&self,
|
|
||||||
request: &'r rocket::Request<'_>,
|
|
||||||
response: &mut rocket::Response<'r>,
|
|
||||||
) {
|
|
||||||
println!(
|
|
||||||
"[{}] {} | {} | {} {}",
|
|
||||||
chrono::Local::now()
|
|
||||||
.format("%Y/%m/%d %H:%M:%S")
|
|
||||||
.to_string()
|
|
||||||
.bright_blue(),
|
|
||||||
request
|
|
||||||
.client_ip()
|
|
||||||
.unwrap_or(IpAddr::from([0, 0, 0, 0]))
|
|
||||||
.to_string()
|
|
||||||
.bright_blue(),
|
|
||||||
if response.status().code < 400 {
|
|
||||||
response.status().code.to_string().bright_green()
|
|
||||||
} else {
|
|
||||||
response.status().code.to_string().bright_red()
|
|
||||||
},
|
|
||||||
request.method().to_string().bright_blue(),
|
|
||||||
request.uri().to_string().bright_blue()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CORS {}
|
|
||||||
|
|
||||||
#[rocket::async_trait]
|
|
||||||
|
|
||||||
impl Fairing for CORS {
|
|
||||||
fn info(&self) -> Info {
|
|
||||||
Info {
|
|
||||||
name: "CORS",
|
|
||||||
kind: Kind::Response,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async fn on_response<'r>(
|
|
||||||
&self,
|
|
||||||
_request: &'r rocket::Request<'_>,
|
|
||||||
response: &mut rocket::Response<'r>,
|
|
||||||
) {
|
|
||||||
if std::env::var("ENABLE_CORS").unwrap_or("false".to_string()) == "true" {
|
|
||||||
response.adjoin_header(rocket::http::Header::new(
|
|
||||||
"Access-Control-Allow-Origin",
|
|
||||||
std::env::var("CORS").unwrap_or("*".to_string()),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn display_path(path: &std::path::Path) -> String {
|
fn display_path(path: &Path) -> String {
|
||||||
let root = Path::canonicalize(path).unwrap().display().to_string();
|
let root = Path::canonicalize(path).unwrap().display().to_string();
|
||||||
if root.starts_with("\\\\?\\") {
|
if root.starts_with("\\\\?\\") {
|
||||||
root[4..root.len()].to_string()
|
root[4..root.len()].to_string()
|
||||||
@ -378,19 +292,46 @@ fn display_path(path: &std::path::Path) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::main]
|
#[inline]
|
||||||
async fn main() {
|
fn hash(from: &str) -> String {
|
||||||
|
let mut hasher = sha2::Sha512::new();
|
||||||
|
hasher.update(from);
|
||||||
|
format!("{:?}", hasher.finalize())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
async fn validator(
|
||||||
|
req: actix_web::dev::ServiceRequest,
|
||||||
|
auth: actix_web_httpauth::extractors::basic::BasicAuth,
|
||||||
|
) -> Result<actix_web::dev::ServiceRequest, actix_web::Error> {
|
||||||
|
if auth.user_id() == var("AUTH_USERNAME").unwrap_or("".to_string()).as_str()
|
||||||
|
&& hash(auth.password().unwrap_or(&std::borrow::Cow::from("")))
|
||||||
|
== var("AUTH_PASSWORD").unwrap_or("".to_string()).as_str()
|
||||||
|
{
|
||||||
|
return Ok(req);
|
||||||
|
}
|
||||||
|
let err = actix_web_httpauth::extractors::AuthenticationError::new(
|
||||||
|
actix_web_httpauth::headers::www_authenticate::basic::Basic::with_realm(
|
||||||
|
"Incorrect username or password",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Err(actix_web::Error::from(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
let matches = clap_app!((crate_name!()) =>
|
let matches = clap_app!((crate_name!()) =>
|
||||||
(version: crate_version!())
|
(version: crate_version!())
|
||||||
(author: crate_authors!())
|
(author: crate_authors!())
|
||||||
(about: crate_description!())
|
(about: crate_description!())
|
||||||
(@arg noindex: --noindex "Disable automatic index page generation")
|
(@arg noindex: --noindex "Disable automatic index page generation")
|
||||||
(@arg upload: -u --upload "Enable file upload")
|
(@arg compress: -c --compress "Enable streaming compression (Content-length/segment download will be disabled)")
|
||||||
// (@arg nocache: --nocache "Disable HTTP cache") // Not support now
|
// (@arg upload: -u --upload "Enable file upload")
|
||||||
|
(@arg nocache: --nocache "Disable HTTP cache")
|
||||||
(@arg nocolor: --nocolor "Disable cli colors")
|
(@arg nocolor: --nocolor "Disable cli colors")
|
||||||
(@arg cors: --cors [VALUE] min_values(0) max_values(1) "Enable CORS")
|
(@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 spa: --spa "Enable Single-Page Application mode (always serve /index.html when the file is not found)")
|
||||||
(@arg dotfiles: --dotfiles "Show dotfiles")
|
(@arg dotfiles: -d --dotfiles "Show dotfiles")
|
||||||
(@arg open: -o --open "Open the page in the default browser")
|
(@arg open: -o --open "Open the page in the default browser")
|
||||||
(@arg ROOT: default_value["."] {
|
(@arg ROOT: default_value["."] {
|
||||||
|path| match std::fs::metadata(path) {
|
|path| match std::fs::metadata(path) {
|
||||||
@ -455,90 +396,69 @@ async fn main() {
|
|||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
std::env::set_var(
|
set_var(
|
||||||
"ROOT",
|
"ROOT",
|
||||||
display_path(Path::new(matches.value_of("ROOT").unwrap_or("."))),
|
display_path(Path::new(matches.value_of("ROOT").unwrap_or("."))),
|
||||||
);
|
);
|
||||||
|
|
||||||
std::env::set_var("NOINDEX", matches.is_present("noindex").to_string());
|
set_var("NOINDEX", matches.is_present("noindex").to_string());
|
||||||
std::env::set_var("SPA", matches.is_present("spa").to_string());
|
set_var("SPA", matches.is_present("spa").to_string());
|
||||||
std::env::set_var("DOTFILES", matches.is_present("dotfiles").to_string());
|
set_var("DOTFILES", matches.is_present("dotfiles").to_string());
|
||||||
|
set_var("NOCACHE", matches.is_present("nocache").to_string());
|
||||||
|
set_var("COMPRESS", matches.is_present("compress").to_string());
|
||||||
|
|
||||||
if matches.is_present("nocolor") {
|
if matches.is_present("nocolor") {
|
||||||
colored::control::set_override(false);
|
set_var("RUST_LOG_STYLE", "never");
|
||||||
}
|
}
|
||||||
|
|
||||||
match matches.value_of("auth") {
|
match matches.value_of("auth") {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
|
set_var("ENABLE_AUTH", matches.is_present("auth").to_string());
|
||||||
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
|
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
|
||||||
let mut hash = std::collections::hash_map::DefaultHasher::new();
|
set_var("AUTH_USERNAME", parts[0]);
|
||||||
parts[1].hash(&mut hash);
|
set_var("AUTH_PASSWORD", hash(parts[1]));
|
||||||
std::env::set_var("USERNAME", parts[0]);
|
|
||||||
std::env::set_var("PASSWORD", hash.finish().to_string());
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches.is_present("cors") {
|
if matches.is_present("cors") {
|
||||||
std::env::set_var("ENABLE_CORS", "true");
|
set_var("ENABLE_CORS", matches.is_present("cors").to_string());
|
||||||
match matches.value_of("cors") {
|
match matches.value_of("cors") {
|
||||||
Some(str) => {
|
Some(str) => {
|
||||||
std::env::set_var("CORS", str.to_string());
|
set_var("CORS", str.to_string());
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
std::env::set_var("CORS", "*");
|
set_var("CORS", "*");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let figment = rocket::Config::figment()
|
|
||||||
.merge((
|
|
||||||
"address",
|
|
||||||
IpAddr::from_str(matches.value_of("address").unwrap_or("0.0.0.0")).unwrap(),
|
|
||||||
))
|
|
||||||
.merge((
|
|
||||||
"port",
|
|
||||||
matches
|
|
||||||
.value_of("port")
|
|
||||||
.unwrap_or("8000")
|
|
||||||
.parse::<u16>()
|
|
||||||
.unwrap(),
|
|
||||||
))
|
|
||||||
.merge((
|
|
||||||
"ident",
|
|
||||||
std::env::var("WEB_SERVER_NAME").unwrap_or("timpaik'web server".to_string()),
|
|
||||||
))
|
|
||||||
.merge(("cli_colors", matches.is_present("color")))
|
|
||||||
.merge(("log_level", "off"))
|
|
||||||
.merge(("template_dir", "."))
|
|
||||||
// The default is "templates/", an error will be reported if the folder is not found
|
|
||||||
.merge(Toml::file(Env::var_or("WEB_CONFIG", "web.toml")).nested())
|
|
||||||
.merge(Env::prefixed("WEB_").ignore(&["PROFILE"]).global());
|
|
||||||
|
|
||||||
let enable_tls = matches.is_present("cert") && matches.is_present("key");
|
let enable_tls = matches.is_present("cert") && matches.is_present("key");
|
||||||
|
let ip = matches
|
||||||
let figment = if enable_tls {
|
.value_of("address")
|
||||||
let cert = Path::new(matches.value_of("cert").unwrap());
|
.unwrap_or("127.0.0.1")
|
||||||
let key = Path::new(matches.value_of("key").unwrap());
|
.to_string();
|
||||||
figment.merge(("tls", TlsConfig::from_paths(cert, key)))
|
let addr = format!(
|
||||||
} else {
|
"{}:{}",
|
||||||
figment
|
ip,
|
||||||
};
|
matches.value_of("port").unwrap_or("8000").to_string()
|
||||||
|
);
|
||||||
|
let url = format!(
|
||||||
|
"{}{}:{}",
|
||||||
|
if enable_tls {
|
||||||
|
"https://".to_string()
|
||||||
|
} else {
|
||||||
|
"http://".to_string()
|
||||||
|
},
|
||||||
|
if ip == "0.0.0.0" {
|
||||||
|
"127.0.0.1"
|
||||||
|
} else {
|
||||||
|
ip.as_str()
|
||||||
|
},
|
||||||
|
matches.value_of("port").unwrap_or("8000").to_string()
|
||||||
|
);
|
||||||
|
|
||||||
if matches.is_present("open") {
|
if matches.is_present("open") {
|
||||||
let url = format!(
|
|
||||||
"{}{}:{}",
|
|
||||||
if enable_tls {
|
|
||||||
"https://".to_string()
|
|
||||||
} else {
|
|
||||||
"http://".to_string()
|
|
||||||
},
|
|
||||||
matches
|
|
||||||
.value_of("address")
|
|
||||||
.unwrap_or("127.0.0.1")
|
|
||||||
.to_string(),
|
|
||||||
matches.value_of("port").unwrap_or("8000").to_string()
|
|
||||||
);
|
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
std::process::Command::new("explorer").arg(url).spawn().ok();
|
std::process::Command::new("explorer").arg(url).spawn().ok();
|
||||||
} else if cfg!(target_os = "macos") {
|
} else if cfg!(target_os = "macos") {
|
||||||
@ -548,23 +468,151 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match rocket::custom(figment)
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||||
.attach(Template::custom(|engines| {
|
.format(|buf, record| {
|
||||||
engines
|
let data = record.args().to_string();
|
||||||
.tera
|
let mut style = buf.style();
|
||||||
.add_raw_template("index", include_str!("../templates/index.html.tera"))
|
let blue = style.set_color(Color::Rgb(52, 152, 219));
|
||||||
|
let mut style = buf.style();
|
||||||
|
let red = style.set_color(Color::Rgb(231, 76, 60));
|
||||||
|
let mut style = buf.style();
|
||||||
|
let green = style.set_color(Color::Rgb(76, 175, 80));
|
||||||
|
if record.target() == "actix_web::middleware::logger" {
|
||||||
|
let data: Vec<&str> = data.splitn(5, "^").collect();
|
||||||
|
let time = blue.value(
|
||||||
|
chrono::NaiveDateTime::from_str(data[0])
|
||||||
|
.unwrap()
|
||||||
|
.format("%Y/%m/%d %H:%M:%S")
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
let ipaddr = blue.value(data[1]);
|
||||||
|
let status_code = data[2].parse().unwrap_or(500);
|
||||||
|
let status_code = if status_code < 400 {
|
||||||
|
green.value(status_code)
|
||||||
|
} else {
|
||||||
|
red.value(status_code)
|
||||||
|
};
|
||||||
|
let process_time: Vec<&str> = data[3].splitn(2, ".").collect();
|
||||||
|
let process_time = process_time[0].to_string() + "ms";
|
||||||
|
let process_time = blue.value(if process_time.len() == 3 {
|
||||||
|
" ".to_string() + process_time.as_str()
|
||||||
|
} else if process_time.len() == 4 {
|
||||||
|
" ".to_string() + process_time.as_str()
|
||||||
|
} else {
|
||||||
|
process_time
|
||||||
|
});
|
||||||
|
let content = blue.value(data[4]);
|
||||||
|
return writeln!(
|
||||||
|
buf,
|
||||||
|
"\r[{}] {} | {} | {} | {}",
|
||||||
|
time, ipaddr, status_code, process_time, content
|
||||||
|
);
|
||||||
|
// Add '\r' to remove the input ^C
|
||||||
|
} else if record.target() == "actix_server::builder" {
|
||||||
|
if data.starts_with("SIGINT received, exiting") {
|
||||||
|
return writeln!(buf, "\r{}", green.value("[INFO] SIGINT received, exiting"));
|
||||||
|
} else {
|
||||||
|
let data = data.replace("actix-web-service-", "");
|
||||||
|
let re1 = regex::Regex::new("Starting (.*) workers").unwrap();
|
||||||
|
if re1.is_match(&data) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let re2 = regex::Regex::new("Starting \"(.*)\" service on (.*)").unwrap();
|
||||||
|
if re2.is_match(&data) {
|
||||||
|
let addr = re2
|
||||||
|
.captures(&data)
|
||||||
|
.unwrap()
|
||||||
|
.get(1)
|
||||||
|
.map_or("", |m| m.as_str());
|
||||||
|
let data = format!(
|
||||||
|
"[INFO] Serving {} on {}",
|
||||||
|
var("ROOT").unwrap_or(".".to_string()).as_str(),
|
||||||
|
addr
|
||||||
|
);
|
||||||
|
return writeln!(buf, "\r{}", green.value(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if data.starts_with("[ERROR]") {
|
||||||
|
writeln!(buf, "\r{}", red.value(data))
|
||||||
|
} else {
|
||||||
|
writeln!(buf, "\r{}", green.value(data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let server = HttpServer::new(move || {
|
||||||
|
let compress = if var("COMPRESS").unwrap_or("false".to_string()) == "true" {
|
||||||
|
http::header::ContentEncoding::Auto
|
||||||
|
} else {
|
||||||
|
http::header::ContentEncoding::Identity
|
||||||
|
};
|
||||||
|
let app = App::new()
|
||||||
|
.wrap(middleware::Compress::new(compress))
|
||||||
|
.wrap(middleware::Condition::new(
|
||||||
|
true,
|
||||||
|
middleware::NormalizePath::default(),
|
||||||
|
))
|
||||||
|
.wrap(middleware::Condition::new(
|
||||||
|
var("ENABLE_AUTH").unwrap_or("false".to_string()) == "true",
|
||||||
|
actix_web_httpauth::middleware::HttpAuthentication::basic(validator),
|
||||||
|
))
|
||||||
|
.wrap_fn(|req, srv| {
|
||||||
|
let paths = PathBuf::from_str(req.path()).unwrap_or(PathBuf::default());
|
||||||
|
let mut isdotfile = false;
|
||||||
|
for path in paths.iter() {
|
||||||
|
if path.to_string_lossy().starts_with('.') {
|
||||||
|
isdotfile = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let fut = srv.call(req);
|
||||||
|
async move {
|
||||||
|
let res = fut.await?.map_body(|head, mut body| {
|
||||||
|
if var("NOCACHE").unwrap_or("false".to_string()) == "true" {
|
||||||
|
head.headers_mut().insert(
|
||||||
|
http::header::CACHE_CONTROL,
|
||||||
|
http::HeaderValue::from_static("no-store"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if var("ENABLE_CORS").unwrap_or("false".to_string()) == "true" {
|
||||||
|
let cors = var("CORS").unwrap_or("*".to_string());
|
||||||
|
let cors = http::HeaderValue::from_str(cors.as_str())
|
||||||
|
.unwrap_or(http::HeaderValue::from_static("*"));
|
||||||
|
head.headers_mut()
|
||||||
|
.insert(http::header::ACCESS_CONTROL_ALLOW_ORIGIN, cors);
|
||||||
|
}
|
||||||
|
if isdotfile && !(var("DOTFILES").unwrap_or("false".to_string()) == "true")
|
||||||
|
{
|
||||||
|
head.status = http::StatusCode::FORBIDDEN;
|
||||||
|
*head.headers_mut() = http::HeaderMap::new();
|
||||||
|
let _ = body.take_body();
|
||||||
|
}
|
||||||
|
body
|
||||||
|
});
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.wrap(middleware::Logger::new("%t^%a^%s^%D^%r"));
|
||||||
|
let files = fs::Files::new("/", var("ROOT").unwrap_or(".".to_string()))
|
||||||
|
.use_hidden_files()
|
||||||
|
.prefer_utf8(true)
|
||||||
|
.show_files_listing()
|
||||||
|
.files_listing_renderer(render_index);
|
||||||
|
return app.service(files);
|
||||||
|
});
|
||||||
|
let server = if enable_tls {
|
||||||
|
let cert = Path::new(matches.value_of("cert").unwrap());
|
||||||
|
let key = Path::new(matches.value_of("key").unwrap());
|
||||||
|
let mut builder =
|
||||||
|
openssl::ssl::SslAcceptor::mozilla_intermediate(openssl::ssl::SslMethod::tls())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}))
|
builder
|
||||||
.attach(CORS {})
|
.set_private_key_file(key, openssl::ssl::SslFiletype::PEM)
|
||||||
.attach(Logger {})
|
.unwrap();
|
||||||
.mount("/", routes![file_server])
|
builder.set_certificate_chain_file(cert).unwrap();
|
||||||
.register("/", catchers![not_found, internal_server_error])
|
server.bind_openssl(addr, builder)
|
||||||
.launch()
|
} else {
|
||||||
.await
|
server.bind(addr)
|
||||||
{
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
println!("{}", format!("[Error] {}", e.to_string()).bright_red());
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
server?.run().await
|
||||||
}
|
}
|
||||||
|
1
templates/index.html.tera.embed
Normal file
1
templates/index.html.tera.embed
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user