diff --git a/Cargo.lock b/Cargo.lock index bfaf051..478b6a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1078,8 +1078,8 @@ dependencies = [ [[package]] name = "golgi" -version = "0.1.4" -source = "git+https://git.coopcloud.tech/golgi-ssb/golgi#ca4c1114ddf328b818144c5a1af0187b1357e9be" +version = "0.2.2" +source = "git+https://git.coopcloud.tech/golgi-ssb/golgi.git#22d12f31ac37a05ec5db6e62bc667bb006fc31fd" dependencies = [ "async-std", "async-stream 0.3.3", diff --git a/peach-config/src/error.rs b/peach-config/src/error.rs index 4b3ef41..862a866 100644 --- a/peach-config/src/error.rs +++ b/peach-config/src/error.rs @@ -40,6 +40,8 @@ pub enum PeachConfigError { PeachLibError { source: PeachError }, #[snafu(display("Error in golgi: {}", source))] Golgi { source: GolgiError }, + #[snafu(display("{}", message))] + CmdInputError { message: String }, } impl From for PeachConfigError { diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index be860f5..2fd7624 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -2,6 +2,7 @@ mod change_password; mod constants; mod error; mod generate_manifest; +mod publish_address; mod set_permissions; mod setup_networking; mod setup_peach; @@ -45,7 +46,7 @@ enum PeachConfig { Update(UpdateOpts), /// Changes the password for the peach-web interface - #[structopt(name = "changepassword")] + #[structopt(name = "change-password")] ChangePassword(ChangePasswordOpts), /// Updates file permissions on PeachCloud device @@ -55,6 +56,11 @@ enum PeachConfig { /// 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), } #[derive(StructOpt, Debug)] @@ -95,6 +101,13 @@ pub struct ChangePasswordOpts { password: Option, } +#[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)] @@ -169,6 +182,17 @@ async fn run() { 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 + ) + } + } + } } } } diff --git a/peach-config/src/publish_address.rs b/peach-config/src/publish_address.rs new file mode 100644 index 0000000..60ce1f6 --- /dev/null +++ b/peach-config/src/publish_address.rs @@ -0,0 +1,37 @@ +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(()) +} diff --git a/peach-lib/Cargo.toml b/peach-lib/Cargo.toml index ee14e50..7c8ea4a 100644 --- a/peach-lib/Cargo.toml +++ b/peach-lib/Cargo.toml @@ -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" } +golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi.git" } jsonrpc-client-core = "0.5" jsonrpc-client-http = "0.5" jsonrpc-core = "8.0" diff --git a/peach-web/src/utils/sbot.rs b/peach-web/src/utils/sbot.rs index 28ce5d1..24c1b15 100644 --- a/peach-web/src/utils/sbot.rs +++ b/peach-web/src/utils/sbot.rs @@ -12,7 +12,7 @@ use async_std::task; use dirs; use futures::stream::TryStreamExt; use golgi::{ - api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, sbot::Keystore, Sbot, + api::friends::RelationshipQuery, blobs, messages::SsbMessageKVT, sbot::Keystore, Sbot, }; use log::debug; use peach_lib::config_manager; @@ -134,7 +134,7 @@ pub fn latest_sequence_number() -> Result> { let id = sbot_client.whoami().await?; let history_stream = sbot_client.create_history_stream(id).await?; - let mut msgs: Vec = history_stream.try_collect().await?; + let mut msgs: Vec = history_stream.try_collect().await?; // there will be zero messages when the sbot is run for the first time if msgs.is_empty() { @@ -144,7 +144,7 @@ pub fn latest_sequence_number() -> Result> { msgs.reverse(); // return the sequence number of the latest msg - Ok(msgs[0].sequence) + Ok(msgs[0].value.sequence) } }) }