Compare commits

..

No commits in common. "main" and "update_wpactrl_api" have entirely different histories.

49 changed files with 1080 additions and 2599 deletions

700
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
[workspace]
members = [
"peach-buttons",
"peach-oled",
"peach-lib",
"peach-config",
@ -11,4 +13,3 @@ members = [
"peach-jsonrpc-server",
"peach-dyndns-updater"
]

View File

@ -1,6 +1,6 @@
[package]
name = "peach-config"
version = "0.1.27"
version = "0.1.25"
authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"]
edition = "2018"
description = "Command line tool for installing, updating and configuring PeachCloud"
@ -37,5 +37,3 @@ log = "0.4"
lazy_static = "1.4.0"
peach-lib = { path = "../peach-lib" }
rpassword = "5.0"
golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi.git" }
async-std = "1.10.0"

View File

@ -1,5 +1,4 @@
#![allow(clippy::nonstandard_macro_braces)]
use golgi::error::GolgiError;
use peach_lib::error::PeachError;
pub use snafu::ResultExt;
use snafu::Snafu;
@ -36,14 +35,6 @@ pub enum PeachConfigError {
ChangePasswordError { source: PeachError },
#[snafu(display("Entered passwords did not match. Please try again."))]
InvalidPassword,
#[snafu(display("Error in peach lib: {}", source))]
PeachLibError { source: PeachError },
#[snafu(display("Error in golgi: {}", source))]
Golgi { source: GolgiError },
#[snafu(display("{}", message))]
CmdInputError { message: String },
#[snafu(display("{}", message))]
WaitForSbotError { message: String },
}
impl From<std::io::Error> for PeachConfigError {
@ -60,15 +51,3 @@ impl From<serde_json::Error> for PeachConfigError {
PeachConfigError::SerdeError { source: err }
}
}
impl From<PeachError> for PeachConfigError {
fn from(err: PeachError) -> PeachConfigError {
PeachConfigError::PeachLibError { source: err }
}
}
impl From<GolgiError> for PeachConfigError {
fn from(err: GolgiError) -> PeachConfigError {
PeachConfigError::Golgi { source: err }
}
}

View File

@ -2,15 +2,12 @@ mod change_password;
mod constants;
mod error;
mod generate_manifest;
mod publish_address;
mod set_permissions;
mod setup_networking;
mod setup_peach;
mod setup_peach_deb;
mod status;
mod update;
mod utils;
mod wait_for_sbot;
use clap::arg_enum;
use log::error;
@ -47,25 +44,12 @@ enum PeachConfig {
Update(UpdateOpts),
/// Changes the password for the peach-web interface
#[structopt(name = "change-password")]
#[structopt(name = "changepassword")]
ChangePassword(ChangePasswordOpts),
/// Updates file permissions on PeachCloud device
#[structopt(name = "permissions")]
SetPermissions,
/// Returns sbot id if sbot is running
#[structopt(name = "whoami")]
WhoAmI,
/// Publish domain and port.
/// It takes an address argument of the form host:port
#[structopt(name = "publish-address")]
PublishAddress(PublishAddressOpts),
/// Wait for a successful connection to sbot
#[structopt(name = "wait-for-sbot")]
WaitForSbot,
}
#[derive(StructOpt, Debug)]
@ -106,13 +90,6 @@ pub struct ChangePasswordOpts {
password: Option<String>,
}
#[derive(StructOpt, Debug)]
pub struct PublishAddressOpts {
/// Specify address in the form domain:port
#[structopt(short, long)]
address: String,
}
arg_enum! {
/// enum options for real-time clock choices
#[derive(Debug)]
@ -125,7 +102,7 @@ arg_enum! {
}
}
async fn run() {
fn main() {
// initialize the logger
env_logger::init();
@ -178,42 +155,6 @@ async fn run() {
)
}
},
PeachConfig::WhoAmI => match status::whoami().await {
Ok(sbot_id) => {
println!("{:?}", sbot_id);
{}
}
Err(err) => {
error!("sbot whoami encountered an error: {}", err)
}
},
PeachConfig::PublishAddress(opts) => {
match publish_address::publish_address(opts.address).await {
Ok(_) => {}
Err(err) => {
error!(
"peach-config encountered an error during publish address: {}",
err
)
}
}
}
PeachConfig::WaitForSbot => match wait_for_sbot::wait_for_sbot().await {
Ok(sbot_id) => {
println!("connected with sbot and found sbot_id: {:?}", sbot_id)
}
Err(err) => {
error!("peach-config did not successfully connect to sbot: {}", err)
}
},
}
}
}
// Enable an async main function and execute the `run()` function,
// catching any errors and printing them to `stderr` before exiting the
// process.
#[async_std::main]
async fn main() {
run().await;
}

View File

@ -1,37 +0,0 @@
use crate::error::PeachConfigError;
use golgi::kuska_ssb::api::dto::content::PubAddress;
use golgi::messages::SsbMessageContent;
use peach_lib::sbot::init_sbot;
/// Utility function to publish the address (domain:port) of the pub
/// publishing the address causes the domain and port to be used for invite generation,
/// and also gossips this pub address to their peers
pub async fn publish_address(address: String) -> Result<(), PeachConfigError> {
// split address into domain:port
let split: Vec<&str> = address.split(':').collect();
let (domain, port): (&str, &str) = (split[0], split[1]);
// convert port to u16
let port_as_u16: u16 = port
.parse()
.map_err(|_err| PeachConfigError::CmdInputError {
message: "Failure to parse domain and port. Address must be of the format host:port."
.to_string(),
})?;
// publish address
let mut sbot = init_sbot().await?;
let pub_id = sbot.whoami().await?;
// Compose a `pub` address type message.
let pub_address_msg = SsbMessageContent::Pub {
address: Some(PubAddress {
// Host name (can be an IP address if onboarding over WiFi).
host: Some(domain.to_string()),
// Port.
port: port_as_u16,
// Public key.
key: pub_id,
}),
};
// Publish the `pub` address message.
let _pub_msg_ref = sbot.publish(pub_address_msg).await?;
Ok(())
}

View File

@ -1,17 +1,17 @@
use lazy_static::lazy_static;
use peach_lib::config_manager;
use peach_lib::config_manager::get_config_value;
use crate::error::PeachConfigError;
use crate::utils::cmd;
lazy_static! {
pub static ref PEACH_CONFIGDIR: String = config_manager::get_config_value("PEACH_CONFIGDIR")
pub static ref PEACH_CONFIGDIR: String = get_config_value("PEACH_CONFIGDIR")
.expect("Failed to load config value for PEACH_CONFIGDIR");
pub static ref PEACH_WEBDIR: String = config_manager::get_config_value("PEACH_WEBDIR")
.expect("Failed to load config value for PEACH_WEBDIR");
pub static ref PEACH_HOMEDIR: String = config_manager::get_config_value("PEACH_HOMEDIR")
.expect("Failed to load config value for PEACH_HOMEDIR");
pub static ref PEACH_WEBDIR: String =
get_config_value("PEACH_WEBDIR").expect("Failed to load config value for PEACH_WEBDIR");
pub static ref PEACH_HOMEDIR: String =
get_config_value("PEACH_HOMEDIR").expect("Failed to load config value for PEACH_HOMEDIR");
}
/// Utility function to set correct file permissions on the PeachCloud device.

View File

@ -1,9 +0,0 @@
use crate::error::PeachConfigError;
use peach_lib::sbot::init_sbot;
/// Utility function to check if sbot is running via the whoami method
pub async fn whoami() -> Result<String, PeachConfigError> {
let mut sbot = init_sbot().await?;
let sbot_id = sbot.whoami().await?;
Ok(sbot_id)
}

View File

@ -1,52 +0,0 @@
use std::{thread, time};
use crate::error::PeachConfigError;
use peach_lib::sbot::init_sbot;
static MAX_NUM_ATTEMPTS: u8 = 10;
/// Utility function to wait for a successful whoami call with sbot
/// After each attempt to call whoami it waits 2 seconds,
/// and if after MAX_NUM_ATTEMPTS (10) there is no successful whoami call
/// it returns an Error. Otherwise it returns Ok(sbot_id).
pub async fn wait_for_sbot() -> Result<String, PeachConfigError> {
let mut num_attempts = 0;
let mut whoami = None;
while num_attempts < MAX_NUM_ATTEMPTS {
let mut sbot = None;
let sbot_res = init_sbot().await;
match sbot_res {
Ok(sbot_instance) => {
sbot = Some(sbot_instance);
}
Err(err) => {
eprintln!("failed to connect to sbot: {:?}", err);
}
}
if sbot.is_some() {
let sbot_id_res = sbot.unwrap().whoami().await;
match sbot_id_res {
Ok(sbot_id) => {
whoami = Some(sbot_id);
break;
}
Err(err) => {
eprintln!("whoami failed: {:?}", err);
}
}
}
println!("trying to connect to sbot again {:?}", num_attempts);
num_attempts += 1;
let sleep_duration = time::Duration::from_secs(2);
thread::sleep(sleep_duration);
}
whoami.ok_or(PeachConfigError::WaitForSbotError {
message: "Failed to find sbot_id after 10 attempts".to_string(),
})
}

View File

@ -1,6 +1,6 @@
[package]
name = "peach-lib"
version = "1.3.4"
version = "1.3.3"
authors = ["Andrew Reid <glyph@mycelial.technology>"]
edition = "2018"
@ -9,7 +9,7 @@ async-std = "1.10"
chrono = "0.4"
dirs = "4.0"
fslock="0.1"
golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi.git" }
golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi" }
jsonrpc-client-core = "0.5"
jsonrpc-client-http = "0.5"
jsonrpc-core = "8.0"

View File

@ -60,7 +60,6 @@ pub fn get_peach_config_defaults() -> HashMap<String, String> {
("ADMIN_PASSWORD_HASH", "47"),
("TEMPORARY_PASSWORD_HASH", ""),
("GO_SBOT_DATADIR", "/home/peach/.ssb-go"),
("GO_SBOT_SERVICE", "go-sbot.service"),
("PEACH_CONFIGDIR", "/var/lib/peachcloud"),
("PEACH_HOMEDIR", "/home/peach"),
("PEACH_WEBDIR", "/usr/share/peach-web"),
@ -132,10 +131,7 @@ pub fn save_peach_config_to_disc(
peach_config: HashMap<String, String>,
) -> Result<HashMap<String, String>, PeachError> {
// use a file lock to avoid race conditions while saving config
let mut lock = LockFile::open(&*LOCK_FILE_PATH).map_err(|source| PeachError::Read {
source,
path: LOCK_FILE_PATH.to_string(),
})?;
let mut lock = LockFile::open(&*LOCK_FILE_PATH)?;
lock.lock()?;
// first convert Hashmap to BTreeMap (so that keys are saved in deterministic alphabetical order)

View File

@ -2,7 +2,6 @@
//! Error handling for various aspects of the PeachCloud system, including the network, OLED, stats and dyndns JSON-RPC clients, as well as the configuration manager, sbot client and password utilities.
use golgi::GolgiError;
use std::{io, str, string};
/// This type represents all possible errors that can occur when interacting with the PeachCloud library.
@ -103,9 +102,6 @@ pub enum PeachError {
/// The file path for the write attempt.
path: String,
},
/// Represents a Golgi error
Golgi(GolgiError),
}
impl std::error::Error for PeachError {
@ -134,7 +130,6 @@ impl std::error::Error for PeachError {
PeachError::Utf8ToStr(_) => None,
PeachError::Utf8ToString(_) => None,
PeachError::Write { ref source, .. } => Some(source),
PeachError::Golgi(_) => None,
}
}
}
@ -192,7 +187,6 @@ impl std::fmt::Display for PeachError {
PeachError::Write { ref path, .. } => {
write!(f, "Write error: {}", path)
}
PeachError::Golgi(ref err) => err.fmt(f),
}
}
}
@ -262,9 +256,3 @@ impl From<string::FromUtf8Error> for PeachError {
PeachError::Utf8ToString(err)
}
}
impl From<GolgiError> for PeachError {
fn from(err: GolgiError) -> PeachError {
PeachError::Golgi(err)
}
}

View File

