add configuration routes for the sbot

This commit is contained in:
glyph 2022-02-02 14:14:12 +02:00
parent 90a90096f4
commit 89b502be25
4 changed files with 174 additions and 41 deletions

View File

@ -51,6 +51,8 @@ pub fn mount_peachpub_routes(rocket: Rocket<Build>) -> Rocket<Build> {
routes![
ssb_settings_menu,
configure_sbot,
configure_sbot_default,
configure_sbot_post,
restart_sbot,
start_sbot,
stop_sbot

View File

@ -93,11 +93,7 @@ pub struct Post {
/// Publish a public Scuttlebutt post. Redirects to profile page of the PeachCloud local identity with a flash message describing the outcome of the action (may be successful or unsuccessful).
#[post("/publish", data = "<post>")]
pub fn publish(
post: Form<Post>,
flash: Option<FlashMessage>,
_auth: Authenticated,
) -> Flash<Redirect> {
pub fn publish(post: Form<Post>, _auth: Authenticated) -> Flash<Redirect> {
let post_text = &post.text;
// perform the sbotcli publish action using post_text
// if successful, redirect to home profile page and flash "success"
@ -119,11 +115,7 @@ pub struct PublicKey {
/// Follow a Scuttlebutt profile specified by the given public key. Redirects to the appropriate profile page with a flash message describing the outcome of the action (may be successful or unsuccessful).
#[post("/follow", data = "<pub_key>")]
pub fn follow(
pub_key: Form<PublicKey>,
flash: Option<FlashMessage>,
_auth: Authenticated,
) -> Flash<Redirect> {
pub fn follow(pub_key: Form<PublicKey>, _auth: Authenticated) -> Flash<Redirect> {
let public_key = &pub_key.key;
// perform the sbotcli follow action using &pub_key.0
// if successful, redirect to profile page with provided public key and flash "success"
@ -138,11 +130,7 @@ pub fn follow(
/// Unfollow a Scuttlebutt profile specified by the given public key. Redirects to the appropriate profile page with a flash message describing the outcome of the action (may be successful or unsuccessful).
#[post("/unfollow", data = "<pub_key>")]
pub fn unfollow(
pub_key: Form<PublicKey>,
flash: Option<FlashMessage>,
_auth: Authenticated,
) -> Flash<Redirect> {
pub fn unfollow(pub_key: Form<PublicKey>, _auth: Authenticated) -> Flash<Redirect> {
let public_key = &pub_key.key;
// perform the sbotcli unfollow action using &pub_key.0
// if successful, redirect to profile page with provided public key and flash "success"
@ -157,11 +145,7 @@ pub fn unfollow(
/// Block a Scuttlebutt profile specified by the given public key. Redirects to the appropriate profile page with a flash message describing the outcome of the action (may be successful or unsuccessful).
#[post("/block", data = "<pub_key>")]
pub fn block(
pub_key: Form<PublicKey>,
flash: Option<FlashMessage>,
_auth: Authenticated,
) -> Flash<Redirect> {
pub fn block(pub_key: Form<PublicKey>, _auth: Authenticated) -> Flash<Redirect> {
let public_key = &pub_key.key;
// perform the sbotcli block action using &pub_key.0
// if successful, redirect to profile page with provided public key and flash "success"

View File

@ -3,26 +3,64 @@ use std::{
process::{Command, Output},
};
use log::info;
use peach_stats::sbot;
use log::{info, warn};
use peach_lib::sbot::{SbotConfig, SbotStatus};
use rocket::{
get,
form::{Form, FromForm},
get, post,
request::FlashMessage,
response::{Flash, Redirect},
serde::Deserialize,
};
use rocket_dyn_templates::{tera::Context, Template};
use crate::routes::authentication::Authenticated;
#[derive(Debug, Deserialize, FromForm)]
pub struct SbotConfigForm {
/// Directory path for the log and indexes.
repo: String,
/// Directory path for writing debug output.
debugdir: String,
/// Secret-handshake app-key (aka. network key).
shscap: String,
/// HMAC hash used to sign messages.
hmac: String,
/// Replication hops (1: friends, 2: friends of friends).
hops: u8,
/// IP address to listen on.
lis_ip: String,
/// Port to listen on.
lis_port: String,
/// Address to listen on for WebSocket connections.
wslis: String,
/// Address to for metrics and pprof HTTP server.
debuglis: String,
/// Enable sending local UDP broadcasts.
localadv: bool,
/// Enable listening for UDP broadcasts and connecting.
localdiscov: bool,
/// Enable syncing by using epidemic-broadcast-trees (EBT).
enable_ebt: bool,
/// Bypass graph auth and fetch remote's feed (useful for pubs that are restoring their data
/// from peer; user beware - caveats about).
promisc: bool,
/// Disable the UNIX socket RPC interface.
nounixsock: bool,
/// Run the go-sbot on system start-up (systemd service enabled).
startup: bool,
}
// HELPERS AND ROUTES FOR /settings/scuttlebutt
/// Scuttlebutt settings menu.
#[get("/")]
pub fn ssb_settings_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// retrieve go-sbot systemd process status
let sbot_status = SbotStatus::read().ok();
let mut context = Context::new();
// retrieve go-sbot systemd process stats
let sbot_stats = sbot::sbot_stats().ok();
context.insert("sbot_stats", &sbot_stats);
context.insert("sbot_status", &sbot_status);
context.insert("back", &Some("/settings".to_string()));
context.insert("title", &Some("Scuttlebutt Settings".to_string()));
@ -37,9 +75,18 @@ pub fn ssb_settings_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> T
/// Sbot configuration page (includes form for updating configuration parameters).
#[get("/configure")]
pub fn configure_sbot(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// retrieve go-sbot systemd process status
let sbot_status = SbotStatus::read().ok();
let run_on_startup = sbot_status.map(|status| status.boot_state);
// retrieve sbot config parameters
let sbot_config = SbotConfig::read().ok();
let mut context = Context::new();
context.insert("back", &Some("/settings/scuttlebutt".to_string()));
context.insert("title", &Some("Sbot Configuration".to_string()));
context.insert("sbot_config", &sbot_config);
context.insert("run_on_startup", &Some(run_on_startup));
if let Some(flash) = flash {
context.insert("flash_name", &Some(flash.kind().to_string()));
@ -49,12 +96,90 @@ pub fn configure_sbot(flash: Option<FlashMessage>, _auth: Authenticated) -> Temp
Template::render("settings/scuttlebutt/configure_sbot", &context.into_json())
}
// TODO: consider using `Contextual` here to collect all submitted form
// fields to re-render forms with submitted values on error
/// Receive the sbot configuration form data and save it to file.
#[post("/configure", data = "<config>")]
pub fn configure_sbot_post(config: Form<SbotConfigForm>, _auth: Authenticated) -> Flash<Redirect> {
// call `into_inner()` to take ownership of the `config` data
let owned_config = config.into_inner();
// concat the ip and port for listen address
let lis = format!("{}:{}", owned_config.lis_ip, owned_config.lis_port);
// instantiate `SbotConfig` from form data
let config = SbotConfig {
lis,
hops: owned_config.hops,
repo: owned_config.repo,
debugdir: owned_config.debugdir,
shscap: owned_config.shscap,
localadv: owned_config.localadv,
localdiscov: owned_config.localdiscov,
hmac: owned_config.hmac,
wslis: owned_config.wslis,
debuglis: owned_config.debuglis,
enable_ebt: owned_config.enable_ebt,
promisc: owned_config.promisc,
nounixsock: owned_config.nounixsock,
};
match owned_config.startup {
true => {
info!("Enabling go-sbot.service");
match systemctl_sbot_cmd("enable") {
Err(e) => warn!("Failed to enable go-sbot.service: {}", e),
_ => (),
}
}
false => {
info!("Disabling go-sbot.service");
match systemctl_sbot_cmd("disable") {
Err(e) => warn!("Failed to disable go-sbot.service: {}", e),
_ => (),
}
}
};
// write config to file
match SbotConfig::write(config) {
Ok(_) => Flash::success(
Redirect::to("/settings/scuttlebutt/configure"),
"Updated configuration",
),
Err(e) => Flash::error(
Redirect::to("/settings/scuttlebutt/configure"),
format!("Failed to update configuration: {}", e),
),
}
}
/// Set default configuration parameters for the go-sbot and save them to file.
#[get("/configure/default")]
pub fn configure_sbot_default(_auth: Authenticated) -> Flash<Redirect> {
let default_config = SbotConfig::default();
// write default config to file
match SbotConfig::write(default_config) {
Ok(_) => Flash::success(
Redirect::to("/settings/scuttlebutt/configure"),
"Restored default configuration",
),
Err(e) => Flash::error(
Redirect::to("/settings/scuttlebutt/configure"),
format!("Failed to restore default configuration: {}", e),
),
}
}
/// Attempt to start the go-sbot.service process.
/// Redirect to the Scuttlebutt settings menu and communicate the outcome of
/// the attempt via a flash message.
#[get("/start")]
pub fn start_sbot(_auth: Authenticated) -> Flash<Redirect> {
match start_sbot_cmd() {
info!("Starting go-sbot.service");
match systemctl_sbot_cmd("start") {
Ok(_) => Flash::success(
Redirect::to("/settings/scuttlebutt"),
"Sbot process has been started",
@ -71,7 +196,8 @@ pub fn start_sbot(_auth: Authenticated) -> Flash<Redirect> {
/// the attempt via a flash message.
#[get("/stop")]
pub fn stop_sbot(_auth: Authenticated) -> Flash<Redirect> {
match stop_sbot_cmd() {
info!("Stopping go-sbot.service");
match systemctl_sbot_cmd("stop") {
Ok(_) => Flash::success(
Redirect::to("/settings/scuttlebutt"),
"Sbot process has been stopped",
@ -88,10 +214,10 @@ pub fn stop_sbot(_auth: Authenticated) -> Flash<Redirect> {
/// the attempt via a flash message.
#[get("/restart")]
pub fn restart_sbot(_auth: Authenticated) -> Flash<Redirect> {
// try to stop the process
match stop_sbot_cmd() {
info!("Restarting go-sbot.service");
match systemctl_sbot_cmd("stop") {
// if stop was successful, try to start the process
Ok(_) => match start_sbot_cmd() {
Ok(_) => match systemctl_sbot_cmd("start") {
Ok(_) => Flash::success(
Redirect::to("/settings/scuttlebutt"),
"Sbot process has been restarted",
@ -111,6 +237,16 @@ pub fn restart_sbot(_auth: Authenticated) -> Flash<Redirect> {
// HELPER FUNCTIONS
/// Executes a systemctl command for the go-sbot.service process.
pub fn systemctl_sbot_cmd(cmd: &str) -> io::Result<Output> {
//info!("Disabling go-sbot.service");
Command::new("systemctl")
.arg("--user")
.arg(cmd)
.arg("go-sbot.service")
.output()
}
/// Executes a systemctl disable command for the go-sbot.service process.
pub fn disable_sbot_cmd() -> io::Result<Output> {
info!("Disabling go-sbot.service");

View File

@ -14,7 +14,7 @@
{%- endif -%}
<!-- SBOT CONFIGURATION FORM -->
<div class="card center">
<form>
<form id="sbotConfig" action="/settings/scuttlebutt/configure" method="post">
<div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Number of hops to replicate">
<label for="hops" class="label-small">HOPS</label>
<div id="hops" style="display: flex; justify-content: space-evenly;">
@ -34,37 +34,48 @@
<div class="center" style="display: flex; justify-content: space-between; width: 80%;">
<div style="display: flex; flex-direction: column; width: 60%;" title="IP address on which the sbot runs">
<label for="ip" class="label-small">IP ADDRESS</label>
<input type="text" id="ip" name="ip" value="{{ listen_addr.0 }}">
<input type="text" id="ip" name="lis_ip" value="{{ listen_addr.0 }}">
</div>
<div style="display: flex; flex-direction: column; width: 20%;" title="Port on which the sbot runs">
<label for="port" class="label-small">PORT</label>
<input type="text" id="port" name="port" value="{{ listen_addr.1 }}">
<input type="text" id="port" name="lis_port" value="{{ listen_addr.1 }}">
</div>
</div>
<br>
<div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Network key (aka 'caps key') to define the Scuttleverse in which the sbot operates in">
<label for="network_key" class="label-small">NETWORK KEY</label>
<input type="text" id="network_key" name="network_key" value="{{ sbot_config.shscap }}">
<input type="text" id="network_key" name="shscap" value="{{ sbot_config.shscap }}">
</div>
<br>
<div class="center" style="display: flex; flex-direction: column; width: 80%;" title="Directory in which the sbot database is saved">
<label for="database_dir" class="label-small">DATABASE DIRECTORY</label>
<input type="text" id="database_dir" name="database_dir" value="{{ sbot_config.repo }}">
<input type="text" id="database_dir" name="repo" value="{{ sbot_config.repo }}">
</div>
<br>
<div class="center" style="width: 80%;" title="Broadcast the IP and port of this sbot instance so that local peers can discovery it and attempt to connect">
<input type="checkbox" id="lanBroadcast" name="lan_broadcast"{% if sbot_config.localadv == true %} checked{% endif %}>
<input type="checkbox" id="lanBroadcast" name="localadv"{% if sbot_config.localadv == true %} checked{% endif %}>
<label for="lanBroadcast">Enable LAN Broadcasting</label><br>
<br>
<input type="checkbox" id="lanDiscovery" name="lan_discovery" title="Listen for the presence of local peers and attempt to connect if found"{% if sbot_config.localdiscov == true %} checked{% endif %}>
<input type="checkbox" id="lanDiscovery" name="localdiscov" title="Listen for the presence of local peers and attempt to connect if found"{% if sbot_config.localdiscov == true %} checked{% endif %}>
<label for="lanDiscovery">Enable LAN Discovery</label><br>
<br>
<input type="checkbox" id="startup" name="startup" title="Define whether the pub runs automatically on system startup"{% if run_on_startup == "enabled" %}checked{% endif %}>
<input type="checkbox" id="startup" name="startup" title="Define whether the pub runs automatically on system startup"{% if run_on_startup == "enabled" %} checked{% endif %}>
<label for="startup">Run pub on system startup</label><br>
</div>
<br>
<input class="button button-primary center" type="button" value="Save">
<input class="button button-warning center" type="button" value="Restore Defaults">
<!-- hidden input elements for all other config variables -->
<input type="hidden" id="debugdir" name="debugdir" value="{{ sbot_config.debugdir }}">
<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 }}">
<!-- BUTTONS -->
<input id="saveConfig" class="button button-primary center" type="submit" title="Save configuration parameters to file" value="Save">
<a id="restoreDefaults" class="button button-warning center" href="/settings/scuttlebutt/configure/default" title="Restore default configuration parameters and save them to file">Restore Defaults</a>
</form>
<!-- FLASH MESSAGE -->
{% include "snippets/flash_message" %}
</div>
{%- endblock card -%}