From e45e35125e623a8fb683a28f55ad96d42d01a1d0 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 12 Apr 2020 01:40:46 -0400 Subject: rename some stuff to be less confusing --- src/agent.rs | 27 --------------------------- src/bin/rbw-agent/actions.rs | 4 ++-- src/bin/rbw-agent/agent.rs | 14 +++++++------- src/bin/rbw-agent/sock.rs | 4 ++-- src/bin/rbw/actions.rs | 28 ++++++++++++++-------------- src/bin/rbw/sock.rs | 7 +++++-- src/lib.rs | 2 +- src/protocol.rs | 27 +++++++++++++++++++++++++++ 8 files changed, 58 insertions(+), 55 deletions(-) delete mode 100644 src/agent.rs create mode 100644 src/protocol.rs diff --git a/src/agent.rs b/src/agent.rs deleted file mode 100644 index 8d7cd69..0000000 --- a/src/agent.rs +++ /dev/null @@ -1,27 +0,0 @@ -#[derive(serde::Serialize, serde::Deserialize, Debug)] -pub struct Request { - pub tty: Option, - pub action: Action, -} - -#[derive(serde::Serialize, serde::Deserialize, Debug)] -#[serde(tag = "type")] -pub enum Action { - Login, - Unlock, - Lock, - Sync, - Decrypt { cipherstring: String }, - // add - // update - // remove - Quit, -} - -#[derive(serde::Serialize, serde::Deserialize, Debug)] -#[serde(tag = "type")] -pub enum Response { - Ack, - Error { error: String }, - Decrypt { plaintext: String }, -} diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs index 0eec665..81e2597 100644 --- a/src/bin/rbw-agent/actions.rs +++ b/src/bin/rbw-agent/actions.rs @@ -179,7 +179,7 @@ pub async fn decrypt( } async fn respond_ack(sock: &mut crate::sock::Sock) -> anyhow::Result<()> { - sock.send(&rbw::agent::Response::Ack) + sock.send(&rbw::protocol::Response::Ack) .await .context("failed to send response")?; @@ -190,7 +190,7 @@ async fn respond_decrypt( sock: &mut crate::sock::Sock, plaintext: String, ) -> anyhow::Result<()> { - sock.send(&rbw::agent::Response::Decrypt { plaintext }) + sock.send(&rbw::protocol::Response::Decrypt { plaintext }) .await .context("failed to send response")?; diff --git a/src/bin/rbw-agent/agent.rs b/src/bin/rbw-agent/agent.rs index f092787..46af63c 100644 --- a/src/bin/rbw-agent/agent.rs +++ b/src/bin/rbw-agent/agent.rs @@ -51,7 +51,7 @@ impl Agent { = handle_request(&mut sock, state.clone()).await; if let Err(e) = res { // unwrap is the only option here - sock.send(&rbw::agent::Response::Error { + sock.send(&rbw::protocol::Response::Error { error: format!("{:#}", e), }).await.unwrap(); } @@ -77,21 +77,21 @@ async fn handle_request( .await .context("failed to receive incoming message")?; match &req.action { - rbw::agent::Action::Login => { + rbw::protocol::Action::Login => { crate::actions::login(sock, state.clone(), req.tty.as_deref()) .await } - rbw::agent::Action::Unlock => { + rbw::protocol::Action::Unlock => { crate::actions::unlock(sock, state.clone(), req.tty.as_deref()) .await } - rbw::agent::Action::Lock => { + rbw::protocol::Action::Lock => { crate::actions::lock(sock, state.clone()).await } - rbw::agent::Action::Sync => crate::actions::sync(sock).await, - rbw::agent::Action::Decrypt { cipherstring } => { + rbw::protocol::Action::Sync => crate::actions::sync(sock).await, + rbw::protocol::Action::Decrypt { cipherstring } => { crate::actions::decrypt(sock, state.clone(), &cipherstring).await } - rbw::agent::Action::Quit => std::process::exit(0), + rbw::protocol::Action::Quit => std::process::exit(0), } } diff --git a/src/bin/rbw-agent/sock.rs b/src/bin/rbw-agent/sock.rs index 6931b13..688f87a 100644 --- a/src/bin/rbw-agent/sock.rs +++ b/src/bin/rbw-agent/sock.rs @@ -10,7 +10,7 @@ impl Sock { pub async fn send( &mut self, - res: &rbw::agent::Response, + res: &rbw::protocol::Response, ) -> anyhow::Result<()> { let Self(sock) = self; sock.write_all( @@ -26,7 +26,7 @@ impl Sock { Ok(()) } - pub async fn recv(&mut self) -> anyhow::Result { + pub async fn recv(&mut self) -> anyhow::Result { let Self(sock) = self; let mut buf = tokio::io::BufStream::new(sock); let mut line = String::new(); diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs index 87ecbc8..5951185 100644 --- a/src/bin/rbw/actions.rs +++ b/src/bin/rbw/actions.rs @@ -1,27 +1,27 @@ use anyhow::Context as _; pub fn login() -> anyhow::Result<()> { - simple_action(rbw::agent::Action::Login, "login") + simple_action(rbw::protocol::Action::Login, "login") } pub fn unlock() -> anyhow::Result<()> { - simple_action(rbw::agent::Action::Unlock, "unlock") + simple_action(rbw::protocol::Action::Unlock, "unlock") } pub fn sync() -> anyhow::Result<()> { - simple_action(rbw::agent::Action::Sync, "sync") + simple_action(rbw::protocol::Action::Sync, "sync") } pub fn lock() -> anyhow::Result<()> { - simple_action(rbw::agent::Action::Lock, "lock") + simple_action(rbw::protocol::Action::Lock, "lock") } pub fn quit() -> anyhow::Result<()> { match crate::sock::Sock::connect() { Ok(mut sock) => { - sock.send(&rbw::agent::Request { + sock.send(&rbw::protocol::Request { tty: std::env::var("TTY").ok(), - action: rbw::agent::Action::Quit, + action: rbw::protocol::Action::Quit, })?; Ok(()) } @@ -38,17 +38,17 @@ pub fn quit() -> anyhow::Result<()> { pub fn decrypt(cipherstring: &str) -> anyhow::Result { let mut sock = crate::sock::Sock::connect() .context("failed to connect to rbw-agent")?; - sock.send(&rbw::agent::Request { + sock.send(&rbw::protocol::Request { tty: std::env::var("TTY").ok(), - action: rbw::agent::Action::Decrypt { + action: rbw::protocol::Action::Decrypt { cipherstring: cipherstring.to_string(), }, })?; let res = sock.recv()?; match res { - rbw::agent::Response::Decrypt { plaintext } => Ok(plaintext), - rbw::agent::Response::Error { error } => { + rbw::protocol::Response::Decrypt { plaintext } => Ok(plaintext), + rbw::protocol::Response::Error { error } => { Err(anyhow::anyhow!("failed to decrypt: {}", error)) } _ => Err(anyhow::anyhow!("unexpected message: {:?}", res)), @@ -56,21 +56,21 @@ pub fn decrypt(cipherstring: &str) -> anyhow::Result { } fn simple_action( - action: rbw::agent::Action, + action: rbw::protocol::Action, desc: &str, ) -> anyhow::Result<()> { let mut sock = crate::sock::Sock::connect() .context("failed to connect to rbw-agent")?; - sock.send(&rbw::agent::Request { + sock.send(&rbw::protocol::Request { tty: std::env::var("TTY").ok(), action, })?; let res = sock.recv()?; match res { - rbw::agent::Response::Ack => Ok(()), - rbw::agent::Response::Error { error } => { + rbw::protocol::Response::Ack => Ok(()), + rbw::protocol::Response::Error { error } => { Err(anyhow::anyhow!("failed to {}: {}", desc, error)) } _ => Err(anyhow::anyhow!("unexpected message: {:?}", res)), diff --git a/src/bin/rbw/sock.rs b/src/bin/rbw/sock.rs index 8dca169..1ba13ec 100644 --- a/src/bin/rbw/sock.rs +++ b/src/bin/rbw/sock.rs @@ -10,7 +10,10 @@ impl Sock { )?)) } - pub fn send(&mut self, msg: &rbw::agent::Request) -> anyhow::Result<()> { + pub fn send( + &mut self, + msg: &rbw::protocol::Request, + ) -> anyhow::Result<()> { let Self(sock) = self; sock.write_all( serde_json::to_string(msg) @@ -23,7 +26,7 @@ impl Sock { Ok(()) } - pub fn recv(&mut self) -> anyhow::Result { + pub fn recv(&mut self) -> anyhow::Result { let Self(sock) = self; let mut buf = std::io::BufReader::new(sock); let mut line = String::new(); diff --git a/src/lib.rs b/src/lib.rs index e5d887a..f1460c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ #![allow(clippy::similar_names)] pub mod actions; -pub mod agent; pub mod api; pub mod cipherstring; pub mod config; @@ -17,4 +16,5 @@ pub mod identity; pub mod locked; pub mod pinentry; mod prelude; +pub mod protocol; pub mod pwgen; diff --git a/src/protocol.rs b/src/protocol.rs new file mode 100644 index 0000000..8d7cd69 --- /dev/null +++ b/src/protocol.rs @@ -0,0 +1,27 @@ +#[derive(serde::Serialize, serde::Deserialize, Debug)] +pub struct Request { + pub tty: Option, + pub action: Action, +} + +#[derive(serde::Serialize, serde::Deserialize, Debug)] +#[serde(tag = "type")] +pub enum Action { + Login, + Unlock, + Lock, + Sync, + Decrypt { cipherstring: String }, + // add + // update + // remove + Quit, +} + +#[derive(serde::Serialize, serde::Deserialize, Debug)] +#[serde(tag = "type")] +pub enum Response { + Ack, + Error { error: String }, + Decrypt { plaintext: String }, +} -- cgit v1.2.3-54-g00ecf