From e77ee9c8a3fb0b427b12fceddf91b6be8078c3fc Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Fri, 25 Apr 2025 16:04:42 +0200 Subject: [PATCH] added hotspot functions WIP --- src/hotspot.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/hotspot.rs diff --git a/src/hotspot.rs b/src/hotspot.rs new file mode 100644 index 0000000..df1b1ff --- /dev/null +++ b/src/hotspot.rs @@ -0,0 +1,88 @@ +use std::error::Error; +use tokio::process::Command; + +const SSID: &str = "fwa"; +const CON_NAME: &str = "fwa-hotspot"; +const PASSWORD: &str = "hunter22"; +const IPV4_ADDRES: &str = "192.168.4.1/24"; + +async fn create_hotspot() -> Result<(), Box> { + let mut cmd = Command::new("nmcli") + .args(["device", "wifi", "hotspot"]) + .arg("con-name") + .arg(CON_NAME) + .arg("ssid") + .arg(SSID) + .arg("password") + .arg(PASSWORD) + .spawn()?; + + let status = cmd.wait().await?; + + if !status.success() { + return Err("Failed to create hotspot".into()); + } + + let mut cmd = Command::new("nmcli") + .arg("connection") + .arg("modify") + .arg(CON_NAME) + .arg("ipv4.method") + .arg("shared") + .arg("ipv4.addresses") + .arg(IPV4_ADDRES) + .spawn()?; + + let status = cmd.wait().await?; + + if !status.success() { + return Err("Failed to create hotspot".into()); + } + + Ok(()) +} + +async fn exists() -> Result> { + let mut cmd = Command::new("nmcli") + .args(["connection", "show"]) + .arg(CON_NAME) + .spawn()?; + + let status = cmd.wait().await?; + + Ok(status.success()) +} + +pub async fn enable_hotspot() -> Result<(), Box> { + if !exists().await? { + create_hotspot().await?; + } + + let mut cmd = Command::new("nmcli") + .args(["connection", "up"]) + .arg(CON_NAME) + .spawn()?; + + let status = cmd.wait().await?; + + if !status.success() { + return Err("Failed to enable hotspot".into()); + } + + Ok(()) +} + +pub async fn disable_hotspot() -> Result<(), Box> { + let mut cmd = Command::new("nmcli") + .args(["connection", "down"]) + .arg(CON_NAME) + .spawn()?; + + let status = cmd.wait().await?; + + if !status.success() { + return Err("Failed to enable hotspot".into()); + } + + Ok(()) +}