Initial Commit
This commit is contained in:
commit
af221bedf7
5 changed files with 1268 additions and 0 deletions
414
src/main.rs
Normal file
414
src/main.rs
Normal file
|
|
@ -0,0 +1,414 @@
|
|||
// To hide console on Windows for GUI (but makes CLI output complex):
|
||||
// #![cfg_attr(all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows")]
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use clap::Parser;
|
||||
use http_body_util::Full;
|
||||
use hyper::{
|
||||
body::{Bytes, Incoming},
|
||||
header::{CONTENT_TYPE, LOCATION},
|
||||
server::conn::http1,
|
||||
service::service_fn,
|
||||
Method, Request, Response, StatusCode,
|
||||
};
|
||||
use hyper_util::rt::TokioIo;
|
||||
use log::{debug, error, info, warn};
|
||||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||
use std::{
|
||||
convert::Infallible,
|
||||
env,
|
||||
fs as std_fs,
|
||||
io::{Read, Write},
|
||||
net::{IpAddr, SocketAddr},
|
||||
path::{Component, Path, PathBuf},
|
||||
str::FromStr,
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
const APP_NAME: &str = clap::crate_name!();
|
||||
const VERSION: &str = clap::crate_version!();
|
||||
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
||||
const PATH_SEGMENT: &AsciiSet = &FRAGMENT.add(b'%').add(b'/').add(b'?').add(b'#');
|
||||
|
||||
// --- CLI Argument Parsing Setup ---
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(name = APP_NAME, version = VERSION, about = "A quick, simple HTTP file server. Auto-installs/updates on first run or version change.")]
|
||||
struct Cli {
|
||||
#[clap(subcommand)]
|
||||
command: Option<Commands>,
|
||||
|
||||
#[clap(short, long, default_value = ".", global = true)]
|
||||
dir: PathBuf,
|
||||
|
||||
#[clap(short, long, default_value_t = 8080, global = true)]
|
||||
port: u16,
|
||||
|
||||
#[clap(short, long, value_parser = parse_ip_addr, default_value_t = IpAddr::from_str("0.0.0.0").unwrap(), global = true)]
|
||||
bind: IpAddr,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand, Debug)]
|
||||
enum Commands {
|
||||
/// (Re)Install this application to a local directory for easy PATH access
|
||||
Install,
|
||||
}
|
||||
|
||||
fn parse_ip_addr(s: &str) -> Result<IpAddr, String> {
|
||||
IpAddr::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
// --- Installation Helper Functions ---
|
||||
fn get_install_paths() -> Result<(PathBuf, PathBuf, PathBuf)> {
|
||||
let home_dir = dirs::home_dir().context("Failed to determine home directory.")?;
|
||||
let install_dir = home_dir.join(format!(".{}", APP_NAME.to_lowercase())).join("bin");
|
||||
let exe_name = if cfg!(windows) { format!("{}.exe", APP_NAME) } else { APP_NAME.to_string() };
|
||||
let exe_path = install_dir.join(&exe_name);
|
||||
let version_path = install_dir.join(format!("{}.version", APP_NAME.to_lowercase()));
|
||||
Ok((install_dir, exe_path, version_path))
|
||||
}
|
||||
|
||||
fn read_version(path: &Path) -> Result<Option<String>> {
|
||||
if !path.exists() { return Ok(None); }
|
||||
let mut file = std_fs::File::open(path)?;
|
||||
let mut version = String::new();
|
||||
file.read_to_string(&mut version)?;
|
||||
Ok(Some(version.trim().to_string()))
|
||||
}
|
||||
|
||||
fn write_version(path: &Path, version: &str) -> Result<()> {
|
||||
std_fs::create_dir_all(path.parent().unwrap())?;
|
||||
std_fs::File::create(path)?.write_all(version.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn install_app(current_exe: &Path, target_dir: &Path, target_exe: &Path, version_path: &Path) -> Result<()> {
|
||||
if current_exe == target_exe {
|
||||
let installed_version = read_version(version_path)?;
|
||||
if installed_version.as_deref() != Some(VERSION) {
|
||||
write_version(version_path, VERSION)?;
|
||||
info!("Updated version file for executable at target location.");
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
std_fs::create_dir_all(target_dir)?;
|
||||
std_fs::copy(current_exe, target_exe)?;
|
||||
write_version(version_path, VERSION)?;
|
||||
info!("Copied executable to {} and wrote version: {}", target_exe.display(), VERSION);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_path_instructions(target_dir: &Path, target_exe: &Path) {
|
||||
println!("\n--- {} Installation/Update ---", APP_NAME.to_uppercase());
|
||||
println!("{} has been copied/updated to: {}", APP_NAME, target_exe.display());
|
||||
println!("\nIMPORTANT: For easy access, add this directory to your PATH:");
|
||||
println!(" {}", target_dir.display());
|
||||
|
||||
if cfg!(windows) {
|
||||
println!("\nFor example:");
|
||||
println!(" - PowerShell (current session): $env:Path += \";{}\"", target_dir.display());
|
||||
println!(" - Command Prompt (current session): set PATH=%PATH%;{}", target_dir.display());
|
||||
println!(" - To make it permanent, search for 'environment variables' in Windows settings.");
|
||||
} else {
|
||||
println!("\nFor example (add to ~/.bashrc or ~/.profile):");
|
||||
println!(" export PATH=\"{}:$PATH\"", target_dir.display());
|
||||
}
|
||||
println!("\nOnce PATH is updated, you can run '{}' from any terminal.", APP_NAME);
|
||||
}
|
||||
|
||||
// --- Server Error Type ---
|
||||
#[derive(Debug)]
|
||||
enum ServeError {
|
||||
NotFound,
|
||||
Forbidden,
|
||||
Io(std::io::Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ServeError {
|
||||
fn from(err: std::io::Error) -> ServeError {
|
||||
match err.kind() {
|
||||
std::io::ErrorKind::NotFound => ServeError::NotFound,
|
||||
std::io::ErrorKind::PermissionDenied => ServeError::Forbidden,
|
||||
_ => ServeError::Io(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ServeError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ServeError::NotFound => write!(f, "Not Found"),
|
||||
ServeError::Forbidden => write!(f, "Forbidden"),
|
||||
ServeError::Io(e) => write!(f, "IO Error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::error::Error for ServeError {}
|
||||
|
||||
// --- Main Entry Point ---
|
||||
fn main() -> Result<()> {
|
||||
let current_exe = env::current_exe().context("Failed to get current exe path")?;
|
||||
let (install_dir, install_path, version_path) = get_install_paths()?;
|
||||
|
||||
let mut explicit_install = false;
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() > 1 && args[1] == "install" {
|
||||
explicit_install = true;
|
||||
}
|
||||
|
||||
if explicit_install {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||
info!("Explicit install command detected.");
|
||||
match install_app(¤t_exe, &install_dir, &install_path, &version_path) {
|
||||
Ok(_) => print_path_instructions(&install_dir, &install_path),
|
||||
Err(e) => error!("Installation failed: {}", e),
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let needs_update = if current_exe != install_path {
|
||||
println!("[{}] Current executable is not at the standard install location.", APP_NAME);
|
||||
true
|
||||
} else {
|
||||
match read_version(&version_path) {
|
||||
Ok(Some(ver)) if ver != VERSION => {
|
||||
println!("[{}] Version mismatch: {} vs {}. Will update.", APP_NAME, ver, VERSION);
|
||||
true
|
||||
}
|
||||
Ok(None) => {
|
||||
println!("[{}] No version file found. Will write version info.", APP_NAME);
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[WARNING] Could not read version file: {}. Consider running `{} install`.", e, APP_NAME);
|
||||
false
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
};
|
||||
|
||||
if needs_update {
|
||||
println!("[{}] Updating to: {}", APP_NAME, install_path.display());
|
||||
match install_app(¤t_exe, &install_dir, &install_path, &version_path) {
|
||||
Ok(_) => {
|
||||
print_path_instructions(&install_dir, &install_path);
|
||||
println!("\n[{}] Proceeding to run from: {}", APP_NAME, current_exe.display());
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[WARNING] Auto-update failed: {}. Will run from current location.", e);
|
||||
eprintln!(" Try running `{} install` manually.", APP_NAME);
|
||||
}
|
||||
}
|
||||
println!("---");
|
||||
}
|
||||
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
|
||||
rt.block_on(async {
|
||||
let cli = Cli::parse();
|
||||
if cli.command.is_some() {
|
||||
warn!("Unhandled subcommand after initial checks.");
|
||||
return Ok(());
|
||||
}
|
||||
run_server(cli.dir, cli.port, cli.bind).await
|
||||
})
|
||||
}
|
||||
|
||||
// --- Server Logic ---
|
||||
async fn run_server(serve_dir_arg: PathBuf, port: u16, bind_ip: IpAddr) -> Result<()> {
|
||||
let root_dir = tokio::fs::canonicalize(&serve_dir_arg).await
|
||||
.with_context(|| format!("Failed to resolve path: {}", serve_dir_arg.display()))?;
|
||||
|
||||
if !root_dir.is_dir() {
|
||||
bail!("The specified path '{}' is not a directory.", root_dir.display());
|
||||
}
|
||||
|
||||
let app_state = Arc::new(root_dir.clone());
|
||||
let addr = SocketAddr::new(bind_ip, port);
|
||||
info!("Serving directory: {}", root_dir.display());
|
||||
info!("Listening on http://{}", addr);
|
||||
info!("Press Ctrl+C to shut down.");
|
||||
|
||||
let listener = TcpListener::bind(addr).await?;
|
||||
|
||||
loop {
|
||||
let (stream, remote_addr) = tokio::select! {
|
||||
biased;
|
||||
_ = shutdown_signal() => break,
|
||||
res = listener.accept() => match res {
|
||||
Ok(s_ra) => s_ra,
|
||||
Err(e) => {
|
||||
if e.kind() == std::io::ErrorKind::InvalidInput {
|
||||
info!("Listener closed, shutting down.");
|
||||
break;
|
||||
}
|
||||
error!("Failed to accept connection: {}", e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
debug!("Accepted connection from: {}", remote_addr);
|
||||
|
||||
let io = TokioIo::new(stream);
|
||||
let app_state_clone = Arc::clone(&app_state);
|
||||
|
||||
tokio::task::spawn(async move {
|
||||
let service = service_fn(move |req| handle_request(req, Arc::clone(&app_state_clone)));
|
||||
if let Err(err) = http1::Builder::new().serve_connection(io, service).with_upgrades().await {
|
||||
let err_str = err.to_string();
|
||||
if !err_str.contains("connection closed") && !err_str.contains("reset by peer")
|
||||
&& !err_str.contains("broken pipe") && !err_str.contains("unexpected end of file") {
|
||||
warn!("Error serving connection from {}: {}", remote_addr, err);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
info!("Server shut down successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_request(req: Request<Incoming>, root_dir: Arc<PathBuf>) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||
let response = match serve_path(&root_dir, req.uri().path(), &req).await {
|
||||
Ok(resp) => resp,
|
||||
Err(e) => {
|
||||
warn!("Error serving path {}: {}", req.uri().path(), e);
|
||||
let status = match e {
|
||||
ServeError::NotFound => StatusCode::NOT_FOUND,
|
||||
ServeError::Forbidden => StatusCode::FORBIDDEN,
|
||||
ServeError::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
Response::builder()
|
||||
.status(status)
|
||||
.header(CONTENT_TYPE, "text/plain")
|
||||
.body(Full::from(status.canonical_reason().unwrap_or("Error").to_string()))
|
||||
.unwrap()
|
||||
}
|
||||
};
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
async fn serve_path(root_dir: &Path, req_path: &str, req: &Request<Incoming>) -> Result<Response<Full<Bytes>>, ServeError> {
|
||||
if req.method() != Method::GET && req.method() != Method::HEAD {
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::METHOD_NOT_ALLOWED)
|
||||
.header(CONTENT_TYPE, "text/plain")
|
||||
.body(Full::from("Method Not Allowed"))
|
||||
.unwrap());
|
||||
}
|
||||
|
||||
let decoded_path = percent_encoding::percent_decode_str(req_path)
|
||||
.decode_utf8()
|
||||
.map_err(|_| ServeError::Forbidden)?;
|
||||
|
||||
let mut path_to_check = root_dir.to_path_buf();
|
||||
for component in Path::new(decoded_path.as_ref()).components() {
|
||||
match component {
|
||||
Component::Normal(comp) => path_to_check.push(comp),
|
||||
Component::ParentDir => {
|
||||
if path_to_check == *root_dir { return Err(ServeError::Forbidden); }
|
||||
path_to_check.pop();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let full_path = tokio::fs::canonicalize(&path_to_check).await?;
|
||||
if !full_path.starts_with(root_dir) {
|
||||
warn!("Path traversal attempt: '{}' resolved to '{}'", req_path, full_path.display());
|
||||
return Err(ServeError::Forbidden);
|
||||
}
|
||||
|
||||
let metadata = tokio::fs::metadata(&full_path).await?;
|
||||
|
||||
if metadata.is_dir() {
|
||||
let index_path = full_path.join("index.html");
|
||||
match tokio::fs::metadata(&index_path).await {
|
||||
Ok(index_md) if index_md.is_file() => {
|
||||
serve_file(&index_path, req.method() == Method::HEAD).await
|
||||
}
|
||||
_ => {
|
||||
if !req_path.ends_with('/') {
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::FOUND)
|
||||
.header(LOCATION, format!("{}/", req_path))
|
||||
.body(Full::from(Bytes::new()))
|
||||
.unwrap());
|
||||
}
|
||||
list_directory(&full_path, root_dir, req_path).await
|
||||
}
|
||||
}
|
||||
} else if metadata.is_file() {
|
||||
serve_file(&full_path, req.method() == Method::HEAD).await
|
||||
} else {
|
||||
Err(ServeError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_file(file_path: &Path, is_head: bool) -> Result<Response<Full<Bytes>>, ServeError> {
|
||||
let mime_type = mime_guess::from_path(file_path).first_or_octet_stream();
|
||||
let body = if is_head {
|
||||
Full::new(Bytes::new())
|
||||
} else {
|
||||
Full::from(tokio::fs::read(file_path).await?)
|
||||
};
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(CONTENT_TYPE, mime_type.as_ref())
|
||||
.body(body)
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
async fn list_directory(dir_path: &Path, root_dir: &Path, req_path: &str) -> Result<Response<Full<Bytes>>, ServeError> {
|
||||
let mut body = format!(
|
||||
"<html><head><meta charset=\"UTF-8\"><title>Index of {}</title></head><body><h1>Index of {}</h1><hr><pre>",
|
||||
req_path, req_path
|
||||
);
|
||||
|
||||
if dir_path != root_dir && req_path != "/" && !req_path.is_empty() {
|
||||
let mut parent_uri_path = PathBuf::from(req_path.trim_end_matches('/'));
|
||||
parent_uri_path.pop();
|
||||
let parent_link = if parent_uri_path.as_os_str().is_empty() {
|
||||
"/".to_string()
|
||||
} else {
|
||||
format!("{}/", parent_uri_path.to_string_lossy())
|
||||
};
|
||||
body.push_str(&format!("<a href=\"{}\">..</a>\n",
|
||||
utf8_percent_encode(&parent_link, PATH_SEGMENT)));
|
||||
}
|
||||
|
||||
let mut entries = Vec::new();
|
||||
let mut dir_iter = tokio::fs::read_dir(dir_path).await?;
|
||||
while let Some(entry) = dir_iter.next_entry().await? {
|
||||
entries.push(entry);
|
||||
}
|
||||
|
||||
entries.sort_by_key(|e| (!e.path().is_dir(), e.file_name().to_ascii_lowercase()));
|
||||
|
||||
for entry in entries {
|
||||
let name = entry.file_name().to_string_lossy().into_owned();
|
||||
let mut link = name.clone();
|
||||
if entry.file_type().await?.is_dir() { link.push('/'); }
|
||||
|
||||
body.push_str(&format!("<a href=\"{}\">{}</a>\n",
|
||||
utf8_percent_encode(&link, PATH_SEGMENT), name));
|
||||
}
|
||||
|
||||
body.push_str("</pre><hr></body></html>");
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(CONTENT_TYPE, "text/html; charset=utf-8")
|
||||
.body(Full::from(body))
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
async fn shutdown_signal() {
|
||||
tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler");
|
||||
info!("Received Ctrl+C, shutting down gracefully...");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue