Phase 2: PostgreSQL, E2EE, channel CRUD, voice signaling, Slint migration
Server: - PostgreSQL backend (Pool enum over Sqlite/PgPool, all storage methods dual-dialect) - E2EE for DM (4 new protobuf packets, handlers, storage of ciphertext only) - ChannelEdit packet + server-authoritative channel create/delete - Gateway → voice-node membership signaling via broadcast channel Client: - E2EE crypto (X25519 from Ed25519, HKDF, ChaCha20-Poly1305) + UI lock icons - E2EE net layer (PID 0x0071-0x0074, JSON payloads, dispatch, session handlers) - Slint migration: all .slint files, bridge.rs, update.rs, main.rs rewritten - Removed egui UI (~50 files) and all dead code (clean clippy -D warnings)
This commit is contained in:
parent
c34ed2a192
commit
742cf03a5f
31 changed files with 2203 additions and 639 deletions
131
gateway/src/handler/e2ee/key_exchange.rs
Normal file
131
gateway/src/handler/e2ee/key_exchange.rs
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
use anyhow::Result;
|
||||
use prost::Message;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
domain::session,
|
||||
net::{
|
||||
io,
|
||||
state::{BroadcastMsg, State},
|
||||
},
|
||||
proto::{
|
||||
self, E2eeDmKeyExchangeAckPayload, E2eeDmKeyExchangePayload, ErrorCode, PacketId,
|
||||
SessionCrypto, encode_packet, to_payload,
|
||||
},
|
||||
};
|
||||
|
||||
pub async fn handle_e2ee_key_exchange(
|
||||
stream: &mut (impl AsyncRead + AsyncWrite + Unpin),
|
||||
seq: &mut u32,
|
||||
session_id: &str,
|
||||
payload: &[u8],
|
||||
crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
) -> Result<()> {
|
||||
let msg = E2eeDmKeyExchangePayload::decode(payload)?;
|
||||
|
||||
let sess = session::get(&state.sessions, session_id)
|
||||
.await
|
||||
.ok_or_else(|| anyhow::anyhow!("session not found"))?;
|
||||
let my_id = sess.user_id.clone();
|
||||
drop(sess);
|
||||
|
||||
let other_id = state
|
||||
.storage
|
||||
.get_dm_user_id(&msg.dm_id, &my_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("user not in DM"))?;
|
||||
|
||||
if state.storage.is_blocked(&other_id, &my_id).await? {
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
seq,
|
||||
&to_payload(&proto::ErrorPayload {
|
||||
code: ErrorCode::Blocked as u32,
|
||||
message: "blocked".into(),
|
||||
}),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
{
|
||||
let mut keys = state.e2ee_keys.write().await;
|
||||
keys.insert(my_id.clone(), msg.e2ee_public_key.clone());
|
||||
}
|
||||
|
||||
if let Some(recipient_sid) =
|
||||
session::get_session_id_by_user_id(&state.sessions, &other_id).await
|
||||
{
|
||||
let data = encode_packet(
|
||||
PacketId::E2eeDmKeyExchange,
|
||||
0,
|
||||
&to_payload(&E2eeDmKeyExchangePayload {
|
||||
dm_id: msg.dm_id.clone(),
|
||||
e2ee_public_key: msg.e2ee_public_key.clone(),
|
||||
}),
|
||||
);
|
||||
let _ = state.broadcast.send(BroadcastMsg {
|
||||
channel_id: None,
|
||||
exclude_session: None,
|
||||
target_session_id: Some(recipient_sid),
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
warn!("e2ee key exchange recipient offline: {}", &other_id[..8]);
|
||||
}
|
||||
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::E2eeDmKeyExchange,
|
||||
seq,
|
||||
&to_payload(&E2eeDmKeyExchangeAckPayload { dm_id: msg.dm_id }),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_e2ee_key_exchange_ack(
|
||||
_stream: &mut (impl AsyncRead + AsyncWrite + Unpin),
|
||||
_seq: &mut u32,
|
||||
session_id: &str,
|
||||
payload: &[u8],
|
||||
_crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
) -> Result<()> {
|
||||
let msg = E2eeDmKeyExchangeAckPayload::decode(payload)?;
|
||||
|
||||
let sess = session::get(&state.sessions, session_id)
|
||||
.await
|
||||
.ok_or_else(|| anyhow::anyhow!("session not found"))?;
|
||||
let my_id = sess.user_id.clone();
|
||||
drop(sess);
|
||||
|
||||
let other_id = state
|
||||
.storage
|
||||
.get_dm_user_id(&msg.dm_id, &my_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("user not in DM"))?;
|
||||
|
||||
if let Some(recipient_sid) =
|
||||
session::get_session_id_by_user_id(&state.sessions, &other_id).await
|
||||
{
|
||||
let data = encode_packet(
|
||||
PacketId::E2eeDmKeyExchangeAck,
|
||||
0,
|
||||
&to_payload(&E2eeDmKeyExchangeAckPayload { dm_id: msg.dm_id }),
|
||||
);
|
||||
let _ = state.broadcast.send(BroadcastMsg {
|
||||
channel_id: None,
|
||||
exclude_session: None,
|
||||
target_session_id: Some(recipient_sid),
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
159
gateway/src/handler/e2ee/message.rs
Normal file
159
gateway/src/handler/e2ee/message.rs
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
use anyhow::Result;
|
||||
use prost::Message;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
domain::session,
|
||||
net::{
|
||||
io,
|
||||
state::{BroadcastMsg, State},
|
||||
},
|
||||
proto::{
|
||||
self, E2eeDmHistoryPayload, E2eeDmMessagePayload, ErrorCode, PacketId, SessionCrypto,
|
||||
encode_packet, to_payload,
|
||||
},
|
||||
};
|
||||
|
||||
pub async fn handle_e2ee_dm_message(
|
||||
stream: &mut (impl AsyncRead + AsyncWrite + Unpin),
|
||||
seq: &mut u32,
|
||||
session_id: &str,
|
||||
payload: &[u8],
|
||||
crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
) -> Result<()> {
|
||||
let msg = E2eeDmMessagePayload::decode(payload)?;
|
||||
|
||||
let sess = session::get(&state.sessions, session_id)
|
||||
.await
|
||||
.ok_or_else(|| anyhow::anyhow!("session not found"))?;
|
||||
let my_id = sess.user_id.clone();
|
||||
drop(sess);
|
||||
|
||||
if !state.rate_limiter.try_consume(session_id) {
|
||||
state.metrics.inc(&state.metrics.rate_limited_events);
|
||||
warn!("rate-limited e2ee dm session {session_id}");
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
seq,
|
||||
&to_payload(&proto::ErrorPayload {
|
||||
code: ErrorCode::RateLimited as u32,
|
||||
message: "you are sending messages too quickly".into(),
|
||||
}),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let other_id = state
|
||||
.storage
|
||||
.get_dm_user_id(&msg.dm_id, &my_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("user not in DM"))?;
|
||||
|
||||
if state.storage.is_blocked(&other_id, &my_id).await? {
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
seq,
|
||||
&to_payload(&proto::ErrorPayload {
|
||||
code: proto::ErrorCode::Blocked as u32,
|
||||
message: "blocked".into(),
|
||||
}),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let saved = state
|
||||
.storage
|
||||
.save_e2ee_dm_message(&msg.dm_id, &my_id, &msg.ciphertext)
|
||||
.await?;
|
||||
state.metrics.inc(&state.metrics.dm_messages_sent);
|
||||
|
||||
state
|
||||
.storage
|
||||
.increment_dm_unread(&msg.dm_id, &other_id)
|
||||
.await?;
|
||||
|
||||
if let Some(recipient_sid) =
|
||||
session::get_session_id_by_user_id(&state.sessions, &other_id).await
|
||||
{
|
||||
let data = encode_packet(
|
||||
PacketId::E2eeDmMessage,
|
||||
0,
|
||||
&to_payload(&E2eeDmMessagePayload {
|
||||
dm_id: msg.dm_id.clone(),
|
||||
sender_id: my_id.clone(),
|
||||
ciphertext: msg.ciphertext.clone(),
|
||||
timestamp: saved.timestamp,
|
||||
}),
|
||||
);
|
||||
let _ = state.broadcast.send(BroadcastMsg {
|
||||
channel_id: None,
|
||||
exclude_session: None,
|
||||
target_session_id: Some(recipient_sid),
|
||||
data,
|
||||
});
|
||||
} else {
|
||||
warn!("e2ee dm recipient offline: {}", &other_id[..8]);
|
||||
}
|
||||
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::E2eeDmMessage,
|
||||
seq,
|
||||
&to_payload(&saved),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_e2ee_dm_history(
|
||||
stream: &mut (impl AsyncRead + AsyncWrite + Unpin),
|
||||
seq: &mut u32,
|
||||
session_id: &str,
|
||||
payload: &[u8],
|
||||
crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
) -> Result<()> {
|
||||
let req = E2eeDmHistoryPayload::decode(payload)?;
|
||||
|
||||
let sess = session::get(&state.sessions, session_id)
|
||||
.await
|
||||
.ok_or_else(|| anyhow::anyhow!("session not found"))?;
|
||||
let my_id = sess.user_id.clone();
|
||||
drop(sess);
|
||||
|
||||
let _other_id = state
|
||||
.storage
|
||||
.get_dm_user_id(&req.dm_id, &my_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("user not in DM"))?;
|
||||
|
||||
let limit = req.limit.unwrap_or(50);
|
||||
let messages = state
|
||||
.storage
|
||||
.get_e2ee_dm_messages(&req.dm_id, limit)
|
||||
.await?;
|
||||
|
||||
let resp = E2eeDmHistoryPayload {
|
||||
dm_id: req.dm_id,
|
||||
messages,
|
||||
limit: None,
|
||||
};
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::E2eeDmHistory,
|
||||
seq,
|
||||
&to_payload(&resp),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
5
gateway/src/handler/e2ee/mod.rs
Normal file
5
gateway/src/handler/e2ee/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
mod key_exchange;
|
||||
mod message;
|
||||
|
||||
pub use key_exchange::{handle_e2ee_key_exchange, handle_e2ee_key_exchange_ack};
|
||||
pub use message::{handle_e2ee_dm_history, handle_e2ee_dm_message};
|
||||
Loading…
Add table
Add a link
Reference in a new issue