@ -2,10 +2,7 @@
use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str};
use golgi::{sbot::Keystore, Sbot};
use log::debug;
use crate::config_manager;
use crate::config_manager::get_config_value;
use serde::{Deserialize, Serialize};
use crate::error::PeachError;
@ -70,7 +67,7 @@ impl SbotStatus {
// because non-privileged users are able to run systemctl show
let info_output = Command::new("systemctl")
.arg("show")
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.arg("go-sbot.service")
.arg("--no-page")
.output()?;
@ -92,7 +89,7 @@ impl SbotStatus {
// because non-privileged users are able to run systemctl status
let status_output = Command::new("systemctl")
.arg("status")
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.arg("go-sbot.service")
.output()?;
let service_status = str::from_utf8(&status_output.stdout)?;
@ -131,10 +128,7 @@ impl SbotStatus {
}
// get path to blobstore
let blobstore_path = format!(
"{}/blobs/sha256",
config_manager::get_config_value("GO_SBOT_DATADIR")?
);
let blobstore_path = format!("{}/blobs/sha256", get_config_value("GO_SBOT_DATADIR")?);
// determine the size of the blobstore directory in bytes
status.blobstore = dir_size(blobstore_path).ok();
@ -205,11 +199,9 @@ impl Default for SbotConfig {
impl SbotConfig {
/// Read the go-sbot `config.toml` file from file and deserialize into `SbotConfig`.
pub fn read() -> Result<Self, PeachError> {
// determine path of user's go-sbot config.toml
let config_path = format!(
"{}/config.toml",
config_manager::get_config_value("GO_SBOT_DATADIR")?
);
// determine path of user's home directory
let mut config_path = dirs::home_dir().ok_or(PeachError::HomeDir)?;
config_path.push(".ssb-go/config.toml");
let config_contents = fs::read_to_string(config_path)?;
@ -225,11 +217,8 @@ impl SbotConfig {
// convert the provided `SbotConfig` instance to a string
let config_string = toml::to_string(&config)?;
// determine path of user's go-sbot config.toml
let config_path = format!(
"{}/config.toml",
config_manager::get_config_value("GO_SBOT_DATADIR")?
);
// determine path of user's home directory
let config_path = format!("{}/config.toml", get_config_value("GO_SBOT_DATADIR")?);
// open config file for writing
let mut file = File::create(config_path)?;
@ -243,25 +232,3 @@ impl SbotConfig {
Ok(())
}
}
/// Initialise an sbot client
pub async fn init_sbot() -> Result<Sbot, PeachError> {
// read sbot config from config.toml
let sbot_config = SbotConfig::read().ok();
debug!("Initialising an sbot client with configuration parameters");
// initialise sbot connection with ip:port and shscap from config file
let key_path = format!(
"{}/secret",
config_manager::get_config_value("GO_SBOT_DATADIR")?
);
let sbot_client = match sbot_config {
// TODO: panics if we pass `Some(conf.shscap)` as second arg
Some(conf) => {
let ip_port = conf.lis.clone();
Sbot::init(Keystore::CustomGoSbot(key_path), Some(ip_port), None).await?
}
None => Sbot::init(Keystore::CustomGoSbot(key_path), None, None).await?,
};
Ok(sbot_client)
}

View File

@ -1,6 +1,6 @@
[package]
name = "peach-network"
version = "0.5.0"
version = "0.4.2"
authors = ["Andrew Reid <glyph@mycelial.technology>"]
edition = "2021"
description = "Query and configure network interfaces."

View File

@ -14,7 +14,6 @@
//! access point credentials to `wpa_supplicant-<wlan_iface>.conf`.
use std::{
collections::HashMap,
fs::OpenOptions,
io::prelude::*,
process::{Command, Stdio},
@ -107,86 +106,8 @@ pub struct Traffic {
pub transmitted: u64,
}
/// Access point data including state and signal strength.
#[derive(Debug)]
#[cfg_attr(feature = "miniserde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct AccessPoint {
/// Access point data retrieved via scan.
pub detail: Option<Scan>,
/// Current state of the access point (e.g. "Available" or "Out of range").
pub state: String,
/// Signal strength of the access point as a percentage.
pub signal: Option<i32>,
}
impl AccessPoint {
fn available(detail: Option<Scan>, signal: Option<i32>) -> AccessPoint {
AccessPoint {
detail,
state: String::from("Available"),
signal,
}
}
fn saved() -> AccessPoint {
AccessPoint {
detail: None,
state: String::from("Out of range"),
signal: None,
}
}
}
/* GET - Methods for retrieving data */
/// Retrieve combined list of available (in-range) and saved wireless access
/// points for a given network interface.
///
/// # Arguments
///
/// * `iface` - A string slice holding the name of a wireless network interface
///
/// If the list results include one or more access points for the given network
/// interface, an `Ok` `Result` type is returned containing `HashMap<String,
/// AccessPoint>`.
///
/// Each entry in the returned `HashMap` contains an SSID (`String`) and
/// `AccessPoint` `struct`. If no access points are found, an empty `HashMap`
/// is returned in the `Result`. In the event of an error, a `NetworkError`
/// is returned in the `Result`.
pub fn all_networks(iface: &str) -> Result<HashMap<String, AccessPoint>, NetworkError> {
let mut wlan_networks = HashMap::new();
if let Ok(Some(networks)) = available_networks(iface) {
for ap in networks {
let ssid = ap.ssid.clone();
let rssi = ap.signal_level.clone();
// parse the string to a signed integer (for math)
let rssi_parsed = rssi.parse::<i32>().unwrap();
// perform rssi (dBm) to quality (%) conversion
let quality_percent = 2 * (rssi_parsed + 100);
let ap_detail = AccessPoint::available(Some(ap), Some(quality_percent));
wlan_networks.insert(ssid, ap_detail);
}
}
if let Ok(Some(networks)) = saved_networks() {
for saved_ssid in networks {
if !wlan_networks.contains_key(&saved_ssid) {
let ssid = saved_ssid.clone();
let ap_detail = AccessPoint::saved();
wlan_networks.insert(ssid, ap_detail);
}
}
}
Ok(wlan_networks)
}
/// Retrieve list of available wireless access points for a given network
/// interface.
///

View File

@ -1,6 +1,6 @@
[package]
name = "peach-stats"
version = "0.3.1"
version = "0.3.0"
authors = ["Andrew Reid <glyph@mycelial.technology>"]
edition = "2018"
description = "Query system statistics. Provides a wrapper around the probes and systemstat crates."

View File

@ -44,8 +44,8 @@ impl SbotStat {
pub fn sbot_stats() -> Result<SbotStat, StatsError> {
let mut status = SbotStat::default();
let info_output = Command::new("sudo")
.arg("systemctl")
let info_output = Command::new("/usr/bin/systemctl")
.arg("--user")
.arg("show")
.arg("go-sbot.service")
.arg("--no-page")
@ -66,8 +66,8 @@ pub fn sbot_stats() -> Result<SbotStat, StatsError> {
}
}
let status_output = Command::new("sudo")
.arg("systemctl")
let status_output = Command::new("/usr/bin/systemctl")
.arg("--user")
.arg("status")
.arg("go-sbot.service")
.output()

View File

@ -1,7 +1,7 @@
[package]
name = "peach-web"
version = "0.6.21"
authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"]
version = "0.6.13"
authors = ["Andrew Reid <gnomad@cryptolab.net>"]
edition = "2018"
description = "peach-web is a web application which provides a web interface for monitoring and interacting with the PeachCloud device. This allows administration of the single-board computer (ie. Raspberry Pi) running PeachCloud, as well as the ssb-server and related plugins."
homepage = "https://opencollective.com/peachcloud"
@ -44,9 +44,9 @@ lazy_static = "1.4"
log = "0.4"
maud = "0.23"
peach-lib = { path = "../peach-lib" }
peach-network = { path = "../peach-network" }
peach-stats = { path = "../peach-stats" }
# these will be reintroduced when the full peachcloud mode is added
#peach-network = { path = "../peach-network" }
#peach-stats = { path = "../peach-stats" }
rouille = { version = "3.5", default-features = false }
temporary = "0.6"
vnstat_parse = "0.1.0"
xdg = "2.2"

View File

@ -1,6 +1,6 @@
# peach-web
![Generic badge](https://img.shields.io/badge/version-0.6.18-<COLOR>.svg)
![Generic badge](https://img.shields.io/badge/version-0.6.0-<COLOR>.svg)
## Web Interface for PeachCloud
@ -17,7 +17,7 @@ The web interface is primarily designed as a means of managing a Scuttlebutt pub
Additional features are focused on administration of the device itself. This includes networking functionality and device statistics.
The peach-web stack currently consists of [Rouille](https://crates.io/crates/rouille) (Rust web framework), [Maud](https://maud.lambda.xyz/) (Rust template engine), HTML, CSS and a tiny bit of JS. Scuttlebutt functionality is provided by [golgi](http://golgi.mycelial.technology).
The peach-web stack currently consists of [Rouille](https://crates.io/crates/rouille) (Rust web framework), [Maud](https://maud.lambda.xyz/) (Rust template engine), HTML and CSS. Scuttlebutt functionality is provided by [golgi](http://golgi.mycelial.technology).
_Note: This is a work-in-progress._
@ -36,46 +36,11 @@ Run the binary:
`../target/release/peach-web`
## Development Setup
In order to test `peach-web` on a development machine you will need to have a running instance of `go-sbot` (please see the [go-sbot README](https://github.com/cryptoscope/ssb) for installation details). The `GO_SBOT_DATADIR` environment variable or corresponding config variable must be set to `/home/<user>/.ssb-go` and the `PEACH_HOMEDIR` variable must be set to `/home/<user>`. See the Configuration section below for more details.
The `go-sbot` process must be managed by `systemd` in order for it to be controlled via the `peach-web` web interface. Here is a basic `go-sbot.service` file:
```
[Unit]
Description=GoSSB server.
[Service]
ExecStart=/usr/bin/go-sbot
Environment="LIBRARIAN_WRITEALL=0"
Restart=always
[Install]
WantedBy=multi-user.target
```
And a `sudoers` rule must be created to allow the `go-sbot.service` state to be modified without requiring a password. Here is an example `/etc/sudoers.d/peach-web` file:
```
# Control go-sbot service without sudo passworkd
<user> ALL=(ALL) NOPASSWD: /bin/systemctl start go-sbot.service, /bin/systemctl restart go-sbot.service, /bin/systemctl stop go-sbot.service, /bin/systemctl enable go-sbot.service, /bin/systemctl disable go-sbot.service
```
## Configuration
By default, configuration variables are stored in `/var/lib/peachcloud/config.yml`. The variables in the file are updated by `peach-web` when changes are made to configurations via the web interface. Since `peach-web` has no database, all configurations are stored in this file.
A non-default configuration directory can be defined via the `PEACH_CONFIGDIR` environment variable or corresponding key in the `config.yml` file.
## Environment
### Configuration Mode
The web application can be run with a minimal set of routes and functionality (PeachPub - a simple sbot manager) or with the full-suite of capabilities, including network management and access to device statistics (PeachCloud).
The application runs in PeachPub mode by default. The complete PeachCloud mode will be available once a large refactor is complete; it is not currently in working order so it's best to stick with PeachPub for now.
The running mode can be defined by setting the `STANDALONE_MODE` environment variable (`true` for PeachPub or `false` for PeachCloud). Alternatively, the desired mode can be set by modifying the PeachCloud configuration file.
The web application can be run with a minimal set of routes and functionality (PeachPub - a simple sbot manager) or with the full-suite of capabilities, including network management and access to device statistics (PeachCloud). The mode is enabled by default (as defined in `Rocket.toml`) but can be overwritten using the `STANDALONE_MODE` environment variable: `true` or `false`. If the variable is unset or the value is incorrectly set, the application defaults to standalone mode.
### Authentication
@ -91,14 +56,6 @@ Logging is made available with `env_logger`:
Other logging levels include `debug`, `warn` and `error`.
### Dynamic DNS Configuration
Most users will want to use the default PeachCloud dynamic dns server.
If the config dyn_use_custom_server=false, then default values will be used.
If the config dyn_use_custom_server=true, then a value must also be set for dyn_dns_server_address (e.g. "http://peachdynserver.commoninternet.net").
This value is the URL of the instance of peach-dyndns-server that requests will be sent to for domain registration.
Using a custom value can here can be useful for testing.
## Debian Packaging
A `systemd` service file and Debian maintainer scripts are included in the `debian` directory, allowing `peach-web` 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.
@ -131,6 +88,20 @@ Remove configuration files (not removed with `apt-get remove`):
`sudo apt-get purge peach-web`
## Configuration
Configuration variables are stored in /var/lib/peachcloud/config.yml.
Peach-web also updates this file when changes are made to configurations via
the web interface. peach-web has no database, so all configurations are stored in this file.
### Dynamic DNS Configuration
Most users will want to use the default PeachCloud dynamic dns server.
If the config dyn_use_custom_server=false, then default values will be used.
If the config dyn_use_custom_server=true, then a value must also be set for dyn_dns_server_address (e.g. "http://peachdynserver.commoninternet.net").
This value is the URL of the instance of peach-dyndns-server that requests will be sent to for domain registration.
Using a custom value can here can be useful for testing.
## Design
`peach-web` has been designed with simplicity and resource minimalism in mind. Both the dependencies used by the project, as well as the code itself, reflect these design priorities. The Rouille micro-web-framework and Maud templating engine have been used to present a web interface for interacting with the device. HTML is rendered server-side and request handlers call `peach-` libraries and serve HTML and assets. The optimised binary for `peach-web` can be compiled on a RPi 3 B+ in approximately 30 minutes.

View File

@ -39,12 +39,6 @@ lazy_static! {
static ref THEME: RwLock<Theme> = RwLock::new(Theme::Light);
}
/// Wireless interface identifier.
pub const WLAN_IFACE: &str = "wlan0";
/// Access point interface identifier.
pub const AP_IFACE: &str = "ap0";
/// Session data for each authenticated client.
#[derive(Debug, Clone)]
pub struct SessionData {

View File

@ -1,10 +1,6 @@
use rouille::{router, Request, Response};
use crate::{
routes, templates,
utils::{cookie::CookieResponse, flash::FlashResponse},
SessionData,
};
use crate::{routes, templates, utils::flash::FlashResponse, SessionData};
// TODO: add mount_peachcloud_routes()
// https://github.com/tomaka/rouille/issues/232#issuecomment-919225104
@ -26,8 +22,6 @@ pub fn mount_peachpub_routes(
router!(request,
(GET) (/) => {
Response::html(routes::home::build_template())
// reset the back_url cookie each time we visit the homepage
.reset_cookie("back_url")
},
(GET) (/auth/change) => {
@ -55,9 +49,6 @@ pub fn mount_peachpub_routes(
(GET) (/scuttlebutt/blocks) => {
Response::html(routes::scuttlebutt::blocks::build_template())
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/blocks")
},
(POST) (/scuttlebutt/follow) => {
@ -66,16 +57,10 @@ pub fn mount_peachpub_routes(
(GET) (/scuttlebutt/follows) => {
Response::html(routes::scuttlebutt::follows::build_template())
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/follows")
},
(GET) (/scuttlebutt/friends) => {
Response::html(routes::scuttlebutt::friends::build_template())
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/friends")
},
(GET) (/scuttlebutt/invites) => {
@ -132,9 +117,6 @@ pub fn mount_peachpub_routes(
(POST) (/scuttlebutt/search) => {
routes::scuttlebutt::search::handle_form(request)
// add a back_url cookie to allow the path of the back button
// to be set correctly on the /scuttlebutt/profile page
.add_cookie("back_url=/scuttlebutt/search")
},
(POST) (/scuttlebutt/unblock) => {
@ -166,18 +148,6 @@ pub fn mount_peachpub_routes(
routes::settings::admin::delete::handle_form(request)
},
(GET) (/settings/power) => {
Response::html(routes::settings::power::menu::build_template(request))
},
(GET) (/settings/power/reboot) => {
routes::settings::power::reboot::handle_reboot()
},
(GET) (/settings/power/shutdown) => {
routes::settings::power::shutdown::handle_shutdown()
},
(GET) (/settings/scuttlebutt) => {
Response::html(routes::settings::scuttlebutt::menu::build_template(request))
.reset_flash()
@ -212,64 +182,12 @@ pub fn mount_peachpub_routes(
routes::settings::scuttlebutt::default::write_config()
},
(GET) (/settings/network) => {
Response::html(routes::settings::network::menu::build_template(request)).reset_flash()
},
(GET) (/settings/network/dns) => {
Response::html(routes::settings::network::configure_dns::build_template(request)).reset_flash()
},
(POST) (/settings/network/dns) => {
routes::settings::network::configure_dns::handle_form(request)
},
(GET) (/settings/network/wifi) => {
Response::html(routes::settings::network::list_aps::build_template())
},
(GET) (/settings/network/wifi/add) => {
Response::html(routes::settings::network::add_ap::build_template(request, None)).reset_flash()
},
(POST) (/settings/network/wifi/add) => {
routes::settings::network::add_ap::handle_form(request)
},
(GET) (/settings/network/wifi/add/{ssid: String}) => {
Response::html(routes::settings::network::add_ap::build_template(request, Some(ssid))).reset_flash()
},
(GET) (/settings/network/wifi/modify) => {
Response::html(routes::settings::network::modify_ap::build_template(request, None)).reset_flash()
},
(POST) (/settings/network/wifi/modify) => {
routes::settings::network::modify_ap::handle_form(request)
},
(GET) (/settings/network/wifi/modify/{ssid: String}) => {
Response::html(routes::settings::network::modify_ap::build_template(request, Some(ssid))).reset_flash()
},
(GET) (/settings/network/wifi/{ssid: String}) => {
Response::html(routes::settings::network::ap_details::build_template(request, ssid))
},
(GET) (/settings/theme/{theme: String}) => {
routes::settings::theme::set_theme(theme)
},
(GET) (/status) => {
Response::html(routes::status::device::build_template())
},
(GET) (/status/scuttlebutt) => {
Response::html(routes::status::scuttlebutt::build_template()).add_cookie("back_url=/status/scuttlebutt")
},
(GET) (/status/network) => {
Response::html(routes::status::network::build_template())
Response::html(routes::status::scuttlebutt::build_template())
},
// render the not_found template and set a 404 status code if none of

View File

@ -30,7 +30,7 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
form id="sendPasswordReset" action="/auth/temporary" method="post" {
div id="buttonDiv" {
input class="button button-primary center" style="margin-top: 1rem;" type="submit" value="Send Temporary Password" title="Send temporary password to Scuttlebutt admin(s)";
a href="/auth/reset" class="button button-primary center" title="Set a new password using the temporary password" {
a href="/auth/reset_password" class="button button-primary center" title="Set a new password using the temporary password" {
"Set New Password"
}
}

View File

@ -1,7 +1,7 @@
use maud::{html, PreEscaped};
use peach_lib::sbot::SbotStatus;
use crate::{templates, utils::theme, SERVER_CONFIG};
use crate::{templates, utils::theme};
/// Read the state of the go-sbot process and define status-related
/// elements accordingly.
@ -24,23 +24,9 @@ fn render_status_elements<'a>() -> (&'a str, &'a str, &'a str) {
}
}
/// Render the URL for the status element (icon / link).
///
/// If the application is running in standalone mode then the element links
/// directly to the Scuttlebutt status page. If not, it links to the device
/// status page.
fn render_status_url<'a>() -> &'a str {
if SERVER_CONFIG.standalone_mode {
"/status/scuttlebutt"
} else {
"/status"
}
}
/// Home template builder.
pub fn build_template() -> PreEscaped<String> {
let (circle_color, center_circle_text, circle_border) = render_status_elements();
let status_url = render_status_url();
// render the home template html
let home_template = html! {
@ -77,7 +63,7 @@ pub fn build_template() -> PreEscaped<String> {
}
(PreEscaped("<!-- bottom-left -->"))
(PreEscaped("<!-- SYSTEM STATUS LINK AND ICON -->"))
a class="bottom-left" href=(status_url) title="Status" {
a class="bottom-left" href="/status/scuttlebutt" title="Status" {
div class={ "circle circle-small border-circle-small " (circle_border) } {
img class="icon-medium" src="/icons/heart-pulse.svg";
}

View File

@ -4,7 +4,7 @@ use rouille::Request;
use crate::{
templates,
utils::{cookie::CookieRequest, flash::FlashRequest, sbot, sbot::Profile, theme},
utils::{flash::FlashRequest, sbot, sbot::Profile, theme},
};
// ROUTE: /scuttlebutt/profile
@ -83,15 +83,13 @@ fn social_interaction_buttons_template(profile: &Profile) -> Markup {
@match (profile.following, &profile.id) {
(Some(false), Some(ssb_id)) => {
form id="followForm" class="center" action="/scuttlebutt/follow" method="post" {
// url encode the ssb_id value
input type="hidden" id="publicKey" name="public_key" value=(ssb_id.replace('/', "%2F"));
input type="hidden" id="publicKey" name="public_key" value=(ssb_id);
input id="followPeer" class="button button-primary center" type="submit" title="Follow Peer" value="Follow";
}
},
(Some(true), Some(ssb_id)) => {
form id="unfollowForm" class="center" action="/scuttlebutt/unfollow" method="post" {
// url encode the ssb_id value
input type="hidden" id="publicKey" name="public_key" value=(ssb_id.replace('/', "%2F"));
input type="hidden" id="publicKey" name="public_key" value=(ssb_id);
input id="unfollowPeer" class="button button-primary center" type="submit" title="Unfollow Peer" value="Unfollow";
}
},
@ -100,15 +98,13 @@ fn social_interaction_buttons_template(profile: &Profile) -> Markup {
@match (profile.blocking, &profile.id) {
(Some(false), Some(ssb_id)) => {
form id="blockForm" class="center" action="/scuttlebutt/block" method="post" {
// url encode the ssb_id value
input type="hidden" id="publicKey" name="public_key" value=(ssb_id.replace('/', "%2F"));
input type="hidden" id="publicKey" name="public_key" value=(ssb_id);
input id="blockPeer" class="button button-primary center" type="submit" title="Block Peer" value="Block";
}
},
(Some(true), Some(ssb_id)) => {
form id="unblockForm" class="center" action="/scuttlebutt/unblock" method="post" {
// url encode the ssb_id value
input type="hidden" id="publicKey" name="public_key" value=(ssb_id.replace('/', "%2F"));
input type="hidden" id="publicKey" name="public_key" value=(ssb_id);
input id="unblockPeer" class="button button-primary center" type="submit" title="Unblock Peer" value="Unblock";
}
},
@ -116,8 +112,7 @@ fn social_interaction_buttons_template(profile: &Profile) -> Markup {
}
@if let Some(ssb_id) = &profile.id {
form class="center" {
// url encode the ssb_id
a id="privateMessage" class="button button-primary center" href={ "/scuttlebutt/private/" (ssb_id.replace('/', "%2F")) } title="Private Message" {
a id="privateMessage" class="button button-primary center" href={ "/scuttlebutt/private/" (ssb_id) } title="Private Message" {
"Send Private Message"
}
}
@ -174,15 +169,7 @@ pub fn build_template(request: &Request, ssb_id: Option<String>) -> PreEscaped<S
_ => templates::inactive::build_template("Profile is unavailable."),
};
// a request to /scuttlebutt/profile can originate via the Friends,
// Follows or Blocks menu - as well as the Search page and Homepage.
// therefore, we check to see if the `back_url` cookie has been set
// and assign the path of the back button accordingly.
// for example, if the request has come via the Friends menu then the
// `back_url` cookie will be set with a value of "/scuttlebutt/friends".
let back_url = request.retrieve_cookie("back_url").or(Some("/"));
let body = templates::nav::build_template(profile_template, "Profile", back_url);
let body = templates::nav::build_template(profile_template, "Profile", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();

View File

@ -11,10 +11,8 @@ pub fn build_template() -> PreEscaped<String> {
div class="card center" {
(PreEscaped("<!-- BUTTONS -->"))
div id="settingsButtons" {
// render the network settings and power menu buttons if we're
// not in standalone mode
// render the network settings button if we're not in standalone mode
@if !SERVER_CONFIG.standalone_mode {
a id="power" class="button button-primary center" href="/settings/power" title="Power Menu" { "Power" }
a id="network" class="button button-primary center" href="/settings/network" title="Network Settings" { "Network" }
}
a id="scuttlebutt" class="button button-primary center" href="/settings/scuttlebutt" title="Scuttlebutt Settings" { "Scuttlebutt" }

View File

@ -1,7 +1,6 @@
pub mod admin;
//pub mod dns;
pub mod menu;
pub mod network;
pub mod power;
//pub mod network;
pub mod scuttlebutt;
pub mod theme;

View File

@ -0,0 +1,322 @@
use log::{debug, warn};
use rocket::{
form::{Form, FromForm},
get, post,
request::FlashMessage,
response::{Flash, Redirect},
uri, UriDisplayQuery,
};
use rocket_dyn_templates::{tera::Context, Template};
use peach_network::network;
use crate::{
context,
context::network::{NetworkAlertContext, NetworkDetailContext, NetworkListContext},
routes::authentication::Authenticated,
utils::{monitor, monitor::Threshold},
AP_IFACE, WLAN_IFACE,
};
// STRUCTS USED BY NETWORK ROUTES
#[derive(Debug, FromForm, UriDisplayQuery)]
pub struct Ssid {
pub ssid: String,
}
#[derive(Debug, FromForm)]
pub struct WiFi {
pub ssid: String,
pub pass: String,
}
// HELPERS AND ROUTES FOR /settings/network/wifi/usage/reset
#[get("/wifi/usage/reset")]
pub fn wifi_usage_reset(_auth: Authenticated) -> Flash<Redirect> {
let url = uri!(wifi_usage);
match monitor::reset_data() {
Ok(_) => Flash::success(Redirect::to(url), "Reset stored network traffic total"),
Err(_) => Flash::error(
Redirect::to(url),
"Failed to reset stored network traffic total",
),
}
}
#[post("/wifi/connect", data = "<network>")]
pub fn connect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &network.ssid;
let url = uri!(network_detail(ssid = ssid));
match network::id(&*WLAN_IFACE, ssid) {
Ok(Some(id)) => match network::connect(&id, &*WLAN_IFACE) {
Ok(_) => Flash::success(Redirect::to(url), "Connected to chosen network"),
Err(_) => Flash::error(Redirect::to(url), "Failed to connect to chosen network"),
},
_ => Flash::error(Redirect::to(url), "Failed to retrieve the network ID"),
}
}
#[post("/wifi/disconnect", data = "<network>")]
pub fn disconnect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &network.ssid;
let url = uri!(network_home);
match network::disable(&*WLAN_IFACE, ssid) {
Ok(_) => Flash::success(Redirect::to(url), "Disconnected from WiFi network"),
Err(_) => Flash::error(Redirect::to(url), "Failed to disconnect from WiFi network"),
}
}
#[post("/wifi/forget", data = "<network>")]
pub fn forget_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &network.ssid;
let url = uri!(network_home);
match network::forget(&*WLAN_IFACE, ssid) {
Ok(_) => Flash::success(Redirect::to(url), "WiFi credentials removed"),
Err(_) => Flash::error(
Redirect::to(url),
"Failed to remove WiFi credentials".to_string(),
),
}
}
#[get("/wifi/modify?<ssid>")]
pub fn wifi_password(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = Context::new();
context.insert("back", &Some("/settings/network/wifi".to_string()));
context.insert("title", &Some("Update WiFi Password".to_string()));
context.insert("selected", &Some(ssid.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.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
Template::render("settings/network/modify_ap", &context.into_json())
}
#[post("/wifi/modify", data = "<wifi>")]
pub fn wifi_set_password(wifi: Form<WiFi>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &wifi.ssid;
let pass = &wifi.pass;
let url = uri!(network_detail(ssid = ssid));
match network::update(&*WLAN_IFACE, ssid, pass) {
Ok(_) => Flash::success(Redirect::to(url), "WiFi password updated".to_string()),
Err(_) => Flash::error(
Redirect::to(url),
"Failed to update WiFi password".to_string(),
),
}
}
// HELPERS AND ROUTES FOR /settings/network
#[get("/")]
pub fn network_home(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context
let mut context = Context::new();
context.insert("back", &Some("/settings"));
context.insert("title", &Some("Network Configuration"));
context.insert("ap_state", &context::network::ap_state());
// 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.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
// template_dir is set in Rocket.toml
Template::render("settings/network/menu", &context.into_json())
}
// HELPERS AND ROUTES FOR /settings/network/ap/activate
#[get("/ap/activate")]
pub fn deploy_ap(_auth: Authenticated) -> Flash<Redirect> {
// activate the wireless access point
debug!("Activating WiFi access point.");
match network::start_iface_service(&*AP_IFACE) {
Ok(_) => Flash::success(
Redirect::to("/settings/network"),
"Activated WiFi access point",
),
Err(_) => Flash::error(
Redirect::to("/settings/network"),
"Failed to activate WiFi access point",
),
}
}
// HELPERS AND ROUTES FOR /settings/network/wifi
#[get("/wifi")]
pub fn wifi_list(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context through context_builder call
let mut context = NetworkListContext::build();
context.back = Some("/settings/network".to_string());
context.title = Some("WiFi Networks".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::render("settings/network/list_aps", &context)
}
// HELPERS AND ROUTES FOR /settings/network/wifi<ssid>
#[get("/wifi?<ssid>")]
pub fn network_detail(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkDetailContext::build();
context.back = Some("/settings/network/wifi".to_string());
context.title = Some("WiFi Network".to_string());
context.selected = Some(ssid.to_string());
if let Some(flash) = flash {
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
Template::render("settings/network/ap_details", &context)
}
// HELPERS AND ROUTES FOR /settings/network/wifi/activate
#[get("/wifi/activate")]
pub fn deploy_client(_auth: Authenticated) -> Flash<Redirect> {
// activate the wireless client
debug!("Activating WiFi client mode.");
match network::start_iface_service(&*WLAN_IFACE) {
Ok(_) => Flash::success(Redirect::to("/settings/network"), "Activated WiFi client"),
Err(_) => Flash::error(
Redirect::to("/settings/network"),
"Failed to activate WiFi client",
),
}
}
// HELPERS AND ROUTES FOR /settings/network/wifi/add
#[get("/wifi/add")]
pub fn add_wifi(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = Context::new();
context.insert("back", &Some("/settings/network".to_string()));
context.insert("title", &Some("Add WiFi Network".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.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
Template::render("settings/network/add_ap", &context.into_json())
}
#[get("/wifi/add?<ssid>")]
pub fn add_ssid(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = Context::new();
context.insert("back", &Some("/settings/network".to_string()));
context.insert("title", &Some("Add WiFi Network".to_string()));
context.insert("selected", &Some(ssid.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.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
Template::render("settings/network/add_ap", &context.into_json())
}
#[post("/wifi/add", data = "<wifi>")]
pub fn add_credentials(wifi: Form<WiFi>, _auth: Authenticated) -> Template {
let mut context = Context::new();
context.insert("back", &Some("/settings/network".to_string()));
context.insert("title", &Some("Add WiFi Network".to_string()));
// check if the credentials already exist for this access point
// note: this is nicer but it's an unstable feature:
// if check_saved_aps(&wifi.ssid).contains(true)
// use unwrap_or instead, set value to false if err is returned
//let creds_exist = network::saved_networks(&wifi.ssid).unwrap_or(false);
let creds_exist = match network::saved_networks() {
Ok(Some(networks)) => networks.contains(&wifi.ssid),
_ => false,
};
// if credentials not found, generate and write wifi config to wpa_supplicant
let (flash_name, flash_msg) = if creds_exist {
(
"error".to_string(),
"Network credentials already exist for this access point".to_string(),
)
} else {
match network::add(&*WLAN_IFACE, &wifi.ssid, &wifi.pass) {
Ok(_) => {
debug!("Added WiFi credentials.");
// force reread of wpa_supplicant.conf file with new credentials
match network::reconfigure() {
Ok(_) => debug!("Successfully reconfigured wpa_supplicant"),
Err(_) => warn!("Failed to reconfigure wpa_supplicant"),
}
("success".to_string(), "Added WiFi credentials".to_string())
}
Err(e) => {
debug!("Failed to add WiFi credentials.");
("error".to_string(), format!("{}", e))
}
}
};
context.insert("flash_name", &Some(flash_name));
context.insert("flash_msg", &Some(flash_msg));
Template::render("settings/network/add_ap", &context.into_json())
}
// HELPERS AND ROUTES FOR WIFI USAGE
#[get("/wifi/usage")]
pub fn wifi_usage(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkAlertContext::build();
// set back icon link to network route
context.back = Some("/settings/network".to_string());
context.title = Some("Network Data Usage".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("settings/network/data_usage_limits", &context)
}
#[post("/wifi/usage", data = "<thresholds>")]
pub fn wifi_usage_alerts(thresholds: Form<Threshold>, _auth: Authenticated) -> Flash<Redirect> {
match monitor::update_store(thresholds.into_inner()) {
Ok(_) => {
debug!("WiFi data usage thresholds updated.");
Flash::success(
Redirect::to("/settings/network/wifi/usage"),
"Updated alert thresholds and flags",
)
}
Err(_) => {
warn!("Failed to update WiFi data usage thresholds.");
Flash::error(
Redirect::to("/settings/network/wifi/usage"),
"Failed to update alert thresholds and flags",
)
}
}
}

View File

@ -1,97 +0,0 @@
use maud::{html, Markup, PreEscaped};
use peach_network::network;
use rouille::{post_input, try_or_400, Request, Response};
use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
theme,
},
WLAN_IFACE,
};
// ROUTE: /settings/network/wifi/add
fn render_ssid_input(selected_ap: Option<String>) -> Markup {
html! {
(PreEscaped("<!-- input for network ssid -->"))
input id="ssid" name="ssid" class="center input" type="text" placeholder="SSID" title="Network name (SSID) for WiFi access point" value=[selected_ap] autofocus;
}
}
fn render_password_input() -> Markup {
html! {
(PreEscaped("<!-- input for network password -->"))
input id="pass" name="pass" class="center input" type="password" placeholder="Password" title="Password for WiFi access point";
}
}
fn render_buttons() -> Markup {
html! {
(PreEscaped("<!-- BUTTONS -->"))
div id="buttons" {
input id="addWifi" class="button button-primary center" title="Add" type="submit" value="Add";
a class="button button-secondary center" href="/settings/network" title="Cancel" { "Cancel" }
}
}
}
/// WiFi access point credentials form template builder.
pub fn build_template(request: &Request, selected_ap: Option<String>) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let form_template = html! {
(PreEscaped("<!-- WIFI ADD CREDENTIALS FORM -->"))
div class="card center" {
form id="wifiCreds" action="/settings/network/wifi/add" method="post" {
(render_ssid_input(selected_ap))
(render_password_input())
(render_buttons())
}
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
};
let body = templates::nav::build_template(
form_template,
"Add WiFi Network",
Some("/settings/network"),
);
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Parse the SSID and password for an access point and save the new credentials.
pub fn handle_form(request: &Request) -> Response {
let data = try_or_400!(post_input!(request, {
ssid: String,
pass: String,
}));
let (name, msg) = match network::add(WLAN_IFACE, &data.ssid, &data.pass) {
Ok(_) => match network::reconfigure() {
Ok(_) => ("success".to_string(), "Added WiFi credentials".to_string()),
Err(err) => (
"error".to_string(),
format!(
"Added WiFi credentials but failed to reconfigure interface: {}",
err
),
),
},
Err(err) => (
"error".to_string(),
format!("Failed to add WiFi credentials for {}: {}", &data.ssid, err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/settings/network/wifi/add").add_flash(flash_name, flash_msg)
}

View File

@ -1,197 +0,0 @@
use std::collections::HashMap;
use maud::{html, Markup, PreEscaped};
use peach_network::{network, network::AccessPoint, NetworkError};
use rouille::Request;
use crate::{
templates,
utils::{flash::FlashRequest, theme},
WLAN_IFACE,
};
// ROUTE: /settings/network/wifi?<ssid>
fn render_network_status_icon(ssid: &str, wlan_ssid: &str, ap_state: &str) -> Markup {
let status_label_value = if ssid == wlan_ssid {
"CONNECTED"
} else if ap_state == "Available" {
"AVAILABLE"
} else {
"NOT IN RANGE"
};
html! {
(PreEscaped("<!-- NETWORK STATUS ICON -->"))
div class="grid-column-1" {
img id="wifiIcon" class="center icon" src="/icons/wifi.svg" alt="WiFi icon";
label class="center label-small font-gray" for="wifiIcon" title="Access Point Status" { (status_label_value) }
}
}
}
fn render_network_detailed_info(ssid: &str, ap_protocol: &str, ap_signal: Option<i32>) -> Markup {
let ap_signal_value = match ap_signal {
Some(signal) => signal.to_string(),
None => "Unknown".to_string(),
};
html! {
(PreEscaped("<!-- NETWORK DETAILED INFO -->"))
div class="grid-column-2" {
label class="label-small font-gray" for="netSsid" title="WiFi network SSID" { "SSID" };
p id="netSsid" class="card-text" title="SSID" { (ssid) }
label class="label-small font-gray" for="netSec" title="Security protocol" { "SECURITY" };
p id="netSec" class="card-text" title={ "Security protocol in use by " (ssid) } { (ap_protocol) }
label class="label-small font-gray" for="netSig" title="Signal Strength" { "SIGNAL" };
p id="netSig" class="card-text" title="Signal strength of WiFi access point" { (ap_signal_value) }
}
}
}
fn render_disconnect_form(ssid: &str) -> Markup {
html! {
form id="wifiDisconnect" action="/settings/network/wifi/disconnect" method="post" {
(PreEscaped("<!-- hidden element: allows ssid to be sent in request -->"))
input id="disconnectSsid" name="ssid" type="text" value=(ssid) style="display: none;";
input id="disconnectWifi" class="button button-warning center" title="Disconnect from Network" type="submit" value="Disconnect";
}
}
}
fn render_connect_form(ssid: &str) -> Markup {
html! {
form id="wifiConnect" action="/settings/network/wifi/connect" method="post" {
(PreEscaped("<!-- hidden element: allows ssid to be sent in request -->"))
input id="connectSsid" name="ssid" type="text" value=(ssid) style="display: none;";
input id="connectWifi" class="button button-primary center" title="Connect to Network" type="submit" value="Connect";
}
}
}
fn render_forget_form(ssid: &str) -> Markup {
html! {
form id="wifiForget" action="/settings/network/wifi/forget" method="post" {
(PreEscaped("<!-- hidden element: allows ssid to be sent in request -->"))
input id="forgetSsid" name="ssid" type="text" value=(ssid) style="display: none;";
input id="forgetWifi" class="button button-warning center" title="Forget Network" type="submit" value="Forget";
}
}
}
fn render_buttons(
selected_ap: &str,
wlan_ssid: &str,
ap: &AccessPoint,
saved_wifi_networks: Vec<String>,
) -> Markup {
html! {
(PreEscaped("<!-- BUTTONS -->"))
div id="buttons" {
@if wlan_ssid == selected_ap {
(render_disconnect_form(selected_ap))
}
@if saved_wifi_networks.contains(&selected_ap.to_string()) {
@if wlan_ssid != selected_ap && ap.state == "Available" {
(render_connect_form(selected_ap))
}
a class="button button-primary center" href={ "/settings/network/wifi/modify?ssid=" (selected_ap) } { "Modify" }
(render_forget_form(selected_ap))
} @else {
// display the Add button if AP creds not already in saved
// networks list
a class="button button-primary center" href={ "/settings/network/wifi/add?ssid=" (selected_ap) } { "Add" }
}
a class="button button-secondary center" href="/settings/network/wifi" title="Cancel" { "Cancel" }
}
}
}
/// Retrieve the list of all saved and in-range networks (including SSID and
/// AP details for each network), the list of all saved networks (SSIDs only)
/// and the SSID for the WiFi interface.
fn retrieve_network_data() -> (
Result<HashMap<String, AccessPoint>, NetworkError>,
Vec<String>,
String,
) {
let all_wifi_networks = network::all_networks(WLAN_IFACE);
let saved_wifi_networks = match network::saved_networks() {
Ok(Some(ssids)) => ssids,
_ => Vec::new(),
};
let wlan_ssid = match network::ssid(WLAN_IFACE) {
Ok(Some(ssid)) => ssid,
_ => String::from("Not connected"),
};
(all_wifi_networks, saved_wifi_networks, wlan_ssid)
}
/// WiFi access point (AP) template builder.
///
/// Render a UI card with details about the selected access point, including
/// the connection state, security protocol being used, the SSID and the
/// signal strength. Buttons are also rendering based on the state of the
/// access point and whether or not credentials for the AP have previously
/// been saved.
///
/// If the AP is available (ie. in-range) then a Connect button is rendered.
/// A Disconnect button is rendered if the WiFi client is currently
/// connected to the AP.
///
/// If credentials have not previously been saved for the AP, an Add button is
/// rendered. Forget and Modify buttons are rendered if credentials for the AP
/// have previously been saved.
pub fn build_template(request: &Request, selected_ap: String) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let (all_wifi_networks, saved_wifi_networks, wlan_ssid) = retrieve_network_data();
let network_info_box_class = if selected_ap == wlan_ssid {
"two-grid capsule success-border"
} else {
"two-grid capsule"
};
let network_list_template = html! {
(PreEscaped("<!-- NETWORK CARD -->"))
div class="card center" {
@if let Ok(wlan_networks) = all_wifi_networks {
// select only the access point we are interested in displaying
@if let Some((ssid, ap)) = wlan_networks.get_key_value(&selected_ap) {
@let ap_protocol = match &ap.detail {
Some(detail) => detail.protocol.clone(),
None => "None".to_string()
};
(PreEscaped("<!-- NETWORK INFO BOX -->"))
div class=(network_info_box_class) title="PeachCloud network mode and status" {
(PreEscaped("<!-- left column -->"))
(render_network_status_icon(ssid, &wlan_ssid, &ap.state))
(PreEscaped("<!-- right column -->"))
(render_network_detailed_info(ssid, &ap_protocol, ap.signal))
}
(render_buttons(ssid, &wlan_ssid, ap, saved_wifi_networks))
} @else {
p class="card-text list-item" { (selected_ap) " not found in saved or in-range networks" }
}
} @else {
p class="card-text list-item" { "No saved or in-range networks found" }
}
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
};
let body = templates::nav::build_template(
network_list_template,
"WiFi Networks",
Some("/settings/network"),
);
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}

View File

@ -1,201 +0,0 @@
use log::info;
use maud::{html, Markup, PreEscaped};
use peach_lib::{
config_manager, dyndns_client,
error::PeachError,
jsonrpc_client_core::{Error, ErrorKind},
jsonrpc_core::types::error::ErrorCode,
};
use rouille::{post_input, try_or_400, Request, Response};
use crate::{
error::PeachWebError,
templates,
utils::{
flash::{FlashRequest, FlashResponse},
theme,
},
};
// ROUTE: /settings/network/dns
fn render_dyndns_status_indicator() -> Markup {
let (indicator_class, indicator_label) = match dyndns_client::is_dns_updater_online() {
Ok(true) => ("success-border", "Dynamic DNS is currently online."),
_ => (
"warning-border",
"Dynamic DNS is enabled but may be offline.",
),
};
html! {
(PreEscaped("<!-- DYNDNS STATUS INDICATOR -->"))
div id="dyndns-status-indicator" class={ "stack capsule " (indicator_class) } {
div class="stack" {
label class="label-small font-near-black" { (indicator_label) }
}
}
}
}
fn render_external_domain_input() -> Markup {
let external_domain = config_manager::get_config_value("EXTERNAL_DOMAIN").ok();
html! {
div class="input-wrapper" {
(PreEscaped("<!-- input for externaldomain -->"))
label id="external_domain" class="label-small input-label font-near-black" {
label class="label-small input-label font-gray" for="external_domain" style="padding-top: 0.25rem;" { "External Domain (optional)" }
input id="external_domain" class="form-input" style="margin-bottom: 0;" name="external_domain" type="text" title="external domain" value=[external_domain];
}
}
}
}
fn render_dyndns_enabled_checkbox() -> Markup {
let dyndns_enabled = config_manager::get_dyndns_enabled_value().unwrap_or(false);
html! {
div class="input-wrapper" {
div {
(PreEscaped("<!-- checkbox for dyndns flag -->"))
label class="label-small input-label font-gray" { "Enable Dynamic DNS" }
input style="margin-left: 0px;" id="enable_dyndns" name="enable_dyndns" title="Activate dynamic DNS" type="checkbox" checked[dyndns_enabled];
}
}
}
}
fn render_dynamic_domain_input() -> Markup {
let dyndns_domain =
config_manager::get_config_value("DYN_DOMAIN").unwrap_or_else(|_| String::from(""));
let dyndns_subdomain =
dyndns_client::get_dyndns_subdomain(&dyndns_domain).unwrap_or(dyndns_domain);
html! {
div class="input-wrapper" {
(PreEscaped("<!-- input for dyndns domain -->"))
label id="cut" class="label-small input-label font-near-black" {
label class="label-small input-label font-gray" for="cut" style="padding-top: 0.25rem;" { "Dynamic DNS Domain" }
input id="dyndns_domain" class="alert-input" name="dynamic_domain" placeholder="" type="text" title="dyndns_domain" value=(dyndns_subdomain);
{ ".dyn.peachcloud.org" }
}
}
}
}
fn render_save_button() -> Markup {
html! {
div id="buttonDiv" style="margin-top: 2rem;" {
input id="configureDNSButton" class="button button-primary center" title="Add" type="submit" value="Save";
}
}
}
/// DNS configuration form template builder.
pub fn build_template(request: &Request) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let dyndns_enabled = config_manager::get_dyndns_enabled_value().unwrap_or(false);
let form_template = html! {
(PreEscaped("<!-- CONFIGURE DNS FORM -->"))
div class="card center" {
@if dyndns_enabled {
(render_dyndns_status_indicator())
}
form id="configureDNS" class="center" action="/settings/network/dns" method="post" {
(render_external_domain_input())
(render_dyndns_enabled_checkbox())
(render_dynamic_domain_input())
(render_save_button())
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
}
};
let body = templates::nav::build_template(
form_template,
"Configure Dynamic DNS",
Some("/settings/network"),
);
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
pub fn save_dns_configuration(
external_domain: String,
enable_dyndns: bool,
dynamic_domain: String,
) -> Result<(), PeachWebError> {
// first save local configurations
config_manager::set_external_domain(&external_domain)?;
config_manager::set_dyndns_enabled_value(enable_dyndns)?;
let full_dynamic_domain = dyndns_client::get_full_dynamic_domain(&dynamic_domain);
// if dynamic dns is enabled and this is a new domain name, then register it
if enable_dyndns && dyndns_client::check_is_new_dyndns_domain(&full_dynamic_domain)? {
if let Err(registration_err) = dyndns_client::register_domain(&full_dynamic_domain) {
info!("Failed to register dyndns domain: {:?}", registration_err);
// error message describing the failed update
let err_msg = match registration_err {
PeachError::JsonRpcClientCore(Error(ErrorKind::JsonRpcError(rpc_err), _)) => {
if let ErrorCode::ServerError(-32030) = rpc_err.code {
format!(
"Error registering domain: {} was previously registered",
full_dynamic_domain
)
} else {
format!("Failed to register dyndns domain: {:?}", rpc_err)
}
}
_ => "Failed to register dyndns domain".to_string(),
};
Err(PeachWebError::FailedToRegisterDynDomain(err_msg))
} else {
info!("Registered new dyndns domain");
Ok(())
}
} else {
info!("Domain {} already registered", dynamic_domain);
Ok(())
}
}
/// Parse the DNS configuration parameters and apply them.
pub fn handle_form(request: &Request) -> Response {
let data = try_or_400!(post_input!(request, {
external_domain: String,
enable_dyndns: bool,
dynamic_domain: String,
}));
let (name, msg) = match save_dns_configuration(
data.external_domain,
data.enable_dyndns,
data.dynamic_domain,
) {
Ok(_) => (
"success".to_string(),
"New dynamic DNS configuration is now enabled".to_string(),
),
Err(err) => (
"error".to_string(),
format!("Failed to save DNS configuration: {}", err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/settings/network/dns").add_flash(flash_name, flash_msg)
}

View File

@ -1,164 +0,0 @@
// TODO:
//
// This template and associated feature set requires vnstat_parse.
// - https://crates.io/crates/vnstat_parse
//
// Use the PeachCloud config system to store warning and cutoff flags,
// as well as the associated totals (thresholds):
//
// - DATA_WARNING_ENABLED
// - DATA_WARNING_LIMIT
// - DATA_CUTOFF_ENABLED
// - DATA_CUTOFF_LIMIT
use maud::{html, Markup, PreEscaped};
use peach_network::network;
use rouille::Request;
use vnstat_parse::Vnstat;
use crate::{
templates,
utils::{flash::FlashRequest, theme},
WLAN_IFACE,
};
// ROUTE: /settings/network/wifi/usage
fn render_data_usage_total_capsule() -> Markup {
html! {
div class="stack capsule" style="margin-left: 2rem; margin-right: 2rem;" {
div class="flex-grid" {
label id="dataTotal" class="label-large" title="Data download total in MB" {
data_total.total / 1024 / 1024 | round
}
label class="label-small font-near-black" { "MB" }
}
label class="center-text label-small font-gray" { "USAGE TOTAL" }
}
}
}
fn render_warning_threshold_icon() -> Markup {
// threshold.warn_flag
let warning_enabled = true;
let icon_class = match warning_enabled {
true => "icon",
false => "icon icon-inactive",
};
html! {
div class="card-container container" {
div {
img id="warnIcon" class=(icon_class) alt="Warning" title="Warning threshold" src="/icons/alert.svg";
}
}
}
}
fn render_warning_threshold_input() -> Markup {
// TODO: source threshold.warn value and replace below
html! {
div {
(PreEscaped("<!-- input for warning threshold -->"))
label id="warn" class="label-small font-near-black" {
input id="warnInput" class="alert-input" name="warn" placeholder="0" type="text" title="Warning threshold value" value="{{ threshold.warn }}" { "MB" }
}
label class="label-small font-gray" for="warn" style="padding-top: 0.25rem;" { "WARNING THRESHOLD" }
}
}
}
fn render_warning_threshold_checkbox() -> Markup {
let warning_enabled = true;
html! {
div {
(PreEscaped("<!-- checkbox for warning threshold flag -->"))
input id="warnCheck" name="warn_flag" title="Activate warning" type="checkbox" checked[warning_enabled];
}
}
}
fn render_critical_threshold_icon() -> Markup {
// threshold.cut_flag
let cutoff_enabled = true;
let icon_class = match cutoff_enabled {
true => "icon",
false => "icon icon-inactive",
};
html! {
div {
img id="cutIcon"
class=(icon_class)
alt="Cutoff"
title="Cutoff threshold"
src="/icons/scissor.svg";
}
}
}
fn render_critical_threshold_input() -> Markup {
// TODO: source threshold.cut value and replace below
html! {
div {
(PreEscaped("<!-- input for cutoff threshold -->"))
label id="cut" class="label-small font-near-black"><input id="cutInput" class="alert-input" name="cut" placeholder="0" type="text" title="Critical threshold value" value="{{ threshold.cut }}" { "MB" }
label class="label-small font-gray" for="cut" style="padding-top: 0.25rem;" { "CUTOFF THRESHOLD" }
}
}
}
fn render_critical_threshold_checkbox() -> Markup {
// threshold.cut_flag
let cutoff_enabled = true;
html! {
div {
(PreEscaped("<!-- checkbox for cutoff threshold flag -->"))
input id="cutCheck" name="cut_flag" title="Activate cutoff" type="checkbox" checked[cutoff_enabled];
}
}
}
fn render_buttons() -> Markup {
html! {
div id="buttonDiv" class="button-div" {
input id="updateAlerts" class="button button-primary center" title="Update" type="submit" value="Update";
a id="resetTotal" class="button button-warning center" href="/settings/network/wifi/usage/reset" title="Reset stored usage total to zero" { "Reset" }
a class="button button-secondary center" href="/settings/network" title="Cancel" { "Cancel" }
}
}
}
/// WiFi data usage form template builder.
pub fn build_template(request: &Request) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let wlan_data = Vnstat::get(WLAN_IFACE);
// wlan_data.all_time_total
// wlan_data.all_time_total_unit
let form_template = html! {
(PreEscaped("<!-- NETWORK DATA ALERTS FORM -->"))
form id="wifiAlerts" action="/network/wifi/usage" class="card center" method="post" {
(render_data_usage_total_capsule())
(render_warning_threshold_icon())
(render_warning_threshold_input())
(render_warning_threshold_checkbox())
(render_critical_threshold_icon())
(render_critical_threshold_input())
(render_critical_threshold_checkbox())
(render_buttons())
}
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
};
}

View File

@ -1,106 +0,0 @@
use std::collections::HashMap;
use maud::{html, Markup, PreEscaped};
use peach_network::{network, network::AccessPoint};
use crate::{templates, utils::theme, AP_IFACE, WLAN_IFACE};
// ROUTE: /settings/network/wifi
/// Retrieve network state data required by the WiFi network list template.
fn get_network_state_data(ap: &str, wlan: &str) -> (String, String, HashMap<String, AccessPoint>) {
let ap_state = match network::state(ap) {
Ok(Some(state)) => state,
_ => "Interface unavailable".to_string(),
};
let wlan_ssid = match network::ssid(wlan) {
Ok(Some(ssid)) => ssid,
_ => "Not connected".to_string(),
};
let network_list = match network::all_networks(wlan) {
Ok(networks) => networks,
Err(_) => HashMap::new(),
};
(ap_state, wlan_ssid, network_list)
}
fn render_network_connected_elements(ssid: String) -> Markup {
let ap_detail_url = format!("/network/wifi?ssid={}", ssid);
html! {
a class="list-item link primary-bg" href=(ap_detail_url) {
img id="netStatus" class="icon icon-active icon-medium list-icon" src="/icons/wifi.svg" alt="WiFi online";
p class="list-text" { (ssid) }
label class="label-small list-label font-gray" for="netStatus" title="Status" { "Connected" }
}
}
}
fn render_network_available_elements(ssid: String, ap_state: String) -> Markup {
let ap_detail_url = format!("/network/wifi?ssid={}", ssid);
html! {
a class="list-item link light-bg" href=(ap_detail_url) {
img id="netStatus" class="icon icon-inactive icon-medium list-icon" src="/icons/wifi.svg" alt="WiFi offline";
p class="list-text" { (ssid) }
label class="label-small list-label font-gray" for="netStatus" title="Status" { (ap_state) }
}
}
}
fn render_network_unavailable_elements(ssid: String, ap_state: String) -> Markup {
let ap_detail_url = format!("/network/wifi?ssid={}", ssid);
html! {
a class="list-item link" href=(ap_detail_url) {
img id="netStatus" class="icon icon-inactive icon-medium list-icon" src="/icons/wifi.svg" alt="WiFi offline";
p class="list-text" { (ssid) }
label class="label-small list-label font-gray" for="netStatus" title="Status" { (ap_state) }
}
}
}
/// WiFi network list template builder.
pub fn build_template() -> PreEscaped<String> {
let (ap_state, wlan_ssid, network_list) = get_network_state_data(AP_IFACE, WLAN_IFACE);
let list_template = html! {
div class="card center" {
div class="center list-container" {
ul class="list" {
@if ap_state == "up" {
li class="list-item light-bg warning-border" {
"Enable WiFi client mode to view saved and available networks."
}
} @else if network_list.is_empty() {
li class="list-item light-bg" {
"No saved or available networks found."
}
} @else {
@for (ssid, ap) in network_list {
li {
@if ssid == wlan_ssid {
(render_network_connected_elements(ssid))
} @else if ap.state == "Available" {
(render_network_available_elements(ssid, ap.state))
} @else {
(render_network_unavailable_elements(ssid, ap.state))
}
}
}
}
}
}
}
};
let body =
templates::nav::build_template(list_template, "WiFi Networks", Some("/settings/network"));
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}

View File

@ -1,65 +0,0 @@
use maud::{html, Markup, PreEscaped};
use peach_network::network;
use rouille::Request;
use crate::{
templates,
utils::{flash::FlashRequest, theme},
AP_IFACE,
};
// ROUTE: /settings/network
/// Read the wireless interface mode (WiFi AP or client) and selectively render
/// the activation button for the deactivated mode.
fn render_mode_toggle_button() -> Markup {
match network::state(AP_IFACE) {
Ok(Some(state)) if state == "up" => {
html! {
a id="connectWifi" class="button button-primary center" href="/settings/network/wifi/activate" title="Enable WiFi" { "Enable WiFi" }
}
}
_ => html! {
a id="deployAccessPoint" class="button button-primary center" href="/settings/network/ap/activate" title="Deploy Access Point" { "Deploy Access Point" }
},
}
}
fn render_buttons() -> Markup {
html! {
(PreEscaped("<!-- BUTTONS -->"))
div id="buttons" {
a class="button button-primary center" href="/settings/network/wifi/add" title="Add WiFi Network" { "Add WiFi Network" }
a id="configureDNS" class="button button-primary center" href="/settings/network/dns" title="Configure DNS" { "Configure DNS" }
(PreEscaped("<!-- if ap is up, show 'Enable WiFi' button, else show 'Deplay Access Point' -->"))
(render_mode_toggle_button())
a id="listWifi" class="button button-primary center" href="/settings/network/wifi" title="List WiFi Networks" { "List WiFi Networks" }
// TODO: uncomment this once data usage feature is in place
// a id="viewUsage" class="button button-primary center" href="/settings/network/wifi/usage" title="View Data Usage" { "View Data Usage" }
a id="viewStatus" class="button button-primary center" href="/status/network" title="View Network Status" { "View Network Status" }
}
}
}
/// Network settings menu template builder.
pub fn build_template(request: &Request) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let menu_template = html! {
(PreEscaped("<!-- NETWORK SETTINGS MENU -->"))
div class="card center" {
(render_buttons())
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
};
let body = templates::nav::build_template(menu_template, "Network Settings", Some("/settings"));
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}

View File

@ -1,8 +0,0 @@
pub mod add_ap;
pub mod ap_details;
pub mod configure_dns;
// TODO: uncomment this once data usage feature is in place
// pub mod data_usage_limits;
pub mod list_aps;
pub mod menu;
pub mod modify_ap;

View File

@ -1,105 +0,0 @@
use maud::{html, Markup, PreEscaped};
use peach_network::network;
use rouille::{post_input, try_or_400, Request, Response};
use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
theme,
},
WLAN_IFACE,
};
// ROUTE: /settings/network/wifi/modify?<ssid>
fn render_ssid_input(selected_ap: Option<String>) -> Markup {
html! {
(PreEscaped("<!-- input for network ssid -->"))
input id="ssid" name="ssid" class="center input" type="text" placeholder="SSID" title="Network name (SSID) for WiFi access point" value=[selected_ap] autofocus;
}
}
fn render_password_input() -> Markup {
html! {
(PreEscaped("<!-- input for network password -->"))
input id="pass" name="pass" class="center input" type="password" placeholder="Password" title="Password for WiFi access point";
}
}
fn render_buttons() -> Markup {
html! {
(PreEscaped("<!-- BUTTONS -->"))
div id="buttons" {
input id="savePassword" class="button button-primary center" title="Save" type="submit" value="Save";
a class="button button-secondary center" href="/settings/network" title="Cancel" { "Cancel" }
}
}
}
/// WiFi access point password modification form template builder.
pub fn build_template(request: &Request, selected_ap: Option<String>) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let form_template = html! {
(PreEscaped("<!-- NETWORK MODIFY AP PASSWORD FORM -->"))
div class="card center" {
form id="wifiModify" action="/settings/network/wifi/modify" method="post" {
(render_ssid_input(selected_ap))
(render_password_input())
(render_buttons())
}
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
};
let body = templates::nav::build_template(
form_template,
"Change WiFi Password",
Some("/settings/network"),
);
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Parse the SSID and password for an access point and save the new password.
pub fn handle_form(request: &Request) -> Response {
let data = try_or_400!(post_input!(request, {
ssid: String,
pass: String,
}));
let (name, msg) = match network::id(WLAN_IFACE, &data.ssid) {
Ok(Some(id)) => match network::modify(&id, &data.ssid, &data.pass) {
Ok(_) => ("success".to_string(), "WiFi password updated".to_string()),
Err(err) => (
"error".to_string(),
format!("Failed to update WiFi password: {}", err),
),
},
Ok(None) => (
"error".to_string(),
format!(
"Failed to update WiFi password: no saved credentials found for network {}",
&data.ssid
),
),
Err(err) => (
"error".to_string(),
format!(
"Failed to update WiFi password: no ID found for network {}: {}",
&data.ssid, err
),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/settings/network/wifi/modify").add_flash(flash_name, flash_msg)
}

View File

@ -1,37 +0,0 @@
use maud::{html, PreEscaped};
use rouille::Request;
use crate::{
templates,
utils::{flash::FlashRequest, theme},
};
/// Power menu template builder.
///
/// Presents options for rebooting or shutting down the device.
pub fn build_template(request: &Request) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
let power_menu_template = html! {
(PreEscaped("<!-- POWER MENU -->"))
div class="card center" {
div class="card-container" {
div id="buttons" {
a id="rebootBtn" class="button button-primary center" href="/reboot" title="Reboot Device" { "Reboot" }
a id="shutdownBtn" class="button button-warning center" href="/shutdown" title="Shutdown Device" { "Shutdown" }
a id="cancelBtn" class="button button-secondary center" href="/settings" title="Cancel" { "Cancel" }
}
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
}
};
let body = templates::nav::build_template(power_menu_template, "Power Menu", Some("/"));
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}

View File

@ -1,3 +0,0 @@
pub mod menu;
pub mod reboot;
pub mod shutdown;

View File

@ -1,36 +0,0 @@
use log::info;
use rouille::Response;
use std::{
io::Result,
process::{Command, Output},
};
use crate::utils::flash::FlashResponse;
/// Executes a system command to reboot the device immediately.
fn reboot() -> Result<Output> {
info!("Rebooting the device");
// ideally, we'd like to reboot after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "rebooting..." message to `peach-oled` for display
Command::new("sudo")
.arg("shutdown")
.arg("-r")
.arg("now")
.output()
}
pub fn handle_reboot() -> Response {
let (name, msg) = match reboot() {
Ok(_) => ("success".to_string(), "Rebooting the device".to_string()),
Err(err) => (
"error".to_string(),
format!("Failed to reboot the device: {}", err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/power").add_flash(flash_name, flash_msg)
}

View File

@ -1,35 +0,0 @@
use log::info;
use rouille::Response;
use std::{
io::Result,
process::{Command, Output},
};
use crate::utils::flash::FlashResponse;
/// Executes a system command to shutdown the device immediately.
fn shutdown() -> Result<Output> {
info!("Shutting down the device");
// ideally, we'd like to shutdown after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "shutting down..." message to `peach-oled` for display
Command::new("sudo").arg("shutdown").arg("now").output()
}
pub fn handle_shutdown() -> Response {
let (name, msg) = match shutdown() {
Ok(_) => (
"success".to_string(),
"Shutting down the device".to_string(),
),
Err(err) => (
"error".to_string(),
format!("Failed to shutdown the device: {}", err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/power").add_flash(flash_name, flash_msg)
}

View File

@ -122,15 +122,6 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
input type="text" id="database_dir" name="repo" value=(sbot_config.repo);
}
div class="center" {
@if sbot_config.enable_ebt {
input type="checkbox" id="ebtReplication" style="margin-bottom: 1rem;" name="enable_ebt" checked;
} @else {
input type="checkbox" id="ebtReplication" style="margin-bottom: 1rem;" name="enable_ebt";
}
label class="font-normal" for="ebtReplication" title="Enable Epidemic Broadcast Tree (EBT) replication instead of legacy replication" {
"Enable EBT Replication"
}
br;
@if sbot_config.localadv {
input type="checkbox" id="lanBroadcast" style="margin-bottom: 1rem;" name="localadv" checked;
} @else {
@ -166,6 +157,7 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
input type="hidden" id="hmac" name="hmac" value=(sbot_config.hmac);
input type="hidden" id="wslis" name="wslis" value=(sbot_config.wslis);
input type="hidden" id="debuglis" name="debuglis" value=(sbot_config.debuglis);
input type="hidden" id="enable_ebt" name="enable_ebt" value=(sbot_config.enable_ebt);
input type="hidden" id="promisc" name="promisc" value=(sbot_config.promisc);
input type="hidden" id="nounixsock" name="nounixsock" value=(sbot_config.nounixsock);
(PreEscaped("<!-- BUTTONS -->"))
@ -183,11 +175,8 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body = templates::nav::build_template(
menu_template,
"Scuttlebutt Settings",
Some("/settings/scuttlebutt"),
);
let body =
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();

View File

@ -4,7 +4,7 @@ use rouille::Request;
use crate::{
templates,
utils::{cookie::CookieRequest, flash::FlashRequest, theme},
utils::{flash::FlashRequest, theme},
};
/// Read the status of the go-sbot service and render buttons accordingly.
@ -53,13 +53,10 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
}
};
// retrieve the value of the "back_url" cookie
// if the cookie value is not found then set a hardcoded fallback value
let back_url = request.retrieve_cookie("back_url").or(Some("/settings"));
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body = templates::nav::build_template(menu_template, "Scuttlebutt Settings", back_url);
let body =
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();

View File

@ -1,381 +1,238 @@
use std::process::Command;
use maud::{html, Markup, PreEscaped};
use peach_lib::{config_manager, dyndns_client, oled_client};
use peach_stats::{
stats,
stats::{CpuStatPercentages, MemStat},
use log::info;
use rocket::{
get,
request::FlashMessage,
response::{Flash, Redirect},
};
use rocket_dyn_templates::Template;
use serde::Serialize;
use std::{
io,
process::{Command, Output},
};
use crate::{templates, utils::theme};
use peach_lib::{
config_manager::load_peach_config, dyndns_client, network_client, oled_client, sbot::SbotStatus,
};
use peach_stats::{
stats,
stats::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat},
};
// ROUTE: /status
use crate::routes::authentication::Authenticated;
/// Query systemd to determine the state of the networking service.
fn retrieve_networking_state() -> Option<String> {
// call: `systemctl show networking.service --no-page`
let networking_service_output = Command::new("systemctl")
.arg("show")
.arg("networking.service")
.arg("--no-page")
.output()
.ok()?;
// HELPERS AND ROUTES FOR /status
let service_info = std::str::from_utf8(&networking_service_output.stdout).ok()?;
// find the line starting with "ActiveState=" and return the value
service_info
.lines()
.find(|line| line.starts_with("ActiveState="))
.and_then(|line| line.strip_prefix("ActiveState="))
.map(|state| state.to_string())
/// System statistics data.
#[derive(Debug, Serialize)]
pub struct StatusContext {
pub back: Option<String>,
pub cpu_stat_percent: Option<CpuStatPercentages>,
pub disk_stats: Vec<DiskUsage>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
pub load_average: Option<LoadAverage>,
pub mem_stats: Option<MemStat>,
pub network_ping: String,
pub oled_ping: String,
pub dyndns_enabled: bool,
pub dyndns_is_online: bool,
pub config_is_valid: bool,
pub sbot_is_online: bool,
pub title: Option<String>,
pub uptime: Option<i32>,
}
/// Query systemd to determine the state of the sbot service.
fn retrieve_sbot_state() -> Option<String> {
// retrieve the name of the go-sbot service or set default
let go_sbot_service = config_manager::get_config_value("GO_SBOT_SERVICE")
.unwrap_or_else(|_| "go-sbot.service".to_string());
let sbot_service_output = Command::new("systemctl")
.arg("show")
.arg(go_sbot_service)
.arg("--no-page")
.output()
.ok()?;
let service_info = std::str::from_utf8(&sbot_service_output.stdout).ok()?;
// find the line starting with "ActiveState=" and return the value
service_info
.lines()
.find(|line| line.starts_with("ActiveState="))
.and_then(|line| line.strip_prefix("ActiveState="))
.map(|state| state.to_string())
}
fn retrieve_device_status_data() -> (Option<u64>, String) {
let uptime = stats::uptime().ok();
let oled_ping = match oled_client::ping() {
Ok(_) => "ONLINE".to_string(),
Err(_) => "OFFLINE".to_string(),
};
(uptime, oled_ping)
}
fn retrieve_device_usage_data() -> (Option<CpuStatPercentages>, Option<MemStat>) {
// convert result to Option<CpuStatPercentages>, discard any error
let cpu_stat_percent = stats::cpu_stats_percent().ok();
let mem_stats = stats::mem_stats().ok();
(cpu_stat_percent, mem_stats)
}
fn render_network_capsule() -> Markup {
let (state, stack_class, img_class) = match retrieve_networking_state() {
Some(state) if state.as_str() == "active" => {
("active", "stack capsule border-success", "icon icon-medium")
}
Some(state) if state.as_str() == "inactive" => (
"inactive",
"stack capsule border-warning",
"icon icon-inactive icon-medium",
),
Some(state) if state.as_str() == "failed" => (
"failed",
"stack capsule border-danger",
"icon icon-inactive icon-medium",
),
_ => (
"error",
"stack capsule border-danger",
"icon icon-inactive icon-medium",
),
};
html! {
(PreEscaped("<!-- PEACH-NETWORK STATUS STACK -->"))
div class=(stack_class) {
img id="networkIcon" class=(img_class) alt="Network" title="Networking service status" src="icons/wifi.svg";
div class="stack" style="padding-top: 0.5rem;" {
label class="label-small font-near-black" { "Networking" }
label class="label-small font-near-black" { (state.to_uppercase()) }
}
}
}
}
fn render_oled_capsule(state: String) -> Markup {
let (stack_class, img_class) = match state.as_str() {
"ONLINE" => ("stack capsule border-success", "icon icon-medium"),
_ => (
"stack capsule border-warning",
"icon icon-inactive icon-medium",
),
};
html! {
(PreEscaped("<!-- PEACH-OLED STATUS STACK -->"))
div class=(stack_class) {
img id="oledIcon" class=(img_class) alt="Display" title="OLED display microservice status" src="icons/lcd.svg";
div class="stack" style="padding-top: 0.5rem;" {
label class="label-small font-near-black" { "Display" }
label class="label-small font-near-black" { (state) }
}
}
}
}
fn render_diagnostics_capsule() -> Markup {
// TODO: write a diagnostics module (maybe in peach-lib)
let diagnostics_state = "CLEAR";
html! {
(PreEscaped("<!-- DIAGNOSTICS AND LOGS STACK -->"))
div class="stack capsule border-success" {
img id="statsIcon" class="icon icon-medium" alt="Line chart" title="System diagnostics and logs" src="icons/chart.svg";
div class="stack" style="padding-top: 0.5rem;" {
label class="label-small font-near-black" { "Diagnostics" }
label class="label-small font-near-black" { (diagnostics_state) };
}
}
}
}
fn render_dyndns_capsule() -> Markup {
let (state, stack_class, img_class) = match dyndns_client::is_dns_updater_online() {
Ok(true) => ("ONLINE", "stack capsule border-success", "icon icon-medium"),
Ok(false) => (
"OFFLINE",
"stack capsule border-warning",
"icon icon-inactive icon-medium",
),
Err(_) => (
"ERROR",
"stack capsule border-danger",
"icon icon-inactive icon-medium",
),
};
html! {
(PreEscaped("<!-- DYNDNS STATUS STACK -->"))
div class=(stack_class) {
img id="dnsIcon" class=(img_class) alt="Dyndns" title="Dyndns status" src="icons/dns.png";
div class="stack" style="padding-top: 0.5rem;" {
label class="label-small font-near-black" { "Dyn DNS" }
label class="label-small font-near-black" { (state) }
}
}
}
}
fn render_config_capsule() -> Markup {
let (state, stack_class, img_class) =
match config_manager::load_peach_config_from_disc().is_ok() {
true => ("LOADED", "stack capsule border-success", "icon icon-medium"),
false => (
"INVALID",
"stack capsule border-warning",
"icon icon-inactive icon-medium",
),
impl StatusContext {
pub fn build() -> StatusContext {
// convert result to Option<CpuStatPercentages>, discard any error
let cpu_stat_percent = stats::cpu_stats_percent().ok();
let load_average = stats::load_average().ok();
let mem_stats = stats::mem_stats().ok();
// TODO: add `wpa_supplicant_status` to peach_network to replace this ping call
// instead of: "is the network json-rpc server running?", we want to ask:
// "is the wpa_supplicant systemd service functioning correctly?"
let network_ping = match network_client::ping() {
Ok(_) => "ONLINE".to_string(),
Err(_) => "OFFLINE".to_string(),
};
let oled_ping = match oled_client::ping() {
Ok(_) => "ONLINE".to_string(),
Err(_) => "OFFLINE".to_string(),
};
html! {
(PreEscaped("<!-- CONFIG STATUS STACK -->"))
div class=(stack_class) {
img id="configIcon" class=(img_class) alt="Config" title="Config status" src="icons/clipboard.png";
div class="stack" style="padding-top: 0.5rem;" {
label class="label-small font-near-black" { "Config" }
label class="label-small font-near-black" { (state) }
let uptime = match stats::uptime() {
Ok(secs) => {
let uptime_mins = secs / 60;
uptime_mins.to_string()
}
Err(_) => "Unavailable".to_string(),
};
// parse the uptime string to a signed integer (for math)
let uptime_parsed = uptime.parse::<i32>().ok();
// serialize disk usage data into Vec<DiskUsage>
let disk_usage_stats = match stats::disk_usage() {
Ok(disks) => disks,
Err(_) => Vec::new(),
};
let mut disk_stats = Vec::new();
// select only the partition we're interested in: /dev/mmcblk0p2 ("/")
for disk in disk_usage_stats {
if disk.mountpoint == "/" {
disk_stats.push(disk);
}
}
}
}
fn render_sbot_capsule() -> Markup {
let (state, stack_class, img_class) = match retrieve_sbot_state() {
Some(state) if state.as_str() == "active" => {
("active", "stack capsule border-success", "icon icon-medium")
}
Some(state) if state.as_str() == "inactive" => (
"inactive",
"stack capsule border-warning",
"icon icon-inactive icon-medium",
),
Some(state) if state.as_str() == "failed" => (
"failed",
"stack capsule border-danger",
"icon icon-inactive icon-medium",
),
_ => (
"error",
"stack capsule border-danger",
"icon icon-inactive icon-medium",
),
};
html! {
(PreEscaped("<!-- SBOT STATUS STACK -->"))
div class=(stack_class) {
img id="sbotIcon" class=(img_class) alt="Sbot" title="Sbot status" src="icons/hermies.svg";
div class="stack" style="padding-top: 0.5rem;" {
label class="label-small font-near-black" { "Sbot" }
label class="label-small font-near-black" { (state.to_uppercase()) }
}
}
}
}
fn render_cpu_usage_meter(cpu_usage_percent: Option<CpuStatPercentages>) -> Markup {
html! {
@if let Some(cpu_usage) = cpu_usage_percent {
@let cpu_usage_total = (cpu_usage.nice + cpu_usage.system + cpu_usage.user).round();
div class="flex-grid" {
span class="card-text" { "CPU" }
span class="label-small push-right" { (cpu_usage_total) "%" }
}
meter value=(cpu_usage_total) min="0" max="100" title="CPU usage" {
div class="meter-gauge" {
span style={ "width: " (cpu_usage_total) "%;" } {
"CPU Usage"
// dyndns_is_online & config_is_valid
let dyndns_enabled: bool;
let dyndns_is_online: bool;
let config_is_valid: bool;
let load_peach_config_result = load_peach_config();
match load_peach_config_result {
Ok(peach_config) => {
dyndns_enabled = peach_config.dyn_enabled;
config_is_valid = true;
if dyndns_enabled {
let is_dyndns_online_result = dyndns_client::is_dns_updater_online();
match is_dyndns_online_result {
Ok(is_online) => {
dyndns_is_online = is_online;
}
Err(_err) => {
dyndns_is_online = false;
}
}
} else {
dyndns_is_online = false;
}
}
} @else {
p class="card-text" { "CPU usage data unavailable" }
Err(_err) => {
dyndns_enabled = false;
dyndns_is_online = false;
config_is_valid = false;
}
}
// test if go-sbot is running
let sbot_status = SbotStatus::read();
let sbot_is_online: bool = match sbot_status {
// return true if state is active
Ok(status) => matches!(status.state == Some("active".to_string()), true),
_ => false,
};
StatusContext {
back: None,
cpu_stat_percent,
disk_stats,
flash_name: None,
flash_msg: None,
load_average,
mem_stats,
network_ping,
oled_ping,
dyndns_enabled,
dyndns_is_online,
config_is_valid,
sbot_is_online,
title: None,
uptime: uptime_parsed,
}
}
}
fn render_mem_usage_meter(mem_stats: Option<MemStat>) -> Markup {
html! {
@if let Some(mem) = mem_stats {
// convert kilobyte values to megabyte values
@let mem_free_mb = mem.free / 1024;
@let mem_total_mb = mem.total / 1024;
@let mem_used_mb = mem.used / 1024;
// calculate memory usage as a percentage
@let mem_used_percent = mem_used_mb * 100 / mem_total_mb;
// render disk free value as megabytes or gigabytes based on size
@let mem_free_value = if mem_free_mb > 1024 {
format!("{} GB", (mem_free_mb / 1024))
} else {
format!("{} MB", mem_free_mb)
};
div class="flex-grid" {
span class="card-text" { "Memory" }
span class="label-small push-right" { (mem_used_percent) "% (" (mem_free_value) " free)" }
}
meter value=(mem_used_mb) min="0" max=(mem_total_mb) title="Memory usage" {
div class="meter-gauge" {
span style={ "width: " (mem_used_percent) "%;" } { "Memory Usage" }
}
}
} @else {
p class="card-text" { "Memory usage data unavailable" }
}
}
}
fn render_disk_usage_meter() -> Markup {
let disk_usage_stats = match stats::disk_usage() {
Ok(disks) => disks,
Err(_) => Vec::new(),
#[get("/")]
pub fn device_status(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context through context_builder call
let mut context = StatusContext::build();
context.back = Some("/".to_string());
context.title = Some("Device 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/device", &context)
}
// select only the partition we're interested in: /dev/mmcblk0p2 ("/")
let disk_usage = disk_usage_stats.iter().find(|disk| disk.mountpoint == "/");
// HELPERS AND ROUTES FOR /power/reboot
html! {
@if let Some(disk) = disk_usage {
// calculate free disk space in megabytes
@let disk_free_mb = disk.one_k_blocks_free / 1024;
// calculate free disk space in gigabytes
@let disk_free_gb = disk_free_mb / 1024;
// render disk free value as megabytes or gigabytes based on size
@let disk_free_value = if disk_free_mb > 1024 {
format!("{} GB", disk_free_gb)
} else {
format!("{} MB", disk_free_mb)
};
/// Executes a system command to reboot the device immediately.
pub fn reboot() -> io::Result<Output> {
info!("Rebooting the device");
// ideally, we'd like to reboot after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "rebooting..." message to `peach-oled` for display
Command::new("sudo")
.arg("shutdown")
.arg("-r")
.arg("now")
.output()
}
div class="flex-grid" {
span class="card-text" { "Disk" }
span class="label-small push-right" { (disk.used_percentage) "% (" (disk_free_value) " free)" }
}
meter value=(disk.used_percentage) min="0" max="100" title="Disk usage" {
div class="meter-gauge" {
span style={ "width: " (disk.used_percentage) "%;" } {
"Disk Usage"
}
}
}
} @else {
p class="card-text" { "Disk usage data unavailable" }
#[get("/power/reboot")]
pub fn reboot_cmd(_auth: Authenticated) -> Flash<Redirect> {
match reboot() {
Ok(_) => Flash::success(Redirect::to("/power"), "Rebooting the device"),
Err(_) => Flash::error(Redirect::to("/power"), "Failed to reboot the device"),
}
}
// HELPERS AND ROUTES FOR /power/shutdown
/// Executes a system command to shutdown the device immediately.
pub fn shutdown() -> io::Result<Output> {
info!("Shutting down the device");
// ideally, we'd like to reboot after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "shutting down..." message to `peach-oled` for display
Command::new("sudo").arg("shutdown").arg("now").output()
}
#[get("/power/shutdown")]
pub fn shutdown_cmd(_auth: Authenticated) -> Flash<Redirect> {
match shutdown() {
Ok(_) => Flash::success(Redirect::to("/power"), "Shutting down the device"),
Err(_) => Flash::error(Redirect::to("/power"), "Failed to shutdown the device"),
}
}
// HELPERS AND ROUTES FOR /power
#[derive(Debug, Serialize)]
pub struct PowerContext {
pub back: Option<String>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
pub title: Option<String>,
}
impl PowerContext {
pub fn build() -> PowerContext {
PowerContext {
back: None,
flash_name: None,
flash_msg: None,
title: None,
}
}
}
/// Display system uptime in hours and minutes.
fn render_uptime_capsule(uptime: Option<u64>) -> Markup {
html! {
@if let Some(uptime_secs) = uptime {
@let uptime_mins = uptime_secs / 60;
@if uptime_mins < 60 {
// display system uptime in minutes
p class="capsule center-text" {
"Uptime: " (uptime_mins) " minutes"
}
} @else {
// display system uptime in hours and minutes
@let hours = uptime_mins / 60;
@let mins = uptime_mins % 60;
p class="capsule center-text" {
"Uptime: " (hours) " hours, " (mins) " minutes"
}
}
} @else {
p class="card-text" { "Uptime data unavailable" }
}
}
}
/// Device status template builder.
pub fn build_template() -> PreEscaped<String> {
let (uptime, oled_state) = retrieve_device_status_data();
let (cpu_usage, mem_usage) = retrieve_device_usage_data();
let device_status_template = html! {
(PreEscaped("<!-- DEVICE STATUS CARD -->"))
div class="card center" {
div class="card-container" {
// display status capsules for network, oled and diagnostics
div class="three-grid" {
(render_network_capsule())
(render_oled_capsule(oled_state))
(render_diagnostics_capsule())
}
// display status capsules for dyndns, config and sbot
div class="three-grid" style="padding-top: 1rem; padding-bottom: 1rem;" {
(render_dyndns_capsule())
(render_config_capsule())
(render_sbot_capsule())
}
(render_cpu_usage_meter(cpu_usage))
(render_mem_usage_meter(mem_usage))
(render_disk_usage_meter())
(render_uptime_capsule(uptime))
}
}
#[get("/power")]
pub fn power_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = PowerContext::build();
context.back = Some("/".to_string());
context.title = Some("Power Menu".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());
};
let body = templates::nav::build_template(device_status_template, "Device Status", Some("/"));
let theme = theme::get_theme();
templates::base::build_template(body, theme)
Template::render("power", &context)
}

View File

@ -1,3 +1,3 @@
pub mod device;
pub mod network;
//pub mod device;
//pub mod network;
pub mod scuttlebutt;

View File

@ -1,285 +1,21 @@
use maud::{html, Markup, PreEscaped};
use peach_network::network;
use vnstat_parse::Vnstat;
use rocket::{get, request::FlashMessage};
use rocket_dyn_templates::Template;
use crate::{templates, utils::theme, AP_IFACE, WLAN_IFACE};
use crate::context::network::NetworkStatusContext;
use crate::routes::authentication::Authenticated;
enum NetworkState {
AccessPoint,
WiFiClient,
}
// HELPERS AND ROUTES FOR /status/network
// ROUTE: /status/network
#[get("/network")]
pub fn network_status(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkStatusContext::build();
context.back = Some("/status".to_string());
context.title = Some("Network Status".to_string());
/// Render the cog icon which is used as a link to the network settings page.
fn render_network_config_icon() -> Markup {
html! {
(PreEscaped("<!-- top-right config icon -->"))
a class="link two-grid-top-right" href="/settings/network" title="Configure network settings" {
img id="configureNetworking" class="icon-small" src="/icons/cog.svg" alt="Configure";
}
}
}
/// Render the network mode icon, either a WiFi signal or router, based
/// on the state of the AP and WiFi interfaces.
///
/// A router icon is shown if the AP is online (interface is "up").
///
/// A WiFi signal icon is shown if the AP interface is down. The colour of
/// the icon is black if the WLAN interface is up and gray if it's down.
fn render_network_mode_icon(state: &NetworkState) -> Markup {
// TODO: make this DRYer
let (icon_class, icon_src, icon_alt, label_title, label_value) = match state {
NetworkState::AccessPoint => (
"center icon icon-active",
"/icons/router.svg",
"WiFi router",
"Access Point Online",
"ONLINE",
),
NetworkState::WiFiClient => match network::state(WLAN_IFACE) {
Ok(Some(state)) if state == "up" => (
"center icon icon-active",
"/icons/wifi.svg",
"WiFi signal",
"WiFi Client Online",
"ONLINE",
),
_ => (
"center icon icon-inactive",
"/icons/wifi.svg",
"WiFi signal",
"WiFi Client Offline",
"OFFLINE",
),
},
if let Some(flash) = flash {
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
html! {
(PreEscaped("<!-- network mode icon with label -->"))
div class="grid-column-1" {
img id="netModeIcon" class=(icon_class) src=(icon_src) alt=(icon_alt);
label id="netModeLabel" for="netModeIcon" class="center label-small font-gray" title=(label_title) { (label_value) }
}
}
}
/// Render the network data associated with the deployed access point or
/// connected WiFi client depending on active mode.
///
/// Data includes the network mode (access point or WiFi client), SSID and IP
/// address.
fn render_network_data(state: &NetworkState, ssid: String, ip: String) -> Markup {
let (mode_value, mode_title, ssid_value, ip_title) = match state {
NetworkState::AccessPoint => (
"Access Point",
"Access Point SSID",
// TODO: remove hardcoding of this value (query interface instead)
"peach",
"Access Point IP Address",
),
NetworkState::WiFiClient => (
"WiFi Client",
"WiFi SSID",
ssid.as_str(),
"WiFi Client IP Address",
),
};
html! {
(PreEscaped("<!-- network mode, ssid & ip with labels -->"))
div class="grid-column-2" {
label class="label-small font-gray" for="netMode" title="Network Mode" { "MODE" }
p id="netMode" class="card-text" title="Network Mode" { (mode_value) }
label class="label-small font-gray" for="netSsid" title=(mode_title) { "SSID" }
p id="netSsid" class="card-text" title="SSID" { (ssid_value) }
label class="label-small font-gray" for="netIp" title=(ip_title) { "IP" }
p id="netIp" class="card-text" title="IP" { (ip) }
}
}
}
/// Render the network status grid comprised of the network config icon,
/// network mode icon and network data text.
fn render_network_status_grid(state: &NetworkState, ssid: String, ip: String) -> Markup {
html! {
(PreEscaped("<!-- NETWORK STATUS GRID -->"))
div class="two-grid" title="PeachCloud network mode and status" {
(render_network_config_icon())
(PreEscaped("<!-- left column -->"))
(render_network_mode_icon(state))
(PreEscaped("<!-- right column -->"))
(render_network_data(state, ssid, ip))
}
}
}
/// Render the signal strength stack comprised of a signal icon, RSSI value
/// and label.
///
/// This stack is displayed when the network mode is set to WiFi
/// client (ie. the value reported is the strength of the connection of the
/// local WiFi interface to a remote access point).
fn render_signal_strength_stack() -> Markup {
let wlan_rssi = match network::rssi(WLAN_IFACE) {
Ok(Some(rssi)) => rssi,
_ => 0.to_string(),
};
html! {
div class="stack" {
img id="netSignal" class="icon icon-medium" alt="Signal" title="WiFi Signal (%)" src="/icons/low-signal.svg";
div class="flex-grid" style="padding-top: 0.5rem;" {
label class="label-medium" for="netSignal" style="padding-right: 3px;" title="Signal strength of WiFi connection (%)" { (wlan_rssi) }
}
label class="label-small font-gray" { "SIGNAL" }
}
}
}
/// Render the connected devices stack comprised of a devices icon, value
/// of connected devices and label.
///
/// This stack is displayed when the network mode is set to access point
/// (ie. the value reported is the number of remote devices connected to the
/// local access point).
fn render_connected_devices_stack() -> Markup {
html! {
div class="stack" {
img id="devices" class="icon icon-medium" title="Connected devices" src="/icons/devices.svg" alt="Digital devices";
div class="flex-grid" style="padding-top: 0.5rem;" {
label class="label-medium" for="devices" style="padding-right: 3px;" title="Number of connected devices";
}
label class="label-small font-gray" { "DEVICES" }
}
}
}
/// Render the data download stack comprised of a download icon, traffic value
/// and label.
///
/// A zero value is displayed if no interface traffic is available for the
/// WLAN interface.
fn render_data_download_stack(iface_traffic: &Option<Vnstat>) -> Markup {
html! {
div class="stack" {
img id="dataDownload" class="icon icon-medium" title="Download" src="/icons/down-arrow.svg" alt="Download";
div class="flex-grid" style="padding-top: 0.5rem;" {
@if let Some(traffic) = iface_traffic {
label class="label-medium" for="dataDownload" style="padding-right: 3px;" title={ "Data download total in " (traffic.all_time_rx_unit) } { (traffic.all_time_rx) }
label class="label-small font-near-black" { (traffic.all_time_rx_unit) }
} @else {
label class="label-medium" for="dataDownload" style="padding-right: 3px;" title="Data download total" { "0" }
label class="label-small font-near-black";
}
}
label class="label-small font-gray" { "DOWNLOAD" }
}
}
}
/// Render the data upload stack comprised of an upload icon, traffic value
/// and label.
///
/// A zero value is displayed if no interface traffic is available for the
/// WLAN interface.
fn render_data_upload_stack(iface_traffic: Option<Vnstat>) -> Markup {
html! {
div class="stack" {
img id="dataUpload" class="icon icon-medium" title="Upload" src="/icons/up-arrow.svg" alt="Upload";
div class="flex-grid" style="padding-top: 0.5rem;" {
@if let Some(traffic) = iface_traffic {
label class="label-medium" for="dataUpload" style="padding-right: 3px;" title={ "Data upload total in " (traffic.all_time_tx_unit) } { (traffic.all_time_tx) }
label class="label-small font-near-black" { (traffic.all_time_tx_unit) }
} @else {
label class="label-medium" for="dataUpload" style="padding-right: 3px;" title="Data upload total" { "0" }
label class="label-small font-near-black";
}
}
label class="label-small font-gray" { "UPLOAD" }
}
}
}
/// Render the device / signal and traffic grid.
///
/// The connected devices stack is displayed if the network mode is set to
/// access point and the signal strength stack is displayed if the network
/// mode is set to WiFi client.
fn render_device_and_traffic_grid(state: NetworkState, iface_traffic: Option<Vnstat>) -> Markup {
html! {
div class="three-grid card-container" {
@match state {
NetworkState::AccessPoint => (render_connected_devices_stack()),
NetworkState::WiFiClient => (render_signal_strength_stack()),
}
(render_data_download_stack(&iface_traffic))
(render_data_upload_stack(iface_traffic))
}
}
}
/// Network state data retrieval.
///
/// This data is injected into the template rendering functions.
fn retrieve_network_data() -> (NetworkState, Option<Vnstat>, String, String) {
// if the access point interface is "up",
// retrieve the traffic stats, ip and ssidfor the ap interface.
// otherwise retrieve the stats and ip for the wlan interface.
let (state, traffic, ip, ssid) = match network::state(AP_IFACE) {
Ok(Some(state)) if state == "up" => {
let ap_traffic = Vnstat::get(AP_IFACE).ok();
let ap_ip = match network::ip(AP_IFACE) {
Ok(Some(ip)) => ip,
_ => String::from("x.x.x.x"),
};
let ap_ssid = String::from("peach");
(NetworkState::AccessPoint, ap_traffic, ap_ip, ap_ssid)
}
_ => {
let wlan_traffic = Vnstat::get(WLAN_IFACE).ok();
let wlan_ip = match network::ip(WLAN_IFACE) {
Ok(Some(ip)) => ip,
_ => String::from("x.x.x.x"),
};
let wlan_ssid = match network::ssid(WLAN_IFACE) {
Ok(Some(ssid)) => ssid,
_ => String::from("Not connected"),
};
(NetworkState::WiFiClient, wlan_traffic, wlan_ip, wlan_ssid)
}
};
(state, traffic, ip, ssid)
}
/// Network status template builder.
pub fn build_template() -> PreEscaped<String> {
let (state, traffic, ip, ssid) = retrieve_network_data();
let network_status_template = html! {
(PreEscaped("<!-- NETWORK STATUS CARD -->"))
div class="card center" {
(PreEscaped("<!-- NETWORK INFO BOX -->"))
div class="capsule capsule-container success-border" {
(render_network_status_grid(&state, ssid, ip))
hr style="color: var(--light-gray);";
(render_device_and_traffic_grid(state, traffic))
}
}
};
let body =
templates::nav::build_template(network_status_template, "Network Status", Some("/status"));
let theme = theme::get_theme();
templates::base::build_template(body, theme)
Template::render("status/network", &context)
}

View File

@ -1,25 +1,5 @@
use maud::{html, PreEscaped, DOCTYPE};
/// JavaScript event listener for the back button on the top navigation bar of
/// the UI.
///
/// When the button is clicked, prevent the default behaviour and invoke
/// the history API to load the previous URL (page) in the history list.
fn js_back_button_script() -> PreEscaped<String> {
html! {
(PreEscaped("
<script>
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('backButton').onclick = function(e) {
e.preventDefault();
history.back();
}
});
</script>
"))
}
}
/// Base template builder.
///
/// Takes an HTML body as input and splices it into the base template.
@ -34,7 +14,6 @@ pub fn build_template(body: PreEscaped<String>, theme: String) -> PreEscaped<Str
meta name="viewport" content="width=devide-width, initial-scale=1.0";
link rel="stylesheet" href="/css/peachcloud.css";
link rel="stylesheet" href="/css/_variables.css";
(js_back_button_script())
title { "PeachCloud" }
}
body {

View File

@ -14,7 +14,7 @@ pub fn build_template(
let theme = theme::get_theme();
// conditionally render the hermies icon and theme-switcher icon with correct link
let (hermies, theme_switcher) = match theme.as_str() {
let (hermies, switcher) = match theme.as_str() {
// if we're using the dark theme, render light icons and "light" query param
"dark" => (
"/icons/hermies_hex_light.svg",
@ -38,7 +38,7 @@ pub fn build_template(
html! {
(PreEscaped("<!-- Top navigation bar -->"))
nav class="nav-bar" {
a id="backButton" class="nav-item" href=[back] title="Back" {
a class="nav-item" href=[back] title="Back" {
img class="icon-medium nav-icon-left icon-active" src="/icons/back.svg" alt="Back";
}
h1 class="nav-title" { (title) }
@ -56,7 +56,8 @@ pub fn build_template(
a class="nav-item" href="/" {
img class="icon nav-icon-left" src="/icons/peach-icon.png" alt="PeachCloud" title="Home";
}
(theme_switcher)
// render the pre-defined theme-switcher icon
(switcher)
}
}
}

View File

@ -1,64 +0,0 @@
use rouille::{input, Request, Response};
// The CookieRequest and CookieResponse traits are currently only used
// to add, retrieve and reset the `back_url` cookie. That cookie is
// used to set the URL of the in-UI back button when visiting a page
// which can be arrived at via several paths.
//
// An example of this is the Scuttlebutt Settings menu (/settings/scuttlebutt),
// which can be accessed via the Settings menu (/settings) or the Scuttlebutt
// Status page (/status/scuttlebutt). We need to be able to set the path of
// the back button to point to the correct page (ie. the one from which we've
// come).
//
// The `back_url` cookie is also used on the Profile page
// (/scuttlebutt/profile).
/// Cookie trait for `Request`.
pub trait CookieRequest {
/// Retrieve a cookie value from a `Request`.
fn retrieve_cookie(&self, cookie_name: &str) -> Option<&str>;
}
impl CookieRequest for Request {
fn retrieve_cookie(&self, cookie_name: &str) -> Option<&str> {
// check for cookie using given name
let cookie_val = input::cookies(self)
.find(|&(n, _)| n == cookie_name)
// return the value of the cookie (key is already known)
.map(|key_val| key_val.1);
cookie_val
}
}
/// Cookie trait for `Response`.
pub trait CookieResponse {
/// Add a cookie containing the given data to a `Response`. Data should be
/// in the form of `cookie_name=cookie_val`.
fn add_cookie(self, cookie_name_val: &str) -> Response;
/// Reset a cookie value for a `Response`.
fn reset_cookie(self, cookie_name: &str) -> Response;
}
impl CookieResponse for Response {
fn add_cookie(self, cookie_name_val: &str) -> Response {
// set the cookie header
// max-age is currently set to 3600 seconds (1 hour)
self.with_additional_header(
"Set-Cookie",
format!("{}; Max-Age=3600; SameSite=Lax; Path=/", cookie_name_val),
)
}
fn reset_cookie(self, cookie_name: &str) -> Response {
// set a blank cookie to clear the cookie from the previous request
self.with_additional_header(
"Set-Cookie",
format!(
"{}=; Max-Age=0; SameSite=Lax; Path=/; Expires=Fri, 21 Aug 1987 12:00:00 UTC",
cookie_name
),
)
}
}

View File

@ -1,4 +1,3 @@
pub mod cookie;
pub mod flash;
pub mod sbot;
pub mod theme;

View File

@ -3,6 +3,7 @@ use std::{
error::Error,
fs,
fs::File,
io,
io::prelude::*,
path::Path,
process::{Command, Output},
@ -12,14 +13,9 @@ use async_std::task;
use dirs;
use futures::stream::TryStreamExt;
use golgi::{
api::{friends::RelationshipQuery, history_stream::CreateHistoryStream},
blobs,
messages::SsbMessageKVT,
sbot::Keystore,
Sbot,
api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, sbot::Keystore, Sbot,
};
use log::debug;
use peach_lib::config_manager;
use peach_lib::sbot::SbotConfig;
use rouille::input::post::BufferedFile;
use temporary::Directory;
@ -29,13 +25,12 @@ use crate::{error::PeachWebError, utils::sbot};
// SBOT HELPER FUNCTIONS
/// Executes a systemctl command for the go-sbot.service process.
pub fn systemctl_sbot_cmd(cmd: &str) -> Result<Output, PeachWebError> {
let output = Command::new("sudo")
pub fn systemctl_sbot_cmd(cmd: &str) -> io::Result<Output> {
Command::new("sudo")
.arg("systemctl")
.arg(cmd)
.arg(config_manager::get_config_value("GO_SBOT_SERVICE")?)
.output()?;
Ok(output)
.arg("go-sbot.service")
.output()
}
/// Executes a systemctl stop command followed by start command.
@ -73,18 +68,15 @@ pub async fn init_sbot_with_config(
) -> Result<Sbot, PeachWebError> {
debug!("Initialising an sbot client with configuration parameters");
// initialise sbot connection with ip:port and shscap from config file
let key_path = format!(
"{}/secret",
config_manager::get_config_value("GO_SBOT_DATADIR")?
);
let sbot_client = match sbot_config {
// TODO: panics if we pass `Some(conf.shscap)` as second arg
Some(conf) => {
let ip_port = conf.lis.clone();
Sbot::init(Keystore::CustomGoSbot(key_path), Some(ip_port), None).await?
Sbot::init(Keystore::GoSbot, Some(ip_port), None).await?
}
None => Sbot::init(Keystore::CustomGoSbot(key_path), None, None).await?,
None => Sbot::init(Keystore::GoSbot, None, None).await?,
};
Ok(sbot_client)
}
@ -137,9 +129,8 @@ pub fn latest_sequence_number() -> Result<u64, Box<dyn Error>> {
// retrieve the local id
let id = sbot_client.whoami().await?;
let args = CreateHistoryStream::new(id).keys_values(true, true);
let history_stream = sbot_client.create_history_stream(args).await?;
let mut msgs: Vec<SsbMessageKVT> = history_stream.try_collect().await?;
let history_stream = sbot_client.create_history_stream(id).await?;
let mut msgs: Vec<SsbMessageValue> = history_stream.try_collect().await?;
// there will be zero messages when the sbot is run for the first time
if msgs.is_empty() {
@ -149,7 +140,7 @@ pub fn latest_sequence_number() -> Result<u64, Box<dyn Error>> {
msgs.reverse();
// return the sequence number of the latest msg
Ok(msgs[0].value.sequence)
Ok(msgs[0].sequence)
}
})
}
@ -162,13 +153,7 @@ pub fn create_invite(uses: u16) -> Result<String, Box<dyn Error>> {
let mut sbot_client = init_sbot_with_config(&sbot_config).await?;
debug!("Generating Scuttlebutt invite code");
let mut invite_code = sbot_client.invite_create(uses).await?;
// insert domain into invite if one is configured
let domain = config_manager::get_config_value("EXTERNAL_DOMAIN")?;
if !domain.is_empty() {
invite_code = domain + &invite_code[4..];
}
let invite_code = sbot_client.invite_create(uses).await?;
Ok(invite_code)
})