diff --git a/Dockerfile b/Dockerfile index 5f6162d..4bcb68c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ COPY . . RUN cargo build --release -FROM gcr.io/distroless/static-debian12 +FROM gcr.io/distroless/cc-debian12 COPY --from=build /build/target/release/wakeonlan /app/wakeonlan diff --git a/src/main.rs b/src/main.rs index 54017ee..b62cbf8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use axum::{ routing::{get, post}, Router, }; -use eyre::{bail, Context, ContextCompat}; +use eyre::{bail, Context}; use std::net::{Ipv4Addr, ToSocketAddrs, UdpSocket}; use tracing_subscriber::EnvFilter; @@ -47,18 +47,9 @@ async fn wake() -> Response { fn wake_inner() -> eyre::Result<()> { let hosts = load_possible_hosts()?; let host = hosts - .iter() + .into_iter() .find(|(host, _)| host.contains("PC-Nora")) - .wrap_err_with(|| { - format!( - "failed to find host, found: {}", - hosts - .iter() - .map(|(host, _)| host.clone()) - .collect::>() - .join(",") - ) - })?; + .unwrap_or_else(|| ("PC-Nora".into(), parse_mac_addr("00:d8:61:ca:3a:18"))); let magic_packet = MagicPacket::new(&host.1); magic_packet.send().wrap_err("failed to send packet")?; @@ -67,6 +58,15 @@ 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,13 +81,7 @@ fn load_possible_hosts() -> eyre::Result> { .skip(1) .map(|line| line.split_whitespace().collect::>()) .map(|line_parts| { - 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"); + let mac = parse_mac_addr(line_parts[2]); (line_parts[0].to_owned(), mac) }) .collect())