Skip to content

Commit 64a5c25

Browse files
committed
wip: watch peer connections
Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
1 parent 9006253 commit 64a5c25

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version.workspace = true
1111
[dependencies]
1212
base64 = "0.22.1"
1313
clap = { version = "4.4.6", features = [ "cargo", "derive" ] }
14+
futures-util = "0.3.30"
1415
oo7 = { workspace = true, features = ["unstable"] }
1516
rpassword = "7.2.0"
1617
serde.workspace = true

server/src/daemon/service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ impl Service {
440440
// loading login.keyring into the objects tree
441441
Service::init_collections(&object_server, self).await;
442442

443+
ServiceManager::watch_peer_connections(&cnx).await?;
444+
443445
Ok(())
444446
}
445447

server/src/daemon/service_manager.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
use std::{collections::HashMap, sync::RwLock};
22

3-
use zbus::zvariant::{ObjectPath, OwnedObjectPath};
3+
use futures_util::stream::StreamExt;
4+
use zbus::{
5+
zvariant::{ObjectPath, OwnedObjectPath},
6+
Connection, MatchRule, MessageStream,
7+
};
48

59
use super::{collection::Collection, session::Session};
10+
use crate::daemon;
611

712
#[derive(Debug, Default)]
813
pub struct ServiceManager {
914
sessions: HashMap<OwnedObjectPath, Session>,
1015
collections: HashMap<String, Collection>,
1116
collections_to_unlock: Vec<OwnedObjectPath>,
17+
peers: HashMap<String, OwnedObjectPath>,
1218
unlock_request_sender: RwLock<String>,
1319
unlock_prompt_path: RwLock<OwnedObjectPath>,
1420
prompts_counter: RwLock<i32>,
@@ -70,6 +76,18 @@ impl ServiceManager {
7076
*self.unlock_prompt_path.write().unwrap() = path.into();
7177
}
7278

79+
pub fn peer(&self, peer: String) -> Option<OwnedObjectPath> {
80+
self.peers.get(&peer).cloned()
81+
}
82+
83+
pub fn insert_peer(&mut self, peer: String, path: ObjectPath<'_>) {
84+
self.peers.insert(peer, path.into());
85+
}
86+
87+
pub fn remove_peer(&mut self, peer: String) {
88+
self.peers.remove(&peer);
89+
}
90+
7391
pub fn prompts_counter(&self) -> i32 {
7492
*self.prompts_counter.read().unwrap()
7593
}
@@ -102,4 +120,37 @@ impl ServiceManager {
102120
pub fn set_prompt_dismissed(&mut self, dismissed: bool) {
103121
self.prompt_dismissed = dismissed;
104122
}
123+
124+
pub async fn watch_peer_connections(connection: &Connection) -> daemon::Result<()> {
125+
// monitor client disconnects
126+
127+
let rule = MatchRule::builder()
128+
.msg_type(zbus::message::Type::Signal)
129+
.sender("org.freedesktop.DBus")?
130+
.interface("org.freedesktop.DBus")?
131+
.member("NameOwnerChanged")?
132+
.add_arg("org.gnome.seahorse.Application")?
133+
.build();
134+
135+
let mut stream = MessageStream::for_match_rule(rule, connection, Some(1)).await?;
136+
137+
while let Some(Ok(message)) = stream.next().await {
138+
let Ok((_, old_owner, new_owner)) =
139+
message.body().deserialize::<(String, String, String)>()
140+
else {
141+
continue;
142+
};
143+
if new_owner == "" {
144+
// a peer is disconnecting, remove session info
145+
// https://github.com/GNOME/gnome-keyring/blob/4132075144c7bb21b897570dd53b005ac38250aa/daemon/dbus/gkd-secret-service.c#L936
146+
tracing::info!("A peer is disconnected.");
147+
// todo: clean up
148+
println!("------------------ {old_owner}");
149+
} else {
150+
tracing::info!("A peer connected.");
151+
}
152+
}
153+
154+
Ok(())
155+
}
105156
}

0 commit comments

Comments
 (0)