connlib: Hook up callbacks (#1744)

Co-authored-by: Jamil <jamilbk@users.noreply.github.com>
This commit is contained in:
Francesca Lovebloom
2023-07-11 12:40:35 -07:00
committed by GitHub
parent b70124f090
commit 58c2ce5e80
5 changed files with 38 additions and 6 deletions

1
rust/Cargo.lock generated
View File

@@ -1138,6 +1138,7 @@ dependencies = [
"rand_core 0.6.4",
"rtnetlink",
"serde",
"serde_json",
"thiserror",
"tokio",
"tracing",

View File

@@ -43,7 +43,8 @@ pub trait ControlSession<T, CB: Callbacks> {
/// A session is created using [Session::connect], then to stop a session we use [Session::disconnect].
pub struct Session<T, U, V, R, M, CB: Callbacks> {
runtime: Option<Runtime>,
_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)

View File

@@ -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"] }

View File

@@ -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(())

View File

@@ -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<ResourceList, serde_json::Error> {
self.id_table
.values()
.map(serde_json::to_string)
.collect::<Result<_, _>>()
.map(|resources| ResourceList { resources })
}
}