Compare commits
9 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 815c4db23b | |||
| e5cb2d05f9 | |||
| a005d923fa | |||
| bcb17136d9 | |||
| c6fd190660 | |||
| fb68623acb | |||
| 0ce0705e19 | |||
| e30ecd06e6 | |||
| 0dfd17cc94 |
4 changed files with 218 additions and 26 deletions
2
README.md
Normal file
2
README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Wifi-Connector
|
||||||
|
Written in Rust. There will be GUI as well, hopefully.
|
||||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod network;
|
||||||
156
src/main.rs
156
src/main.rs
|
|
@ -1,19 +1,6 @@
|
||||||
use std::{process::Command, vec};
|
|
||||||
use anyhow::*;
|
use anyhow::*;
|
||||||
|
use std::{collections::HashMap, io::Write, process::Command, vec};
|
||||||
struct Network {
|
use wifi_connector::network::Network;
|
||||||
in_use: bool,
|
|
||||||
ssid: String,
|
|
||||||
signal: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Network {
|
|
||||||
fn from_nmcli_stdout(line: String) {
|
|
||||||
for element in line.split(' ') {
|
|
||||||
println!("{}", element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_available_wifis() -> Result<String> {
|
fn get_available_wifis() -> Result<String> {
|
||||||
let output = Command::new("nmcli")
|
let output = Command::new("nmcli")
|
||||||
|
|
@ -30,19 +17,140 @@ fn get_descriptor_positions(header_line: &String) -> Vec<u8> {
|
||||||
let mut positions: Vec<u8> = vec![];
|
let mut positions: Vec<u8> = vec![];
|
||||||
|
|
||||||
positions.push(0);
|
positions.push(0);
|
||||||
positions.push(header_line.find(" SSID").expect("SSID not found in header string") as u8 +1);
|
positions.push(
|
||||||
positions.push(header_line.find("SIGNAL").expect("SIGNAL not found in header string") as u8);
|
header_line
|
||||||
|
.find(" SSID")
|
||||||
|
.expect("SSID not found in header string") as u8
|
||||||
|
+ 1,
|
||||||
|
);
|
||||||
|
positions.push(
|
||||||
|
header_line
|
||||||
|
.find("SIGNAL")
|
||||||
|
.expect("SIGNAL not found in header string") as u8,
|
||||||
|
);
|
||||||
|
|
||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_all_networks() -> Vec<Network> {
|
||||||
|
let nmcli_output: String = get_available_wifis().expect("Wifi fetching exploded");
|
||||||
|
let positions = get_descriptor_positions(&nmcli_output);
|
||||||
|
|
||||||
|
let mut all_wifi_lines: Vec<String> =
|
||||||
|
nmcli_output.split('\n').map(|s| String::from(s)).collect();
|
||||||
|
let mut all_networks: Vec<Network> = vec![];
|
||||||
|
all_wifi_lines.remove(0);
|
||||||
|
|
||||||
|
for line in all_wifi_lines {
|
||||||
|
if line.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
all_networks.push(Network::from_nmcli_stdout(line.to_owned(), &positions));
|
||||||
|
}
|
||||||
|
|
||||||
|
return all_networks;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connect_to_network(network: &Network, password: &str) -> Result<()> {
|
||||||
|
let output = Command::new("nmcli")
|
||||||
|
.arg("device")
|
||||||
|
.arg("wifi")
|
||||||
|
.arg("connect")
|
||||||
|
.arg(&network.ssid.trim())
|
||||||
|
.arg("password")
|
||||||
|
.arg(password.trim())
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
if output.status.success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Failed to connect to network"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_connect_to_network_without_password(network: &Network) -> Result<()> {
|
||||||
|
let output = Command::new("nmcli")
|
||||||
|
.arg("device")
|
||||||
|
.arg("wifi")
|
||||||
|
.arg("connect")
|
||||||
|
.arg(&network.ssid.trim())
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
if output.status.success() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Couldn't connect to wifi without password"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_networks(networks: &Vec<Network>) {
|
||||||
|
for (idx, network) in networks.iter().enumerate() {
|
||||||
|
println!("{} - {}", idx, network);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn password_prompt_for_network(network: &Network) -> String {
|
||||||
|
let mut password = String::new();
|
||||||
|
print!(
|
||||||
|
"Now enter the password for the selected wifi '{}': ",
|
||||||
|
network.ssid
|
||||||
|
);
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
std::io::stdin().read_line(&mut password).unwrap();
|
||||||
|
password
|
||||||
|
}
|
||||||
|
|
||||||
|
fn choose_network(max: usize) -> String {
|
||||||
|
println!("Which network do you choose? (0-{})", max);
|
||||||
|
let mut input = String::new();
|
||||||
|
std::io::stdin().read_line(&mut input).unwrap();
|
||||||
|
input
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_header() {
|
||||||
|
println!("Index - In use - SSID - Signal");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uniquify_networks(networks: Vec<Network>) -> Vec<Network> {
|
||||||
|
let mut unique_networks: HashMap<String, Network> = HashMap::new();
|
||||||
|
|
||||||
|
for network in &networks {
|
||||||
|
let current_ssid = &network.ssid;
|
||||||
|
if unique_networks.keys().any(|key| key == current_ssid) {
|
||||||
|
if unique_networks[current_ssid].signal < network.signal {
|
||||||
|
unique_networks.insert(current_ssid.to_owned(), network.to_owned());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unique_networks.insert(current_ssid.to_owned(), network.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return unique_networks.into_values().collect();
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let all_wifis: String = get_available_wifis().expect("Wifi fetching exploded");
|
let all_networks = uniquify_networks(get_all_networks());
|
||||||
let positions = get_descriptor_positions(&all_wifis);
|
|
||||||
for position in (positions) {
|
print_header();
|
||||||
println!("{}", position);
|
print_networks(&all_networks);
|
||||||
|
|
||||||
|
let chosen_network_index = choose_network(all_networks.len() - 1);
|
||||||
|
|
||||||
|
let network = all_networks
|
||||||
|
.get(chosen_network_index.trim().parse::<usize>().unwrap())
|
||||||
|
.expect("Given number does not reference a network");
|
||||||
|
let mut connected = try_connect_to_network_without_password(network);
|
||||||
|
|
||||||
|
if !connected.is_ok() {
|
||||||
|
let password = password_prompt_for_network(network);
|
||||||
|
connected = connect_to_network(&network, password.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
if connected.is_ok() {
|
||||||
|
println!("Successfully connected to network '{}'", network.ssid);
|
||||||
|
} else {
|
||||||
|
println!("Connection failed");
|
||||||
}
|
}
|
||||||
// for line in all_wifis.split("\n") {
|
|
||||||
// Network::from_nmcli_stdout(line.to_owned());
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
81
src/network.rs
Normal file
81
src/network.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Network {
|
||||||
|
pub in_use: bool,
|
||||||
|
pub ssid: String,
|
||||||
|
pub signal: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Network {
|
||||||
|
pub fn from_nmcli_stdout(line: String, header_positions: &Vec<u8>) -> Self {
|
||||||
|
let mut network = Self {
|
||||||
|
in_use: false,
|
||||||
|
ssid: "".to_string(),
|
||||||
|
signal: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let ssid_pos = header_positions
|
||||||
|
.get(1)
|
||||||
|
.expect("header_positions has no index for SSID")
|
||||||
|
.to_owned() as usize;
|
||||||
|
let signal_pos = header_positions
|
||||||
|
.get(2)
|
||||||
|
.expect("header_positions has no index for SSID")
|
||||||
|
.to_owned() as usize;
|
||||||
|
|
||||||
|
if line
|
||||||
|
.chars()
|
||||||
|
.nth(
|
||||||
|
header_positions
|
||||||
|
.get(0)
|
||||||
|
.expect("header_positions has no index 0")
|
||||||
|
.to_owned() as usize,
|
||||||
|
)
|
||||||
|
.expect("Given line has no index 0")
|
||||||
|
== '*'
|
||||||
|
{
|
||||||
|
network.in_use = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut current_spaces = 0;
|
||||||
|
for char in line.chars().skip(ssid_pos) {
|
||||||
|
if char == ' ' {
|
||||||
|
current_spaces += 1;
|
||||||
|
if current_spaces == 2 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if current_spaces == 1 {
|
||||||
|
network.ssid.push(' ');
|
||||||
|
}
|
||||||
|
current_spaces = 0;
|
||||||
|
network.ssid.push(char);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut signal_as_string: String = line
|
||||||
|
.chars()
|
||||||
|
.nth(signal_pos)
|
||||||
|
.expect("Line has no Signal pos")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
signal_as_string.push(
|
||||||
|
line.chars()
|
||||||
|
.nth(signal_pos + 1)
|
||||||
|
.expect("Line has no Signal pos"),
|
||||||
|
);
|
||||||
|
|
||||||
|
network.signal = signal_as_string
|
||||||
|
.parse::<u8>()
|
||||||
|
.expect("Could not parse signal");
|
||||||
|
|
||||||
|
return network;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Network {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
return write!(f, "{} - {} - {} ", self.in_use, self.ssid, self.signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue