feat(server): protocol v2 with signed handshake, secure voice relay, media broadcast and channel authorization
Revoke voice access on channel delete, kick and permission change, and acknowledge the sender's own chat messages.
This commit is contained in:
parent
bc5157046e
commit
3e7f5b22dc
44 changed files with 2825 additions and 386 deletions
|
|
@ -5,10 +5,7 @@ use tracing::{info, warn};
|
|||
|
||||
use crate::{
|
||||
domain::{channels, session},
|
||||
net::{
|
||||
io,
|
||||
state::{BroadcastMsg, State},
|
||||
},
|
||||
net::{io, state::State},
|
||||
proto::{
|
||||
ChannelCreatePayload, ChannelDeletePayload, ChannelListItem, ChannelListPayload,
|
||||
ChannelStatePayload, PacketId, SessionCrypto, encode_packet, to_payload,
|
||||
|
|
@ -62,7 +59,12 @@ pub async fn handle_channel_create(
|
|||
req.channel_name.trim().to_string()
|
||||
};
|
||||
|
||||
if channel_id.is_empty() {
|
||||
if channel_id.is_empty()
|
||||
|| channel_id.len() > 128
|
||||
|| channel_name.chars().count() > 100
|
||||
|| channel_id.chars().any(char::is_control)
|
||||
|| channel_name.chars().any(char::is_control)
|
||||
{
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
PacketId::Error,
|
||||
|
|
@ -96,6 +98,13 @@ pub async fn handle_channel_create(
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let access = state.access.write().await;
|
||||
if !super::may_manage(state, &sess.user_id, req.guild_id.as_deref()).await? {
|
||||
drop(access);
|
||||
super::denied(stream, seq, crypto).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let guild_id = req.guild_id.clone();
|
||||
|
||||
let created = channels::create(
|
||||
|
|
@ -107,10 +116,8 @@ pub async fn handle_channel_create(
|
|||
)
|
||||
.await;
|
||||
|
||||
// Persist to DB (best-effort, log and continue on failure).
|
||||
#[allow(clippy::collapsible_if)]
|
||||
if created {
|
||||
if let Err(e) = state
|
||||
if created
|
||||
&& let Err(error) = state
|
||||
.storage
|
||||
.create_channel(
|
||||
&channel_id,
|
||||
|
|
@ -119,11 +126,12 @@ pub async fn handle_channel_create(
|
|||
guild_id.as_deref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
warn!("failed to persist channel to storage: {e}");
|
||||
}
|
||||
{
|
||||
channels::delete(&state.channels, &channel_id).await;
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
drop(access);
|
||||
if !created {
|
||||
io::send_encrypted(
|
||||
stream,
|
||||
|
|
@ -165,22 +173,22 @@ pub async fn handle_channel_create(
|
|||
)
|
||||
.await?;
|
||||
|
||||
// Broadcast a ChannelCreate event to all other sessions so their sidebars update.
|
||||
let _ = state.broadcast.send(BroadcastMsg {
|
||||
channel_id: None,
|
||||
exclude_session: Some(session_id.into()),
|
||||
target_session_id: None,
|
||||
data: encode_packet(
|
||||
super::notify_guild(
|
||||
state,
|
||||
guild_id.as_deref(),
|
||||
Some(session_id),
|
||||
encode_packet(
|
||||
PacketId::ChannelCreate,
|
||||
0,
|
||||
&to_payload(&ChannelCreatePayload {
|
||||
channel_id: channel_id.clone(),
|
||||
channel_name: channel_name.clone(),
|
||||
kind: kind.as_str().into(),
|
||||
guild_id: req.guild_id.clone(),
|
||||
guild_id: guild_id.clone(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -217,15 +225,23 @@ pub async fn handle_channel_delete(
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let existed = channels::delete(&state.channels, &req.channel_id).await;
|
||||
|
||||
// Remove from DB (best-effort).
|
||||
#[allow(clippy::collapsible_if)]
|
||||
if existed {
|
||||
if let Err(e) = state.storage.delete_channel(&req.channel_id).await {
|
||||
warn!("failed to remove channel from storage: {e}");
|
||||
}
|
||||
let access = state.access.write().await;
|
||||
let channel = channels::get_channel(&state.channels, &req.channel_id).await;
|
||||
if !super::may_manage(
|
||||
state,
|
||||
&sess.user_id,
|
||||
channel.as_ref().and_then(|c| c.guild_id.as_deref()),
|
||||
)
|
||||
.await?
|
||||
{
|
||||
drop(access);
|
||||
super::denied(stream, seq, crypto).await?;
|
||||
return Ok(());
|
||||
}
|
||||
state.storage.delete_channel(&req.channel_id).await?;
|
||||
super::revoke_channel(state, &req.channel_id).await;
|
||||
let existed = channels::delete(&state.channels, &req.channel_id).await;
|
||||
drop(access);
|
||||
|
||||
if !existed {
|
||||
io::send_encrypted(
|
||||
|
|
@ -247,19 +263,21 @@ pub async fn handle_channel_delete(
|
|||
req.channel_id, sess.nickname
|
||||
);
|
||||
|
||||
// Broadcast deletion to all sessions.
|
||||
let _ = state.broadcast.send(BroadcastMsg {
|
||||
channel_id: None,
|
||||
exclude_session: Some(session_id.into()),
|
||||
target_session_id: None,
|
||||
data: encode_packet(
|
||||
super::notify_guild(
|
||||
state,
|
||||
channel
|
||||
.as_ref()
|
||||
.and_then(|channel| channel.guild_id.as_deref()),
|
||||
None,
|
||||
encode_packet(
|
||||
PacketId::ChannelDelete,
|
||||
0,
|
||||
&to_payload(&ChannelDeletePayload {
|
||||
channel_id: req.channel_id.clone(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -273,7 +291,10 @@ pub async fn handle_channel_list(
|
|||
crypto: &SessionCrypto,
|
||||
state: &State,
|
||||
) -> Result<()> {
|
||||
let _sess = session::get(&state.sessions, session_id).await;
|
||||
let Some(sess) = session::get(&state.sessions, session_id).await else {
|
||||
return Ok(());
|
||||
};
|
||||
let guilds = state.storage.list_user_guilds(&sess.user_id).await?;
|
||||
|
||||
// Decode optional guild_id filter from request.
|
||||
let filter_guild_id = if !payload.is_empty() {
|
||||
|
|
@ -286,6 +307,11 @@ pub async fn handle_channel_list(
|
|||
let channels = channels::list(&state.channels).await;
|
||||
let items: Vec<ChannelListItem> = channels
|
||||
.iter()
|
||||
.filter(|c| {
|
||||
c.guild_id
|
||||
.as_ref()
|
||||
.is_none_or(|guild| guilds.iter().any(|g| &g.id == guild))
|
||||
})
|
||||
.filter(|c| {
|
||||
filter_guild_id
|
||||
.as_ref()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue