Added printing for both shells and download paths

This commit is contained in:
Random936 2025-06-08 21:47:13 -07:00
parent ad6d7dcc26
commit 59a22d3616
4 changed files with 63 additions and 43 deletions

View File

@ -43,4 +43,8 @@ impl Config {
let key_str = key.into(); let key_str = key.into();
self.shells.get(&key_str).cloned() self.shells.get(&key_str).cloned()
} }
pub fn get_shell_keys(&self) -> Vec<String> {
self.shells.keys().map(|s| s.to_owned()).collect()
}
} }

View File

@ -8,7 +8,7 @@ use axum::{
}; };
mod argparse; mod argparse;
mod print_dir; mod print_interface;
mod config; mod config;
mod shells; mod shells;
mod logging; mod logging;
@ -23,7 +23,14 @@ async fn main() {
None => conf.web_port None => conf.web_port
}; };
print_dir::print_interface(&args.interface, &port, &args.directory); println!("Files in current directory:");
print_interface::print_directory(&args.interface, &port, &args.directory, None).unwrap();
println!("\nFiles in persistent download directory:");
print_interface::print_directory(&args.interface, &port, &conf.get_download_path(), Some("download")).unwrap();
println!("\nConfigured shells:");
print_interface::print_interface(&args.interface, &port, &conf.get_shell_keys(), Some("shells"));
let app = Router::new() let app = Router::new()
.route("/upload", post(upload::upload_handler)) .route("/upload", post(upload::upload_handler))

View File

@ -1,41 +0,0 @@
use std::{fs, io};
use std::net::IpAddr;
use std::path::Path;
use if_addrs::{get_if_addrs, IfAddr, Interface};
fn print_single_interface<P: AsRef<Path>>(iface: Interface, port: &u16, path: P) -> io::Result<()> {
for entry in fs::read_dir(path)? {
let entry = entry?;
let entry_path = entry.path();
if entry_path.is_dir() { continue }
let filename = entry_path.file_name().unwrap();
if let IfAddr::V4(_) = iface.addr {
println!("http://{}:{}/{}", iface.ip(), port, filename.to_str().unwrap());
}
}
Ok(())
}
fn is_last_octet_one(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(addr) => addr.octets()[3] == 1,
_ => false
}
}
pub fn print_interface<P: AsRef<Path>>(iface_str: &str, port: &u16, path: P) {
for iface in get_if_addrs().unwrap() {
if iface_str == "all" {
if iface.is_loopback() || is_last_octet_one(iface.ip()) {
continue
}
} else {
if iface_str != iface.name { continue }
}
print_single_interface(iface, port, &path).unwrap();
}
}

50
src/print_interface.rs Normal file
View File

@ -0,0 +1,50 @@
use std::{fs, io};
use std::net::IpAddr;
use std::path::Path;
use if_addrs::{get_if_addrs, IfAddr, Interface};
fn print_single_interface(iface: Interface, port: &u16, keys: &Vec<String>, prefix: Option<&str>) {
for key in keys.iter() {
if let IfAddr::V4(_) = iface.addr {
match prefix {
Some(prefix) => println!("http://{}:{}/{}/{}", iface.ip(), port, prefix, key),
None => println!("http://{}:{}/{}", iface.ip(), port, key),
}
}
}
}
fn is_last_octet_one(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(addr) => addr.octets()[3] == 1,
_ => false
}
}
pub fn print_interface(iface_str: &str, port: &u16, keys: &Vec<String>, prefix: Option<&str>) {
for iface in get_if_addrs().unwrap() {
if iface_str == "all" {
if iface.is_loopback() || is_last_octet_one(iface.ip()) {
continue
}
} else {
if iface_str != iface.name { continue }
}
print_single_interface(iface, port, keys, prefix);
}
}
pub fn print_directory<P: AsRef<Path>>(iface_str: &str, port: &u16, path: P, prefix: Option<&str>) -> io::Result<()> {
let files = fs::read_dir(path)?
.filter_map(|entry| entry.ok())
.map(|entry| entry.path())
.filter(|path| path.is_file())
.map(|path| path.file_name().unwrap().to_string_lossy().to_string())
.collect::<Vec<String>>();
print_interface(iface_str.into(), port, &files, prefix);
Ok(())
}