2023-02-07 15:21:00 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2023-02-09 19:01:21 +00:00
|
|
|
use prometheus::{IntCounter, IntGauge, Registry as MetricsRegistry};
|
2023-02-09 18:19:03 +00:00
|
|
|
use serde::Deserialize;
|
2023-02-13 18:58:05 +00:00
|
|
|
use tokio::io::{AsyncBufReadExt, AsyncWrite, AsyncWriteExt, BufReader, BufWriter};
|
2023-02-10 18:47:58 +00:00
|
|
|
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
2023-02-07 15:21:00 +00:00
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
use tokio::sync::oneshot::channel;
|
|
|
|
|
2023-02-15 20:49:52 +00:00
|
|
|
use crate::core::player::{PlayerConnection, PlayerId, PlayerRegistry, Updates};
|
2023-02-14 17:53:43 +00:00
|
|
|
use crate::core::room::{RoomId, RoomInfo, RoomRegistry};
|
2023-02-07 15:21:00 +00:00
|
|
|
use crate::prelude::*;
|
2023-02-10 17:09:29 +00:00
|
|
|
use crate::protos::irc::client::{client_message, ClientMessage};
|
|
|
|
use crate::protos::irc::server::{ServerMessage, ServerMessageBody};
|
2023-02-13 17:08:37 +00:00
|
|
|
use crate::protos::irc::{Chan, Recipient};
|
2023-02-07 15:21:00 +00:00
|
|
|
use crate::util::Terminator;
|
|
|
|
|
2023-02-13 20:04:08 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
|
|
|
|
2023-02-10 17:09:29 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2023-02-07 15:21:00 +00:00
|
|
|
pub struct ServerConfig {
|
|
|
|
pub listen_on: SocketAddr,
|
2023-02-10 17:09:29 +00:00
|
|
|
pub server_name: String,
|
2023-02-07 15:21:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 18:47:58 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct RegisteredUser {
|
|
|
|
nickname: Vec<u8>,
|
|
|
|
username: Vec<u8>,
|
|
|
|
realname: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2023-02-07 15:21:00 +00:00
|
|
|
async fn handle_socket(
|
2023-02-10 17:09:29 +00:00
|
|
|
config: ServerConfig,
|
2023-02-07 15:21:00 +00:00
|
|
|
mut stream: TcpStream,
|
|
|
|
socket_addr: SocketAddr,
|
2023-02-14 22:49:56 +00:00
|
|
|
players: PlayerRegistry,
|
2023-02-14 17:53:43 +00:00
|
|
|
rooms: RoomRegistry,
|
2023-02-10 21:27:29 +00:00
|
|
|
) -> Result<()> {
|
2023-02-07 15:21:00 +00:00
|
|
|
let (reader, writer) = stream.split();
|
2023-02-10 18:47:58 +00:00
|
|
|
let mut reader: BufReader<ReadHalf> = BufReader::new(reader);
|
2023-02-07 15:21:00 +00:00
|
|
|
let mut writer = BufWriter::new(writer);
|
|
|
|
|
2023-02-10 21:27:29 +00:00
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::Notice {
|
|
|
|
first_target: b"*".to_vec(),
|
|
|
|
rest_targets: vec![],
|
|
|
|
text: b"Welcome to my server!".to_vec(),
|
|
|
|
},
|
2023-02-10 18:47:58 +00:00
|
|
|
}
|
2023-02-10 21:27:29 +00:00
|
|
|
.write_async(&mut writer)
|
|
|
|
.await?;
|
|
|
|
writer.flush().await?;
|
2023-02-10 18:47:58 +00:00
|
|
|
|
2023-02-13 18:58:05 +00:00
|
|
|
let registered_user: Result<RegisteredUser> =
|
|
|
|
handle_registration(&mut reader, &mut writer).await;
|
|
|
|
match registered_user {
|
|
|
|
Ok(user) => {
|
2023-02-14 22:49:56 +00:00
|
|
|
handle_registered_socket(config, players, rooms, &mut reader, &mut writer, user)
|
|
|
|
.await?;
|
2023-02-13 18:58:05 +00:00
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.shutdown().await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-02-07 15:21:00 +00:00
|
|
|
|
2023-02-13 18:58:05 +00:00
|
|
|
async fn handle_registration<'a>(
|
|
|
|
reader: &mut BufReader<ReadHalf<'a>>,
|
|
|
|
writer: &mut BufWriter<WriteHalf<'a>>,
|
|
|
|
) -> Result<RegisteredUser> {
|
|
|
|
let mut buffer = vec![];
|
2023-02-07 15:21:00 +00:00
|
|
|
|
2023-02-10 18:47:58 +00:00
|
|
|
let mut future_nickname: Option<Vec<u8>> = None;
|
|
|
|
let mut future_username: Option<(Vec<u8>, Vec<u8>)> = None;
|
|
|
|
|
2023-02-13 18:58:05 +00:00
|
|
|
loop {
|
2023-02-10 18:47:58 +00:00
|
|
|
let res = reader.read_until(b'\n', &mut buffer).await;
|
|
|
|
match res {
|
|
|
|
Ok(len) => {
|
|
|
|
if len == 0 {
|
|
|
|
log::info!("Terminating socket");
|
|
|
|
break Err(anyhow::Error::msg("EOF"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("Failed to read from socket: {err}");
|
|
|
|
break Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let parsed = client_message(&buffer[..]);
|
|
|
|
match parsed {
|
2023-02-14 22:49:56 +00:00
|
|
|
Ok((_, msg)) => {
|
2023-02-10 18:47:58 +00:00
|
|
|
log::info!("Incoming IRC message: {msg:?}");
|
|
|
|
match msg {
|
|
|
|
ClientMessage::Nick { nickname } => {
|
|
|
|
if let Some((username, realname)) = future_username {
|
|
|
|
break Ok(RegisteredUser {
|
|
|
|
nickname,
|
|
|
|
username,
|
|
|
|
realname,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
future_nickname = Some(nickname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ClientMessage::User { username, realname } => {
|
|
|
|
if let Some(nickname) = future_nickname {
|
|
|
|
break Ok(RegisteredUser {
|
|
|
|
nickname,
|
|
|
|
username,
|
|
|
|
realname,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
future_username = Some((username, realname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("Failed to parse IRC message: {err}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_registered_socket<'a>(
|
|
|
|
config: ServerConfig,
|
|
|
|
mut players: PlayerRegistry,
|
2023-02-14 17:53:43 +00:00
|
|
|
rooms: RoomRegistry,
|
2023-02-13 18:58:05 +00:00
|
|
|
reader: &mut BufReader<ReadHalf<'a>>,
|
|
|
|
writer: &mut BufWriter<WriteHalf<'a>>,
|
2023-02-10 18:47:58 +00:00
|
|
|
user: RegisteredUser,
|
2023-02-10 21:27:29 +00:00
|
|
|
) -> Result<()> {
|
2023-02-10 18:47:58 +00:00
|
|
|
let mut buffer = vec![];
|
|
|
|
log::info!("Handling registered user: {user:?}");
|
|
|
|
|
2023-02-14 19:07:07 +00:00
|
|
|
let player_id = PlayerId::from_bytes(user.nickname.clone())?;
|
2023-02-13 20:58:44 +00:00
|
|
|
let mut connection = players.connect_to_player(player_id.clone()).await;
|
2023-02-12 23:31:16 +00:00
|
|
|
|
2023-02-10 21:27:29 +00:00
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N001Welcome {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
text: b"Welcome to Kek Server".to_vec(),
|
|
|
|
},
|
2023-02-10 18:47:58 +00:00
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
.write_async(writer)
|
2023-02-10 21:27:29 +00:00
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N002YourHost {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
text: b"Welcome to Kek Server".to_vec(),
|
|
|
|
},
|
2023-02-10 18:47:58 +00:00
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
.write_async(writer)
|
2023-02-10 21:27:29 +00:00
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N003Created {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
text: b"Welcome to Kek Server".to_vec(),
|
|
|
|
},
|
2023-02-10 18:47:58 +00:00
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
.write_async(writer)
|
2023-02-10 21:27:29 +00:00
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N004MyInfo {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
hostname: config.server_name.as_bytes().to_vec(),
|
|
|
|
softname: b"kek-0.1.alpha.3".to_vec(),
|
|
|
|
},
|
2023-02-10 18:47:58 +00:00
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
.write_async(writer)
|
2023-02-10 21:27:29 +00:00
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N005ISupport {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
params: b"CHANTYPES=#".to_vec(),
|
|
|
|
},
|
2023-02-10 18:47:58 +00:00
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
.write_async(writer)
|
2023-02-10 21:27:29 +00:00
|
|
|
.await?;
|
2023-02-14 18:28:49 +00:00
|
|
|
|
2023-02-15 20:49:52 +00:00
|
|
|
let rooms_list = connection.get_rooms().await?;
|
2023-02-14 18:28:49 +00:00
|
|
|
for room in &rooms_list {
|
2023-02-14 18:46:42 +00:00
|
|
|
produce_on_join_cmd_messages(
|
|
|
|
&config,
|
|
|
|
&user,
|
2023-02-14 19:07:07 +00:00
|
|
|
&Chan::Global(room.id.as_bytes().clone()),
|
2023-02-14 18:46:42 +00:00
|
|
|
room,
|
|
|
|
writer,
|
|
|
|
)
|
|
|
|
.await?;
|
2023-02-14 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 21:27:29 +00:00
|
|
|
writer.flush().await?;
|
2023-02-10 18:47:58 +00:00
|
|
|
|
2023-02-07 15:21:00 +00:00
|
|
|
loop {
|
|
|
|
select! {
|
|
|
|
biased;
|
2023-02-13 18:58:05 +00:00
|
|
|
len = reader.read_until(b'\n', &mut buffer) => {
|
|
|
|
let len = len?;
|
|
|
|
let len = if len == 0 {
|
|
|
|
log::info!("EOF, Terminating socket");
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
len
|
|
|
|
};
|
2023-02-14 22:49:56 +00:00
|
|
|
handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, &player_id, writer).await?;
|
2023-02-07 15:21:00 +00:00
|
|
|
buffer.clear();
|
|
|
|
},
|
2023-02-13 19:16:00 +00:00
|
|
|
update = connection.receiver.recv() => {
|
2023-02-13 17:08:37 +00:00
|
|
|
match update.unwrap() {
|
2023-02-14 22:22:04 +00:00
|
|
|
Updates::RoomJoined { new_member_id, room_id } => {
|
2023-02-14 19:42:52 +00:00
|
|
|
if player_id == new_member_id {
|
2023-02-14 17:53:43 +00:00
|
|
|
if let Some(room) = rooms.get_room(&room_id) {
|
|
|
|
let room_info = room.get_room_info().await;
|
2023-02-14 19:07:07 +00:00
|
|
|
let chan = Chan::Global(room_id.as_bytes().clone());
|
2023-02-14 17:53:43 +00:00
|
|
|
produce_on_join_cmd_messages(&config, &user, &chan, &room_info, writer).await?;
|
|
|
|
writer.flush().await?;
|
|
|
|
} else {
|
|
|
|
log::warn!("Received join to a non-existant room");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
2023-02-14 19:42:52 +00:00
|
|
|
sender: Some(new_member_id.as_bytes().clone()),
|
2023-02-14 19:07:07 +00:00
|
|
|
body: ServerMessageBody::Join(Chan::Global(room_id.as_bytes().clone())),
|
2023-02-14 17:53:43 +00:00
|
|
|
}.write_async(writer).await?;
|
|
|
|
writer.flush().await?
|
|
|
|
}
|
|
|
|
},
|
2023-02-15 17:54:48 +00:00
|
|
|
Updates::RoomLeft { room_id, former_member_id } => {
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(former_member_id.as_bytes().clone()),
|
|
|
|
body: ServerMessageBody::Part(Chan::Global(room_id.as_bytes().clone())),
|
|
|
|
}.write_async(writer).await?;
|
|
|
|
writer.flush().await?
|
|
|
|
},
|
2023-02-14 22:22:04 +00:00
|
|
|
Updates::NewMessage { author_id, room_id, body } => {
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(author_id.as_bytes().clone()),
|
|
|
|
body: ServerMessageBody::PrivateMessage { target: Recipient::Chan(Chan::Global(room_id.as_bytes().clone())), body: body.as_bytes().to_vec() }
|
|
|
|
}.write_async(writer).await?;
|
|
|
|
writer.flush().await?
|
2023-02-13 17:08:37 +00:00
|
|
|
},
|
2023-02-14 19:42:52 +00:00
|
|
|
Updates::RoomTopicChanged { room_id, new_topic } => {
|
2023-02-14 18:46:42 +00:00
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N332Topic {
|
|
|
|
client: user.nickname.clone(),
|
2023-02-14 19:07:07 +00:00
|
|
|
chat: Chan::Global(room_id.as_bytes().clone()),
|
2023-02-14 18:46:42 +00:00
|
|
|
topic: new_topic,
|
|
|
|
},
|
|
|
|
}.write_async(writer).await?;
|
|
|
|
writer.flush().await?
|
|
|
|
},
|
2023-02-13 17:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 15:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 16:47:48 +00:00
|
|
|
connection.terminate().await;
|
2023-02-10 21:27:29 +00:00
|
|
|
Ok(())
|
2023-02-07 15:21:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 18:58:05 +00:00
|
|
|
async fn handle_incoming_message(
|
|
|
|
buffer: &[u8],
|
|
|
|
config: &ServerConfig,
|
|
|
|
user: &RegisteredUser,
|
2023-02-13 19:16:00 +00:00
|
|
|
user_handle: &mut PlayerConnection,
|
2023-02-14 22:22:04 +00:00
|
|
|
player_id: &PlayerId,
|
2023-02-13 18:58:05 +00:00
|
|
|
writer: &mut (impl AsyncWrite + Unpin),
|
|
|
|
) -> Result<()> {
|
|
|
|
let parsed = client_message(buffer);
|
|
|
|
match parsed {
|
2023-02-14 22:49:56 +00:00
|
|
|
Ok((_, msg)) => match msg {
|
2023-02-13 18:58:05 +00:00
|
|
|
ClientMessage::Ping { token } => {
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: None,
|
|
|
|
body: ServerMessageBody::Pong {
|
|
|
|
from: config.server_name.as_bytes().to_vec(),
|
|
|
|
token,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
writer.flush().await?;
|
|
|
|
}
|
|
|
|
ClientMessage::Join(ref chan) => {
|
|
|
|
handle_join(&config, &user, user_handle, chan, writer).await?;
|
|
|
|
}
|
2023-02-15 17:54:48 +00:00
|
|
|
ClientMessage::Part { chan, message } => {
|
|
|
|
handle_part(config, user, user_handle, &chan, writer).await?;
|
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
ClientMessage::PrivateMessage { recipient, body } => match recipient {
|
2023-02-14 19:07:07 +00:00
|
|
|
Recipient::Chan(Chan::Global(chan)) => match String::from_utf8(body) {
|
2023-02-13 18:58:05 +00:00
|
|
|
Ok(body) => {
|
2023-02-14 19:07:07 +00:00
|
|
|
let room_id = RoomId::from_bytes(chan)?;
|
2023-02-14 22:22:04 +00:00
|
|
|
user_handle.send_message(room_id, body.clone()).await?;
|
2023-02-13 18:58:05 +00:00
|
|
|
}
|
|
|
|
Err(err) => log::warn!("failed to parse incoming message: {err}"),
|
|
|
|
},
|
|
|
|
_ => log::warn!("Unsupported target type"),
|
|
|
|
},
|
2023-02-14 18:46:42 +00:00
|
|
|
ClientMessage::Topic { chan, topic } => {
|
|
|
|
match chan {
|
2023-02-14 19:07:07 +00:00
|
|
|
Chan::Global(chan) => {
|
|
|
|
let room_id = RoomId::from_bytes(chan)?;
|
2023-02-14 22:22:04 +00:00
|
|
|
user_handle
|
|
|
|
.change_topic(room_id.clone(), topic.clone())
|
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N332Topic {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
chat: Chan::Global(room_id.as_bytes().clone()),
|
|
|
|
topic,
|
|
|
|
},
|
2023-02-14 18:46:42 +00:00
|
|
|
}
|
2023-02-14 22:22:04 +00:00
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
writer.flush().await?;
|
2023-02-14 18:46:42 +00:00
|
|
|
}
|
|
|
|
Chan::Local(_) => {}
|
|
|
|
};
|
2023-02-14 22:22:04 +00:00
|
|
|
}
|
2023-02-14 19:56:31 +00:00
|
|
|
cmd => {
|
|
|
|
log::warn!("Not implemented handler for client command: {cmd:?}");
|
2023-02-14 18:46:42 +00:00
|
|
|
}
|
2023-02-13 18:58:05 +00:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!("Failed to parse IRC message: {err}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_join(
|
|
|
|
config: &ServerConfig,
|
|
|
|
user: &RegisteredUser,
|
2023-02-13 19:16:00 +00:00
|
|
|
user_handle: &mut PlayerConnection,
|
2023-02-13 18:58:05 +00:00
|
|
|
chan: &Chan,
|
|
|
|
writer: &mut (impl AsyncWrite + Unpin),
|
|
|
|
) -> Result<()> {
|
|
|
|
match chan {
|
2023-02-14 22:22:04 +00:00
|
|
|
Chan::Global(chan_name) => {
|
|
|
|
let room_id = RoomId::from_bytes(chan_name.clone())?;
|
2023-02-14 19:07:07 +00:00
|
|
|
let room_info = user_handle.join_room(room_id).await?;
|
2023-02-14 22:22:04 +00:00
|
|
|
produce_on_join_cmd_messages(&config, &user, chan, &room_info, writer).await?;
|
|
|
|
writer.flush().await?;
|
2023-02-13 18:58:05 +00:00
|
|
|
}
|
|
|
|
Chan::Local(_) => {}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:54:48 +00:00
|
|
|
async fn handle_part(
|
|
|
|
config: &ServerConfig,
|
|
|
|
user: &RegisteredUser,
|
|
|
|
user_handle: &mut PlayerConnection,
|
|
|
|
chan: &Chan,
|
|
|
|
writer: &mut (impl AsyncWrite + Unpin),
|
|
|
|
) -> Result<()> {
|
|
|
|
if let Chan::Global(chan_name) = chan {
|
|
|
|
let room_id = RoomId::from_bytes(chan_name.clone())?;
|
|
|
|
user_handle.leave_room(room_id).await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(user.nickname.clone()),
|
|
|
|
body: ServerMessageBody::Part(Chan::Global(chan_name.clone())),
|
|
|
|
}
|
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
writer.flush().await?;
|
|
|
|
} else {
|
|
|
|
log::warn!("Local chans unsupported");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:53:43 +00:00
|
|
|
async fn produce_on_join_cmd_messages(
|
|
|
|
config: &ServerConfig,
|
|
|
|
user: &RegisteredUser,
|
|
|
|
chan: &Chan,
|
|
|
|
room_info: &RoomInfo,
|
|
|
|
writer: &mut (impl AsyncWrite + Unpin),
|
|
|
|
) -> Result<()> {
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(user.nickname.clone()),
|
|
|
|
body: ServerMessageBody::Join(chan.clone()),
|
|
|
|
}
|
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N332Topic {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
chat: chan.clone(),
|
|
|
|
topic: room_info.topic.clone(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
let mut members = if let Some(head) = room_info.members.first() {
|
2023-02-14 19:07:07 +00:00
|
|
|
head.as_bytes().clone()
|
2023-02-14 17:53:43 +00:00
|
|
|
} else {
|
|
|
|
user.nickname.clone()
|
|
|
|
};
|
|
|
|
for i in &room_info.members[1..] {
|
|
|
|
members.push(b' ');
|
2023-02-14 19:07:07 +00:00
|
|
|
members.extend(i.as_bytes());
|
2023-02-14 17:53:43 +00:00
|
|
|
}
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N353NamesReply {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
chan: chan.clone(),
|
|
|
|
members,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
ServerMessage {
|
|
|
|
tags: vec![],
|
|
|
|
sender: Some(config.server_name.as_bytes().to_vec()),
|
|
|
|
body: ServerMessageBody::N366NamesReplyEnd {
|
|
|
|
client: user.nickname.clone(),
|
|
|
|
chan: chan.clone(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
.write_async(writer)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-09 19:01:21 +00:00
|
|
|
pub async fn launch(
|
2023-02-10 17:09:29 +00:00
|
|
|
config: ServerConfig,
|
2023-02-09 19:01:21 +00:00
|
|
|
players: PlayerRegistry,
|
2023-02-14 17:53:43 +00:00
|
|
|
rooms: RoomRegistry,
|
2023-02-09 19:01:21 +00:00
|
|
|
metrics: MetricsRegistry,
|
|
|
|
) -> Result<Terminator> {
|
2023-02-07 15:21:00 +00:00
|
|
|
log::info!("Starting IRC projection");
|
|
|
|
let (signal, mut rx) = channel();
|
2023-02-09 19:01:21 +00:00
|
|
|
let current_connections =
|
|
|
|
IntGauge::new("irc_current_connections", "Open and alive TCP connections")?;
|
|
|
|
let total_connections = IntCounter::new(
|
|
|
|
"irc_total_connections",
|
|
|
|
"Total number of opened connections",
|
|
|
|
)?;
|
|
|
|
metrics.register(Box::new(current_connections.clone()))?;
|
|
|
|
metrics.register(Box::new(total_connections.clone()))?;
|
|
|
|
|
2023-02-07 15:21:00 +00:00
|
|
|
let listener = TcpListener::bind(config.listen_on).await?;
|
|
|
|
log::debug!("Listener started");
|
|
|
|
|
|
|
|
let handle = tokio::task::spawn(async move {
|
|
|
|
loop {
|
|
|
|
select! {
|
|
|
|
biased;
|
|
|
|
_ = &mut rx => break,
|
|
|
|
new_conn = listener.accept() => {
|
|
|
|
match new_conn {
|
|
|
|
Ok((stream, socket_addr)) => {
|
2023-02-10 17:09:29 +00:00
|
|
|
let config = config.clone();
|
2023-02-09 19:01:21 +00:00
|
|
|
total_connections.inc();
|
|
|
|
current_connections.inc();
|
2023-02-07 15:21:00 +00:00
|
|
|
log::debug!("Incoming connection from {socket_addr}");
|
2023-02-14 17:53:43 +00:00
|
|
|
let players = players.clone();
|
|
|
|
let rooms = rooms.clone();
|
2023-02-10 21:27:29 +00:00
|
|
|
let current_connections_clone = current_connections.clone();
|
|
|
|
let handle = tokio::task::spawn(async move {
|
2023-02-14 17:53:43 +00:00
|
|
|
match handle_socket(config, stream, socket_addr, players, rooms).await {
|
2023-02-10 21:27:29 +00:00
|
|
|
Ok(_) => log::info!("Connection terminated"),
|
|
|
|
Err(err) => log::warn!("Connection failed: {err}"),
|
|
|
|
}
|
|
|
|
current_connections_clone.dec();
|
|
|
|
});
|
2023-02-07 15:21:00 +00:00
|
|
|
},
|
|
|
|
Err(err) => log::warn!("Failed to accept new connection: {err}"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log::info!("Stopping IRC projection");
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
let terminator = Terminator::from_raw(signal, handle);
|
|
|
|
Ok(terminator)
|
|
|
|
}
|