Added manual specification of interfaces

This commit is contained in:
Random936
2024-09-22 19:44:12 -07:00
parent 121f6d6417
commit 1f7f636219
5 changed files with 753 additions and 8 deletions

View File

@@ -0,0 +1,26 @@
use std::env;
use clap::Parser;
fn current_dir_as_string() -> String {
env::current_dir()
.unwrap()
.into_os_string()
.into_string()
.unwrap()
}
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Interface to print URLs for. Default is all interfaces.
#[arg(short, long, default_value_t = String::from("all"))]
pub interface: String,
/// Directory to serve. Default is your current working directory.
#[arg(short, long, default_value_t = current_dir_as_string())]
pub directory: String,
}
pub fn parse_args() -> Args {
Args::parse()
}

View File

@@ -1,8 +1,8 @@
use std::env;
mod argparse;
mod print_dir;
fn main() {
let cwd = env::current_dir().unwrap();
print_dir::print_all_interface(cwd);
let args = argparse::parse_args();
println!("{:#?}", args);
print_dir::print_interface(&args.interface, &args.directory);
}

View File

@@ -1,8 +1,9 @@
use std::{fs, io};
use std::net::IpAddr;
use std::path::Path;
use if_addrs::{Interface, get_if_addrs};
pub fn print_single_interface<P: AsRef<Path>>(iface: Interface, path: P) -> io::Result<()> {
fn print_single_interface<P: AsRef<Path>>(iface: Interface, path: P) -> io::Result<()> {
for entry in fs::read_dir(path)? {
let entry = entry?;
let entry_path = entry.path();
@@ -16,10 +17,21 @@ pub fn print_single_interface<P: AsRef<Path>>(iface: Interface, path: P) -> io::
Ok(())
}
pub fn print_all_interface<P: AsRef<Path>>(path: P) {
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, path: P) {
for iface in get_if_addrs().unwrap() {
if iface.is_loopback() {
continue
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, &path).unwrap();