diff --git a/Dockerfile b/Dockerfile index 4bcb68c..5f6162d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ COPY . . RUN cargo build --release -FROM gcr.io/distroless/cc-debian12 +FROM gcr.io/distroless/static-debian12 COPY --from=build /build/target/release/wakeonlan /app/wakeonlan diff --git a/src/main.rs b/src/main.rs index b62cbf8..54017ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use axum::{ routing::{get, post}, Router, }; -use eyre::{bail, Context}; +use eyre::{bail, Context, ContextCompat}; use std::net::{Ipv4Addr, ToSocketAddrs, UdpSocket}; use tracing_subscriber::EnvFilter; @@ -47,9 +47,18 @@ async fn wake() -> Response { fn wake_inner() -> eyre::Result<()> { let hosts = load_possible_hosts()?; let host = hosts - .into_iter() + .iter() .find(|(host, _)| host.contains("PC-Nora")) - .unwrap_or_else(|| ("PC-Nora".into(), parse_mac_addr("00:d8:61:ca:3a:18"))); + .wrap_err_with(|| { + format!( + "failed to find host, found: {}", + hosts + .iter() + .map(|(host, _)| host.clone()) + .collect::>() + .join(",") + ) + })?; let magic_packet = MagicPacket::new(&host.1); magic_packet.send().wrap_err("failed to send packet")?; @@ -58,15 +67,6 @@ fn wake_inner() -> eyre::Result<()> { Ok(()) } -fn parse_mac_addr(addr: &str) -> [u8; 6] { - addr.split(":") - .map(|part| u8::from_str_radix(part, 16).expect("invalid mac address")) - .collect::>() - .as_slice() - .try_into() - .expect("invalid mac address") -} - fn load_possible_hosts() -> eyre::Result> { // TODO: It would be very cool to instead read /proc/net/arp and then call getnameinfo but that's annoying... let arp = std::process::Command::new("arp") @@ -81,7 +81,13 @@ fn load_possible_hosts() -> eyre::Result> { .skip(1) .map(|line| line.split_whitespace().collect::>()) .map(|line_parts| { - let mac = parse_mac_addr(line_parts[2]); + let mac = line_parts[2] + .split(":") + .map(|part| u8::from_str_radix(part, 16).expect("invalid mac address")) + .collect::>() + .as_slice() + .try_into() + .expect("invalid mac address"); (line_parts[0].to_owned(), mac) }) .collect())