Merge pull request 'Update golgi init functions with keystore selector' (#107) from keystore_selector into main

Reviewed-on: #107
This commit is contained in:
glyph 2022-05-16 13:12:00 +00:00
commit bf7f2c8e31
5 changed files with 316 additions and 299 deletions

564
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
use async_std::task; use async_std::task;
use golgi::Sbot; use golgi::{sbot::Keystore, Sbot};
use log::debug; use log::debug;
use nanorand::{Rng, WyRand}; use nanorand::{Rng, WyRand};
use sha3::{Digest, Sha3_256}; use sha3::{Digest, Sha3_256};
@ -126,11 +126,13 @@ async fn publish_private_msg(msg: &str, recipient: &str) -> Result<(), String> {
// TODO: panics if we pass `Some(conf.shscap)` as second arg // TODO: panics if we pass `Some(conf.shscap)` as second arg
Some(conf) => { Some(conf) => {
let ip_port = conf.lis.clone(); let ip_port = conf.lis.clone();
Sbot::init(Some(ip_port), None) Sbot::init(Keystore::GoSbot, Some(ip_port), None)
.await .await
.map_err(|e| e.to_string())? .map_err(|e| e.to_string())?
} }
None => Sbot::init(None, None).await.map_err(|e| e.to_string())?, None => Sbot::init(Keystore::GoSbot, None, None)
.await
.map_err(|e| e.to_string())?,
}; };
debug!("Publishing a Scuttlebutt private message with temporary password"); debug!("Publishing a Scuttlebutt private message with temporary password");

View File

@ -6,7 +6,7 @@ use std::num::ParseIntError;
use io::Error as IoError; use io::Error as IoError;
use probes::ProbeError; use probes::ProbeError;
use regex::Error as RegexError; use regex::Error as RegexError;
use wpactrl::WpaError; use wpactrl::Error as WpaError;
/// Custom error type encapsulating all possible errors when querying /// Custom error type encapsulating all possible errors when querying
/// network interfaces and modifying their state. /// network interfaces and modifying their state.

View File

