2021-11-16 13:50:06 +00:00
|
|
|
use rocket::{get, request::FlashMessage};
|
|
|
|
use rocket_dyn_templates::Template;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2022-01-04 12:55:17 +00:00
|
|
|
use peach_network::{
|
|
|
|
network,
|
|
|
|
network::{Status, Traffic},
|
|
|
|
};
|
2021-11-16 13:50:06 +00:00
|
|
|
|
|
|
|
use crate::routes::authentication::Authenticated;
|
|
|
|
|
|
|
|
// HELPERS AND ROUTES FOR /status/network
|
|
|
|
|
2022-01-04 12:55:17 +00:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct IfaceTraffic {
|
|
|
|
pub rx: u64,
|
|
|
|
pub rx_unit: Option<String>,
|
|
|
|
pub tx: u64,
|
|
|
|
pub tx_unit: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IfaceTraffic {
|
|
|
|
fn default() -> Self {
|
|
|
|
IfaceTraffic {
|
|
|
|
rx: 0,
|
|
|
|
rx_unit: None,
|
|
|
|
tx: 0,
|
|
|
|
tx_unit: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_traffic(traffic: Traffic) -> Option<IfaceTraffic> {
|
|
|
|
let mut t = IfaceTraffic::default();
|
|
|
|
// modify traffic values & assign measurement unit
|
|
|
|
// based on received and transmitted values
|
|
|
|
// if received > 999 MB, convert it to GB
|
|
|
|
if traffic.received > 1_047_527_424 {
|
|
|
|
t.rx = traffic.received / 1_073_741_824;
|
|
|
|
t.rx_unit = Some("GB".to_string());
|
|
|
|
} else if traffic.received > 0 {
|
|
|
|
// otherwise, convert it to MB
|
|
|
|
t.rx = (traffic.received / 1024) / 1024;
|
|
|
|
t.rx_unit = Some("MB".to_string());
|
|
|
|
} else {
|
|
|
|
t.rx = 0;
|
|
|
|
t.rx_unit = Some("MB".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if traffic.transmitted > 1_047_527_424 {
|
|
|
|
t.tx = traffic.transmitted / 1_073_741_824;
|
|
|
|
t.tx_unit = Some("GB".to_string());
|
|
|
|
} else if traffic.transmitted > 0 {
|
|
|
|
t.tx = (traffic.transmitted / 1024) / 1024;
|
|
|
|
t.tx_unit = Some("MB".to_string());
|
|
|
|
} else {
|
|
|
|
t.tx = 0;
|
|
|
|
t.tx_unit = Some("MB".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(t)
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:50:06 +00:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct NetworkContext {
|
|
|
|
pub ap_ip: String,
|
|
|
|
pub ap_ssid: String,
|
|
|
|
pub ap_state: String,
|
2022-01-04 12:55:17 +00:00
|
|
|
pub ap_traffic: Option<IfaceTraffic>,
|
2021-11-16 13:50:06 +00:00
|
|
|
pub wlan_ip: String,
|
|
|
|
pub wlan_rssi: Option<String>,
|
|
|
|
pub wlan_ssid: String,
|
|
|
|
pub wlan_state: String,
|
2022-01-04 12:55:17 +00:00
|
|
|
pub wlan_status: Option<Status>,
|
|
|
|
pub wlan_traffic: Option<IfaceTraffic>,
|
2021-11-16 13:50:06 +00:00
|
|
|
pub flash_name: Option<String>,
|
|
|
|
pub flash_msg: Option<String>,
|
|
|
|
// page title for header in navbar
|
|
|
|
pub title: Option<String>,
|
|
|
|
// url for back-arrow link
|
|
|
|
pub back: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkContext {
|
|
|
|
pub fn build() -> NetworkContext {
|
2022-01-04 12:55:17 +00:00
|
|
|
let ap_ip = match network::ip("ap0") {
|
|
|
|
Ok(Some(ip)) => ip,
|
|
|
|
_ => "x.x.x.x".to_string(),
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let ap_ssid = match network::ssid("ap0") {
|
|
|
|
Ok(Some(ssid)) => ssid,
|
|
|
|
_ => "Not currently activated".to_string(),
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let ap_state = match network::state("ap0") {
|
|
|
|
Ok(Some(state)) => state,
|
|
|
|
_ => "Interface unavailable".to_string(),
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let ap_traffic = match network::traffic("ap0") {
|
|
|
|
// convert bytes to mb or gb and add appropriate units
|
|
|
|
Ok(Some(traffic)) => convert_traffic(traffic),
|
|
|
|
_ => None,
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let wlan_ip = match network::ip("wlan0") {
|
|
|
|
Ok(Some(ip)) => ip,
|
|
|
|
_ => "x.x.x.x".to_string(),
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let wlan_rssi = match network::rssi_percent("wlan0") {
|
|
|
|
Ok(rssi) => rssi,
|
|
|
|
_ => None,
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let wlan_ssid = match network::ssid("wlan0") {
|
|
|
|
Ok(Some(ssid)) => ssid,
|
|
|
|
_ => "Not connected".to_string(),
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let wlan_state = match network::state("wlan0") {
|
|
|
|
Ok(Some(state)) => state,
|
|
|
|
_ => "Interface unavailable".to_string(),
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let wlan_status = match network::status("wlan0") {
|
2021-11-16 13:50:06 +00:00
|
|
|
Ok(status) => status,
|
2022-01-04 12:55:17 +00:00
|
|
|
_ => None,
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
2022-01-04 12:55:17 +00:00
|
|
|
let wlan_traffic = match network::traffic("wlan0") {
|
|
|
|
// convert bytes to mb or gb and add appropriate units
|
|
|
|
Ok(Some(traffic)) => convert_traffic(traffic),
|
|
|
|
_ => None,
|
2021-11-16 13:50:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NetworkContext {
|
|
|
|
ap_ip,
|
|
|
|
ap_ssid,
|
|
|
|
ap_state,
|
|
|
|
ap_traffic,
|
|
|
|
wlan_ip,
|
|
|
|
wlan_rssi,
|
|
|
|
wlan_ssid,
|
|
|
|
wlan_state,
|
|
|
|
wlan_status,
|
|
|
|
wlan_traffic,
|
|
|
|
flash_name: None,
|
|
|
|
flash_msg: None,
|
|
|
|
title: None,
|
|
|
|
back: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/network")]
|
|
|
|
pub fn network_status(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
|
|
|
|
// assign context through context_builder call
|
|
|
|
let mut context = NetworkContext::build();
|
|
|
|
context.back = Some("/status".to_string());
|
|
|
|
context.title = Some("Network Status".to_string());
|
|
|
|
// check to see if there is a flash message to display
|
|
|
|
if let Some(flash) = flash {
|
|
|
|
// add flash message contents to the context object
|
|
|
|
context.flash_name = Some(flash.kind().to_string());
|
|
|
|
context.flash_msg = Some(flash.message().to_string());
|
|
|
|
};
|
|
|
|
// template_dir is set in Rocket.toml
|
|
|
|
Template::render("status/network", &context)
|
|
|
|
}
|