Introduce foreigner network (#25)

* support network identity for instance

* introduce foreign network

foreign network allow a node serving as one public node. other nodes can
connect to this node to discover peers and exchange route info.
This commit is contained in:
Sijie.Sun
2024-03-06 20:59:17 +08:00
committed by GitHub
parent 9261d0d32d
commit d8d1c64df7
13 changed files with 868 additions and 57 deletions
+54 -9
View File
@@ -2,6 +2,8 @@ use std::{io::Write, sync::Arc};
use crate::rpc::PeerConnInfo;
use crossbeam::atomic::AtomicCell;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::{
config_fs::ConfigFs,
@@ -12,8 +14,8 @@ use super::{
#[derive(Debug, Clone, PartialEq)]
pub enum GlobalCtxEvent {
PeerAdded,
PeerRemoved,
PeerAdded(Uuid),
PeerRemoved(Uuid),
PeerConnAdded(PeerConnInfo),
PeerConnRemoved(PeerConnInfo),
}
@@ -21,11 +23,31 @@ pub enum GlobalCtxEvent {
type EventBus = tokio::sync::broadcast::Sender<GlobalCtxEvent>;
type EventBusSubscriber = tokio::sync::broadcast::Receiver<GlobalCtxEvent>;
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct NetworkIdentity {
pub network_name: String,
pub network_secret: String,
}
impl NetworkIdentity {
pub fn new(network_name: String, network_secret: String) -> Self {
NetworkIdentity {
network_name,
network_secret,
}
}
pub fn default() -> Self {
Self::new("default".to_string(), "".to_string())
}
}
pub struct GlobalCtx {
pub inst_name: String,
pub id: uuid::Uuid,
pub config_fs: ConfigFs,
pub net_ns: NetNS,
pub network: NetworkIdentity,
event_bus: EventBus,
@@ -54,11 +76,17 @@ impl std::fmt::Debug for GlobalCtx {
pub type ArcGlobalCtx = std::sync::Arc<GlobalCtx>;
impl GlobalCtx {
pub fn new(inst_name: &str, config_fs: ConfigFs, net_ns: NetNS) -> Self {
pub fn new(
inst_name: &str,
config_fs: ConfigFs,
net_ns: NetNS,
network: Option<NetworkIdentity>,
) -> Self {
let id = config_fs
.get_or_add_file("inst_id", || uuid::Uuid::new_v4().to_string())
.unwrap();
let id = uuid::Uuid::parse_str(&id).unwrap();
let network = network.unwrap_or(NetworkIdentity::default());
let (event_bus, _) = tokio::sync::broadcast::channel(100);
@@ -67,6 +95,8 @@ impl GlobalCtx {
id,
config_fs,
net_ns: net_ns.clone(),
network,
event_bus,
cached_ipv4: AtomicCell::new(None),
cached_proxy_cidrs: AtomicCell::new(None),
@@ -193,6 +223,10 @@ impl GlobalCtx {
pub fn get_id(&self) -> uuid::Uuid {
self.id
}
pub fn get_network_identity(&self) -> NetworkIdentity {
self.network.clone()
}
}
#[cfg(test)]
@@ -203,18 +237,22 @@ pub mod tests {
async fn test_global_ctx() {
let config_fs = ConfigFs::new("/tmp/easytier");
let net_ns = NetNS::new(None);
let global_ctx = GlobalCtx::new("test", config_fs, net_ns);
let global_ctx = GlobalCtx::new("test", config_fs, net_ns, None);
let mut subscriber = global_ctx.subscribe();
global_ctx.issue_event(GlobalCtxEvent::PeerAdded);
global_ctx.issue_event(GlobalCtxEvent::PeerRemoved);
let uuid = Uuid::new_v4();
global_ctx.issue_event(GlobalCtxEvent::PeerAdded(uuid.clone()));
global_ctx.issue_event(GlobalCtxEvent::PeerRemoved(uuid.clone()));
global_ctx.issue_event(GlobalCtxEvent::PeerConnAdded(PeerConnInfo::default()));
global_ctx.issue_event(GlobalCtxEvent::PeerConnRemoved(PeerConnInfo::default()));
assert_eq!(subscriber.recv().await.unwrap(), GlobalCtxEvent::PeerAdded);
assert_eq!(
subscriber.recv().await.unwrap(),
GlobalCtxEvent::PeerRemoved
GlobalCtxEvent::PeerAdded(uuid.clone())
);
assert_eq!(
subscriber.recv().await.unwrap(),
GlobalCtxEvent::PeerRemoved(uuid.clone())
);
assert_eq!(
subscriber.recv().await.unwrap(),
@@ -226,7 +264,9 @@ pub mod tests {
);
}
pub fn get_mock_global_ctx() -> ArcGlobalCtx {
pub fn get_mock_global_ctx_with_network(
network_identy: Option<NetworkIdentity>,
) -> ArcGlobalCtx {
let node_id = uuid::Uuid::new_v4();
let config_fs = ConfigFs::new_with_dir(node_id.to_string().as_str(), "/tmp/easytier");
let net_ns = NetNS::new(None);
@@ -234,6 +274,11 @@ pub mod tests {
format!("test_{}", node_id).as_str(),
config_fs,
net_ns,
network_identy,
))
}
pub fn get_mock_global_ctx() -> ArcGlobalCtx {
get_mock_global_ctx_with_network(None)
}
}