merge update_network_args branch
This commit is contained in:
commit
699f2b13c9
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -2501,7 +2501,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "peach-network"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
dependencies = [
|
||||
"get_if_addrs",
|
||||
"miniserde",
|
||||
|
@ -1,11 +1,11 @@
|
||||
[package]
|
||||
name = "peach-network"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
authors = ["Andrew Reid <glyph@mycelial.technology>"]
|
||||
edition = "2021"
|
||||
description = "Query and configure network interfaces."
|
||||
homepage = "https://opencollective.com/peachcloud"
|
||||
repository = "ihttps://git.coopcloud.tech/PeachCloud/peach-workspace/src/branch/main/peach-network"
|
||||
repository = "https://git.coopcloud.tech/PeachCloud/peach-workspace/src/branch/main/peach-network"
|
||||
readme = "README.md"
|
||||
license = "LGPL-3.0-only"
|
||||
publish = false
|
||||
|
@ -16,11 +16,15 @@ API documentation can be built and served with `cargo doc --no-deps --open`. The
|
||||
use peach_network::{network, NetworkError};
|
||||
|
||||
fn main() -> Result<(), NetworkError> {
|
||||
let ip = network::ip("wlan0")?;
|
||||
let ssid = network::ssid("wlan0")?;
|
||||
let wlan_iface = "wlan0";
|
||||
|
||||
let new_ap = Wifi { ssid: "Home".to_string(), pass: "SuperSecret".to_string() };
|
||||
network::add(new_ap)?;
|
||||
let wlan_ip = network::ip(wlan_iface)?;
|
||||
let wlan_ssid = network::ssid(wlan_iface)?;
|
||||
|
||||
let ssid = "Home";
|
||||
let pass = "SuperSecret";
|
||||
|
||||
network::add(&wlan_iface, &ssid, &pass)?;
|
||||
network::save()?;
|
||||
|
||||
Ok(())
|
||||
|
@ -13,15 +13,18 @@
|
||||
//! ## Example Usage
|
||||
//!
|
||||
//! ```rust
|
||||
//! use peach_network::{network, NetworkError, network::WiFi};
|
||||
//! use peach_network::{network, NetworkError};
|
||||
//!
|
||||
//! fn main() -> Result<(), NetworkError> {
|
||||
//! let ip = network::ip("wlan0");
|
||||
//! let ssid = network::ssid("wlan0");
|
||||
//! let wlan_iface = "wlan0";
|
||||
//!
|
||||
//! let new_ap = WiFi { ssid: "Home".to_string(), pass: "SuperSecret".to_string() };
|
||||
//! let wlan_ip = network::ip(wlan_iface);
|
||||
//! let wlan_ssid = network::ssid(wlan_iface);
|
||||
//!
|
||||
//! let ssid = "Home";
|
||||
//! let pass = "SuperSecret";
|
||||
//!
|
||||
//! //network::add(&new_ap)?;
|
||||
//! //network::add(&wlan_iface, &ssid, &pass)?;
|
||||
//! //network::save()?;
|
||||
//!
|
||||
//! Ok(())
|
||||
|
@ -32,15 +32,6 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::error::NetworkError;
|
||||
use crate::utils;
|
||||
|
||||
/// Network SSID.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct Network {
|
||||
/// Service Set Identifier (SSID).
|
||||
pub ssid: String,
|
||||
}
|
||||
|
||||
/// Access point data retrieved via scan.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
|
||||
@ -114,17 +105,6 @@ pub struct Traffic {
|
||||
pub transmitted: u64,
|
||||
}
|
||||
|
||||
/// SSID and password for a wireless access point.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct WiFi {
|
||||
/// SSID.
|
||||
pub ssid: String,
|
||||
/// Password.
|
||||
pub pass: String,
|
||||
}
|
||||
|
||||
/* GET - Methods for retrieving data */
|
||||
|
||||
/// Retrieve list of available wireless access points for a given network
|
||||
@ -310,7 +290,7 @@ pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> {
|
||||
/// of all saved networks. If no network credentials are found, a `None` type
|
||||
/// is returned in the `Result`. In the event of an error, a `NetworkError` is
|
||||
/// returned in the `Result`.
|
||||
pub fn saved_networks() -> Result<Option<Vec<Network>>, NetworkError> {
|
||||
pub fn saved_networks() -> Result<Option<Vec<String>>, NetworkError> {
|
||||
let mut wpa = wpactrl::WpaCtrl::builder().open()?;
|
||||
let networks = wpa.request("LIST_NETWORKS")?;
|
||||
let mut ssids = Vec::new();
|
||||
@ -319,8 +299,7 @@ pub fn saved_networks() -> Result<Option<Vec<Network>>, NetworkError> {
|
||||
let len = v.len();
|
||||
if len > 1 {
|
||||
let ssid = v[1].trim().to_string();
|
||||
let response = Network { ssid };
|
||||
ssids.push(response)
|
||||
ssids.push(ssid)
|
||||
}
|
||||
}
|
||||
|
||||
@ -484,7 +463,7 @@ pub fn traffic(iface: &str) -> Result<Option<Traffic>, NetworkError> {
|
||||
/// network interface. If the command executes successfully, an `Ok` `Result`
|
||||
/// type is returned. In the event of an error, a `NetworkError` is returned
|
||||
/// in the `Result`.
|
||||
pub fn start_iface_service(iface: String) -> Result<(), NetworkError> {
|
||||
pub fn start_iface_service(iface: &str) -> Result<(), NetworkError> {
|
||||
let iface_service = format!("wpa_supplicant@{}.service", &iface);
|
||||
|
||||
// start the interface service
|
||||
@ -493,7 +472,10 @@ pub fn start_iface_service(iface: String) -> Result<(), NetworkError> {
|
||||
.arg("start")
|
||||
.arg(iface_service)
|
||||
.output()
|
||||
.map_err(|source| NetworkError::StartInterface { source, iface })?;
|
||||
.map_err(|source| NetworkError::StartInterface {
|
||||
source,
|
||||
iface: iface.to_string(),
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -510,15 +492,15 @@ pub fn start_iface_service(iface: String) -> Result<(), NetworkError> {
|
||||
/// `<wlan_iface>` is the provided interface parameter), an `Ok` `Result` type
|
||||
/// is returned. In the event of an error, a `NetworkError` is returned in the
|
||||
/// `Result`.
|
||||
pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> {
|
||||
pub fn add(wlan_iface: &str, ssid: &str, pass: &str) -> Result<(), NetworkError> {
|
||||
// generate configuration based on provided ssid & password
|
||||
let output = Command::new("wpa_passphrase")
|
||||
.arg(&wifi.ssid)
|
||||
.arg(&wifi.pass)
|
||||
.arg(&ssid)
|
||||
.arg(&pass)
|
||||
.stdout(Stdio::piped())
|
||||
.output()
|
||||
.map_err(|source| NetworkError::GenWpaPassphrase {
|
||||
ssid: wifi.ssid.to_string(),
|
||||
ssid: ssid.to_string(),
|
||||
source,
|
||||
})?;
|
||||
|
||||
@ -545,7 +527,7 @@ pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> {
|
||||
} else {
|
||||
let err_msg = String::from_utf8_lossy(&output.stdout);
|
||||
Err(NetworkError::GenWpaPassphraseWarning {
|
||||
ssid: wifi.ssid.to_string(),
|
||||
ssid: ssid.to_string(),
|
||||
err_msg: err_msg.to_string(),
|
||||
})
|
||||
}
|
||||
@ -558,7 +540,7 @@ pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> {
|
||||
/// are checked. If the service is active but the interface is down (ie. not
|
||||
/// currently connected to an access point), then the access point is activated
|
||||
/// by calling the `activate_ap()` function.
|
||||
pub fn check_iface(wlan_iface: String, ap_iface: String) -> Result<(), NetworkError> {
|
||||
pub fn check_iface(wlan_iface: &str, ap_iface: &str) -> Result<(), NetworkError> {
|
||||
let wpa_service = format!("wpa_supplicant@{}.service", &wlan_iface);
|
||||
|
||||
// returns 0 if the service is currently active
|
||||
@ -569,7 +551,7 @@ pub fn check_iface(wlan_iface: String, ap_iface: String) -> Result<(), NetworkEr
|
||||
.map_err(NetworkError::WlanState)?;
|
||||
|
||||
// returns the current state of the wlan interface
|
||||
let iface_state = state(&wlan_iface)?;
|
||||
let iface_state = state(wlan_iface)?;
|
||||
|
||||
// returns down if the interface is not currently connected to an ap
|
||||
let wlan_state = match iface_state {
|
||||
|
Loading…
Reference in New Issue
Block a user