From 58c2ce5e80e4386257b366dc00ccc7919ac8974f Mon Sep 17 00:00:00 2001 From: Francesca Lovebloom Date: Tue, 11 Jul 2023 12:40:35 -0700 Subject: [PATCH] connlib: Hook up callbacks (#1744) Co-authored-by: Jamil --- rust/Cargo.lock | 1 + rust/connlib/libs/common/src/session.rs | 10 +++++++--- rust/connlib/libs/tunnel/Cargo.toml | 1 + rust/connlib/libs/tunnel/src/lib.rs | 19 +++++++++++++++++-- .../connlib/libs/tunnel/src/resource_table.rs | 13 ++++++++++++- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index b6171b0d5..0554d3868 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1138,6 +1138,7 @@ dependencies = [ "rand_core 0.6.4", "rtnetlink", "serde", + "serde_json", "thiserror", "tokio", "tracing", diff --git a/rust/connlib/libs/common/src/session.rs b/rust/connlib/libs/common/src/session.rs index 296161b0b..8d50685be 100644 --- a/rust/connlib/libs/common/src/session.rs +++ b/rust/connlib/libs/common/src/session.rs @@ -43,7 +43,8 @@ pub trait ControlSession { /// A session is created using [Session::connect], then to stop a session we use [Session::disconnect]. pub struct Session { runtime: Option, - _phantom: PhantomData<(T, U, V, R, M, CB)>, + callbacks: CB, + _phantom: PhantomData<(T, U, V, R, M)>, } /// Resource list that will be displayed to the users. @@ -136,13 +137,14 @@ where .build()?; if cfg!(feature = "mock") { - Self::connect_mock(callbacks); + Self::connect_mock(callbacks.clone()); } else { - Self::connect_inner(&runtime, portal_url, token, callbacks); + Self::connect_inner(&runtime, portal_url, token, callbacks.clone()); } Ok(Self { runtime: Some(runtime), + callbacks, _phantom: PhantomData, }) } @@ -235,6 +237,8 @@ where /// For now this just drops the runtime, which should drop all pending tasks. /// Further cleanup should be done here. (Otherwise we can just drop [Session]). pub fn disconnect(&mut self) -> bool { + self.callbacks.on_disconnect(); + // 1. Close the websocket connection // 2. Free the device handle (UNIX) // 3. Close the file descriptor (UNIX) diff --git a/rust/connlib/libs/tunnel/Cargo.toml b/rust/connlib/libs/tunnel/Cargo.toml index b27751712..0f3f9482b 100644 --- a/rust/connlib/libs/tunnel/Cargo.toml +++ b/rust/connlib/libs/tunnel/Cargo.toml @@ -9,6 +9,7 @@ tokio = { version = "1.27", default-features = false, features = ["rt", "rt-mult thiserror = { version = "1.0", default-features = false } rand_core = { version = "0.6", default-features = false, features = ["getrandom"] } serde = { version = "1.0", default-features = false, features = ["derive", "std"] } +serde_json = { version = "1.0", default-features = false, features = ["std"] } futures = { version = "0.3", default-features = false, features = ["std", "async-await", "executor"] } futures-util = { version = "0.3", default-features = false, features = ["std", "async-await", "async-await-macro"] } tracing = { version = "0.1", default-features = false, features = ["std", "attributes"] } diff --git a/rust/connlib/libs/tunnel/src/lib.rs b/rust/connlib/libs/tunnel/src/lib.rs index 52790f0b5..0bd7d1e74 100644 --- a/rust/connlib/libs/tunnel/src/lib.rs +++ b/rust/connlib/libs/tunnel/src/lib.rs @@ -13,7 +13,7 @@ use ip_network::IpNetwork; use ip_network_table::IpNetworkTable; use libs_common::{ error_type::ErrorType::{Fatal, Recoverable}, - Callbacks, + Callbacks, TunnelAddresses, }; use async_trait::async_trait; @@ -224,7 +224,17 @@ where } } } - self.resources.write().insert(resource_description); + let resource_list = { + let mut resources = self.resources.write(); + resources.insert(resource_description); + resources.resource_list() + }; + match resource_list { + Ok(resource_list) => { + self.callbacks.on_update_resources(resource_list); + } + Err(err) => self.callbacks.on_error(&err.into(), Fatal), + } } /// Sets the interface configuration and starts background tasks. @@ -245,6 +255,11 @@ where self.start_timers(); self.start_iface_handler(); + self.callbacks.on_connect(TunnelAddresses { + address4: config.ipv4, + address6: config.ipv6, + }); + tracing::trace!("Started background loops"); Ok(()) diff --git a/rust/connlib/libs/tunnel/src/resource_table.rs b/rust/connlib/libs/tunnel/src/resource_table.rs index f34405bff..747942095 100644 --- a/rust/connlib/libs/tunnel/src/resource_table.rs +++ b/rust/connlib/libs/tunnel/src/resource_table.rs @@ -2,7 +2,10 @@ use std::{collections::HashMap, net::IpAddr, ptr::NonNull}; use ip_network_table::IpNetworkTable; -use libs_common::messages::{Id, ResourceDescription}; +use libs_common::{ + messages::{Id, ResourceDescription}, + ResourceList, +}; // Oh boy... here we go /// The resource table type @@ -148,4 +151,12 @@ impl ResourceTable { } } } + + pub fn resource_list(&self) -> Result { + self.id_table + .values() + .map(serde_json::to_string) + .collect::>() + .map(|resources| ResourceList { resources }) + } }