Initial commit: split VNOX monorepo into VNOX-Server

Unified server binary (vnox-serverd) combining:
- Gateway (TCP, auth, channels, sessions, SQLite)
- Voice-node (UDP relay, Opus, jitter buffer)

Standalone gateway (vnox-gateway) and voice-node (vnox-voice-node) preserved as sub-crates.
This commit is contained in:
Loki 2026-07-09 11:52:56 +02:00
commit e751e0bf5b
94 changed files with 9771 additions and 0 deletions

View file

@ -0,0 +1,251 @@
use anyhow::Result;
use tokio::net::TcpStream;
use tracing::{info, warn};
use crate::{
domain::{channels, session},
net::{
io,
state::{BroadcastMsg, State},
},
proto::{
ChannelCreatePayload, ChannelDeletePayload, ChannelListItem, ChannelListPayload,
ChannelStatePayload, PacketId, SessionCrypto, encode_packet, to_payload,
},
};
/// Handle a ChannelCreate request — register a new channel in the store and
/// broadcast the new ChannelState to all sessions so their sidebars update.
pub async fn handle_channel_create(
stream: &mut TcpStream,
seq: &mut u32,
session_id: &str,
payload: &[u8],
crypto: &SessionCrypto,
state: &State,
) -> Result<()> {
let req: ChannelCreatePayload = serde_json::from_slice(payload)?;
let sess = match session::get(&state.sessions, session_id).await {
Some(s) => s,
None => return Ok(()),
};
// Validate kind.
let kind = match req.kind.as_str() {
"text" => channels::ChannelKind::Text,
"voice" => channels::ChannelKind::Voice,
other => {
warn!(
"channel_create from {}: invalid kind '{other}'",
sess.nickname
);
io::send_encrypted(
stream,
PacketId::Error,
seq,
&to_payload(&crate::proto::ErrorPayload {
code: crate::proto::ErrorCode::InvalidPacket as u32,
message: format!("invalid channel kind: {other}"),
}),
crypto,
)
.await?;
return Ok(());
}
};
let channel_id = req.channel_id.trim().to_string();
let channel_name = if req.channel_name.trim().is_empty() {
channel_id.clone()
} else {
req.channel_name.trim().to_string()
};
if channel_id.is_empty() {
io::send_encrypted(
stream,
PacketId::Error,
seq,
&to_payload(&crate::proto::ErrorPayload {
code: crate::proto::ErrorCode::InvalidPacket as u32,
message: "channel_id is required".into(),
}),
crypto,
)
.await?;
return Ok(());
}
// Limit channel-create rate (reuse the per-session token bucket — 1 token
// means 1 channel-create per rate window).
if !state.rate_limiter.try_consume(session_id) {
state.metrics.inc(&state.metrics.rate_limited_events);
warn!("rate-limited channel_create from {}", sess.nickname);
io::send_encrypted(
stream,
PacketId::Error,
seq,
&to_payload(&crate::proto::ErrorPayload {
code: crate::proto::ErrorCode::RateLimited as u32,
message: "slow down — too many channel operations".into(),
}),
crypto,
)
.await?;
return Ok(());
}
let created = channels::create(&state.channels, &channel_id, &channel_name, kind.clone()).await;
if !created {
io::send_encrypted(
stream,
PacketId::Error,
seq,
&to_payload(&crate::proto::ErrorPayload {
code: crate::proto::ErrorCode::ChannelNotFound as u32,
message: "channel already exists".into(),
}),
crypto,
)
.await?;
return Ok(());
}
info!(
"channel_create: {} created '{}' ({}) by {}",
channel_id,
channel_name,
kind.as_str(),
sess.nickname
);
// Reply to creator with ChannelState (no members yet).
let sp = ChannelStatePayload {
channel_id: channel_id.clone(),
channel_name: channel_name.clone(),
kind: kind.as_str().into(),
members: Vec::new(),
voice_endpoint: state.config.voice.bind.clone(),
};
io::send_encrypted(
stream,
PacketId::ChannelState,
seq,
&to_payload(&sp),
crypto,
)
.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(
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(),
}),
),
});
Ok(())
}
/// Handle a ChannelDelete request — remove the channel from the store and
/// broadcast the deletion to all sessions.
pub async fn handle_channel_delete(
stream: &mut TcpStream,
seq: &mut u32,
session_id: &str,
payload: &[u8],
crypto: &SessionCrypto,
state: &State,
) -> Result<()> {
let req: ChannelDeletePayload = serde_json::from_slice(payload)?;
let sess = match session::get(&state.sessions, session_id).await {
Some(s) => s,
None => return Ok(()),
};
// Protect default channels from deletion.
if req.channel_id == "general" || req.channel_id == "voice" {
io::send_encrypted(
stream,
PacketId::Error,
seq,
&to_payload(&crate::proto::ErrorPayload {
code: crate::proto::ErrorCode::PermissionDenied as u32,
message: "cannot delete default channels".into(),
}),
crypto,
)
.await?;
return Ok(());
}
let existed = channels::delete(&state.channels, &req.channel_id).await;
if !existed {
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(());
}
info!(
"channel_delete: '{}' removed by {}",
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(
PacketId::ChannelDelete,
0,
&to_payload(&ChannelDeletePayload {
channel_id: req.channel_id.clone(),
}),
),
});
Ok(())
}
/// Handle a ChannelList request — reply with all known channels.
pub async fn handle_channel_list(
stream: &mut TcpStream,
seq: &mut u32,
session_id: &str,
crypto: &SessionCrypto,
state: &State,
) -> Result<()> {
let _sess = session::get(&state.sessions, session_id).await;
let channels = channels::list(&state.channels).await;
let items: Vec<ChannelListItem> = channels
.iter()
.map(|c| ChannelListItem {
channel_id: c.id.clone(),
channel_name: c.name.clone(),
kind: c.kind.as_str().into(),
})
.collect();
let p = ChannelListPayload { channels: items };
io::send_encrypted(stream, PacketId::ChannelList, seq, &to_payload(&p), crypto).await?;
Ok(())
}