everything is web nowadays

- code by chatgpt
This commit is contained in:
ldlda
2025-08-21 08:42:25 +07:00 Unverified
parent f3433ed276
commit 6a335bb4c0
+34 -44
View File
@@ -1,55 +1,45 @@
use std::{collections::HashSet, fs, iter, net::IpAddr}; use std::iter;
use tokio::{io, net::UdpSocket}; use tokio::{
io,
net::{TcpListener, UdpSocket},
};
use axum::{Router, response::Html, routing::get};
async fn home() -> Html<&'static str> {
Html(
r#"
<html>
<body>
<form method="POST" action="/wake">
<button type="submit">Wake LDA</button>
</form>
</body>
</html>
"#,
)
}
async fn wake_handler() -> &'static str {
match wake("lda.lan").await {
Err(_) => "Wake failed",
Ok(_) => "Packet sent!",
}
}
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> color_eyre::Result<()> { async fn main() -> color_eyre::Result<()> {
color_eyre::install()?; color_eyre::install()?;
wake("lda.lan").await?; let app = Router::new()
.route("/", get(home))
.route("/wake", axum::routing::post(wake_handler));
let port = TcpListener::bind("0.0.0.0:12012").await?;
axum::serve(port, app.into_make_service()).await?;
Ok(()) Ok(())
} }
// async fn wake(machine_name: &str) -> io::Result<()> {
// let sock = UdpSocket::bind("0.0.0.0:0").await?;
// for (ip, mac) in find_mac(machine_name).await? {
// // println!("yo drop the {ip}/{mac:?}");
// let packet: Vec<u8> = iter::once([0xff; 6])
// .chain(std::iter::repeat_n(mac, 16))
// .flatten()
// .collect();
// // dbg!(&ip, &mac);
// sock.send_to(&packet, (ip, 9)).await?;
// }
// Ok(())
// }
// async fn find_mac(machine_name: &str) -> io::Result<Vec<(IpAddr, [u8; 6])>> {
// let arp = fs::read_to_string("/proc/net/arp")?;
// let ips: HashSet<IpAddr> = tokio::net::lookup_host((machine_name, 0))
// .await?
// .map(|c| c.ip())
// .collect();
// // dbg!(&ips, &arp);
// Ok(arp
// .lines()
// .skip(1)
// .filter_map(|line| {
// let mut parts = line.split_ascii_whitespace();
// let ip: IpAddr = parts.next()?.parse().ok()?;
// // println!("ip looks kinda good! {ip}");
// if !ips.contains(&ip) {
// return None;
// }
// let mac_str = parts.nth(2)?;
// let mut mac = [0u8; 6];
// for (byte, hex) in mac.iter_mut().zip(mac_str.split(':')) {
// *byte = u8::from_str_radix(hex, 16).ok()?;
// }
// Some((ip, mac))
// })
// .collect())
// }
async fn wake(_machine_name: &str) -> io::Result<()> { async fn wake(_machine_name: &str) -> io::Result<()> {
let mac1 = [0x04, 0x7c, 0x16, 0x79, 0x6d, 0xee]; let mac1 = [0x04, 0x7c, 0x16, 0x79, 0x6d, 0xee];