Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrinux committed Jul 19, 2024
1 parent deb70f2 commit d00fe73
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/iwd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::io::{BufRead, BufReader};
use std::process::Command;

use crate::RealCommandRunner;
use crate::{notify_connection, prompt_for_password};
use regex::Regex;
use std::io::{BufRead, BufReader};
use std::process::Command;

pub fn get_iwd_networks(interface: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let mut actions = Vec::new();
Expand Down Expand Up @@ -87,18 +88,35 @@ pub fn connect_to_iwd_wifi(
) -> Result<bool, Box<dyn std::error::Error>> {
if action.starts_with("wifi") {
let ssid = action.split_whitespace().nth(3).unwrap_or("");
if attempt_connection(interface, ssid, None)? {
Ok(true)
} else {
// If the first attempt fails, prompt for a passphrase using dmenu and retry
if !is_known_network(ssid)? {
let passphrase = prompt_for_password(&RealCommandRunner, ssid)?;
attempt_connection(interface, ssid, Some(passphrase))
} else {
attempt_connection(interface, ssid, None)
}
} else {
Ok(false)
}
}

fn is_known_network(ssid: &str) -> Result<bool, Box<dyn std::error::Error>> {
let output = Command::new("iwctl")
.arg("known-networks")
.arg("list")
.output()?;

if output.status.success() {
let reader = BufReader::new(output.stdout.as_slice());
for line in reader.lines() {
let line = line?;
if line.contains(ssid) {
return Ok(true);
}
}
}
Ok(false)
}

fn attempt_connection(
interface: &str,
ssid: &str,
Expand All @@ -122,7 +140,7 @@ fn attempt_connection(
Ok(true)
} else {
#[cfg(debug_assertions)]
eprintln!("Failed to connect to Wi-Fi network: {ssid}");
eprintln!("Failed to connect to Wi-Fi network: {}", ssid);
Ok(false)
}
}
Expand All @@ -147,7 +165,7 @@ pub fn is_iwd_connected(interface: &str) -> Result<bool, Box<dyn std::error::Err
let reader = BufReader::new(output.stdout.as_slice());
for line in reader.lines() {
let line = line?;
if line.contains("Connected") {
if line.contains("connected") {
return Ok(true);
}
}
Expand Down

0 comments on commit d00fe73

Please sign in to comment.