mirror of
https://github.com/Noratrieb/wakeonlan.git
synced 2026-01-17 10:05:10 +01:00
Compare commits
3 commits
68d5e7360d
...
f232b157ef
| Author | SHA1 | Date | |
|---|---|---|---|
| f232b157ef | |||
| b7eca51206 | |||
| 75ebc4907b |
2 changed files with 14 additions and 20 deletions
|
|
@ -6,7 +6,7 @@ COPY . .
|
||||||
|
|
||||||
RUN cargo build --release
|
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
|
COPY --from=build /build/target/release/wakeonlan /app/wakeonlan
|
||||||
|
|
||||||
|
|
|
||||||
32
src/main.rs
32
src/main.rs
|
|
@ -6,7 +6,7 @@ use axum::{
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use eyre::{bail, Context, ContextCompat};
|
use eyre::{bail, Context};
|
||||||
use std::net::{Ipv4Addr, ToSocketAddrs, UdpSocket};
|
use std::net::{Ipv4Addr, ToSocketAddrs, UdpSocket};
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
|
@ -47,18 +47,9 @@ async fn wake() -> Response {
|
||||||
fn wake_inner() -> eyre::Result<()> {
|
fn wake_inner() -> eyre::Result<()> {
|
||||||
let hosts = load_possible_hosts()?;
|
let hosts = load_possible_hosts()?;
|
||||||
let host = hosts
|
let host = hosts
|
||||||
.iter()
|
.into_iter()
|
||||||
.find(|(host, _)| host.contains("PC-Nora"))
|
.find(|(host, _)| host.contains("PC-Nora"))
|
||||||
.wrap_err_with(|| {
|
.unwrap_or_else(|| ("PC-Nora".into(), parse_mac_addr("00:d8:61:ca:3a:18")));
|
||||||
format!(
|
|
||||||
"failed to find host, found: {}",
|
|
||||||
hosts
|
|
||||||
.iter()
|
|
||||||
.map(|(host, _)| host.clone())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(",")
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
let magic_packet = MagicPacket::new(&host.1);
|
let magic_packet = MagicPacket::new(&host.1);
|
||||||
magic_packet.send().wrap_err("failed to send packet")?;
|
magic_packet.send().wrap_err("failed to send packet")?;
|
||||||
|
|
||||||
|
|
@ -67,6 +58,15 @@ fn wake_inner() -> eyre::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_mac_addr(addr: &str) -> [u8; 6] {
|
||||||
|
addr.split(":")
|
||||||
|
.map(|part| u8::from_str_radix(part, 16).expect("invalid mac address"))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.as_slice()
|
||||||
|
.try_into()
|
||||||
|
.expect("invalid mac address")
|
||||||
|
}
|
||||||
|
|
||||||
fn load_possible_hosts() -> eyre::Result<Vec<(String, [u8; 6])>> {
|
fn load_possible_hosts() -> eyre::Result<Vec<(String, [u8; 6])>> {
|
||||||
// TODO: It would be very cool to instead read /proc/net/arp and then call getnameinfo but that's annoying...
|
// 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")
|
let arp = std::process::Command::new("arp")
|
||||||
|
|
@ -81,13 +81,7 @@ fn load_possible_hosts() -> eyre::Result<Vec<(String, [u8; 6])>> {
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(|line| line.split_whitespace().collect::<Vec<_>>())
|
.map(|line| line.split_whitespace().collect::<Vec<_>>())
|
||||||
.map(|line_parts| {
|
.map(|line_parts| {
|
||||||
let mac = line_parts[2]
|
let mac = parse_mac_addr(line_parts[2]);
|
||||||
.split(":")
|
|
||||||
.map(|part| u8::from_str_radix(part, 16).expect("invalid mac address"))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.as_slice()
|
|
||||||
.try_into()
|
|
||||||
.expect("invalid mac address");
|
|
||||||
(line_parts[0].to_owned(), mac)
|
(line_parts[0].to_owned(), mac)
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue