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
92
gateway/src/handler/channel/edit.rs
Normal file
92
gateway/src/handler/channel/edit.rs
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
use anyhow::Result;
|
||||
use prost::Message;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::{
|
||||
domain::{channels, session},
|
||||
net::{
|
||||
io,
|
||||
state::{BroadcastMsg, State},
|
||||
},
|
||||
proto::{ChannelEditPayload, PacketId, SessionCrypto, encode_packet, to_payload},
|
||||
};
|
||||
|
||||
pub async fn handle_channel_edit(
|
||||
stream: &mut (impl AsyncRead + AsyncWrite + Unpin),
|
||||
seq: &mut u32,
|
||||
session_id: &str,
|
||||
payload: &[u8],
|
||||
crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
) -> Result<()> {
|
||||
let req = ChannelEditPayload::decode(payload)?;
|
||||
let sess = match session::get(&state.sessions, session_id).await {
|
||||
Some(s) => s,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
let channel_id = req.channel_id.trim().to_string();
|
||||
let channel_name = req.channel_name.trim().to_string();
|
||||
|
||||
if channel_id.is_empty() || channel_name.is_empty() {
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
seq,
|
||||
&to_payload(&crate::proto::ErrorPayload {
|
||||
code: crate::proto::ErrorCode::InvalidPacket as u32,
|
||||
message: "channel_id and channel_name are required".into(),
|
||||
}),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let renamed = channels::rename(&state.channels, &channel_id, &channel_name).await;
|
||||
|
||||
if !renamed {
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
seq,
|
||||
&to_payload(&crate::proto::ErrorPayload {
|
||||
code: crate::proto::ErrorCode::ChannelNotFound as u32,
|
||||
message: "channel not found".into(),
|
||||
}),
|
||||
crypto,
|
||||
)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Err(e) = state
|
||||
.storage
|
||||
.update_channel(&channel_id, &channel_name)
|
||||
.await
|
||||
{
|
||||
warn!("failed to update channel in storage: {e}");
|
||||
}
|
||||
|
||||
info!(
|
||||
"channel_edit: '{}' renamed to '{}' by {}",
|
||||
channel_id, channel_name, sess.nickname
|
||||
);
|
||||
|
||||
let _ = state.broadcast.send(BroadcastMsg {
|
||||
channel_id: Some(channel_id.clone()),
|
||||
exclude_session: Some(session_id.into()),
|
||||
target_session_id: None,
|
||||
data: encode_packet(
|
||||
PacketId::ChannelEdit,
|
||||
0,
|
||||
&to_payload(&ChannelEditPayload {
|
||||
channel_id: channel_id.clone(),
|
||||
channel_name: channel_name.clone(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use anyhow::Result;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tracing::info;
|
||||
|
|
@ -25,6 +27,7 @@ pub async fn join(
|
|||
channel_id: &str,
|
||||
crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
addr: SocketAddr,
|
||||
) -> Result<()> {
|
||||
let prev_channel = session::get(&state.sessions, session_id)
|
||||
.await
|
||||
|
|
@ -110,14 +113,15 @@ pub async fn join(
|
|||
});
|
||||
}
|
||||
|
||||
if let Some(tx) = &state.voice_member_tx
|
||||
if ch.kind == channels::ChannelKind::Voice
|
||||
&& let Some(tx) = &state.voice_member_tx
|
||||
&& let Some(sess) = session::get(&state.sessions, session_id).await
|
||||
{
|
||||
let event = serde_json::json!({
|
||||
"type": "joined",
|
||||
"type": "join",
|
||||
"channel_id": channel_id,
|
||||
"session_id": session_id,
|
||||
"user_id": sess.user_id,
|
||||
"endpoint": format!("{}:{}", addr.ip(), addr.port()),
|
||||
});
|
||||
let _ = tx.send(event.to_string());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,17 +22,21 @@ pub async fn leave(
|
|||
.await
|
||||
.map(|s| s.user_id);
|
||||
|
||||
let is_voice = channels::get_channel(&state.channels, channel_id)
|
||||
.await
|
||||
.is_some_and(|c| c.kind == channels::ChannelKind::Voice);
|
||||
|
||||
channels::leave(&state.channels, channel_id, session_id).await;
|
||||
set_channel(state, session_id, None).await;
|
||||
broadcast_leave(state, channel_id, session_id).await;
|
||||
|
||||
if let Some(tx) = &state.voice_member_tx
|
||||
if is_voice
|
||||
&& let Some(tx) = &state.voice_member_tx
|
||||
&& let Some(ref uid) = user_id
|
||||
{
|
||||
let event = serde_json::json!({
|
||||
"type": "left",
|
||||
"type": "leave",
|
||||
"channel_id": channel_id,
|
||||
"session_id": session_id,
|
||||
"user_id": uid,
|
||||
});
|
||||
let _ = tx.send(event.to_string());
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
pub mod create;
|
||||
pub mod edit;
|
||||
pub mod join;
|
||||
pub mod leave;
|
||||
|
||||
pub use create::{handle_channel_create, handle_channel_delete, handle_channel_list};
|
||||
pub use edit::handle_channel_edit;
|
||||
pub use join::join;
|
||||
pub use leave::leave;
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
},
|
||||
};
|
||||
|
||||
use super::{Ctx, channel, content, direct_message, friends, guild};
|
||||
use super::{Ctx, channel, content, direct_message, e2ee, friends, guild};
|
||||
|
||||
pub async fn dispatch<S: AsyncRead + AsyncWrite + Unpin>(
|
||||
ctx: &mut Ctx<'_, S>,
|
||||
|
|
@ -43,6 +43,7 @@ pub async fn dispatch<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
&m.channel_id,
|
||||
ctx.crypto,
|
||||
ctx.state,
|
||||
addr,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
|
@ -70,6 +71,12 @@ pub async fn dispatch<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::ChannelEdit => {
|
||||
channel::handle_channel_edit(
|
||||
ctx.stream, ctx.seq, session_id, payload, ctx.crypto, ctx.state,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::ChannelList => {
|
||||
channel::handle_channel_list(ctx.stream, ctx.seq, session_id, ctx.crypto, ctx.state)
|
||||
.await?;
|
||||
|
|
@ -275,6 +282,30 @@ pub async fn dispatch<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::E2eeDmKeyExchange => {
|
||||
e2ee::handle_e2ee_key_exchange(
|
||||
ctx.stream, ctx.seq, session_id, payload, ctx.crypto, ctx.state,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::E2eeDmKeyExchangeAck => {
|
||||
e2ee::handle_e2ee_key_exchange_ack(
|
||||
ctx.stream, ctx.seq, session_id, payload, ctx.crypto, ctx.state,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::E2eeDmMessage => {
|
||||
e2ee::handle_e2ee_dm_message(
|
||||
ctx.stream, ctx.seq, session_id, payload, ctx.crypto, ctx.state,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::E2eeDmHistory => {
|
||||
e2ee::handle_e2ee_dm_history(
|
||||
ctx.stream, ctx.seq, session_id, payload, ctx.crypto, ctx.state,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
PacketId::Disconnect => debug!("{addr} DISCONNECT"),
|
||||
other => debug!("{addr} unhandled {:?}", other),
|
||||
}
|
||||
|
|
|
|||
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};
|
||||
|
|
@ -3,6 +3,7 @@ pub mod content;
|
|||
pub mod deliver;
|
||||
pub mod direct_message;
|
||||
pub mod dispatch;
|
||||
pub mod e2ee;
|
||||
pub mod friends;
|
||||
pub mod guild;
|
||||
pub mod run;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue