up/src/print_dir.rs
2024-12-22 18:47:53 -08:00

42 lines
1.1 KiB
Rust

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();
}
}