@ -22,6 +22,7 @@ use std::{
}; };
use probes::network; use probes::network;
use wpactrl::Client as WpaClient;
#[cfg(feature = "miniserde_support")] #[cfg(feature = "miniserde_support")]
use miniserde::{Deserialize, Serialize}; use miniserde::{Deserialize, Serialize};
@ -121,7 +122,7 @@ pub struct Traffic {
/// In the event of an error, a `NetworkError` is returned in the `Result`. /// In the event of an error, a `NetworkError` is returned in the `Result`.
pub fn available_networks(iface: &str) -> Result<Option<Vec<Scan>>, NetworkError> { pub fn available_networks(iface: &str) -> Result<Option<Vec<Scan>>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
wpa.request("SCAN")?; wpa.request("SCAN")?;
let networks = wpa.request("SCAN_RESULTS")?; let networks = wpa.request("SCAN_RESULTS")?;
let mut scan = Vec::new(); let mut scan = Vec::new();
@ -173,7 +174,7 @@ pub fn available_networks(iface: &str) -> Result<Option<Vec<Scan>>, NetworkError
/// event of an error, a `NetworkError` is returned in the `Result`. /// event of an error, a `NetworkError` is returned in the `Result`.
pub fn id(iface: &str, ssid: &str) -> Result<Option<String>, NetworkError> { pub fn id(iface: &str, ssid: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let networks = wpa.request("LIST_NETWORKS")?; let networks = wpa.request("LIST_NETWORKS")?;
let mut id = Vec::new(); let mut id = Vec::new();
for network in networks.lines() { for network in networks.lines() {
@ -232,7 +233,7 @@ pub fn ip(iface: &str) -> Result<Option<String>, NetworkError> {
/// `Result`. /// `Result`.
pub fn rssi(iface: &str) -> Result<Option<String>, NetworkError> { pub fn rssi(iface: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let status = wpa.request("SIGNAL_POLL")?; let status = wpa.request("SIGNAL_POLL")?;
let rssi = utils::regex_finder(r"RSSI=(.*)\n", &status)?; let rssi = utils::regex_finder(r"RSSI=(.*)\n", &status)?;
@ -259,7 +260,7 @@ pub fn rssi(iface: &str) -> Result<Option<String>, NetworkError> {
/// the `Result`. /// the `Result`.
pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> { pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let status = wpa.request("SIGNAL_POLL")?; let status = wpa.request("SIGNAL_POLL")?;
let rssi = utils::regex_finder(r"RSSI=(.*)\n", &status)?; let rssi = utils::regex_finder(r"RSSI=(.*)\n", &status)?;
@ -291,7 +292,7 @@ pub fn rssi_percent(iface: &str) -> Result<Option<String>, NetworkError> {
/// is returned in the `Result`. In the event of an error, a `NetworkError` is /// is returned in the `Result`. In the event of an error, a `NetworkError` is
/// returned in the `Result`. /// returned in the `Result`.
pub fn saved_networks() -> Result<Option<Vec<String>>, NetworkError> { pub fn saved_networks() -> Result<Option<Vec<String>>, NetworkError> {
let mut wpa = wpactrl::WpaCtrl::builder().open()?; let mut wpa = WpaClient::builder().open()?;
let networks = wpa.request("LIST_NETWORKS")?; let networks = wpa.request("LIST_NETWORKS")?;
let mut ssids = Vec::new(); let mut ssids = Vec::new();
for network in networks.lines() { for network in networks.lines() {
@ -323,7 +324,7 @@ pub fn saved_networks() -> Result<Option<Vec<String>>, NetworkError> {
/// returned in the `Result`. /// returned in the `Result`.
pub fn ssid(iface: &str) -> Result<Option<String>, NetworkError> { pub fn ssid(iface: &str) -> Result<Option<String>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let status = wpa.request("STATUS")?; let status = wpa.request("STATUS")?;
// pass the regex pattern and status output to the regex finder // pass the regex pattern and status output to the regex finder
@ -379,7 +380,7 @@ pub fn state(iface: &str) -> Result<Option<String>, NetworkError> {
/// a `NetworkError` is returned in the `Result`. /// a `NetworkError` is returned in the `Result`.
pub fn status(iface: &str) -> Result<Option<Status>, NetworkError> { pub fn status(iface: &str) -> Result<Option<Status>, NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let wpa_status = wpa.request("STATUS")?; let wpa_status = wpa.request("STATUS")?;
// pass the regex pattern and status output to the regex finder // pass the regex pattern and status output to the regex finder
@ -579,7 +580,7 @@ pub fn check_iface(wlan_iface: &str, ap_iface: &str) -> Result<(), NetworkError>
/// is returned in the `Result`. /// is returned in the `Result`.
pub fn connect(id: &str, iface: &str) -> Result<(), NetworkError> { pub fn connect(id: &str, iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let select = format!("SELECT {}", id); let select = format!("SELECT {}", id);
wpa.request(&select)?; wpa.request(&select)?;
Ok(()) Ok(())
@ -598,7 +599,7 @@ pub fn connect(id: &str, iface: &str) -> Result<(), NetworkError> {
/// returned in the `Result`. /// returned in the `Result`.
pub fn delete(id: &str, iface: &str) -> Result<(), NetworkError> { pub fn delete(id: &str, iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let remove = format!("REMOVE_NETWORK {}", id); let remove = format!("REMOVE_NETWORK {}", id);
wpa.request(&remove)?; wpa.request(&remove)?;
Ok(()) Ok(())
@ -617,7 +618,7 @@ pub fn delete(id: &str, iface: &str) -> Result<(), NetworkError> {
/// `Result`. /// `Result`.
pub fn disable(id: &str, iface: &str) -> Result<(), NetworkError> { pub fn disable(id: &str, iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let disable = format!("DISABLE_NETWORK {}", id); let disable = format!("DISABLE_NETWORK {}", id);
wpa.request(&disable)?; wpa.request(&disable)?;
Ok(()) Ok(())
@ -634,7 +635,7 @@ pub fn disable(id: &str, iface: &str) -> Result<(), NetworkError> {
/// error, a `NetworkError` is returned in the `Result`. /// error, a `NetworkError` is returned in the `Result`.
pub fn disconnect(iface: &str) -> Result<(), NetworkError> { pub fn disconnect(iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let disconnect = "DISCONNECT".to_string(); let disconnect = "DISCONNECT".to_string();
wpa.request(&disconnect)?; wpa.request(&disconnect)?;
Ok(()) Ok(())
@ -685,7 +686,7 @@ pub fn forget(iface: &str, ssid: &str) -> Result<(), NetworkError> {
/// event of an error, a `NetworkError` is returned in the `Result`. /// event of an error, a `NetworkError` is returned in the `Result`.
pub fn modify(id: &str, iface: &str, pass: &str) -> Result<(), NetworkError> { pub fn modify(id: &str, iface: &str, pass: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
let new_pass = format!("NEW_PASSWORD {} {}", id, pass); let new_pass = format!("NEW_PASSWORD {} {}", id, pass);
wpa.request(&new_pass)?; wpa.request(&new_pass)?;
Ok(()) Ok(())
@ -702,7 +703,7 @@ pub fn modify(id: &str, iface: &str, pass: &str) -> Result<(), NetworkError> {
/// error, a `NetworkError` is returned in the `Result`. /// error, a `NetworkError` is returned in the `Result`.
pub fn reassociate(iface: &str) -> Result<(), NetworkError> { pub fn reassociate(iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
wpa.request("REASSOCIATE")?; wpa.request("REASSOCIATE")?;
Ok(()) Ok(())
} }
@ -714,7 +715,7 @@ pub fn reassociate(iface: &str) -> Result<(), NetworkError> {
/// `Result` type is returned. In the event of an error, a `NetworkError` is /// `Result` type is returned. In the event of an error, a `NetworkError` is
/// returned in the `Result`. /// returned in the `Result`.
pub fn reconfigure() -> Result<(), NetworkError> { pub fn reconfigure() -> Result<(), NetworkError> {
let mut wpa = wpactrl::WpaCtrl::builder().open()?; let mut wpa = WpaClient::builder().open()?;
wpa.request("RECONFIGURE")?; wpa.request("RECONFIGURE")?;
Ok(()) Ok(())
} }
@ -730,7 +731,7 @@ pub fn reconfigure() -> Result<(), NetworkError> {
/// event of an error, a `NetworkError` is returned in the `Result`. /// event of an error, a `NetworkError` is returned in the `Result`.
pub fn reconnect(iface: &str) -> Result<(), NetworkError> { pub fn reconnect(iface: &str) -> Result<(), NetworkError> {
let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface); let wpa_path: String = format!("/var/run/wpa_supplicant/{}", iface);
let mut wpa = wpactrl::WpaCtrl::builder().ctrl_path(wpa_path).open()?; let mut wpa = WpaClient::builder().ctrl_path(wpa_path).open()?;
wpa.request("DISCONNECT")?; wpa.request("DISCONNECT")?;
wpa.request("RECONNECT")?; wpa.request("RECONNECT")?;
Ok(()) Ok(())
@ -742,7 +743,7 @@ pub fn reconnect(iface: &str) -> Result<(), NetworkError> {
/// `wpa_supplicant.conf` file, an `Ok` `Result` type is returned. In the /// `wpa_supplicant.conf` file, an `Ok` `Result` type is returned. In the
/// event of an error, a `NetworkError` is returned in the `Result`. /// event of an error, a `NetworkError` is returned in the `Result`.
pub fn save() -> Result<(), NetworkError> { pub fn save() -> Result<(), NetworkError> {
let mut wpa = wpactrl::WpaCtrl::builder().open()?; let mut wpa = WpaClient::builder().open()?;
wpa.request("SAVE_CONFIG")?; wpa.request("SAVE_CONFIG")?;
Ok(()) Ok(())
} }

View File

@ -12,7 +12,9 @@ use std::{
use async_std::task; use async_std::task;
use dirs; use dirs;
use futures::stream::TryStreamExt; use futures::stream::TryStreamExt;
use golgi::{api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, Sbot}; use golgi::{
api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, sbot::Keystore, Sbot,
};
use log::debug; use log::debug;
use peach_lib::sbot::SbotConfig; use peach_lib::sbot::SbotConfig;
use rouille::input::post::BufferedFile; use rouille::input::post::BufferedFile;
@ -70,9 +72,9 @@ pub async fn init_sbot_with_config(
// TODO: panics if we pass `Some(conf.shscap)` as second arg // TODO: panics if we pass `Some(conf.shscap)` as second arg
Some(conf) => { Some(conf) => {
let ip_port = conf.lis.clone(); let ip_port = conf.lis.clone();
Sbot::init(Some(ip_port), None).await? Sbot::init(Keystore::GoSbot, Some(ip_port), None).await?
} }
None => Sbot::init(None, None).await?, None => Sbot::init(Keystore::GoSbot, None, None).await?,
}; };
Ok(sbot_client) Ok(sbot_client)