20 Commits

Author SHA1 Message Date
4d08323d77 bump minor version and remove anyhow dep 2022-01-03 14:06:22 +02:00
df91968762 add network api doc comments 2022-01-03 14:05:23 +02:00
9255abb078 add remaining network rpc methods 2022-01-03 14:04:47 +02:00
1ad956c0c7 add additional network rpc structs 2022-01-03 14:04:33 +02:00
39c15d0fe5 Merge branch 'update_network_args' into network_rpc_server
Merge peach-network argument changes which impact the jsonrpc-server
code.
2022-01-03 12:03:38 +02:00
58f2ddde05 replace String with str and remove unnecessary structs 2022-01-03 11:55:37 +02:00
4b0b2626a4 update code examples 2022-01-03 11:55:20 +02:00
a05e67c22f bump patch version 2022-01-03 11:55:06 +02:00
fd12e97bc4 begin adding set methods for network rpc 2022-01-03 11:15:34 +02:00
318fa9768a add get methods for network rpcs 2022-01-03 10:46:38 +02:00
b5ce677a5b Merge pull request 'Removed hardcoded interfaces from peach-network' (#47) from iface_agnostic into main
Reviewed-on: #47
2021-12-14 20:42:40 +00:00
4d6dbd511e Merge pull request 'Remove jsonrpc from peach-network' (#46) from split_network_logic into main
Reviewed-on: #46
2021-12-14 18:55:16 +00:00
7fe4715014 bump version and switch to rust 2021 2021-12-14 16:13:27 +02:00
dd33fdd47d Merge pull request 'Create JSON-RPC server repo with stats' (#44) from jsonrpc_server into main
Reviewed-on: #44
2021-12-14 13:05:00 +00:00
1986d31461 feature flag docs and license 2021-12-13 11:00:16 +02:00
a824be53b9 update docs and remove unnecessary structs 2021-12-13 10:55:27 +02:00
287082381e remove json-rpc methods 2021-12-13 10:55:04 +02:00
9f40378fce remove json-rpc error code and improve docs 2021-12-13 10:54:51 +02:00
4f5eb3aa04 update dependencies and version 2021-12-13 10:54:38 +02:00
f4113f0632 remove debian files and main 2021-12-13 10:54:24 +02:00
13 changed files with 773 additions and 1729 deletions

15
Cargo.lock generated
View File

@ -2472,15 +2472,17 @@ dependencies = [
[[package]]
name = "peach-jsonrpc-server"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"env_logger 0.9.0",
"jsonrpc-core 18.0.0",
"jsonrpc-http-server 18.0.0",
"jsonrpc-test 18.0.0",
"log 0.4.14",
"miniserde",
"peach-network",
"peach-stats",
"serde 1.0.130",
"serde_json",
]
[[package]]
@ -2532,18 +2534,13 @@ dependencies = [
[[package]]
name = "peach-network"
version = "0.2.13"
version = "0.4.1"
dependencies = [
"env_logger 0.6.2",
"get_if_addrs",
"jsonrpc-core 11.0.0",
"jsonrpc-http-server 11.0.0",
"jsonrpc-test 11.0.0",
"log 0.4.14",
"miniserde",
"probes 0.4.1",
"regex",
"serde 1.0.130",
"serde_json",
"wpactrl",
]

View File

@ -1,7 +1,7 @@
[package]
name = "peach-jsonrpc-server"
authors = ["Andrew Reid <glyph@mycelial.technology>"]
version = "0.1.0"
version = "0.2.0"
edition = "2021"
description = "JSON-RPC over HTTP for the PeachCloud system. Provides a JSON-RPC wrapper around the stats, network and oled libraries."
homepage = "https://opencollective.com/peachcloud"
@ -18,8 +18,10 @@ env_logger = "0.9"
jsonrpc-core = "18"
jsonrpc-http-server = "18"
log = "0.4"
miniserde = "0.1.15"
peach-stats = { path = "../peach-stats", features = ["miniserde_support"] }
peach-network = { path = "../peach-network", features = ["serde_support"] }
peach-stats = { path = "../peach-stats", features = ["serde_support"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
[dev-dependencies]
jsonrpc-test = "18"

View File

@ -1,12 +1,18 @@
use std::fmt;
use jsonrpc_core::{Error as JsonRpcError, ErrorCode};
use peach_network::NetworkError;
use peach_stats::StatsError;
use serde_json::Error as SerdeError;
/// Custom error type encapsulating all possible errors for a JSON-RPC server
/// and associated methods.
#[derive(Debug)]
pub enum JsonRpcServerError {
pub enum ServerError {
/// An error returned from the `peach-network` library.
Network(NetworkError),
/// Failed to serialize a data structure.
Serialize(SerdeError),
/// An error returned from the `peach-stats` library.
Stats(StatsError),
/// An expected JSON-RPC method parameter was not provided.
@ -15,32 +21,48 @@ pub enum JsonRpcServerError {
ParseParameter(JsonRpcError),
}
impl fmt::Display for JsonRpcServerError {
impl fmt::Display for ServerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
JsonRpcServerError::ParseParameter(ref source) => {
ServerError::ParseParameter(ref source) => {
write!(f, "Failed to parse parameter: {}", source)
}
JsonRpcServerError::MissingParameter(ref source) => {
ServerError::MissingParameter(ref source) => {
write!(f, "Missing expected parameter: {}", source)
}
JsonRpcServerError::Stats(ref source) => {
ServerError::Network(ref source) => {
write!(f, "{}", source)
}
ServerError::Serialize(ref source) => {
write!(f, "Serde serialization failure: {}", source)
}
ServerError::Stats(ref source) => {
write!(f, "{}", source)
}
}
}
}
impl From<JsonRpcServerError> for JsonRpcError {
fn from(err: JsonRpcServerError) -> Self {
impl From<ServerError> for JsonRpcError {
fn from(err: ServerError) -> Self {
match &err {
JsonRpcServerError::Stats(source) => JsonRpcError {
ServerError::Network(source) => JsonRpcError {
code: ErrorCode::ServerError(-32001),
message: format!("{}", source),
data: None,
},
JsonRpcServerError::MissingParameter(source) => source.clone(),
JsonRpcServerError::ParseParameter(source) => source.clone(),
ServerError::Stats(source) => JsonRpcError {
code: ErrorCode::ServerError(-32003),
message: format!("{}", source),
data: None,
},
ServerError::Serialize(source) => JsonRpcError {
code: ErrorCode::ServerError(-32000),
message: format!("{}", source),
data: None,
},
ServerError::MissingParameter(source) => source.clone(),
ServerError::ParseParameter(source) => source.clone(),
}
}
}

View File

@ -5,72 +5,332 @@
use std::env;
use std::result::Result;
use jsonrpc_core::{IoHandler, Value};
use jsonrpc_core::{Error as RpcCoreError, IoHandler, Params, Value};
use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
use log::info;
use miniserde::json;
use peach_network::network;
use peach_stats::stats;
mod error;
use crate::error::JsonRpcServerError;
mod params;
use crate::error::ServerError;
use crate::params::{Iface, IfaceId, IfaceIdPass, IfaceSsid, WiFi, WlanAndAp};
/// Create JSON-RPC I/O handler, add RPC methods and launch HTTP server.
pub fn run() -> Result<(), JsonRpcServerError> {
pub fn run() -> Result<(), ServerError> {
info!("Starting up.");
info!("Creating JSON-RPC I/O handler.");
let mut io = IoHandler::default();
io.add_sync_method("ping", |_| Ok(Value::String("success".to_string())));
io.add_method("ping", |_| async {
Ok(Value::String("success".to_string()))
});
// TODO: add blocks of methods according to provided flags
/* PEACH-NETWORK RPC METHODS */
// get - all network rpc methods for querying state
io.add_method("available_networks", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => {
match network::available_networks(&i.iface).map_err(ServerError::Network)? {
Some(list) => {
let json_list =
serde_json::to_string(&list).map_err(ServerError::Serialize)?;
Ok(Value::String(json_list))
}
// return `Null` if no networks were found
None => Ok(Value::Null),
}
}
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("id", |params: Params| async move {
let parsed: Result<IfaceSsid, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::id(&i.iface, &i.ssid).map_err(ServerError::Network)? {
Some(id) => Ok(Value::String(id)),
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("ip", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::ip(&i.iface).map_err(ServerError::Network)? {
Some(ip) => Ok(Value::String(ip)),
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("rssi", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::rssi(&i.iface).map_err(ServerError::Network)? {
Some(rssi) => Ok(Value::String(rssi)),
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("rssi_percent", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::rssi_percent(&i.iface).map_err(ServerError::Network)? {
Some(rssi) => Ok(Value::String(rssi)),
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("saved_networks", |_| async {
let list = network::saved_networks().map_err(ServerError::Network)?;
match list {
Some(list) => {
let json_list = serde_json::to_string(&list).map_err(ServerError::Serialize)?;
Ok(Value::String(json_list))
}
None => Ok(Value::Null),
}
});
io.add_method("ssid", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::ssid(&i.iface).map_err(ServerError::Network)? {
Some(ip) => Ok(Value::String(ip)),
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("state", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::state(&i.iface).map_err(ServerError::Network)? {
Some(state) => Ok(Value::String(state)),
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("status", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::status(&i.iface).map_err(ServerError::Network)? {
Some(status) => {
let json_status =
serde_json::to_string(&status).map_err(ServerError::Serialize)?;
Ok(Value::String(json_status))
}
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("traffic", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::traffic(&i.iface).map_err(ServerError::Network)? {
Some(traffic) => {
let json_traffic =
serde_json::to_string(&traffic).map_err(ServerError::Serialize)?;
Ok(Value::String(json_traffic))
}
None => Ok(Value::Null),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
// set - all network rpc methods for modifying state
io.add_method("add", |params: Params| async move {
let parsed: Result<WiFi, RpcCoreError> = params.parse();
match parsed {
Ok(w) => match network::add(&w.iface, &w.ssid, &w.pass) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("check_iface", |params: Params| async move {
let parsed: Result<WlanAndAp, RpcCoreError> = params.parse();
match parsed {
Ok(w) => match network::check_iface(&w.wlan_iface, &w.ap_iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("connect", |params: Params| async move {
let parsed: Result<IfaceId, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::connect(&i.id, &i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("delete", |params: Params| async move {
let parsed: Result<IfaceId, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::delete(&i.id, &i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("disable", |params: Params| async move {
let parsed: Result<IfaceId, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::disable(&i.id, &i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("disconnect", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::disconnect(&i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("modify", |params: Params| async move {
let parsed: Result<IfaceIdPass, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::modify(&i.iface, &i.id, &i.pass) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("reassociate", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::reassociate(&i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("reconfigure", |_| async {
match network::reconfigure() {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
}
});
io.add_method("reconnect", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::reconnect(&i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
io.add_method("save", |_| async {
match network::save() {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
}
});
io.add_method("start_iface_service", |params: Params| async move {
let parsed: Result<Iface, RpcCoreError> = params.parse();
match parsed {
Ok(i) => match network::start_iface_service(&i.iface) {
Ok(_) => Ok(Value::String("success".to_string())),
Err(e) => Err(RpcCoreError::from(ServerError::Network(e))),
},
Err(e) => Err(RpcCoreError::from(ServerError::MissingParameter(e))),
}
});
/* PEACH-STATS RPC METHODS */
io.add_sync_method("cpu_stats", move |_| {
io.add_method("cpu_stats", |_| async {
info!("Fetching CPU statistics.");
let cpu = stats::cpu_stats().map_err(JsonRpcServerError::Stats)?;
let json_cpu = json::to_string(&cpu);
let cpu = stats::cpu_stats().map_err(ServerError::Stats)?;
let json_cpu = serde_json::to_string(&cpu).map_err(ServerError::Serialize)?;
Ok(Value::String(json_cpu))
});
io.add_sync_method("cpu_stats_percent", move |_| {
io.add_method("cpu_stats_percent", |_| async {
info!("Fetching CPU statistics as percentages.");
let cpu = stats::cpu_stats_percent().map_err(JsonRpcServerError::Stats)?;
let json_cpu = json::to_string(&cpu);
let cpu = stats::cpu_stats_percent().map_err(ServerError::Stats)?;
let json_cpu = serde_json::to_string(&cpu).map_err(ServerError::Serialize)?;
Ok(Value::String(json_cpu))
});
io.add_sync_method("disk_usage", move |_| {
io.add_method("disk_usage", |_| async {
info!("Fetching disk usage statistics.");
let disks = stats::disk_usage().map_err(JsonRpcServerError::Stats)?;
let json_disks = json::to_string(&disks);
let disks = stats::disk_usage().map_err(ServerError::Stats)?;
let json_disks = serde_json::to_string(&disks).map_err(ServerError::Serialize)?;
Ok(Value::String(json_disks))
});
io.add_sync_method("load_average", move |_| {
io.add_method("load_average", |_| async {
info!("Fetching system load average statistics.");
let avg = stats::load_average().map_err(JsonRpcServerError::Stats)?;
let json_avg = json::to_string(&avg);
let avg = stats::load_average().map_err(ServerError::Stats)?;
let json_avg = serde_json::to_string(&avg).map_err(ServerError::Serialize)?;
Ok(Value::String(json_avg))
});
io.add_sync_method("mem_stats", move |_| {
io.add_method("mem_stats", |_| async {
info!("Fetching current memory statistics.");
let mem = stats::mem_stats().map_err(JsonRpcServerError::Stats)?;
let json_mem = json::to_string(&mem);
let mem = stats::mem_stats().map_err(ServerError::Stats)?;
let json_mem = serde_json::to_string(&mem).map_err(ServerError::Serialize)?;
Ok(Value::String(json_mem))
});
io.add_sync_method("uptime", move |_| {
io.add_method("uptime", |_| async {
info!("Fetching system uptime.");
let uptime = stats::uptime().map_err(JsonRpcServerError::Stats)?;
let json_uptime = json::to_string(&uptime);
let uptime = stats::uptime().map_err(ServerError::Stats)?;
let json_uptime = serde_json::to_string(&uptime).map_err(ServerError::Serialize)?;
Ok(Value::String(json_uptime))
});
@ -106,7 +366,7 @@ mod tests {
fn rpc_success() {
let rpc = {
let mut io = IoHandler::new();
io.add_sync_method("rpc_success_response", |_| {
io.add_method("rpc_success_response", |_| async {
Ok(Value::String("success".into()))
});
test_rpc::Rpc::from(io)
@ -119,13 +379,13 @@ mod tests {
fn rpc_parse_error() {
let rpc = {
let mut io = IoHandler::new();
io.add_sync_method("rpc_parse_error", |_| {
io.add_method("rpc_parse_error", |_| async {
let e = JsonRpcError {
code: ErrorCode::ParseError,
message: String::from("Parse error"),
data: None,
};
Err(JsonRpcError::from(JsonRpcServerError::MissingParameter(e)))
Err(JsonRpcError::from(ServerError::MissingParameter(e)))
});
test_rpc::Rpc::from(io)
};

View File

@ -10,12 +10,51 @@
//!
//! | Method | Description | Returns |
//! | --- | --- | --- |
//! | `ping` | Microservice status | `success` if running |
//!
//! ### Network
//!
//! Methods for **retrieving data**:
//!
//! | Method | Description | Returns |
//! | --- | --- | --- |
//! | `available_networks` | `iface` | List SSID, flags (security), frequency and signal level for all networks in range of given interface |
//! | `id` | `iface`, `ssid` | Return ID of given SSID |
//! | `ip` | `iface` | Return IP of given network interface |
//! | `rssi` | `iface` | Return average signal strength (dBm) for given interface |
//! | `rssi_percent` | `iface` | Return average signal strength (%) for given interface |
//! | `saved_networks` | | List all networks saved in wpasupplicant config |
//! | `ssid` | `iface` | Return SSID of currently-connected network for given interface |
//! | `state` | `iface` | Return state of given interface |
//! | `status` | `iface` | Return status parameters for given interface |
//! | `traffic` | `iface` | Return network traffic for given interface |
//!
//! Methods for **modifying state**:
//!
//! | Method | Parameters | Description |
//! | --- | --- | --- |
//! | `add` | `iface`, `ssid`, `pass` | Add WiFi credentials to `wpa_supplicant-<iface>.conf` |
//! | `check_iface` | `wlan_iface`, `ap_iface` | Activate WiFi access point on <ap_iface> if <wlan_iface> is active without a connection |
//! | `connect` | `id`, `iface` | Disable other networks and attempt connection with AP represented by given id |
//! | `delete` | `id`, `iface` | Remove WiFi credentials for given network id and interface |
//! | `disable` | `id`, `iface` | Disable connection with AP represented by given id |
//! | `disconnect` | `iface` | Disconnect given interface |
//! | `modify` | `id`, `iface`, `pass` | Set a new password for given network id and interface |
//! | `reassociate` | `iface` | Reassociate with current AP for given interface |
//! | `reconfigure` | | Force wpa_supplicant to re-read its configuration file |
//! | `reconnect` | `iface` | Disconnect and reconnect given interface |
//! | `save` | | Save configuration changes to `wpa_supplicant-wlan0.conf` |
//! | `start_iface_server` | `iface` | Start the `systemd` service for the given interface |
//!
//! ### System Statistics
//!
//! | Method | Description | Returns |
//! | --- | --- | --- |
//! | `cpu_stats` | CPU statistics | `user`, `system`, `nice`, `idle` |
//! | `cpu_stats_percent` | CPU statistics as percentages | `user`, `system`, `nice`, `idle` |
//! | `disk_usage` | Disk usage statistics (array of disks) | `filesystem`, `one_k_blocks`, `one_k_blocks_used`, `one_k_blocks_free`, `used_percentage`, `mountpoint` |
//! | `load_average` | Load average statistics | `one`, `five`, `fifteen` |
//! | `mem_stats` | Memory statistics | `total`, `free`, `used` |
//! | `ping` | Microservice status | `success` if running |
//! | `uptime` | System uptime | `secs` |
use std::process;

View File

@ -0,0 +1,54 @@
//! Data structures for parsing JSON-RPC method parameters.
use serde::Deserialize;
// why do we have multiple structs when we could rather combine them by using
// `Option<String>` for some fields? simply because it's easier to handle the
// parsed parameters in our json-rpc server methods. we don't have to check
// if a given field is `Some` or `None` before passing it into the relevant
// function.
/// Network interface name.
#[derive(Debug, Deserialize)]
pub struct Iface {
pub iface: String,
}
/// Network interface name and network identifier.
#[derive(Debug, Deserialize)]
pub struct IfaceId {
pub iface: String,
pub id: String,
}
/// Network interface name, network identifier and password.
#[derive(Debug, Deserialize)]
pub struct IfaceIdPass {
pub iface: String,
pub id: String,
pub pass: String,
}
/// Network interface name and network SSID.
#[derive(Debug, Deserialize)]
pub struct IfaceSsid {
pub iface: String,
pub ssid: String,
}
/// Wireless interface (for which the WiFi credentials will be added), SSID
/// and password for a wireless access point.
#[derive(Debug, Deserialize)]
pub struct WiFi {
pub iface: String,
pub ssid: String,
pub pass: String,
}
/// Wireles network interface and local Access Point network interface (the
/// interface on which we deloy or own AP).
#[derive(Debug, Deserialize)]
pub struct WlanAndAp {
pub wlan_iface: String,
pub ap_iface: String,
}

View File

@ -1,43 +1,32 @@
[package]
name = "peach-network"
version = "0.2.13"
authors = ["Andrew Reid <gnomad@cryptolab.net>"]
edition = "2018"
description = "Query and configure network interfaces using JSON-RPC over HTTP."
version = "0.4.1"
authors = ["Andrew Reid <glyph@mycelial.technology>"]
edition = "2021"
description = "Query and configure network interfaces."
homepage = "https://opencollective.com/peachcloud"
repository = "https://github.com/peachcloud/peach-network"
repository = "https://git.coopcloud.tech/PeachCloud/peach-workspace/src/branch/main/peach-network"
readme = "README.md"
license = "AGPL-3.0-only"
license = "LGPL-3.0-only"
publish = false
[package.metadata.deb]
depends = "$auto"
extended-description = """\
peach-network is a microservice to query and configure network interfaces \
using JSON-RPC over HTTP."""
maintainer-scripts="debian"
systemd-units = { unit-name = "peach-network" }
assets = [
["target/release/peach-network", "usr/bin/", "755"],
["README.md", "usr/share/doc/peach-network/README", "644"],
]
[badges]
travis-ci = { repository = "peachcloud/peach-network", branch = "master" }
maintenance = { status = "actively-developed" }
[dependencies]
env_logger = "0.6"
get_if_addrs = "0.5.3"
jsonrpc-core = "11"
jsonrpc-http-server = "11"
log = "0.4"
probes = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
miniserde = { version = "0.1.15", optional = true }
probes = "0.4.1"
serde = { version = "1.0.130", features = ["derive"], optional = true }
regex = "1"
# replace this with crate import once latest changes have been published
wpactrl = { git = "https://github.com/sauyon/wpa-ctrl-rs.git", branch = "master" }
[dev-dependencies]
jsonrpc-test = "11"
[features]
default = []
# Provide `Serialize` and `Deserialize` traits for library structs using `miniserde`
miniserde_support = ["miniserde"]
# Provide `Serialize` and `Deserialize` traits for library structs using `serde`
serde_support = ["serde"]

View File

@ -1,178 +1,46 @@
# peach-network
[![Build Status](https://travis-ci.com/peachcloud/peach-network.svg?branch=master)](https://travis-ci.com/peachcloud/peach-network) ![Generic badge](https://img.shields.io/badge/version-0.2.13-<COLOR>.svg)
![Generic badge](https://img.shields.io/badge/version-0.4.0-<COLOR>.svg)
Networking microservice module for PeachCloud. Query and configure device interfaces using [JSON-RPC](https://www.jsonrpc.org/specification) over http.
Network interface state query and modification library.
Interaction with wireless interfaces occurs primarily through the [wpactrl crate](https://docs.rs/wpactrl/0.3.1/wpactrl/) which provides "a pure-Rust lowlevel library for controlling wpasupplicant remotely". This approach is akin to using `wpa_cli` (a WPA command line client).
_Note: This module is a work-in-progress._
## API Documentation
### JSON-RPC API
API documentation can be built and served with `cargo doc --no-deps --open`. The full set of available data structures and functions is listed in the `peach_network::network` module. A custom error type (`NetworkError`) is also publically exposed for library users; it encapsulates all possible error variants.
Methods for **retrieving data**:
## Example Usage
| Method | Parameters | Description |
| --- | --- | --- |
| `available_networks` | `iface` | List SSID, flags (security), frequency and signal level for all networks in range of given interface |
| `id` | `iface`, `ssid` | Return ID of given SSID |
| `ip` | `iface` | Return IP of given network interface |
| `ping` | | Respond with `success` if microservice is running |
| `rssi` | `iface` | Return average signal strength (dBm) for given interface |
| `rssi_percent` | `iface` | Return average signal strength (%) for given interface |
| `saved_networks` | | List all networks saved in wpasupplicant config |
| `ssid` | `iface` | Return SSID of currently-connected network for given interface |
| `state` | `iface` | Return state of given interface |
| `status` | `iface` | Return status parameters for given interface |
| `traffic` | `iface` | Return network traffic for given interface |
```rust
use peach_network::{network, NetworkError};
Methods for **modifying state**:
fn main() -> Result<(), NetworkError> {
let wlan_iface = "wlan0";
| Method | Parameters | Description |
| --- | --- | --- |
| `activate_ap` | | Activate WiFi access point (start `wpa_supplicant@ap0.service`) |
| `activate_client` | | Activate WiFi client connection (start `wpa_supplicant@wlan0.service`) |
| `add` | `ssid`, `pass` | Add WiFi credentials to `wpa_supplicant-wlan0.conf` |
| `check_iface` | | Activate WiFi access point if client mode is active without a connection |
| `connect` | `id`, `iface` | Disable other networks and attempt connection with AP represented by given id |
| `delete` | `id`, `iface` | Remove WiFi credentials for given network id and interface |
| `disable` | `id`, `iface` | Disable connection with AP represented by given id |
| `disconnect` | `iface` | Disconnect given interface |
| `modify` | `id`, `iface`, `password` | Set a new password for given network id and interface |
| `reassociate` | `iface` | Reassociate with current AP for given interface |
| `reconfigure` | | Force wpa_supplicant to re-read its configuration file |
| `reconnect` | `iface` | Disconnect and reconnect given interface |
| `save` | | Save configuration changes to `wpa_supplicant-wlan0.conf` |
let wlan_ip = network::ip(wlan_iface)?;
let wlan_ssid = network::ssid(wlan_iface)?;
### API Documentation
let ssid = "Home";
let pass = "SuperSecret";
API documentation can be built and served with `cargo doc --no-deps --open`. This set of documentation is intended for developers who wish to work on the project or better understand the API of the `src/network.rs` module.
network::add(&wlan_iface, &ssid, &pass)?;
network::save()?;
### Environment
Ok(())
}
```
The JSON-RPC HTTP server address and port can be configured with the `PEACH_NETWORK_SERVER` environment variable:
## Feature Flags
`export PEACH_NETWORK_SERVER=127.0.0.1:5000`
Feature flags are used to offer `Serialize` and `Deserialize` implementations for all `struct` data types provided by this library. These traits are not provided by default. A choice of `miniserde` and `serde` is provided.
When not set, the value defaults to `127.0.0.1:5110`.
Define the desired feature in the `Cargo.toml` manifest of your project:
Logging is made available with `env_logger`:
```toml
peach-network = { version = "0.3.0", features = ["miniserde_support"] }
```
`export RUST_LOG=info`
## License
Other logging levels include `debug`, `warn` and `error`.
### Setup
Clone this repo:
`git clone https://github.com/peachcloud/peach-network.git`
Move into the repo and compile:
`cd peach-network`
`cargo build --release`
Run the binary (sudo needed to satisfy permission requirements):
`sudo ./target/release/peach-network`
### Debian Packaging
A `systemd` service file and Debian maintainer scripts are included in the `debian` directory, allowing `peach-network` to be easily bundled as a Debian package (`.deb`). The `cargo-deb` [crate](https://crates.io/crates/cargo-deb) can be used to achieve this.
Install `cargo-deb`:
`cargo install cargo-deb`
Move into the repo:
`cd peach-network`
Build the package:
`cargo deb`
The output will be written to `target/debian/peach-network_0.2.4_arm64.deb` (or similar).
Build the package (aarch64):
`cargo deb --target aarch64-unknown-linux-gnu`
Install the package as follows:
`sudo dpkg -i target/debian/peach-network_0.2.4_arm64.deb`
The service will be automatically enabled and started.
Uninstall the service:
`sudo apt-get remove peach-network`
Remove configuration files (not removed with `apt-get remove`):
`sudo apt-get purge peach-network`
### Example Usage
**Retrieve IP address for wlan0**
With microservice running, open a second terminal window and use `curl` to call server methods:
`curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "ip", "params" : {"iface": "wlan0" }, "id":1 }' 127.0.0.1:5110`
Server responds with:
`{"jsonrpc":"2.0","result":"192.168.1.21","id":1}`
**Retrieve SSID of connected access point for wlan1**
`curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "ssid", "params" : {"iface": "wlan1" }, "id":1 }' 127.0.0.1:5110`
Server response when interface is connected:
`{"jsonrpc":"2.0","result":"Home","id":1}`
Server response when interface is not connected:
`{"jsonrpc":"2.0","error":{"code":-32003,"message":"Failed to retrieve SSID for wlan1. Interface may not be connected."},"id":1}`
**Retrieve list of SSIDs for all networks in range of wlan0**
`curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "available_networks", "params" : {"iface": "wlan0" }, "id":1 }' 127.0.0.1:5110`
Server response when interface is connected:
`{"jsonrpc":"2.0","result":"[{\"frequency\":\"2412\",\"signal_level\":\"-72\",\"ssid\":\"Home\",\"flags\":\"[WPA2-PSK-CCMP][ESS]\"},{\"frequency\":\"2472\",\"signal_level\":\"-56\",\"ssid\":\"podetium\",\"flags\":\"[WPA2-PSK-CCMP+TKIP][ESS]\"}]","id":1}`
Server response when interface is not connected:
`{"jsonrpc":"2.0","error":{"code":-32006,"message":"No networks found in range of wlan0"},"id":1}`
**Retrieve network traffic statistics for wlan1**
`curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "traffic", "params" : {"iface": "wlan1" }, "id":1 }' 127.0.0.1:5110`
Server response if interface exists:
`{"jsonrpc":"2.0","result":"{\"received\":26396361,\"transmitted\":22352530}","id":1}`
Server response when interface is not found:
`{"jsonrpc":"2.0","error":{"code":-32004,"message":"Failed to retrieve network traffic for wlan3. Interface may not be connected"},"id":1}`
**Retrieve status information for wlan0**
`curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "status", "params" : {"iface": "wlan0" }, "id":1 }' 127.0.0.1:5110`
Server response if interface exists:
`{"jsonrpc":"2.0","result":"{\"address\":\"b8:27:eb:9b:5d:5f\",\"bssid\":\"f4:8c:eb:cd:31:81\",\"freq\":\"2412\",\"group_cipher\":\"CCMP\",\"id\":\"0\",\"ip_address\":\"192.168.0.162\",\"key_mgmt\":\"WPA2-PSK\",\"mode\":\"station\",\"pairwise_cipher\":\"CCMP\",\"ssid\":\"Home\",\"wpa_state\":\"COMPLETED\"}","id":1}`
Server response when interface is not found:
`{"jsonrpc":"2.0","error":{"code":-32013,"message":"Failed to open control interface for wpasupplicant: No such file or directory (os error 2)"},"id":1}`
### Licensing
AGPL-3.0
LGPL-3.0.

View File

@ -1,13 +0,0 @@
[Unit]
Description=Query and configure network interfaces using JSON-RPC over HTTP.
[Service]
Type=simple
User=root
Group=netdev
Environment="RUST_LOG=error"
ExecStart=/usr/bin/peach-network
Restart=always
[Install]
WantedBy=multi-user.target

View File

@ -1,81 +1,169 @@
//! Custom error type for `peach-network`.
use std::io;
use std::num::ParseIntError;
use io::Error as IoError;
use jsonrpc_core::{types::error::Error as JsonRpcError, ErrorCode};
use probes::ProbeError;
use regex::Error as RegexError;
use serde_json::Error as SerdeError;
use wpactrl::WpaError;
/// Custom error type encapsulating all possible errors when querying
/// network interfaces and modifying their state.
#[derive(Debug)]
pub enum NetworkError {
Add { ssid: String },
NoState { iface: String, source: IoError },
Disable { id: String, iface: String },
Disconnect { iface: String },
GenWpaPassphrase { ssid: String, source: IoError },
GenWpaPassphraseWarning { ssid: String, err_msg: String },
Id { ssid: String, iface: String },
NoIp { iface: String, source: IoError },
Rssi { iface: String },
RssiPercent { iface: String },
Ssid { iface: String },
State { iface: String },
Status { iface: String },
Traffic { iface: String },
/// Failed to add network.
Add {
/// SSID.
ssid: String,
},
/// Failed to retrieve network state.
NoState {
/// Interface.
iface: String,
/// Underlying error source.
source: IoError,
},
/// Failed to disable network.
Disable {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to disconnect interface.
Disconnect {
/// Interface.
iface: String,
},
/// Failed to execute wpa_passphrase command.
GenWpaPassphrase {
/// SSID.
ssid: String,
/// Underlying error source.
source: IoError,
},
/// Failed to successfully generate wpa passphrase.
GenWpaPassphraseWarning {
/// SSID.
ssid: String,
/// Error message describing context.
err_msg: String,
},
/// Failed to retrieve ID for the given SSID and interface.
Id {
/// SSID.
ssid: String,
/// Interface.
iface: String,
},
/// Failed to retrieve IP address.
NoIp {
/// Inteface.
iface: String,
/// Underlying error source.
source: IoError,
},
/// Failed to retrieve RSSI.
Rssi {
/// Interface.
iface: String,
},
/// Failed to retrieve signal quality (%).
RssiPercent {
/// Interface.
iface: String,
},
/// Failed to retrieve SSID.
Ssid {
/// Interface.
iface: String,
},
/// Failed to retrieve state.
State {
/// Interface.
iface: String,
},
/// Failed to retrieve status.
Status {
/// Interface.
iface: String,
},
/// Failed to retieve network traffic.
Traffic {
/// Interface.
iface: String,
},
/// No saved network found for the default interface.
SavedNetworks,
AvailableNetworks { iface: String },
MissingParams(JsonRpcError),
Modify { id: String, iface: String },
Ip { iface: String },
/// No networks found in range.
AvailableNetworks {
/// Interface.
iface: String,
},
/// Failed to set new password.
Modify {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to retrieve IP address.
Ip {
/// Interface.
iface: String,
},
/// Failed to parse integer from string.
ParseInt(ParseIntError),
NoTraffic { iface: String, source: ProbeError },
Reassociate { iface: String },
/// Failed to retrieve network traffic measurement.
NoTraffic {
/// Interface.
iface: String,
/// Underlying error source.
source: ProbeError,
},
/// Failed to reassociate with WiFi network.
Reassociate {
/// Interface.
iface: String,
},
/// Failed to force reread of wpa_supplicant configuration file.
Reconfigure,
Reconnect { iface: String },
/// Failed to reconnect with WiFi network.
Reconnect {
/// Interface.
iface: String,
},
/// Failed to execute Regex command.
Regex(RegexError),
Delete { id: String, iface: String },
/// Failed to delete network.
Delete {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to retrieve state of wlan0 service.
WlanState(IoError),
/// Failed to retrieve connection state of wlan0 interface.
WlanOperstate(IoError),
/// Failed to save wpa_supplicant configuration changes to file.
Save,
Connect { id: String, iface: String },
StartAp0(IoError),
StartWlan0(IoError),
SerdeSerialize(SerdeError),
/// Failed to connect to network.
Connect {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to start systemctl service for a network interface.
StartInterface {
/// Underlying error source.
source: IoError,
/// Interface.
iface: String,
},
/// Failed to execute wpa-ctrl command.
WpaCtrl(WpaError),
}
@ -98,7 +186,6 @@ impl std::error::Error for NetworkError {
NetworkError::Traffic { .. } => None,
NetworkError::SavedNetworks => None,
NetworkError::AvailableNetworks { .. } => None,
NetworkError::MissingParams(ref source) => Some(source),
NetworkError::Modify { .. } => None,
NetworkError::Ip { .. } => None,
NetworkError::ParseInt(ref source) => Some(source),
@ -112,9 +199,7 @@ impl std::error::Error for NetworkError {
NetworkError::WlanOperstate(ref source) => Some(source),
NetworkError::Save => None,
NetworkError::Connect { .. } => None,
NetworkError::StartWlan0(ref source) => Some(source),
NetworkError::StartAp0(ref source) => Some(source),
NetworkError::SerdeSerialize(ref source) => Some(source),
NetworkError::StartInterface { ref source, .. } => Some(source),
NetworkError::WpaCtrl(ref source) => Some(source),
}
}
@ -181,20 +266,13 @@ impl std::fmt::Display for NetworkError {
write!(f, "No status found for interface: {}", iface)
}
NetworkError::Traffic { ref iface } => {
write!(
f,
"Could not find network traffice for interface: {}",
iface
)
write!(f, "Could not find network traffic for interface: {}", iface)
}
NetworkError::SavedNetworks => {
write!(f, "No saved networks found for default interface")
}
NetworkError::AvailableNetworks { ref iface } => {
write!(f, "No networks found in range of interface: {}", iface)
}
NetworkError::MissingParams(ref source) => {
write!(f, "Missing expected parameters: {}", source)
write!(f, "No networks found in range of interface: {}", iface)
}
NetworkError::Modify { ref id, ref iface } => {
write!(
@ -256,20 +334,16 @@ impl std::fmt::Display for NetworkError {
id, iface
)
}
NetworkError::StartWlan0(_) => write!(f, "Failed to start ap0 service"),
NetworkError::StartAp0(_) => write!(f, "Failed to start wlan0 service"),
NetworkError::SerdeSerialize(_) => write!(f, "JSON serialization failed"),
NetworkError::StartInterface { ref iface, .. } => write!(
f,
"Failed to start systemctl service for {} interface",
iface
),
NetworkError::WpaCtrl(_) => write!(f, "WpaCtrl command failed"),
}
}
}
impl From<SerdeError> for NetworkError {
fn from(err: SerdeError) -> Self {
NetworkError::SerdeSerialize(err)
}
}
impl From<WpaError> for NetworkError {
fn from(err: WpaError) -> Self {
NetworkError::WpaCtrl(err)
@ -287,203 +361,3 @@ impl From<RegexError> for NetworkError {
NetworkError::Regex(err)
}
}
impl From<NetworkError> for JsonRpcError {
fn from(err: NetworkError) -> Self {
match &err {
NetworkError::Add { ssid } => JsonRpcError {
code: ErrorCode::ServerError(-32000),
message: format!("Failed to add network for {}", ssid),
data: None,
},
NetworkError::NoState { iface, source } => JsonRpcError {
code: ErrorCode::ServerError(-32022),
message: format!(
"Failed to retrieve interface state for {}: {}",
iface, source
),
data: None,
},
NetworkError::Disable { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32029),
message: format!("Failed to disable network {} for {}", id, iface),
data: None,
},
NetworkError::Disconnect { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32032),
message: format!("Failed to disconnect {}", iface),
data: None,
},
NetworkError::GenWpaPassphrase { ssid, source } => JsonRpcError {
code: ErrorCode::ServerError(-32025),
message: format!("Failed to generate wpa passphrase for {}: {}", ssid, source),
data: None,
},
NetworkError::GenWpaPassphraseWarning { ssid, err_msg } => JsonRpcError {
code: ErrorCode::ServerError(-32036),
message: format!(
"Failed to generate wpa passphrase for {}: {}",
ssid, err_msg
),
data: None,
},
NetworkError::Id { iface, ssid } => JsonRpcError {
code: ErrorCode::ServerError(-32026),
message: format!("No ID found for {} on interface {}", ssid, iface),
data: None,
},
NetworkError::NoIp { iface, source } => JsonRpcError {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve IP address for {}: {}", iface, source),
data: None,
},
NetworkError::Rssi { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32002),
message: format!(
"Failed to retrieve RSSI for {}. Interface may not be connected",
iface
),
data: None,
},
NetworkError::RssiPercent { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32034),
message: format!(
"Failed to retrieve signal quality (%) for {}. Interface may not be connected",
iface
),
data: None,
},
NetworkError::Ssid { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32003),
message: format!(
"Failed to retrieve SSID for {}. Interface may not be connected",
iface
),
data: None,
},
NetworkError::State { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32023),
message: format!("No state found for {}. Interface may not exist", iface),
data: None,
},
NetworkError::Status { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32024),
message: format!("No status found for {}. Interface may not exist", iface),
data: None,
},
NetworkError::Traffic { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32004),
message: format!(
"No network traffic statistics found for {}. Interface may not exist",
iface
),
data: None,
},
NetworkError::SavedNetworks => JsonRpcError {
code: ErrorCode::ServerError(-32005),
message: "No saved networks found".to_string(),
data: None,
},
NetworkError::AvailableNetworks { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32006),
message: format!("No networks found in range of {}", iface),
data: None,
},
NetworkError::MissingParams(e) => e.clone(),
NetworkError::Modify { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32033),
message: format!("Failed to set new password for network {} on {}", id, iface),
data: None,
},
NetworkError::Ip { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32007),
message: format!("No IP address found for {}", iface),
data: None,
},
NetworkError::ParseInt(source) => JsonRpcError {
code: ErrorCode::ServerError(-32035),
message: format!(
"Failed to parse integer from string for RSSI value: {}",
source
),
data: None,
},
NetworkError::NoTraffic { iface, source } => JsonRpcError {
code: ErrorCode::ServerError(-32015),
message: format!(
"Failed to retrieve network traffic statistics for {}: {}",
iface, source
),
data: None,
},
NetworkError::Reassociate { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32008),
message: format!("Failed to reassociate with WiFi network for {}", iface),
data: None,
},
NetworkError::Reconfigure => JsonRpcError {
code: ErrorCode::ServerError(-32030),
message: "Failed to force reread of wpa_supplicant configuration file".to_string(),
data: None,
},
NetworkError::Reconnect { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32009),
message: format!("Failed to reconnect with WiFi network for {}", iface),
data: None,
},
NetworkError::Regex(source) => JsonRpcError {
code: ErrorCode::ServerError(-32010),
message: format!("Regex command error: {}", source),
data: None,
},
NetworkError::Delete { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32028),
message: format!("Failed to delete network {} for {}", id, iface),
data: None,
},
NetworkError::WlanState(source) => JsonRpcError {
code: ErrorCode::ServerError(-32011),
message: format!("Failed to retrieve state of wlan0 service: {}", source),
data: None,
},
NetworkError::WlanOperstate(source) => JsonRpcError {
code: ErrorCode::ServerError(-32021),
message: format!(
"Failed to retrieve connection state of wlan0 interface: {}",
source
),
data: None,
},
NetworkError::Save => JsonRpcError {
code: ErrorCode::ServerError(-32031),
message: "Failed to save configuration changes to file".to_string(),
data: None,
},
NetworkError::Connect { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32027),
message: format!("Failed to connect to network {} for {}", id, iface),
data: None,
},
NetworkError::StartAp0(source) => JsonRpcError {
code: ErrorCode::ServerError(-32016),
message: format!("Failed to start ap0 service: {}", source),
data: None,
},
NetworkError::StartWlan0(source) => JsonRpcError {
code: ErrorCode::ServerError(-32018),
message: format!("Failed to start wlan0 service: {}", source),
data: None,
},
NetworkError::SerdeSerialize(source) => JsonRpcError {
code: ErrorCode::ServerError(-32012),
message: format!("JSON serialization failed: {}", source),
data: None,
},
NetworkError::WpaCtrl(source) => JsonRpcError {
code: ErrorCode::ServerError(-32013),
message: format!("WPA control interface failure: {}", source),
data: None,
},
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +0,0 @@
use std::process;
use log::error;
fn main() {
// initalize the logger
env_logger::init();
// handle errors returned from `run`
if let Err(e) = peach_network::run() {
error!("Application error: {}", e);
process::exit(1);
}
}

View File

@ -1,6 +1,6 @@
//! Retrieve network data and modify interface state.
//!
//! This module contains the core logic of the `peach-network` microservice and
//! This module contains the core logic of the `peach-network` and
//! provides convenience wrappers for a range of `wpasupplicant` commands,
//! many of which are ordinarily executed using `wpa_cli` (a WPA command line
//! client).
@ -11,8 +11,8 @@
//! Switching between client mode and access point mode is achieved by making
//! system calls to systemd (via `systemctl`). Further networking functionality
//! is provided by making system calls to retrieve interface state and write
//! access point credentials to `wpa_supplicant-wlan0.conf`.
//!
//! access point credentials to `wpa_supplicant-<wlan_iface>.conf`.
use std::{
fs::OpenOptions,
io::prelude::*,
@ -21,68 +21,58 @@ use std::{
str,
};
use crate::error::NetworkError;
use probes::network;
#[cfg(feature = "miniserde_support")]
use miniserde::{Deserialize, Serialize};
#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};
use crate::error::NetworkError;
use crate::utils;
/// Network interface name.
#[derive(Debug, Deserialize)]
pub struct Iface {
pub iface: String,
}
/// Network interface name and network identifier.
#[derive(Debug, Deserialize)]
pub struct IfaceId {
pub iface: String,
pub id: String,
}
/// Network interface name, network identifier and password.
#[derive(Debug, Deserialize)]
pub struct IfaceIdPass {
pub iface: String,
pub id: String,
pub pass: String,
}
/// Network interface name and network SSID.
#[derive(Debug, Deserialize)]
pub struct IfaceSsid {
pub iface: String,
pub ssid: String,
}
/// Network SSID.
#[derive(Debug, Serialize)]
pub struct Network {
pub ssid: String,
}
/// Access point data retrieved via scan.
#[derive(Debug, Serialize)]
#[derive(Debug)]
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Scan {
/// Frequency.
pub frequency: String,
/// Protocol.
pub protocol: String,
/// Signal strength.
pub signal_level: String,
/// SSID.
pub ssid: String,
}
/// Status data for a network interface.
#[derive(Debug, Serialize)]
#[derive(Debug)]
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Status {
/// MAC address.
pub address: Option<String>,
/// Basic Service Set Identifier (BSSID).
pub bssid: Option<String>,
/// Frequency.
pub freq: Option<String>,
/// Group cipher.
pub group_cipher: Option<String>,
/// Local ID.
pub id: Option<String>,
/// IP address.
pub ip_address: Option<String>,
/// Key management.
pub key_mgmt: Option<String>,
/// Mode.
pub mode: Option<String>,
/// Pairwise cipher.
pub pairwise_cipher: Option<String>,
/// SSID.
pub ssid: Option<String>,
/// WPA state.
pub wpa_state: Option<String>,
}
@ -105,24 +95,16 @@ impl Status {
}
/// Received and transmitted network traffic (bytes).
#[derive(Debug, Serialize)]
#[derive(Debug)]
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Traffic {
/// Total bytes received.
pub received: u64,
/// Total bytes transmitted.
pub transmitted: u64,
}
/// SSID and password for a wireless access point.
#[derive(Debug, Deserialize)]
pub struct WiFi {
pub ssid: String,
pub pass: String,
}
// TODO: wrap this into a helper function:
//
// let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
// let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
/* GET - Methods for retrieving data */
/// Retrieve list of available wireless access points for a given network
@ -133,15 +115,11 @@ pub struct WiFi {
/// * `iface` - A string slice holding the name of a wireless network interface
///
/// If the scan results include one or more access points for the given network
/// interface, an `Ok` `Result` type is returned containing `Some(String)` -
/// where `String` is a serialized vector of `Scan` structs containing
/// data for the in-range access points. If no access points are found,
/// a `None` type is returned in the `Result`. In the event of an error, a
/// `NetworkError` is returned in the `Result`. The `NetworkError` is then
/// enumerated to a specific error type and an appropriate JSON RPC response is
/// sent to the caller.
///
pub fn available_networks(iface: &str) -> Result<Option<String>, NetworkError> {
/// interface, an `Ok` `Result` type is returned containing `Some(Vec<Scan>)`.
/// The vector of `Scan` structs contains data for the in-range access points.
/// If no access points 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 available_networks(iface: &str) -> Result<Option<Vec<Scan>>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
wpa.request("SCAN")?;
@ -176,8 +154,7 @@ pub fn available_networks(iface: &str) -> Result<Option<String>, NetworkError> {
if scan.is_empty() {
Ok(None)
} else {
let results = serde_json::to_string(&scan)?;
Ok(Some(results))
Ok(Some(scan))
}
}
@ -193,10 +170,7 @@ pub fn available_networks(iface: &str) -> Result<Option<String>, NetworkError> {
/// found in the list of saved networks, an `Ok` `Result` type is returned
/// containing `Some(String)` - where `String` is the network identifier.
/// If no match is found, a `None` type is returned in the `Result`. In the
/// event of an error, a `NetworkError` is returned in the `Result`. The
/// `NetworkError` is then enumerated to a specific error type and an
/// appropriate JSON RPC response is sent to the caller.
///
/// event of an error, a `NetworkError` is returned in the `Result`.
pub fn id(iface: &str, ssid: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -228,10 +202,7 @@ pub fn id(iface: &str, ssid: &str) -> Result<Option<String>, NetworkError> {
/// an `Ok` `Result` type is returned containing `Some(String)` - where `String`
/// is the IP address of the interface. If no match is found, a `None` type is
/// returned in the `Result`. In the event of an error, a `NetworkError` is
/// returned in the `Result`. The `NetworkError` is then enumerated to a
/// specific error type and an appropriate JSON RPC response is sent to the
/// caller.
///
/// returned in the `Result`.
pub fn ip(iface: &str) -> Result<Option<String>, NetworkError> {
let net_if: String = iface.to_string();
let ifaces = get_if_addrs::get_if_addrs().map_err(|source| NetworkError::NoIp {
@ -258,9 +229,7 @@ pub fn ip(iface: &str) -> Result<Option<String>, NetworkError> {
/// is the RSSI (Received Signal Strength Indicator) of the connection measured
/// in dBm. If signal strength is not found, a `None` type is returned in the
/// `Result`. In the event of an error, a `NetworkError` is returned in the
/// `Result`. The `NetworkError` is then enumerated to a specific error type and
/// an appropriate JSON RPC response is sent to the caller.
///
/// `Result`.
pub fn rssi(iface: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -287,9 +256,7 @@ pub fn rssi(iface: &str) -> Result<Option<String>, NetworkError> {
/// is the RSSI (Received Signal Strength Indicator) of the connection measured
/// as a percentage. If signal strength is not found, a `None` type is returned
/// in the `Result`. In the event of an error, a `NetworkError` is returned in
/// the `Result`. The `NetworkError` is then enumerated to a specific error type
/// and an appropriate JSON RPC response is sent to the caller.
///
/// the `Result`.
pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -319,14 +286,11 @@ pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> {
///
/// If the wpasupplicant configuration file contains credentials for one or
/// more access points, an `Ok` `Result` type is returned containing
/// `Some(String)` - where `String` is a serialized vector of `Network` structs
/// containing the SSIDs 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`. The `NetworkError` is then
/// enumerated to a specific error type and an appropriate JSON RPC response is
/// sent to the caller.
///
pub fn saved_networks() -> Result<Option<String>, NetworkError> {
/// `Some(Vec<Network>)`. The vector of `Network` structs contains the SSIDs
/// 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<String>>, NetworkError> {
let mut wpa = wpactrl::WpaCtrl::builder().open()?;
let networks = wpa.request("LIST_NETWORKS")?;
let mut ssids = Vec::new();
@ -335,16 +299,14 @@ pub fn saved_networks() -> Result<Option<String>, 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)
}
}
if ssids.is_empty() {
Ok(None)
} else {
let results = serde_json::to_string(&ssids)?;
Ok(Some(results))
Ok(Some(ssids))
}
}
@ -358,10 +320,7 @@ pub fn saved_networks() -> Result<Option<String>, NetworkError> {
/// an `Ok` `Result` type is returned containing `Some(String)` - where `String`
/// is the SSID of the associated network. If SSID is not found, a `None` type
/// is returned in the `Result`. In the event of an error, a `NetworkError` is
/// returned in the `Result`. The `NetworkError` is then enumerated to a
/// specific error type and an appropriate JSON RPC response is sent to the
/// caller.
///
/// returned in the `Result`.
pub fn ssid(iface: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -383,9 +342,7 @@ pub fn ssid(iface: &str) -> Result<Option<String>, NetworkError> {
/// returned containing `Some(String)` - where `String` is the state of the
/// network interface. If state is not found, a `None` type is returned in the
/// `Result`. In the event of an error, a `NetworkError` is returned in the
/// `Result`. The `NetworkError` is then enumerated to a specific error type and
/// an appropriate JSON RPC response is sent to the caller.
///
/// `Result`.
pub fn state(iface: &str) -> Result<Option<String>, NetworkError> {
// construct the interface operstate path
let iface_path: String = format!("/sys/class/net/{}/operstate", iface);
@ -419,10 +376,7 @@ pub fn state(iface: &str) -> Result<Option<String>, NetworkError> {
/// returned containing `Some(Status)` - where `Status` is a `struct`
/// containing the aggregated interface data in named fields. If status is not
/// found, a `None` type is returned in the `Result`. In the event of an error,
/// a `NetworkError` is returned in the `Result`. The `NetworkError` is then
/// enumerated to a specific error type and an appropriate JSON RPC response is
/// sent to the caller.
///
/// a `NetworkError` is returned in the `Result`.
pub fn status(iface: &str) -> Result<Option<Status>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -475,15 +429,12 @@ pub fn status(iface: &str) -> Result<Option<Status>, NetworkError> {
/// * `iface` - A string slice holding the name of a wireless network interface
///
/// If the network traffic statistics are found for the given interface, an `Ok`
/// `Result` type is returned containing `Some(String)` - where `String` is a
/// serialized `Traffic` `struct` with fields for received and transmitted
/// network data statistics. If network traffic statistics are not found for the
/// given interface, a `None` type is returned in the `Result`. In the event of
/// an error, a `NetworkError` is returned in the `Result`. The `NetworkError`
/// is then enumerated to a specific error type and an appropriate JSON RPC
/// response is sent to the caller.
///
pub fn traffic(iface: &str) -> Result<Option<String>, NetworkError> {
/// `Result` type is returned containing `Some(Traffic)`. The `Traffic` `struct`
/// includes fields for received and transmitted network data statistics. If
/// network traffic statistics are not found for the given interface, a `None`
/// type is returned in the `Result`. In the event of an error, a `NetworkError`
/// is returned in the `Result`.
pub fn traffic(iface: &str) -> Result<Option<Traffic>, NetworkError> {
let network = network::read().map_err(|source| NetworkError::NoTraffic {
iface: iface.to_string(),
source,
@ -497,9 +448,7 @@ pub fn traffic(iface: &str) -> Result<Option<String>, NetworkError> {
received,
transmitted,
};
// TODO: add test for SerdeSerialize error
let t = serde_json::to_string(&traffic)?;
return Ok(Some(t));
return Ok(Some(traffic));
}
}
@ -508,42 +457,25 @@ pub fn traffic(iface: &str) -> Result<Option<String>, NetworkError> {
/* SET - Methods for modifying state */
/// Activate wireless access point.
/// Start network interface service.
///
/// A `systemctl `command is invoked which starts the `ap0` interface service.
/// If the command executes successfully, an `Ok` `Result` type is returned.
/// In the event of an error, a `NetworkError` is returned in the `Result`.
/// The `NetworkError` is then enumerated to a specific error type and an
/// appropriate JSON RPC response is sent to the caller.
///
pub fn activate_ap() -> Result<(), NetworkError> {
// start the ap0 interface service
/// A `systemctl `command is invoked which starts the service for the given
/// 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: &str) -> Result<(), NetworkError> {
let iface_service = format!("wpa_supplicant@{}.service", &iface);
// start the interface service
Command::new("sudo")
.arg("/usr/bin/systemctl")
.arg("start")
.arg("wpa_supplicant@ap0.service")
.arg(iface_service)
.output()
.map_err(NetworkError::StartAp0)?;
Ok(())
}
/// Activate wireless client.
///
/// A `systemctl` command is invoked which starts the `wlan0` interface service.
/// If the command executes successfully, an `Ok` `Result` type is returned.
/// In the event of an error, a `NetworkError` is returned in the `Result`.
/// The `NetworkError` is then enumerated to a specific error type and an
/// appropriate JSON RPC response is sent to the caller.
///
pub fn activate_client() -> Result<(), NetworkError> {
// start the wlan0 interface service
Command::new("sudo")
.arg("/usr/bin/systemctl")
.arg("start")
.arg("wpa_supplicant@wlan0.service")
.output()
.map_err(NetworkError::StartWlan0)?;
.map_err(|source| NetworkError::StartInterface {
source,
iface: iface.to_string(),
})?;
Ok(())
}
@ -552,24 +484,23 @@ pub fn activate_client() -> Result<(), NetworkError> {
///
/// # Arguments
///
/// * `wlan_iface` - A local wireless interface.
/// * `wifi` - An instance of the `WiFi` `struct` with fields `ssid` and `pass`
///
/// If configuration parameters are successfully generated from the provided
/// SSID and password and appended to `wpa_supplicant-wlan0.conf`, an `Ok`
/// `Result` type is returned. In the event of an error, a `NetworkError` is
/// returned in the `Result`. The `NetworkError` is then enumerated to a
/// specific error type and an appropriate JSON RPC response is sent to the
/// caller.
///
pub fn add(wifi: &WiFi) -> Result<(), NetworkError> {
/// SSID and password and appended to `wpa_supplicant-<wlan_iface>.conf` (where
/// `<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: &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,
})?;
@ -577,12 +508,12 @@ pub fn add(wifi: &WiFi) -> Result<(), NetworkError> {
let mut wpa_details = "\n".as_bytes().to_vec();
wpa_details.extend(&*(output.stdout));
// append wpa_passphrase output to wpa_supplicant-wlan0.conf if successful
let wlan_config = format!("/etc/wpa_supplicant/wpa_supplicant-{}.conf", wlan_iface);
// append wpa_passphrase output to wpa_supplicant-<wlan_iface>.conf if successful
if output.status.success() {
// open file in append mode
let file = OpenOptions::new()
.append(true)
.open("/etc/wpa_supplicant/wpa_supplicant-wlan0.conf");
let file = OpenOptions::new().append(true).open(wlan_config);
let _file = match file {
// if file exists & open succeeds, write wifi configuration
@ -596,40 +527,41 @@ pub fn add(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(),
})
}
}
/// Deploy the access point if the `wlan0` interface is `up` without an active
/// Deploy an access point if the wireless interface is `up` without an active
/// connection.
///
/// The status of the `wlan0` service and the state of the `wlan0` interface
/// The status of the wireless service and the state of the wireless interface
/// 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() -> 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
let wlan0_status = Command::new("/usr/bin/systemctl")
let wlan_status = Command::new("/usr/bin/systemctl")
.arg("is-active")
.arg("wpa_supplicant@wlan0.service")
.arg(wpa_service)
.status()
.map_err(NetworkError::WlanState)?;
// returns the current state of the wlan0 interface
let iface_state = state("wlan0")?;
// returns the current state of the wlan interface
let iface_state = state(wlan_iface)?;
// returns down if the interface is not currently connected to an ap
let wlan0_state = match iface_state {
let wlan_state = match iface_state {
Some(state) => state,
None => "error".to_string(),
};
// if wlan0 is active but not connected, start the ap0 service
if wlan0_status.success() && wlan0_state == "down" {
activate_ap()?
// if wlan is active but not connected, start the ap service
if wlan_status.success() && wlan_state == "down" {
start_iface_service(ap_iface)?
}
Ok(())
@ -646,10 +578,7 @@ pub fn check_iface() -> Result<(), NetworkError> {
/// If the network connection is successfully activated for the access point
/// represented by the given network identifier on the given wireless interface,
/// an `Ok` `Result`type is returned. In the event of an error, a `NetworkError`
/// is returned in the `Result`. The `NetworkError` is then enumerated to a
/// specific error type and an appropriate JSON RPC response is sent to the
/// caller.
///
/// is returned in the `Result`.
pub fn connect(id: &str, iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -668,10 +597,7 @@ pub fn connect(id: &str, iface: &str) -> Result<(), NetworkError> {
/// If the network configuration parameters are successfully deleted for
/// the access point represented by the given network identifier, an `Ok`
/// `Result`type is returned. In the event of an error, a `NetworkError` is
/// returned in the `Result`. The `NetworkError` is then enumerated to a
/// specific error type and an appropriate JSON RPC response is sent to the
/// caller.
///
/// returned in the `Result`.
pub fn delete(id: &str, iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -690,9 +616,7 @@ pub fn delete(id: &str, iface: &str) -> Result<(), NetworkError> {
/// If the network connection is successfully disabled for the access point
/// represented by the given network identifier, an `Ok` `Result`type is
/// returned. In the event of an error, a `NetworkError` is returned in the
/// `Result`. The `NetworkError` is then enumerated to a specific error type and
/// an appropriate JSON RPC response is sent to the caller.
///
/// `Result`.
pub fn disable(id: &str, iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -709,10 +633,7 @@ pub fn disable(id: &str, iface: &str) -> Result<(), NetworkError> {
///
/// If the network connection is successfully disconnected for the given
/// wireless interface, an `Ok` `Result` type is returned. In the event of an
/// error, a `NetworkError` is returned in the `Result`. The `NetworkError` is
/// then enumerated to a specific error type and an appropriate JSON RPC
/// response is sent to the caller.
///
/// error, a `NetworkError` is returned in the `Result`.
pub fn disconnect(iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -731,10 +652,7 @@ pub fn disconnect(iface: &str) -> Result<(), NetworkError> {
///
/// If the password is successfully updated for the access point represented by
/// the given network identifier, an `Ok` `Result` type is returned. In the
/// event of an error, a `NetworkError` is returned in the `Result`. The
/// `NetworkError` is then enumerated to a specific error type and an
/// appropriate JSON RPC response is sent to the caller.
///
/// event of an error, a `NetworkError` is returned in the `Result`.
pub fn modify(id: &str, iface: &str, pass: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -751,10 +669,7 @@ pub fn modify(id: &str, iface: &str, pass: &str) -> Result<(), NetworkError> {
///
/// If the network connection is successfully reassociated for the given
/// wireless interface, an `Ok` `Result` type is returned. In the event of an
/// error, a `NetworkError` is returned in the `Result`. The `NetworkError` is
/// then enumerated to a specific error type and an appropriate JSON RPC
/// response is sent to the caller.
///
/// error, a `NetworkError` is returned in the `Result`.
pub fn reassociate(iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -767,10 +682,7 @@ pub fn reassociate(iface: &str) -> Result<(), NetworkError> {
/// If the reconfigure command is successfully executed, indicating a reread
/// of the `wpa_supplicant.conf` file by the `wpa_supplicant` process, an `Ok`
/// `Result` type is returned. In the event of an error, a `NetworkError` is
/// returned in the `Result`. The `NetworkError` is then enumerated to a
/// specific error type and an appropriate JSON RPC response is sent to the
/// caller.
///
/// returned in the `Result`.
pub fn reconfigure() -> Result<(), NetworkError> {
let mut wpa = wpactrl::WpaCtrl::builder().open()?;
wpa.request("RECONFIGURE")?;
@ -785,10 +697,7 @@ pub fn reconfigure() -> Result<(), NetworkError> {
///
/// If the network connection is successfully disconnected and reconnected for
/// the given wireless interface, an `Ok` `Result` type is returned. In the
/// event of an error, a `NetworkError` is returned in the `Result`. The
/// `NetworkError` is then enumerated to a specific error type and an
/// appropriate JSON RPC response is sent to the caller.
///
/// event of an error, a `NetworkError` is returned in the `Result`.
pub fn reconnect(iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?;
@ -801,10 +710,7 @@ pub fn reconnect(iface: &str) -> Result<(), NetworkError> {
///
/// If wireless network configuration updates are successfully save to the
/// `wpa_supplicant.conf` file, an `Ok` `Result` type is returned. In the
/// event of an error, a `NetworkError` is returned in the `Result`. The
/// `NetworkError` is then enumerated to a specific error type and an
/// appropriate JSON RPC response is sent to the caller.
///
/// event of an error, a `NetworkError` is returned in the `Result`.
pub fn save() -> Result<(), NetworkError> {
let mut wpa = wpactrl::WpaCtrl::builder().open()?;
wpa.request("SAVE_CONFIG")?;