Cleanup & More migration To TTP
This commit is contained in:
parent
41d4014ca5
commit
cce13a2b5a
8 changed files with 253 additions and 2226 deletions
2159
Cargo.lock
generated
2159
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
81
Cargo.toml
81
Cargo.toml
|
|
@ -8,91 +8,30 @@ epsilon-core = { git = "https://github.com/Tensamin/Epsilon.git", package = "eps
|
||||||
epsilon-native = { git = "https://github.com/Tensamin/Epsilon.git", package = "epsilon-native" }
|
epsilon-native = { git = "https://github.com/Tensamin/Epsilon.git", package = "epsilon-native" }
|
||||||
|
|
||||||
ansi_term = "*"
|
ansi_term = "*"
|
||||||
async-tungstenite = { version = "0.32.1", features = [
|
uuid = { version = "*", features = ["v4"] }
|
||||||
"futures-03-sink",
|
|
||||||
"futures-util",
|
|
||||||
"handshake",
|
|
||||||
"__rustls-tls",
|
|
||||||
"async-native-tls",
|
|
||||||
"async-std",
|
|
||||||
"async-std-runtime",
|
|
||||||
"async-tls",
|
|
||||||
"gio",
|
|
||||||
"gio-runtime",
|
|
||||||
"glib",
|
|
||||||
"openssl",
|
|
||||||
"real-async-native-tls",
|
|
||||||
"real-async-tls",
|
|
||||||
"real-native-tls",
|
|
||||||
"real-tokio-native-tls",
|
|
||||||
"real-tokio-openssl",
|
|
||||||
"real-tokio-rustls",
|
|
||||||
"rustls-native-certs",
|
|
||||||
"rustls-pki-types",
|
|
||||||
"tokio",
|
|
||||||
"tokio-native-tls",
|
|
||||||
"tokio-openssl",
|
|
||||||
"tokio-runtime",
|
|
||||||
"tokio-rustls-manual-roots",
|
|
||||||
"tokio-rustls-native-certs",
|
|
||||||
"tokio-rustls-webpki-roots",
|
|
||||||
"url",
|
|
||||||
"verbose-logging",
|
|
||||||
"webpki-roots",
|
|
||||||
] }
|
|
||||||
axum = "*"
|
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
bytes = "*"
|
|
||||||
chrono = "*"
|
|
||||||
cmake = "*"
|
|
||||||
crossterm = "*"
|
|
||||||
dashmap = "*"
|
dashmap = "*"
|
||||||
der = "*"
|
|
||||||
futures = "*"
|
futures = "*"
|
||||||
futures-util = "*"
|
|
||||||
hex = "*"
|
hex = "*"
|
||||||
http = "*"
|
|
||||||
json = "*"
|
|
||||||
loom = "0.7.2"
|
|
||||||
once_cell = "1.21.3"
|
once_cell = "1.21.3"
|
||||||
pkcs8 = { version = "*", features = ["alloc"] }
|
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rand_core = { version = "0.6", features = ["getrandom", "std"] }
|
rand_core = { version = "0.6", features = ["getrandom", "std"] }
|
||||||
reqwest = "*"
|
rustls = { version = "0.23.37", default-features = false, features = [
|
||||||
rustls = { version = "0.23.37", default-features = false, features = ["std", "tls12", "aws-lc-rs", "prefer-post-quantum"] }
|
"std",
|
||||||
quinn = { version = "0.11.9", default-features = false, features = ["runtime-tokio", "rustls-aws-lc-rs"] }
|
"tls12",
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
"aws-lc-rs",
|
||||||
|
"prefer-post-quantum",
|
||||||
|
] }
|
||||||
sha2 = "*"
|
sha2 = "*"
|
||||||
sys-info = "*"
|
|
||||||
sysinfo = "0.30"
|
|
||||||
tokio = { version = "*", features = ["full"] }
|
tokio = { version = "*", features = ["full"] }
|
||||||
tokio-rustls = "0.12.0"
|
|
||||||
tokio-stream = "*"
|
|
||||||
tokio-util = { version = "*", features = ["full"] }
|
|
||||||
tokio_websocket_server = "0.1.0"
|
|
||||||
tower = "0.5.2"
|
|
||||||
tracing = "*"
|
|
||||||
tracing-subscriber = "*"
|
|
||||||
tungstenite = "*"
|
|
||||||
uuid = "*"
|
|
||||||
walkdir = "2.5.0"
|
|
||||||
warp = "*"
|
|
||||||
x448 = { version = "*" }
|
x448 = { version = "*" }
|
||||||
x509 = "*"
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
aes-gcm = "0.10.3"
|
aes-gcm = "0.10.3"
|
||||||
tokio-native-tls = "0.3.1"
|
|
||||||
hkdf = "0.12.4"
|
hkdf = "0.12.4"
|
||||||
block-modes = "0.9.1"
|
strum = "0.28.0"
|
||||||
cbc = "0.1.2"
|
strum_macros = "0.28.0"
|
||||||
aes = "0.8.4"
|
|
||||||
strum = "0.27.2"
|
|
||||||
strum_macros = "0.27.2"
|
|
||||||
livekit = "0.7.32"
|
|
||||||
livekit-api = { version = "0.4.14", features = ["native-tls"] }
|
livekit-api = { version = "0.4.14", features = ["native-tls"] }
|
||||||
hyper = { version = "1.8.1", features = ["http1", "http2"] }
|
|
||||||
livekit-protocol = "0.7.1"
|
livekit-protocol = "0.7.1"
|
||||||
rustls-pemfile = "2.2.0"
|
|
||||||
thiserror = "2.0.18"
|
thiserror = "2.0.18"
|
||||||
webpki-roots = "1.0.6"
|
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,9 @@ impl AnonymousClientConnection {
|
||||||
pub fn start(self: Arc<Self>) {
|
pub fn start(self: Arc<Self>) {
|
||||||
let self_clone = self.clone();
|
let self_clone = self.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
/*while let Ok(cv) = self_clone.receiver.receive().await {
|
while let Ok(cv) = self_clone.receiver.receive().await {
|
||||||
self_clone.clone().handle_message(cv).await;
|
self_clone.clone().handle_message(cv).await;
|
||||||
}*/
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +89,14 @@ impl AnonymousClientConnection {
|
||||||
if !cv.is_type(CommunicationType::pong) {
|
if !cv.is_type(CommunicationType::pong) {
|
||||||
log_cv_out!(PrintType::Client, &cv);
|
log_cv_out!(PrintType::Client, &cv);
|
||||||
}
|
}
|
||||||
self.sender.send(&cv).await;
|
if let Err(e) = self.sender.send(&cv).await {
|
||||||
|
log_out!(
|
||||||
|
self.user_id as i64,
|
||||||
|
PrintType::Client,
|
||||||
|
"Send failed: {:?}",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle incoming message from client
|
/// Handle incoming message from client
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,8 @@ pub struct OmegaConnection {
|
||||||
message_send_times: Arc<Mutex<HashMap<Uuid, Instant>>>,
|
message_send_times: Arc<Mutex<HashMap<Uuid, Instant>>>,
|
||||||
pub connection_id: Uuid,
|
pub connection_id: Uuid,
|
||||||
shutdown_tx: Arc<Mutex<Option<watch::Sender<bool>>>>,
|
shutdown_tx: Arc<Mutex<Option<watch::Sender<bool>>>>,
|
||||||
|
// Track if we should reconnect on close
|
||||||
|
reconnect_on_close: Arc<RwLock<bool>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OmegaConnection {
|
impl OmegaConnection {
|
||||||
|
|
@ -120,12 +122,13 @@ impl OmegaConnection {
|
||||||
connection_loop_handle: Arc::new(Mutex::new(None)),
|
connection_loop_handle: Arc::new(Mutex::new(None)),
|
||||||
host: host.to_string(),
|
host: host.to_string(),
|
||||||
port,
|
port,
|
||||||
server_cert, // Store certificate for connection
|
server_cert,
|
||||||
last_ping: Arc::new(Mutex::new(-1)),
|
last_ping: Arc::new(Mutex::new(-1)),
|
||||||
heartbeat_handle: Arc::new(Mutex::new(None)),
|
heartbeat_handle: Arc::new(Mutex::new(None)),
|
||||||
message_send_times: Arc::new(Mutex::new(HashMap::new())),
|
message_send_times: Arc::new(Mutex::new(HashMap::new())),
|
||||||
connection_id: Uuid::new_v4(),
|
connection_id: Uuid::new_v4(),
|
||||||
shutdown_tx: Arc::new(Mutex::new(Some(shutdown_tx))),
|
shutdown_tx: Arc::new(Mutex::new(Some(shutdown_tx))),
|
||||||
|
reconnect_on_close: Arc::new(RwLock::new(true)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,6 +142,9 @@ impl OmegaConnection {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set reconnect flag
|
||||||
|
*self.reconnect_on_close.write().await = true;
|
||||||
|
|
||||||
let self_clone = self.clone();
|
let self_clone = self.clone();
|
||||||
let handle = tokio::spawn(async move {
|
let handle = tokio::spawn(async move {
|
||||||
self_clone.connection_loop().await;
|
self_clone.connection_loop().await;
|
||||||
|
|
@ -148,6 +154,9 @@ impl OmegaConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stop(&self) {
|
pub async fn stop(&self) {
|
||||||
|
// Disable reconnection
|
||||||
|
*self.reconnect_on_close.write().await = false;
|
||||||
|
|
||||||
if let Some(tx) = self.shutdown_tx.lock().await.take() {
|
if let Some(tx) = self.shutdown_tx.lock().await.take() {
|
||||||
let _ = tx.send(true);
|
let _ = tx.send(true);
|
||||||
}
|
}
|
||||||
|
|
@ -160,6 +169,11 @@ impl OmegaConnection {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close sender if connected
|
||||||
|
if let Some(sender) = self.sender.read().await.as_ref() {
|
||||||
|
sender.close();
|
||||||
|
}
|
||||||
|
|
||||||
*self.state.write().await = ConnectionState::Disconnected;
|
*self.state.write().await = ConnectionState::Disconnected;
|
||||||
*self.sender.write().await = None;
|
*self.sender.write().await = None;
|
||||||
}
|
}
|
||||||
|
|
@ -175,14 +189,26 @@ impl OmegaConnection {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if reconnection is enabled
|
||||||
|
if !*self.reconnect_on_close.read().await {
|
||||||
|
log_in!(0, PrintType::Omega, "Reconnection disabled, exiting loop");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
match self.clone().connect_once().await {
|
match self.clone().connect_once().await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
log_err!(
|
// Connection closed gracefully, check if we should reconnect
|
||||||
0,
|
if *self.reconnect_on_close.read().await {
|
||||||
PrintType::Omega,
|
log_err!(
|
||||||
"Connection lost, reconnecting in {:?}...",
|
0,
|
||||||
reconnect_delay
|
PrintType::Omega,
|
||||||
);
|
"Connection lost, reconnecting in {:?}...",
|
||||||
|
reconnect_delay
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
log_in!(0, PrintType::Omega, "Connection closed, not reconnecting");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log_err!(
|
log_err!(
|
||||||
|
|
@ -224,14 +250,21 @@ impl OmegaConnection {
|
||||||
addr_str
|
addr_str
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Reset reconnect delay on successful connection
|
||||||
|
let reconnect_delay = RECONNECT_DELAY;
|
||||||
|
|
||||||
// Store sender
|
// Store sender
|
||||||
*self.sender.write().await = Some(Arc::new(sender));
|
let sender_arc = Arc::new(sender);
|
||||||
|
*self.sender.write().await = Some(sender_arc.clone());
|
||||||
*self.state.write().await = ConnectionState::Connected { identified: false };
|
*self.state.write().await = ConnectionState::Connected { identified: false };
|
||||||
|
|
||||||
|
// Get handle for close monitoring
|
||||||
|
let sender_handle = sender_arc.handle().clone();
|
||||||
|
|
||||||
// Start read loop
|
// Start read loop
|
||||||
let read_self = self.clone();
|
let read_self = self.clone();
|
||||||
let read_handle = tokio::spawn(async move {
|
let read_handle = tokio::spawn(async move {
|
||||||
read_self.read_loop(&mut receiver).await;
|
read_self.read_loop(&mut receiver, sender_handle).await;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send identification
|
// Send identification
|
||||||
|
|
@ -244,7 +277,7 @@ impl OmegaConnection {
|
||||||
});
|
});
|
||||||
*self.heartbeat_handle.lock().await = Some(heartbeat_handle);
|
*self.heartbeat_handle.lock().await = Some(heartbeat_handle);
|
||||||
|
|
||||||
// Wait for read loop to complete
|
// Wait for read loop to complete (connection closed)
|
||||||
let result = read_handle.await;
|
let result = read_handle.await;
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
|
|
@ -256,7 +289,14 @@ impl OmegaConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(()) => Err("Read loop ended".to_string()),
|
Ok(()) => {
|
||||||
|
// Check if we should reconnect
|
||||||
|
if *self.reconnect_on_close.read().await {
|
||||||
|
Err("Connection closed, will reconnect".to_string())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(e) => Err(format!("Read loop error: {}", e)),
|
Err(e) => Err(format!("Read loop error: {}", e)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -404,26 +444,46 @@ impl OmegaConnection {
|
||||||
// Read Loop & Heartbeat
|
// Read Loop & Heartbeat
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
async fn read_loop(self: Arc<Self>, receiver: &mut Receiver) {
|
async fn read_loop(
|
||||||
|
self: Arc<Self>,
|
||||||
|
receiver: &mut Receiver,
|
||||||
|
sender_handle: Arc<epsilon_native::ConnectionHandle>,
|
||||||
|
) {
|
||||||
|
// Monitor both receiver and sender handle for close
|
||||||
|
let mut close_rx = sender_handle.subscribe_close();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match receiver.receive().await {
|
tokio::select! {
|
||||||
Ok(cv) => {
|
result = receiver.receive() => {
|
||||||
log_cv_in!(&cv);
|
match result {
|
||||||
|
Ok(cv) => {
|
||||||
|
log_cv_in!(&cv);
|
||||||
|
|
||||||
if cv.is_type(CommunicationType::pong) || cv.is_type(CommunicationType::ping) {
|
if cv.is_type(CommunicationType::pong) || cv.is_type(CommunicationType::ping) {
|
||||||
self.handle_pong(&cv).await;
|
self.handle_pong(&cv).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg_id = cv.get_id();
|
let msg_id = cv.get_id();
|
||||||
if let Some((_, task)) = WAITING_TASKS.remove(&msg_id) {
|
if let Some((_, task)) = WAITING_TASKS.remove(&msg_id) {
|
||||||
if (task.task)(self.clone(), cv) {
|
if (task.task)(self.clone(), cv) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log_err!(0, PrintType::Omega, "Receive error: {}", e);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
_ = close_rx.changed() => {
|
||||||
log_err!(0, PrintType::Omega, "Receive error: {}", e);
|
// Connection was closed by either side
|
||||||
|
if let Some(reason) = close_rx.borrow().clone() {
|
||||||
|
log_err!(0, PrintType::Omega, "Connection closed: {:?}", reason);
|
||||||
|
} else {
|
||||||
|
log_in!(0, PrintType::Omega, "Connection closed cleanly");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -434,10 +494,21 @@ impl OmegaConnection {
|
||||||
loop {
|
loop {
|
||||||
sleep(HEARTBEAT_INTERVAL).await;
|
sleep(HEARTBEAT_INTERVAL).await;
|
||||||
|
|
||||||
|
// Check if still connected
|
||||||
if !self.state.read().await.is_connected() {
|
if !self.state.read().await.is_connected() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if sender is closed
|
||||||
|
if let Some(sender) = self.sender.read().await.as_ref() {
|
||||||
|
if sender.is_closed() {
|
||||||
|
log_err!(0, PrintType::Omega, "Sender closed, stopping heartbeat");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
self.send_ping().await;
|
self.send_ping().await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -478,6 +549,17 @@ impl OmegaConnection {
|
||||||
|
|
||||||
let sender_guard = self.sender.read().await;
|
let sender_guard = self.sender.read().await;
|
||||||
if let Some(sender) = sender_guard.as_ref() {
|
if let Some(sender) = sender_guard.as_ref() {
|
||||||
|
// Check if closed before sending
|
||||||
|
if sender.is_closed() {
|
||||||
|
log_err!(0, PrintType::Omega, "Cannot send: connection closed");
|
||||||
|
drop(sender_guard);
|
||||||
|
// Trigger reconnection by closing the connection state
|
||||||
|
if let Some(sender) = self.sender.write().await.take() {
|
||||||
|
sender.close();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let sender_clone = Arc::clone(sender);
|
let sender_clone = Arc::clone(sender);
|
||||||
drop(sender_guard);
|
drop(sender_guard);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,9 +42,9 @@ impl ClientConnection {
|
||||||
pub fn start(self: Arc<Self>) {
|
pub fn start(self: Arc<Self>) {
|
||||||
let self_clone = self.clone();
|
let self_clone = self.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
/*while let Ok(cv) = self_clone.receiver.receive().await {
|
while let Ok(cv) = self_clone.receiver.receive().await {
|
||||||
self_clone.clone().handle_message(cv).await;
|
self_clone.clone().handle_message(cv).await;
|
||||||
}*/
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ impl GeneralConnection {
|
||||||
impl GeneralConnection {
|
impl GeneralConnection {
|
||||||
pub async fn handle(self: Arc<Self>) {
|
pub async fn handle(self: Arc<Self>) {
|
||||||
loop {
|
loop {
|
||||||
/*let cv = match self.receiver.receive().await {
|
let cv = match self.receiver.receive().await {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
};
|
};
|
||||||
|
|
@ -70,7 +70,7 @@ impl GeneralConnection {
|
||||||
|
|
||||||
if self.migrate().await {
|
if self.migrate().await {
|
||||||
break;
|
break;
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn handle_identification(self: &Arc<Self>, cv: CommunicationValue) {
|
async fn handle_identification(self: &Arc<Self>, cv: CommunicationValue) {
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ impl IotaConnection {
|
||||||
pub fn start(self: Arc<Self>) {
|
pub fn start(self: Arc<Self>) {
|
||||||
let self_clone = self.clone();
|
let self_clone = self.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
/*while let Ok(cv) = self_clone.receiver.receive().await {
|
while let Ok(cv) = self_clone.receiver.receive().await {
|
||||||
self_clone.clone().handle_message(cv).await;
|
self_clone.clone().handle_message(cv).await;
|
||||||
}*/
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,7 +104,14 @@ impl IotaConnection {
|
||||||
if !cv.is_type(CommunicationType::pong) {
|
if !cv.is_type(CommunicationType::pong) {
|
||||||
log_cv_out!(PrintType::Iota, cv);
|
log_cv_out!(PrintType::Iota, cv);
|
||||||
}
|
}
|
||||||
self.sender.send(&cv).await;
|
if let Err(e) = self.sender.send(&cv).await {
|
||||||
|
log_err!(
|
||||||
|
self.iota_id as i64,
|
||||||
|
PrintType::Iota,
|
||||||
|
"Failed to send message: {:?}",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle incoming message from Iota
|
/// Handle incoming message from Iota
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ pub async fn start(port: u16) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
conn.handle().await;
|
conn.handle().await;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
log!(0, PrintType::General, "Server stopped");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue