From f042c4de82d341acec82aa3fa306551dbb10ebcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rik=20Levente?= <33373714+Levminer@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:11:46 +0200 Subject: [PATCH] Daemon wol --- Cargo.lock | 7 +++++ platforms/unix/daemon/Cargo.toml | 1 + platforms/unix/daemon/src/main.rs | 48 ++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe59176..0ffcade 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -913,6 +913,7 @@ dependencies = [ "tower", "tower-http", "webrtc", + "wol-rs", ] [[package]] @@ -7235,6 +7236,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "wol-rs" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5a8a033ef9b208ec8b5946761958ed2b2693ac49b04f647fdc013000870b8f" + [[package]] name = "wrapcenum-derive" version = "0.4.1" diff --git a/platforms/unix/daemon/Cargo.toml b/platforms/unix/daemon/Cargo.toml index 35f0568..3cc0e3a 100644 --- a/platforms/unix/daemon/Cargo.toml +++ b/platforms/unix/daemon/Cargo.toml @@ -24,3 +24,4 @@ log = "0.4" ezrtc = "0.4.0" webrtc = "0.10" async-channel = "2.3.1" +wol-rs = "1" diff --git a/platforms/unix/daemon/src/main.rs b/platforms/unix/daemon/src/main.rs index b72dc36..50e8c9a 100644 --- a/platforms/unix/daemon/src/main.rs +++ b/platforms/unix/daemon/src/main.rs @@ -18,6 +18,7 @@ use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode}; use std::borrow::Cow; use std::net::SocketAddr; use std::ops::ControlFlow; +use std::str::FromStr; use std::sync::Arc; use tower_http::{ cors::{Any, CorsLayer}, @@ -26,11 +27,12 @@ use tower_http::{ use webrtc::data_channel::data_channel_state::RTCDataChannelState; use webrtc::data_channel::RTCDataChannel; use webrtc::ice_transport::ice_server::RTCIceServer; +use wol::MacAddr; #[derive(Serialize, Deserialize)] -struct NetworkData { +struct GenericMessage { pub r#type: String, - pub data: HardwareInfo, + pub data: T, } struct AppState { @@ -136,7 +138,7 @@ async fn main() { loop { if dc.ready_state() == RTCDataChannelState::Open { let hw_message = receiver.recv().await.unwrap(); - let network_data = NetworkData { + let network_data = GenericMessage:: { r#type: "data".to_string(), data: hw_message.clone(), }; @@ -153,6 +155,35 @@ async fn main() { fn handle_data_channel_message(&self, message: String) { warn!("Data channel message received: {:?}", message); + + let network_message: Result, _> = + serde_json::from_str(&message); + + match network_message { + Ok(message) => match message.r#type.as_str() { + "shutdown" => { + info!("Received shutdown message"); + } + "sleep" => { + info!("Received sleep message"); + } + "restart" => { + info!("Received restart message"); + } + "wol" => { + let formatted_mac = format_mac_address(&message.data); + let mac_addr = MacAddr::from_str(&formatted_mac).unwrap(); + + info!("Sending WOL packet to: {:?}", mac_addr); + } + _ => { + warn!("Unknown message type: {:?}", message.r#type); + } + }, + Err(e) => { + warn!("Failed to parse message: {:?}", e); + } + } } } @@ -225,7 +256,7 @@ async fn handle_socket(mut socket: WebSocket, addr: SocketAddr, state: Arc { r#type: "data".to_string(), data: hw_message.clone(), }; @@ -312,3 +343,12 @@ fn process_message(msg: Message, who: SocketAddr) -> ControlFlow<(), ()> { } ControlFlow::Continue(()) } + +fn format_mac_address(mac: &str) -> String { + mac.chars() + .collect::>() + .chunks(2) + .map(|chunk| chunk.iter().collect::()) + .collect::>() + .join(":") +}