forked from lavina/lavina
Compare commits
38 Commits
a9dfe22d8c
...
b6841ad033
Author | SHA1 | Date |
---|---|---|
homycdev | b6841ad033 | |
Nikita Vilunov | 25605322a0 | |
Nikita Vilunov | 31f9da9b05 | |
Nikita Vilunov | c1dc2df150 | |
Nikita Vilunov | c69513f38b | |
Nikita Vilunov | 8ec9ecfe2c | |
Nikita Vilunov | a047d55ab5 | |
Nikita Vilunov | ea81ddadfc | |
Nikita Vilunov | 4b5ab02322 | |
Nikita Vilunov | 843d0e9c82 | |
Nikita Vilunov | 72f5010988 | |
Nikita Vilunov | 4ff09ea05f | |
Nikita Vilunov | ec49489ef1 | |
Nikita Vilunov | d305f5bf77 | |
Nikita Vilunov | 799da8366c | |
Nikita Vilunov | d805061d5b | |
Nikita Vilunov | 6c08d69f41 | |
Nikita Vilunov | 12d30ca5c2 | |
Nikita Vilunov | ddb348bee9 | |
Nikita Vilunov | 5a09b743c9 | |
Nikita Vilunov | cebe354179 | |
Nikita Vilunov | 02a8309d9e | |
Nikita Vilunov | fbb3d4f4f9 | |
Nikita Vilunov | 048660624d | |
Nikita Vilunov | 6bba699d87 | |
Nikita Vilunov | 6d493d83a3 | |
Nikita Vilunov | 757d7c5665 | |
Nikita Vilunov | 0105a5b710 | |
Nikita Vilunov | 57b6af8732 | |
Nikita Vilunov | 0944c449ca | |
Mikhail | fd694cd75c | |
Nikita Vilunov | cccc05afe9 | |
Nikita Vilunov | 8b099f9be2 | |
Nikita Vilunov | 36b0d50d51 | |
Nikita Vilunov | adece11fef | |
Nikita Vilunov | ab61e975bf | |
Nikita Vilunov | fd437df67e | |
Nikita Vilunov | a325c7307c |
|
@ -0,0 +1,21 @@
|
|||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.5.0
|
||||
hooks:
|
||||
- id: check-toml
|
||||
- id: end-of-file-fixer
|
||||
- id: fix-byte-order-marker
|
||||
- id: mixed-line-ending
|
||||
- id: trailing-whitespace
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: fmt
|
||||
name: fmt
|
||||
description: Format
|
||||
entry: cargo fmt
|
||||
language: system
|
||||
args:
|
||||
- --all
|
||||
types: [ rust ]
|
||||
pass_filenames: false
|
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
|
@ -10,7 +10,7 @@ members = [
|
|||
]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.0.2-dev"
|
||||
version = "0.0.3-dev"
|
||||
|
||||
[workspace.dependencies]
|
||||
nom = "7.1.3"
|
||||
|
@ -27,10 +27,11 @@ clap = { version = "4.4.4", features = ["derive"] }
|
|||
serde = { version = "1.0.152", features = ["rc", "serde_derive"] }
|
||||
tracing = "0.1.37" # logging & tracing api
|
||||
prometheus = { version = "0.13.3", default-features = false }
|
||||
base64 = "0.21.3"
|
||||
base64 = "0.22.0"
|
||||
lavina-core = { path = "crates/lavina-core" }
|
||||
tracing-subscriber = "0.3.16"
|
||||
sasl = { path = "crates/sasl" }
|
||||
chrono = "0.4.37"
|
||||
|
||||
[package]
|
||||
name = "lavina"
|
||||
|
@ -58,6 +59,11 @@ projection-irc = { path = "crates/projection-irc" }
|
|||
projection-xmpp = { path = "crates/projection-xmpp" }
|
||||
mgmt-api = { path = "crates/mgmt-api" }
|
||||
clap.workspace = true
|
||||
opentelemetry = "0.22.0"
|
||||
opentelemetry-semantic-conventions = "0.14.0"
|
||||
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
|
||||
opentelemetry-otlp = "0.15.0"
|
||||
tracing-opentelemetry = "0.23.0"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches.workspace = true
|
||||
|
|
|
@ -9,6 +9,7 @@ server_name = "irc.localhost"
|
|||
listen_on = "127.0.0.1:5222"
|
||||
cert = "./certs/xmpp.pem"
|
||||
key = "./certs/xmpp.key"
|
||||
hostname = "localhost"
|
||||
|
||||
[storage]
|
||||
db_path = "db.sqlite"
|
||||
|
|
|
@ -5,8 +5,11 @@ version.workspace = true
|
|||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
sqlx = { version = "0.7.0-alpha.2", features = ["sqlite", "migrate"] }
|
||||
sqlx = { version = "0.7.4", features = ["sqlite", "migrate", "chrono"] }
|
||||
serde.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
prometheus.workspace = true
|
||||
chrono.workspace = true
|
||||
argon2 = { version = "0.5.3" }
|
||||
rand_core = { version = "0.6.4", features = ["getrandom"] }
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
alter table messages add column created_at text;
|
|
@ -0,0 +1,17 @@
|
|||
create table dialogs(
|
||||
id integer primary key autoincrement not null,
|
||||
participant_1 integer not null,
|
||||
participant_2 integer not null,
|
||||
created_at timestamp not null,
|
||||
message_count integer not null default 0,
|
||||
unique (participant_1, participant_2)
|
||||
);
|
||||
|
||||
create table dialog_messages(
|
||||
dialog_id integer not null,
|
||||
id integer not null, -- unique per dialog, sequential in one dialog
|
||||
author_id integer not null,
|
||||
content string not null,
|
||||
created_at timestamp not null,
|
||||
primary key (dialog_id, id)
|
||||
);
|
|
@ -0,0 +1,4 @@
|
|||
create table challenges_argon2_password(
|
||||
user_id integer primary key not null,
|
||||
hash string not null
|
||||
);
|
|
@ -0,0 +1,66 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString};
|
||||
use argon2::Argon2;
|
||||
use rand_core::OsRng;
|
||||
|
||||
use crate::prelude::log;
|
||||
use crate::repo::Storage;
|
||||
|
||||
pub enum Verdict {
|
||||
Authenticated,
|
||||
UserNotFound,
|
||||
InvalidPassword,
|
||||
}
|
||||
|
||||
pub enum UpdatePasswordResult {
|
||||
PasswordUpdated,
|
||||
UserNotFound,
|
||||
}
|
||||
|
||||
pub struct Authenticator<'a> {
|
||||
storage: &'a Storage,
|
||||
}
|
||||
impl<'a> Authenticator<'a> {
|
||||
pub fn new(storage: &'a Storage) -> Self {
|
||||
Self { storage }
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, provided_password), name = "Authenticator::authenticate")]
|
||||
pub async fn authenticate(&self, login: &str, provided_password: &str) -> Result<Verdict> {
|
||||
let Some(stored_user) = self.storage.retrieve_user_by_name(login).await? else {
|
||||
return Ok(Verdict::UserNotFound);
|
||||
};
|
||||
if let Some(argon2_hash) = stored_user.argon2_hash {
|
||||
let argon2 = Argon2::default();
|
||||
let password_hash =
|
||||
PasswordHash::new(&argon2_hash).map_err(|e| anyhow!("Failed to parse password hash: {e:?}"))?;
|
||||
let password_verifier = argon2.verify_password(provided_password.as_bytes(), &password_hash);
|
||||
if password_verifier.is_ok() {
|
||||
return Ok(Verdict::Authenticated);
|
||||
}
|
||||
}
|
||||
if let Some(expected_password) = stored_user.password {
|
||||
if expected_password == provided_password {
|
||||
return Ok(Verdict::Authenticated);
|
||||
}
|
||||
}
|
||||
Ok(Verdict::InvalidPassword)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, provided_password), name = "Authenticator::set_password")]
|
||||
pub async fn set_password(&self, login: &str, provided_password: &str) -> Result<UpdatePasswordResult> {
|
||||
let Some(u) = self.storage.retrieve_user_by_name(login).await? else {
|
||||
return Ok(UpdatePasswordResult::UserNotFound);
|
||||
};
|
||||
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
let argon2 = Argon2::default();
|
||||
let password_hash = argon2
|
||||
.hash_password(provided_password.as_bytes(), &salt)
|
||||
.map_err(|e| anyhow!("Failed to hash password: {e:?}"))?;
|
||||
|
||||
self.storage.set_argon2_challenge(u.id, password_hash.to_string().as_str()).await?;
|
||||
log::info!("Password changed for player {login}");
|
||||
Ok(UpdatePasswordResult::PasswordUpdated)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,180 @@
|
|||
//! Domain of dialogs – conversations between two participants.
|
||||
//!
|
||||
//! Dialogs are different from rooms in that they are always between two participants.
|
||||
//! There are no admins or other roles in dialogs, both participants have equal rights.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use tokio::sync::RwLock as AsyncRwLock;
|
||||
|
||||
use crate::player::{PlayerId, PlayerRegistry, Updates};
|
||||
use crate::prelude::*;
|
||||
use crate::repo::Storage;
|
||||
|
||||
/// Id of a conversation between two players.
|
||||
///
|
||||
/// Dialogs are identified by the pair of participants' ids. The order of ids does not matter.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct DialogId(PlayerId, PlayerId);
|
||||
impl DialogId {
|
||||
pub fn new(a: PlayerId, b: PlayerId) -> DialogId {
|
||||
if a.as_inner() < b.as_inner() {
|
||||
DialogId(a, b)
|
||||
} else {
|
||||
DialogId(b, a)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_inner(&self) -> (&PlayerId, &PlayerId) {
|
||||
(&self.0, &self.1)
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> (PlayerId, PlayerId) {
|
||||
(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
struct Dialog {
|
||||
storage_id: u32,
|
||||
player_storage_id_1: u32,
|
||||
player_storage_id_2: u32,
|
||||
message_count: u32,
|
||||
}
|
||||
|
||||
struct DialogRegistryInner {
|
||||
dialogs: HashMap<DialogId, AsyncRwLock<Dialog>>,
|
||||
players: Option<PlayerRegistry>,
|
||||
storage: Storage,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DialogRegistry(Arc<AsyncRwLock<DialogRegistryInner>>);
|
||||
|
||||
impl DialogRegistry {
|
||||
pub async fn send_message(
|
||||
&self,
|
||||
from: PlayerId,
|
||||
to: PlayerId,
|
||||
body: Str,
|
||||
created_at: &DateTime<Utc>,
|
||||
) -> Result<()> {
|
||||
let mut guard = self.0.read().await;
|
||||
let id = DialogId::new(from.clone(), to.clone());
|
||||
let dialog = guard.dialogs.get(&id);
|
||||
if let Some(d) = dialog {
|
||||
let mut d = d.write().await;
|
||||
guard
|
||||
.storage
|
||||
.insert_dialog_message(d.storage_id, d.message_count, from.as_inner(), &body, created_at)
|
||||
.await?;
|
||||
d.message_count += 1;
|
||||
} else {
|
||||
drop(guard);
|
||||
let mut guard2 = self.0.write().await;
|
||||
// double check in case concurrent access has loaded this dialog
|
||||
if let Some(d) = guard2.dialogs.get(&id) {
|
||||
let mut d = d.write().await;
|
||||
guard2
|
||||
.storage
|
||||
.insert_dialog_message(d.storage_id, d.message_count, from.as_inner(), &body, created_at)
|
||||
.await?;
|
||||
d.message_count += 1;
|
||||
} else {
|
||||
let (p1, p2) = id.as_inner();
|
||||
tracing::info!("Dialog {id:?} not found locally, trying to load from storage");
|
||||
let stored_dialog = match guard2.storage.retrieve_dialog(p1.as_inner(), p2.as_inner()).await? {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
tracing::info!("Dialog {id:?} does not exist, creating a new one in storage");
|
||||
guard2.storage.initialize_dialog(p1.as_inner(), p2.as_inner(), created_at).await?
|
||||
}
|
||||
};
|
||||
tracing::info!("Dialog {id:?} loaded");
|
||||
guard2
|
||||
.storage
|
||||
.insert_dialog_message(
|
||||
stored_dialog.id,
|
||||
stored_dialog.message_count,
|
||||
from.as_inner(),
|
||||
&body,
|
||||
created_at,
|
||||
)
|
||||
.await?;
|
||||
let dialog = Dialog {
|
||||
storage_id: stored_dialog.id,
|
||||
player_storage_id_1: stored_dialog.participant_1,
|
||||
player_storage_id_2: stored_dialog.participant_2,
|
||||
message_count: stored_dialog.message_count + 1,
|
||||
};
|
||||
guard2.dialogs.insert(id.clone(), AsyncRwLock::new(dialog));
|
||||
}
|
||||
guard = guard2.downgrade();
|
||||
}
|
||||
// TODO send message to the other player and persist it
|
||||
let Some(players) = &guard.players else {
|
||||
tracing::error!("No player registry present");
|
||||
return Ok(());
|
||||
};
|
||||
let Some(player) = players.get_player(&to).await else {
|
||||
tracing::debug!("Player {to:?} not active, not sending message");
|
||||
return Ok(());
|
||||
};
|
||||
let update = Updates::NewDialogMessage {
|
||||
sender: from.clone(),
|
||||
receiver: to.clone(),
|
||||
body: body.clone(),
|
||||
created_at: created_at.clone(),
|
||||
};
|
||||
player.update(update).await;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
impl DialogRegistry {
|
||||
pub fn new(storage: Storage) -> DialogRegistry {
|
||||
DialogRegistry(Arc::new(AsyncRwLock::new(DialogRegistryInner {
|
||||
dialogs: HashMap::new(),
|
||||
players: None,
|
||||
storage,
|
||||
})))
|
||||
}
|
||||
|
||||
pub async fn set_players(&self, players: PlayerRegistry) {
|
||||
let mut guard = self.0.write().await;
|
||||
guard.players = Some(players);
|
||||
}
|
||||
|
||||
pub async fn unset_players(&self) {
|
||||
let mut guard = self.0.write().await;
|
||||
guard.players = None;
|
||||
}
|
||||
|
||||
pub fn shutdown(self) -> Result<()> {
|
||||
let res = match Arc::try_unwrap(self.0) {
|
||||
Ok(e) => e,
|
||||
Err(_) => return Err(fail("failed to acquire dialogs ownership on shutdown")),
|
||||
};
|
||||
let res = res.into_inner();
|
||||
drop(res);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_dialog_id_new() {
|
||||
let a = PlayerId::from("a").unwrap();
|
||||
let b = PlayerId::from("b").unwrap();
|
||||
let id1 = DialogId::new(a.clone(), b.clone());
|
||||
let id2 = DialogId::new(a.clone(), b.clone());
|
||||
// Dialog ids are invariant with respect to the order of participants
|
||||
assert_eq!(id1, id2);
|
||||
assert_eq!(id1.as_inner(), (&a, &b));
|
||||
assert_eq!(id2.as_inner(), (&a, &b));
|
||||
}
|
||||
}
|
|
@ -1,4 +1,14 @@
|
|||
//! Domain definitions and implementation of common chat logic.
|
||||
use anyhow::Result;
|
||||
use prometheus::Registry as MetricsRegistry;
|
||||
|
||||
use crate::dialog::DialogRegistry;
|
||||
use crate::player::PlayerRegistry;
|
||||
use crate::repo::Storage;
|
||||
use crate::room::RoomRegistry;
|
||||
|
||||
pub mod auth;
|
||||
pub mod dialog;
|
||||
pub mod player;
|
||||
pub mod prelude;
|
||||
pub mod repo;
|
||||
|
@ -6,3 +16,34 @@ pub mod room;
|
|||
pub mod terminator;
|
||||
|
||||
mod table;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LavinaCore {
|
||||
pub players: PlayerRegistry,
|
||||
pub rooms: RoomRegistry,
|
||||
pub dialogs: DialogRegistry,
|
||||
}
|
||||
|
||||
impl LavinaCore {
|
||||
pub async fn new(mut metrics: MetricsRegistry, storage: Storage) -> Result<LavinaCore> {
|
||||
// TODO shutdown all services in reverse order on error
|
||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone())?;
|
||||
let dialogs = DialogRegistry::new(storage.clone());
|
||||
let players = PlayerRegistry::empty(rooms.clone(), dialogs.clone(), storage.clone(), &mut metrics)?;
|
||||
dialogs.set_players(players.clone()).await;
|
||||
Ok(LavinaCore {
|
||||
players,
|
||||
rooms,
|
||||
dialogs,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn shutdown(mut self) -> Result<()> {
|
||||
self.players.shutdown_all().await?;
|
||||
self.dialogs.unset_players().await;
|
||||
self.players.shutdown()?;
|
||||
self.dialogs.shutdown()?;
|
||||
self.rooms.shutdown()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,16 +7,19 @@
|
|||
//!
|
||||
//! A player actor is a serial handler of commands from a single player. It is preferable to run all per-player validations in the player actor,
|
||||
//! so that they don't overload the room actor.
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use prometheus::{IntGauge, Registry as MetricsRegistry};
|
||||
use serde::Serialize;
|
||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||
use tokio::sync::RwLock;
|
||||
use tracing::{Instrument, Span};
|
||||
|
||||
use crate::dialog::DialogRegistry;
|
||||
use crate::prelude::*;
|
||||
use crate::repo::Storage;
|
||||
use crate::room::{RoomHandle, RoomId, RoomInfo, RoomRegistry};
|
||||
use crate::table::{AnonTable, Key as AnonKey};
|
||||
|
||||
|
@ -52,12 +55,13 @@ pub struct ConnectionId(pub AnonKey);
|
|||
/// The connection is used to send commands to the player actor and to receive updates that might be sent to the client.
|
||||
pub struct PlayerConnection {
|
||||
pub connection_id: ConnectionId,
|
||||
pub receiver: Receiver<Updates>,
|
||||
pub receiver: Receiver<ConnectionMessage>,
|
||||
player_handle: PlayerHandle,
|
||||
}
|
||||
impl PlayerConnection {
|
||||
/// Handled in [Player::send_message].
|
||||
pub async fn send_message(&mut self, room_id: RoomId, body: Str) -> Result<()> {
|
||||
#[tracing::instrument(skip(self, body), name = "PlayerConnection::send_message")]
|
||||
pub async fn send_message(&mut self, room_id: RoomId, body: Str) -> Result<SendMessageResult> {
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ClientCommand::SendMessage { room_id, body, promise };
|
||||
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
||||
|
@ -65,6 +69,7 @@ impl PlayerConnection {
|
|||
}
|
||||
|
||||
/// Handled in [Player::join_room].
|
||||
#[tracing::instrument(skip(self), name = "PlayerConnection::join_room")]
|
||||
pub async fn join_room(&mut self, room_id: RoomId) -> Result<JoinResult> {
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ClientCommand::JoinRoom { room_id, promise };
|
||||
|
@ -73,6 +78,7 @@ impl PlayerConnection {
|
|||
}
|
||||
|
||||
/// Handled in [Player::change_topic].
|
||||
#[tracing::instrument(skip(self, new_topic), name = "PlayerConnection::change_topic")]
|
||||
pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result<()> {
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ClientCommand::ChangeTopic {
|
||||
|
@ -85,6 +91,7 @@ impl PlayerConnection {
|
|||
}
|
||||
|
||||
/// Handled in [Player::leave_room].
|
||||
#[tracing::instrument(skip(self), name = "PlayerConnection::leave_room")]
|
||||
pub async fn leave_room(&mut self, room_id: RoomId) -> Result<()> {
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ClientCommand::LeaveRoom { room_id, promise };
|
||||
|
@ -97,25 +104,39 @@ impl PlayerConnection {
|
|||
}
|
||||
|
||||
/// Handled in [Player::get_rooms].
|
||||
#[tracing::instrument(skip(self), name = "PlayerConnection::get_rooms")]
|
||||
pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> {
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ClientCommand::GetRooms { promise };
|
||||
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
||||
Ok(deferred.await?)
|
||||
}
|
||||
|
||||
/// Handler in [Player::send_dialog_message].
|
||||
#[tracing::instrument(skip(self, body), name = "PlayerConnection::send_dialog_message")]
|
||||
pub async fn send_dialog_message(&self, recipient: PlayerId, body: Str) -> Result<()> {
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ClientCommand::SendDialogMessage {
|
||||
recipient,
|
||||
body,
|
||||
promise,
|
||||
};
|
||||
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
|
||||
Ok(deferred.await?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle to a player actor.
|
||||
#[derive(Clone)]
|
||||
pub struct PlayerHandle {
|
||||
tx: Sender<ActorCommand>,
|
||||
tx: Sender<(ActorCommand, Span)>,
|
||||
}
|
||||
impl PlayerHandle {
|
||||
pub async fn subscribe(&self) -> PlayerConnection {
|
||||
let (sender, receiver) = channel(32);
|
||||
let (promise, deferred) = oneshot();
|
||||
let cmd = ActorCommand::AddConnection { sender, promise };
|
||||
let _ = self.tx.send(cmd).await;
|
||||
self.send(cmd).await;
|
||||
let connection_id = deferred.await.unwrap();
|
||||
PlayerConnection {
|
||||
connection_id,
|
||||
|
@ -125,8 +146,9 @@ impl PlayerHandle {
|
|||
}
|
||||
|
||||
async fn send(&self, command: ActorCommand) {
|
||||
let span = tracing::span!(tracing::Level::INFO, "PlayerHandle::send");
|
||||
// TODO either handle the error or doc why it is safe to ignore
|
||||
let _ = self.tx.send(command).await;
|
||||
let _ = self.tx.send((command, span)).await;
|
||||
}
|
||||
|
||||
pub async fn update(&self, update: Updates) {
|
||||
|
@ -138,7 +160,7 @@ impl PlayerHandle {
|
|||
enum ActorCommand {
|
||||
/// Establish a new connection.
|
||||
AddConnection {
|
||||
sender: Sender<Updates>,
|
||||
sender: Sender<ConnectionMessage>,
|
||||
promise: Promise<ConnectionId>,
|
||||
},
|
||||
/// Terminate an existing connection.
|
||||
|
@ -163,7 +185,7 @@ pub enum ClientCommand {
|
|||
SendMessage {
|
||||
room_id: RoomId,
|
||||
body: Str,
|
||||
promise: Promise<()>,
|
||||
promise: Promise<SendMessageResult>,
|
||||
},
|
||||
ChangeTopic {
|
||||
room_id: RoomId,
|
||||
|
@ -173,13 +195,24 @@ pub enum ClientCommand {
|
|||
GetRooms {
|
||||
promise: Promise<Vec<RoomInfo>>,
|
||||
},
|
||||
SendDialogMessage {
|
||||
recipient: PlayerId,
|
||||
body: Str,
|
||||
promise: Promise<()>,
|
||||
},
|
||||
}
|
||||
|
||||
pub enum JoinResult {
|
||||
Success(RoomInfo),
|
||||
AlreadyJoined,
|
||||
Banned,
|
||||
}
|
||||
|
||||
pub enum SendMessageResult {
|
||||
Success(DateTime<Utc>),
|
||||
NoSuchRoom,
|
||||
}
|
||||
|
||||
/// Player update event type which is sent to a player actor and from there to a connection handler.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Updates {
|
||||
|
@ -191,6 +224,7 @@ pub enum Updates {
|
|||
room_id: RoomId,
|
||||
author_id: PlayerId,
|
||||
body: Str,
|
||||
created_at: DateTime<Utc>,
|
||||
},
|
||||
RoomJoined {
|
||||
room_id: RoomId,
|
||||
|
@ -202,42 +236,99 @@ pub enum Updates {
|
|||
},
|
||||
/// The player was banned from the room and left it immediately.
|
||||
BannedFrom(RoomId),
|
||||
NewDialogMessage {
|
||||
sender: PlayerId,
|
||||
receiver: PlayerId,
|
||||
body: Str,
|
||||
created_at: DateTime<Utc>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Handle to a player registry — a shared data structure containing information about players.
|
||||
#[derive(Clone)]
|
||||
pub struct PlayerRegistry(Arc<RwLock<PlayerRegistryInner>>);
|
||||
impl PlayerRegistry {
|
||||
pub fn empty(room_registry: RoomRegistry, metrics: &mut MetricsRegistry) -> Result<PlayerRegistry> {
|
||||
pub fn empty(
|
||||
room_registry: RoomRegistry,
|
||||
dialogs: DialogRegistry,
|
||||
storage: Storage,
|
||||
metrics: &mut MetricsRegistry,
|
||||
) -> Result<PlayerRegistry> {
|
||||
let metric_active_players = IntGauge::new("chat_players_active", "Number of alive player actors")?;
|
||||
metrics.register(Box::new(metric_active_players.clone()))?;
|
||||
let inner = PlayerRegistryInner {
|
||||
room_registry,
|
||||
dialogs,
|
||||
storage,
|
||||
players: HashMap::new(),
|
||||
metric_active_players,
|
||||
};
|
||||
Ok(PlayerRegistry(Arc::new(RwLock::new(inner))))
|
||||
}
|
||||
|
||||
pub async fn get_or_create_player(&mut self, id: PlayerId) -> PlayerHandle {
|
||||
let mut inner = self.0.write().unwrap();
|
||||
if let Some((handle, _)) = inner.players.get(&id) {
|
||||
handle.clone()
|
||||
pub fn shutdown(self) -> Result<()> {
|
||||
let res = match Arc::try_unwrap(self.0) {
|
||||
Ok(e) => e,
|
||||
Err(_) => return Err(fail("failed to acquire players ownership on shutdown")),
|
||||
};
|
||||
let res = res.into_inner();
|
||||
drop(res);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "PlayerRegistry::get_player")]
|
||||
pub async fn get_player(&self, id: &PlayerId) -> Option<PlayerHandle> {
|
||||
let inner = self.0.read().await;
|
||||
inner.players.get(id).map(|(handle, _)| handle.clone())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "PlayerRegistry::stop_player")]
|
||||
pub async fn stop_player(&self, id: &PlayerId) -> Result<Option<()>> {
|
||||
let mut inner = self.0.write().await;
|
||||
if let Some((handle, fiber)) = inner.players.remove(id) {
|
||||
handle.send(ActorCommand::Stop).await;
|
||||
drop(handle);
|
||||
fiber.await?;
|
||||
inner.metric_active_players.dec();
|
||||
Ok(Some(()))
|
||||
} else {
|
||||
let (handle, fiber) = Player::launch(id.clone(), inner.room_registry.clone());
|
||||
inner.players.insert(id, (handle.clone(), fiber));
|
||||
inner.metric_active_players.inc();
|
||||
handle
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn connect_to_player(&mut self, id: PlayerId) -> PlayerConnection {
|
||||
let player_handle = self.get_or_create_player(id).await;
|
||||
#[tracing::instrument(skip(self), name = "PlayerRegistry::get_or_launch_player")]
|
||||
pub async fn get_or_launch_player(&mut self, id: &PlayerId) -> PlayerHandle {
|
||||
let inner = self.0.read().await;
|
||||
if let Some((handle, _)) = inner.players.get(id) {
|
||||
handle.clone()
|
||||
} else {
|
||||
drop(inner);
|
||||
let mut inner = self.0.write().await;
|
||||
if let Some((handle, _)) = inner.players.get(id) {
|
||||
handle.clone()
|
||||
} else {
|
||||
let (handle, fiber) = Player::launch(
|
||||
id.clone(),
|
||||
inner.room_registry.clone(),
|
||||
inner.dialogs.clone(),
|
||||
inner.storage.clone(),
|
||||
)
|
||||
.await;
|
||||
inner.players.insert(id.clone(), (handle.clone(), fiber));
|
||||
inner.metric_active_players.inc();
|
||||
handle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "PlayerRegistry::connect_to_player")]
|
||||
pub async fn connect_to_player(&mut self, id: &PlayerId) -> PlayerConnection {
|
||||
let player_handle = self.get_or_launch_player(id).await;
|
||||
player_handle.subscribe().await
|
||||
}
|
||||
|
||||
pub async fn shutdown_all(&mut self) -> Result<()> {
|
||||
let mut inner = self.0.write().unwrap();
|
||||
let mut inner = self.0.write().await;
|
||||
for (i, (k, j)) in inner.players.drain() {
|
||||
k.send(ActorCommand::Stop).await;
|
||||
drop(k);
|
||||
|
@ -252,6 +343,9 @@ impl PlayerRegistry {
|
|||
/// The player registry state representation.
|
||||
struct PlayerRegistryInner {
|
||||
room_registry: RoomRegistry,
|
||||
dialogs: DialogRegistry,
|
||||
storage: Storage,
|
||||
/// Active player actors.
|
||||
players: HashMap<PlayerId, (PlayerHandle, JoinHandle<Player>)>,
|
||||
metric_active_players: IntGauge,
|
||||
}
|
||||
|
@ -259,47 +353,87 @@ struct PlayerRegistryInner {
|
|||
/// Player actor inner state representation.
|
||||
struct Player {
|
||||
player_id: PlayerId,
|
||||
connections: AnonTable<Sender<Updates>>,
|
||||
storage_id: u32,
|
||||
connections: AnonTable<Sender<ConnectionMessage>>,
|
||||
my_rooms: HashMap<RoomId, RoomHandle>,
|
||||
banned_from: HashSet<RoomId>,
|
||||
rx: Receiver<ActorCommand>,
|
||||
rx: Receiver<(ActorCommand, Span)>,
|
||||
handle: PlayerHandle,
|
||||
rooms: RoomRegistry,
|
||||
dialogs: DialogRegistry,
|
||||
storage: Storage,
|
||||
}
|
||||
impl Player {
|
||||
fn launch(player_id: PlayerId, rooms: RoomRegistry) -> (PlayerHandle, JoinHandle<Player>) {
|
||||
async fn launch(
|
||||
player_id: PlayerId,
|
||||
rooms: RoomRegistry,
|
||||
dialogs: DialogRegistry,
|
||||
storage: Storage,
|
||||
) -> (PlayerHandle, JoinHandle<Player>) {
|
||||
let (tx, rx) = channel(32);
|
||||
let handle = PlayerHandle { tx };
|
||||
let handle_clone = handle.clone();
|
||||
let storage_id = storage.retrieve_user_id_by_name(player_id.as_inner()).await.unwrap().unwrap();
|
||||
let player = Player {
|
||||
player_id,
|
||||
storage_id,
|
||||
// connections are empty when the actor is just started
|
||||
connections: AnonTable::new(),
|
||||
// room handlers will be loaded later in the started task
|
||||
my_rooms: HashMap::new(),
|
||||
banned_from: HashSet::from([RoomId::from("Empty").unwrap()]),
|
||||
// TODO implement and load bans
|
||||
banned_from: HashSet::new(),
|
||||
rx,
|
||||
handle,
|
||||
rooms,
|
||||
dialogs,
|
||||
storage,
|
||||
};
|
||||
let fiber = tokio::task::spawn(player.main_loop());
|
||||
(handle_clone, fiber)
|
||||
}
|
||||
|
||||
async fn main_loop(mut self) -> Self {
|
||||
let rooms = self.storage.get_rooms_of_a_user(self.storage_id).await.unwrap();
|
||||
for room_id in rooms {
|
||||
let room = self.rooms.get_room(&room_id).await;
|
||||
if let Some(room) = room {
|
||||
self.my_rooms.insert(room_id, room);
|
||||
} else {
|
||||
tracing::error!("Room #{room_id:?} not found");
|
||||
}
|
||||
}
|
||||
while let Some(cmd) = self.rx.recv().await {
|
||||
match cmd {
|
||||
ActorCommand::AddConnection { sender, promise } => {
|
||||
let connection_id = self.connections.insert(sender);
|
||||
if let Err(connection_id) = promise.send(ConnectionId(connection_id)) {
|
||||
log::warn!("Connection {connection_id:?} terminated before finalization");
|
||||
self.terminate_connection(connection_id);
|
||||
let (cmd, span) = cmd;
|
||||
let should_stop = async {
|
||||
match cmd {
|
||||
ActorCommand::AddConnection { sender, promise } => {
|
||||
let connection_id = self.connections.insert(sender);
|
||||
if let Err(connection_id) = promise.send(ConnectionId(connection_id)) {
|
||||
log::warn!("Connection {connection_id:?} terminated before finalization");
|
||||
self.terminate_connection(connection_id);
|
||||
}
|
||||
false
|
||||
}
|
||||
ActorCommand::TerminateConnection(connection_id) => {
|
||||
self.terminate_connection(connection_id);
|
||||
false
|
||||
}
|
||||
ActorCommand::Update(update) => {
|
||||
self.handle_update(update).await;
|
||||
false
|
||||
}
|
||||
ActorCommand::ClientCommand(cmd, connection_id) => {
|
||||
self.handle_cmd(cmd, connection_id).await;
|
||||
false
|
||||
}
|
||||
ActorCommand::Stop => true,
|
||||
}
|
||||
ActorCommand::TerminateConnection(connection_id) => {
|
||||
self.terminate_connection(connection_id);
|
||||
}
|
||||
ActorCommand::Update(update) => self.handle_update(update).await,
|
||||
ActorCommand::ClientCommand(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await,
|
||||
ActorCommand::Stop => break,
|
||||
}
|
||||
.instrument(span)
|
||||
.await;
|
||||
if should_stop {
|
||||
break;
|
||||
}
|
||||
}
|
||||
log::debug!("Shutting down player actor #{:?}", self.player_id);
|
||||
|
@ -307,8 +441,9 @@ impl Player {
|
|||
}
|
||||
|
||||
/// Handle an incoming update by changing the internal state and broadcasting it to all connections if necessary.
|
||||
#[tracing::instrument(skip(self, update), name = "Player::handle_update")]
|
||||
async fn handle_update(&mut self, update: Updates) {
|
||||
log::info!(
|
||||
log::debug!(
|
||||
"Player received an update, broadcasting to {} connections",
|
||||
self.connections.len()
|
||||
);
|
||||
|
@ -320,7 +455,7 @@ impl Player {
|
|||
_ => {}
|
||||
}
|
||||
for (_, connection) in &self.connections {
|
||||
let _ = connection.send(update.clone()).await;
|
||||
let _ = connection.send(ConnectionMessage::Update(update.clone())).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,8 +477,8 @@ impl Player {
|
|||
let _ = promise.send(());
|
||||
}
|
||||
ClientCommand::SendMessage { room_id, body, promise } => {
|
||||
self.send_message(connection_id, room_id, body).await;
|
||||
let _ = promise.send(());
|
||||
let result = self.send_message(connection_id, room_id, body).await;
|
||||
let _ = promise.send(result);
|
||||
}
|
||||
ClientCommand::ChangeTopic {
|
||||
room_id,
|
||||
|
@ -357,13 +492,25 @@ impl Player {
|
|||
let result = self.get_rooms().await;
|
||||
let _ = promise.send(result);
|
||||
}
|
||||
ClientCommand::SendDialogMessage {
|
||||
recipient,
|
||||
body,
|
||||
promise,
|
||||
} => {
|
||||
self.send_dialog_message(connection_id, recipient, body).await;
|
||||
let _ = promise.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "Player::join_room")]
|
||||
async fn join_room(&mut self, connection_id: ConnectionId, room_id: RoomId) -> JoinResult {
|
||||
if self.banned_from.contains(&room_id) {
|
||||
return JoinResult::Banned;
|
||||
}
|
||||
if self.my_rooms.contains_key(&room_id) {
|
||||
return JoinResult::AlreadyJoined;
|
||||
}
|
||||
|
||||
let room = match self.rooms.get_or_create_room(room_id.clone()).await {
|
||||
Ok(room) => room,
|
||||
|
@ -372,7 +519,8 @@ impl Player {
|
|||
todo!();
|
||||
}
|
||||
};
|
||||
room.subscribe(self.player_id.clone(), self.handle.clone()).await;
|
||||
room.add_member(&self.player_id, self.storage_id).await;
|
||||
room.subscribe(&self.player_id, self.handle.clone()).await;
|
||||
self.my_rooms.insert(room_id.clone(), room.clone());
|
||||
let room_info = room.get_room_info().await;
|
||||
let update = Updates::RoomJoined {
|
||||
|
@ -383,10 +531,12 @@ impl Player {
|
|||
JoinResult::Success(room_info)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "Player::leave_room")]
|
||||
async fn leave_room(&mut self, connection_id: ConnectionId, room_id: RoomId) {
|
||||
let room = self.my_rooms.remove(&room_id);
|
||||
if let Some(room) = room {
|
||||
room.unsubscribe(&self.player_id).await;
|
||||
room.remove_member(&self.player_id, self.storage_id).await;
|
||||
}
|
||||
let update = Updates::RoomLeft {
|
||||
room_id,
|
||||
|
@ -395,32 +545,36 @@ impl Player {
|
|||
self.broadcast_update(update, connection_id).await;
|
||||
}
|
||||
|
||||
async fn send_message(&mut self, connection_id: ConnectionId, room_id: RoomId, body: Str) {
|
||||
let room = self.rooms.get_room(&room_id).await;
|
||||
if let Some(room) = room {
|
||||
room.send_message(self.player_id.clone(), body.clone()).await;
|
||||
} else {
|
||||
#[tracing::instrument(skip(self, body), name = "Player::send_message")]
|
||||
async fn send_message(&mut self, connection_id: ConnectionId, room_id: RoomId, body: Str) -> SendMessageResult {
|
||||
let Some(room) = self.my_rooms.get(&room_id) else {
|
||||
tracing::info!("no room found");
|
||||
}
|
||||
return SendMessageResult::NoSuchRoom;
|
||||
};
|
||||
let created_at = chrono::Utc::now();
|
||||
room.send_message(&self.player_id, body.clone(), created_at.clone()).await;
|
||||
let update = Updates::NewMessage {
|
||||
room_id,
|
||||
author_id: self.player_id.clone(),
|
||||
body,
|
||||
created_at,
|
||||
};
|
||||
self.broadcast_update(update, connection_id).await;
|
||||
SendMessageResult::Success(created_at)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, new_topic), name = "Player::change_topic")]
|
||||
async fn change_topic(&mut self, connection_id: ConnectionId, room_id: RoomId, new_topic: Str) {
|
||||
let room = self.rooms.get_room(&room_id).await;
|
||||
if let Some(mut room) = room {
|
||||
room.set_topic(self.player_id.clone(), new_topic.clone()).await;
|
||||
} else {
|
||||
let Some(room) = self.my_rooms.get(&room_id) else {
|
||||
tracing::info!("no room found");
|
||||
}
|
||||
return;
|
||||
};
|
||||
room.set_topic(&self.player_id, new_topic.clone()).await;
|
||||
let update = Updates::RoomTopicChanged { room_id, new_topic };
|
||||
self.broadcast_update(update, connection_id).await;
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "Player::get_rooms")]
|
||||
async fn get_rooms(&self) -> Vec<RoomInfo> {
|
||||
let mut response = vec![];
|
||||
for (_, handle) in &self.my_rooms {
|
||||
|
@ -429,16 +583,41 @@ impl Player {
|
|||
response
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, body), name = "Player::send_dialog_message")]
|
||||
async fn send_dialog_message(&self, connection_id: ConnectionId, recipient: PlayerId, body: Str) {
|
||||
let created_at = chrono::Utc::now();
|
||||
self.dialogs.send_message(self.player_id.clone(), recipient.clone(), body.clone(), &created_at).await.unwrap();
|
||||
let update = Updates::NewDialogMessage {
|
||||
sender: self.player_id.clone(),
|
||||
receiver: recipient.clone(),
|
||||
body,
|
||||
created_at,
|
||||
};
|
||||
self.broadcast_update(update, connection_id).await;
|
||||
}
|
||||
|
||||
/// Broadcasts an update to all connections except the one with the given id.
|
||||
///
|
||||
/// This is called after handling a client command.
|
||||
/// Sending the update to the connection which sent the command is handled by the connection itself.
|
||||
#[tracing::instrument(skip(self, update), name = "Player::broadcast_update")]
|
||||
async fn broadcast_update(&self, update: Updates, except: ConnectionId) {
|
||||
for (a, b) in &self.connections {
|
||||
if ConnectionId(a) == except {
|
||||
continue;
|
||||
}
|
||||
let _ = b.send(update.clone()).await;
|
||||
let _ = b.send(ConnectionMessage::Update(update.clone())).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ConnectionMessage {
|
||||
Update(Updates),
|
||||
Stop(StopReason),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum StopReason {
|
||||
ServerShutdown,
|
||||
InternalError,
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
use anyhow::Result;
|
||||
|
||||
use crate::repo::Storage;
|
||||
|
||||
impl Storage {
|
||||
#[tracing::instrument(skip(self), name = "Storage::set_argon2_challenge")]
|
||||
pub async fn set_argon2_challenge(&self, user_id: u32, hash: &str) -> Result<()> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
sqlx::query(
|
||||
"insert into challenges_argon2_password(user_id, hash)
|
||||
values (?, ?)
|
||||
on conflict(user_id) do update set hash = excluded.hash;",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(hash)
|
||||
.execute(&mut *executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use chrono::{DateTime, Utc};
|
||||
use sqlx::FromRow;
|
||||
|
||||
use crate::repo::Storage;
|
||||
|
||||
impl Storage {
|
||||
#[tracing::instrument(skip(self), name = "Storage::retrieve_dialog")]
|
||||
pub async fn retrieve_dialog(&self, participant_1: &str, participant_2: &str) -> Result<Option<StoredDialog>> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res = sqlx::query_as(
|
||||
"select r.id, r.participant_1, r.participant_2, r.message_count
|
||||
from dialogs r join users u1 on r.participant_1 = u1.id join users u2 on r.participant_2 = u2.id
|
||||
where u1.name = ? and u2.name = ?;",
|
||||
)
|
||||
.bind(participant_1)
|
||||
.bind(participant_2)
|
||||
.fetch_optional(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, content, created_at), name = "Storage::insert_dialog_message")]
|
||||
pub async fn insert_dialog_message(
|
||||
&self,
|
||||
dialog_id: u32,
|
||||
id: u32,
|
||||
author_id: &str,
|
||||
content: &str,
|
||||
created_at: &DateTime<Utc>,
|
||||
) -> Result<()> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res: Option<(u32,)> = sqlx::query_as("select id from users where name = ?;")
|
||||
.bind(author_id)
|
||||
.fetch_optional(&mut *executor)
|
||||
.await?;
|
||||
let Some((author_id,)) = res else {
|
||||
return Err(anyhow!("No such user"));
|
||||
};
|
||||
sqlx::query(
|
||||
"insert into dialog_messages(dialog_id, id, author_id, content, created_at)
|
||||
values (?, ?, ?, ?, ?);
|
||||
update dialogs set message_count = message_count + 1 where id = ?;",
|
||||
)
|
||||
.bind(dialog_id)
|
||||
.bind(id)
|
||||
.bind(author_id)
|
||||
.bind(content)
|
||||
.bind(created_at)
|
||||
.bind(dialog_id)
|
||||
.execute(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, created_at), name = "Storage::initialize_dialog")]
|
||||
pub async fn initialize_dialog(
|
||||
&self,
|
||||
participant_1: &str,
|
||||
participant_2: &str,
|
||||
created_at: &DateTime<Utc>,
|
||||
) -> Result<StoredDialog> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res: StoredDialog = sqlx::query_as(
|
||||
"insert into dialogs(participant_1, participant_2, created_at)
|
||||
values (
|
||||
(select id from users where name = ?),
|
||||
(select id from users where name = ?),
|
||||
?
|
||||
)
|
||||
returning id, participant_1, participant_2, message_count;",
|
||||
)
|
||||
.bind(participant_1)
|
||||
.bind(participant_2)
|
||||
.bind(&created_at)
|
||||
.fetch_one(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromRow)]
|
||||
pub struct StoredDialog {
|
||||
pub id: u32,
|
||||
pub participant_1: u32,
|
||||
pub participant_2: u32,
|
||||
pub message_count: u32,
|
||||
}
|
|
@ -4,6 +4,7 @@ use std::str::FromStr;
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
use sqlx::sqlite::SqliteConnectOptions;
|
||||
use sqlx::{ConnectOptions, Connection, FromRow, Sqlite, SqliteConnection, Transaction};
|
||||
|
@ -11,6 +12,11 @@ use tokio::sync::Mutex;
|
|||
|
||||
use crate::prelude::*;
|
||||
|
||||
mod auth;
|
||||
mod dialog;
|
||||
mod room;
|
||||
mod user;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct StorageConfig {
|
||||
pub db_path: String,
|
||||
|
@ -34,11 +40,13 @@ impl Storage {
|
|||
Ok(Storage { conn })
|
||||
}
|
||||
|
||||
pub async fn retrieve_user_by_name(&mut self, name: &str) -> Result<Option<StoredUser>> {
|
||||
#[tracing::instrument(skip(self), name = "Storage::retrieve_user_by_name")]
|
||||
pub async fn retrieve_user_by_name(&self, name: &str) -> Result<Option<StoredUser>> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res = sqlx::query_as(
|
||||
"select u.id, u.name, c.password
|
||||
"select u.id, u.name, c.password, a.hash as argon2_hash
|
||||
from users u left join challenges_plain_password c on u.id = c.user_id
|
||||
left join challenges_argon2_password a on u.id = a.user_id
|
||||
where u.name = ?;",
|
||||
)
|
||||
.bind(name)
|
||||
|
@ -48,7 +56,8 @@ impl Storage {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
pub async fn retrieve_room_by_name(&mut self, name: &str) -> Result<Option<StoredRoom>> {
|
||||
#[tracing::instrument(skip(self), name = "Storage::retrieve_room_by_name")]
|
||||
pub async fn retrieve_room_by_name(&self, name: &str) -> Result<Option<StoredRoom>> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res = sqlx::query_as(
|
||||
"select id, name, topic, message_count
|
||||
|
@ -62,6 +71,7 @@ impl Storage {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, topic), name = "Storage::create_new_room")]
|
||||
pub async fn create_new_room(&mut self, name: &str, topic: &str) -> Result<u32> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let (id,): (u32,) = sqlx::query_as(
|
||||
|
@ -77,7 +87,15 @@ impl Storage {
|
|||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn insert_message(&mut self, room_id: u32, id: u32, content: &str, author_id: &str) -> Result<()> {
|
||||
#[tracing::instrument(skip(self, content, created_at), name = "Storage::insert_message")]
|
||||
pub async fn insert_message(
|
||||
&mut self,
|
||||
room_id: u32,
|
||||
id: u32,
|
||||
content: &str,
|
||||
author_id: &str,
|
||||
created_at: &DateTime<Utc>,
|
||||
) -> Result<()> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res: Option<(u32,)> = sqlx::query_as("select id from users where name = ?;")
|
||||
.bind(author_id)
|
||||
|
@ -87,14 +105,15 @@ impl Storage {
|
|||
return Err(anyhow!("No such user"));
|
||||
};
|
||||
sqlx::query(
|
||||
"insert into messages(room_id, id, content, author_id)
|
||||
values (?, ?, ?, ?);
|
||||
"insert into messages(room_id, id, content, author_id, created_at)
|
||||
values (?, ?, ?, ?, ?);
|
||||
update rooms set message_count = message_count + 1 where id = ?;",
|
||||
)
|
||||
.bind(room_id)
|
||||
.bind(id)
|
||||
.bind(content)
|
||||
.bind(author_id)
|
||||
.bind(created_at.to_string())
|
||||
.bind(room_id)
|
||||
.execute(&mut *executor)
|
||||
.await?;
|
||||
|
@ -112,6 +131,7 @@ impl Storage {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "Storage::create_user")]
|
||||
pub async fn create_user(&mut self, name: &str) -> Result<()> {
|
||||
let query = sqlx::query(
|
||||
"insert into users(name)
|
||||
|
@ -124,7 +144,8 @@ impl Storage {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_password<'a>(&'a mut self, name: &'a str, pwd: &'a str) -> Result<Option<()>> {
|
||||
#[tracing::instrument(skip(self, pwd), name = "Storage::set_password")]
|
||||
pub async fn set_password<'a>(&'a self, name: &'a str, pwd: &'a str) -> Result<Option<()>> {
|
||||
async fn inner(txn: &mut Transaction<'_, Sqlite>, name: &str, pwd: &str) -> Result<Option<()>> {
|
||||
let id: Option<(u32,)> = sqlx::query_as("select * from users where name = ? limit 1;")
|
||||
.bind(name)
|
||||
|
@ -162,6 +183,7 @@ pub struct StoredUser {
|
|||
pub id: u32,
|
||||
pub name: String,
|
||||
pub password: Option<String>,
|
||||
pub argon2_hash: Option<Box<str>>,
|
||||
}
|
||||
|
||||
#[derive(FromRow)]
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
use anyhow::Result;
|
||||
|
||||
use crate::repo::Storage;
|
||||
|
||||
impl Storage {
|
||||
#[tracing::instrument(skip(self), name = "Storage::add_room_member")]
|
||||
pub async fn add_room_member(&self, room_id: u32, player_id: u32) -> Result<()> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
sqlx::query(
|
||||
"insert into memberships(user_id, room_id, status)
|
||||
values (?, ?, 1);",
|
||||
)
|
||||
.bind(player_id)
|
||||
.bind(room_id)
|
||||
.execute(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "Storage::remove_room_member")]
|
||||
pub async fn remove_room_member(&self, room_id: u32, player_id: u32) -> Result<()> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
sqlx::query(
|
||||
"delete from memberships
|
||||
where user_id = ? and room_id = ?;",
|
||||
)
|
||||
.bind(player_id)
|
||||
.bind(room_id)
|
||||
.execute(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, topic), name = "Storage::set_room_topic")]
|
||||
pub async fn set_room_topic(&mut self, id: u32, topic: &str) -> Result<()> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
sqlx::query(
|
||||
"update rooms
|
||||
set topic = ?
|
||||
where id = ?;",
|
||||
)
|
||||
.bind(topic)
|
||||
.bind(id)
|
||||
.fetch_optional(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
use anyhow::Result;
|
||||
|
||||
use crate::repo::Storage;
|
||||
use crate::room::RoomId;
|
||||
|
||||
impl Storage {
|
||||
pub async fn retrieve_user_id_by_name(&self, name: &str) -> Result<Option<u32>> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res: Option<(u32,)> = sqlx::query_as("select u.id from users u where u.name = ?;")
|
||||
.bind(name)
|
||||
.fetch_optional(&mut *executor)
|
||||
.await?;
|
||||
|
||||
Ok(res.map(|(id,)| id))
|
||||
}
|
||||
|
||||
pub async fn get_rooms_of_a_user(&self, user_id: u32) -> Result<Vec<RoomId>> {
|
||||
let mut executor = self.conn.lock().await;
|
||||
let res: Vec<(String,)> = sqlx::query_as(
|
||||
"select r.name
|
||||
from memberships m inner join rooms r on m.room_id = r.id
|
||||
where m.user_id = ?;",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_all(&mut *executor)
|
||||
.await?;
|
||||
|
||||
res.into_iter().map(|(room_id,)| RoomId::from(room_id)).collect()
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
//! Domain of rooms — chats with multiple participants.
|
||||
use std::collections::HashSet;
|
||||
use std::{collections::HashMap, hash::Hash, sync::Arc};
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use prometheus::{IntGauge, Registry as MetricRegistry};
|
||||
use serde::Serialize;
|
||||
use tokio::sync::RwLock as AsyncRwLock;
|
||||
|
@ -46,29 +48,23 @@ impl RoomRegistry {
|
|||
Ok(RoomRegistry(Arc::new(AsyncRwLock::new(inner))))
|
||||
}
|
||||
|
||||
pub fn shutdown(self) -> Result<()> {
|
||||
let res = match Arc::try_unwrap(self.0) {
|
||||
Ok(e) => e,
|
||||
Err(_) => return Err(fail("failed to acquire rooms ownership on shutdown")),
|
||||
};
|
||||
let res = res.into_inner();
|
||||
// TODO drop all rooms
|
||||
drop(res);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomRegistry::get_or_create_room")]
|
||||
pub async fn get_or_create_room(&mut self, room_id: RoomId) -> Result<RoomHandle> {
|
||||
let mut inner = self.0.write().await;
|
||||
if let Some(room_handle) = inner.rooms.get(&room_id) {
|
||||
// room was already loaded into memory
|
||||
log::debug!("Room {} was loaded already", &room_id.0);
|
||||
if let Some(room_handle) = inner.get_or_load_room(&room_id).await? {
|
||||
Ok(room_handle.clone())
|
||||
} else if let Some(stored_room) = inner.storage.retrieve_room_by_name(&*room_id.0).await? {
|
||||
// room exists, but was not loaded
|
||||
log::debug!("Loading room {}...", &room_id.0);
|
||||
let room = Room {
|
||||
storage_id: stored_room.id,
|
||||
room_id: room_id.clone(),
|
||||
subscriptions: HashMap::new(), // TODO figure out how to populate subscriptions
|
||||
topic: stored_room.topic.into(),
|
||||
message_count: stored_room.message_count,
|
||||
storage: inner.storage.clone(),
|
||||
};
|
||||
let room_handle = RoomHandle(Arc::new(AsyncRwLock::new(room)));
|
||||
inner.rooms.insert(room_id, room_handle.clone());
|
||||
inner.metric_active_rooms.inc();
|
||||
Ok(room_handle)
|
||||
} else {
|
||||
// room does not exist, create it and load
|
||||
log::debug!("Creating room {}...", &room_id.0);
|
||||
let topic = "New room";
|
||||
let id = inner.storage.create_new_room(&*room_id.0, &*topic).await?;
|
||||
|
@ -76,6 +72,7 @@ impl RoomRegistry {
|
|||
storage_id: id,
|
||||
room_id: room_id.clone(),
|
||||
subscriptions: HashMap::new(),
|
||||
members: HashSet::new(),
|
||||
topic: topic.into(),
|
||||
message_count: 0,
|
||||
storage: inner.storage.clone(),
|
||||
|
@ -87,12 +84,13 @@ impl RoomRegistry {
|
|||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomRegistry::get_room")]
|
||||
pub async fn get_room(&self, room_id: &RoomId) -> Option<RoomHandle> {
|
||||
let inner = self.0.read().await;
|
||||
let res = inner.rooms.get(room_id);
|
||||
res.map(|r| r.clone())
|
||||
let mut inner = self.0.write().await;
|
||||
inner.get_or_load_room(room_id).await.unwrap()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomRegistry::get_all_rooms")]
|
||||
pub async fn get_all_rooms(&self) -> Vec<RoomInfo> {
|
||||
let handles = {
|
||||
let inner = self.0.read().await;
|
||||
|
@ -113,17 +111,71 @@ struct RoomRegistryInner {
|
|||
storage: Storage,
|
||||
}
|
||||
|
||||
impl RoomRegistryInner {
|
||||
#[tracing::instrument(skip(self), name = "RoomRegistryInner::get_or_load_room")]
|
||||
async fn get_or_load_room(&mut self, room_id: &RoomId) -> Result<Option<RoomHandle>> {
|
||||
if let Some(room_handle) = self.rooms.get(room_id) {
|
||||
log::debug!("Room {} was loaded already", &room_id.0);
|
||||
Ok(Some(room_handle.clone()))
|
||||
} else if let Some(stored_room) = self.storage.retrieve_room_by_name(&*room_id.0).await? {
|
||||
log::debug!("Loading room {}...", &room_id.0);
|
||||
let room = Room {
|
||||
storage_id: stored_room.id,
|
||||
room_id: room_id.clone(),
|
||||
subscriptions: HashMap::new(),
|
||||
members: HashSet::new(), // TODO load members from storage
|
||||
topic: stored_room.topic.into(),
|
||||
message_count: stored_room.message_count,
|
||||
storage: self.storage.clone(),
|
||||
};
|
||||
let room_handle = RoomHandle(Arc::new(AsyncRwLock::new(room)));
|
||||
self.rooms.insert(room_id.clone(), room_handle.clone());
|
||||
self.metric_active_rooms.inc();
|
||||
Ok(Some(room_handle))
|
||||
} else {
|
||||
tracing::debug!("Room {} does not exist", &room_id.0);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RoomHandle(Arc<AsyncRwLock<Room>>);
|
||||
impl RoomHandle {
|
||||
pub async fn subscribe(&self, player_id: PlayerId, player_handle: PlayerHandle) {
|
||||
#[tracing::instrument(skip(self, player_handle), name = "RoomHandle::subscribe")]
|
||||
pub async fn subscribe(&self, player_id: &PlayerId, player_handle: PlayerHandle) {
|
||||
let mut lock = self.0.write().await;
|
||||
lock.add_subscriber(player_id, player_handle).await;
|
||||
tracing::info!("Adding a subscriber to a room");
|
||||
lock.subscriptions.insert(player_id.clone(), player_handle);
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomHandle::add_member")]
|
||||
pub async fn add_member(&self, player_id: &PlayerId, player_storage_id: u32) {
|
||||
let mut lock = self.0.write().await;
|
||||
tracing::info!("Adding a new member to a room");
|
||||
let room_storage_id = lock.storage_id;
|
||||
lock.storage.add_room_member(room_storage_id, player_storage_id).await.unwrap();
|
||||
lock.members.insert(player_id.clone());
|
||||
let update = Updates::RoomJoined {
|
||||
room_id: lock.room_id.clone(),
|
||||
new_member_id: player_id.clone(),
|
||||
};
|
||||
lock.broadcast_update(update, player_id).await;
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomHandle::unsubscribe")]
|
||||
pub async fn unsubscribe(&self, player_id: &PlayerId) {
|
||||
let mut lock = self.0.write().await;
|
||||
lock.subscriptions.remove(player_id);
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomHandle::remove_member")]
|
||||
pub async fn remove_member(&self, player_id: &PlayerId, player_storage_id: u32) {
|
||||
let mut lock = self.0.write().await;
|
||||
tracing::info!("Removing a member from a room");
|
||||
let room_storage_id = lock.storage_id;
|
||||
lock.storage.remove_room_member(room_storage_id, player_storage_id).await.unwrap();
|
||||
lock.members.remove(player_id);
|
||||
let update = Updates::RoomLeft {
|
||||
room_id: lock.room_id.clone(),
|
||||
former_member_id: player_id.clone(),
|
||||
|
@ -131,14 +183,16 @@ impl RoomHandle {
|
|||
lock.broadcast_update(update, player_id).await;
|
||||
}
|
||||
|
||||
pub async fn send_message(&self, player_id: PlayerId, body: Str) {
|
||||
#[tracing::instrument(skip(self, body, created_at), name = "RoomHandle::send_message")]
|
||||
pub async fn send_message(&self, player_id: &PlayerId, body: Str, created_at: DateTime<Utc>) {
|
||||
let mut lock = self.0.write().await;
|
||||
let res = lock.send_message(player_id, body).await;
|
||||
let res = lock.send_message(player_id, body, created_at).await;
|
||||
if let Err(err) = res {
|
||||
log::warn!("Failed to send message: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), name = "RoomHandle::get_room_info")]
|
||||
pub async fn get_room_info(&self) -> RoomInfo {
|
||||
let lock = self.0.read().await;
|
||||
RoomInfo {
|
||||
|
@ -148,14 +202,17 @@ impl RoomHandle {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn set_topic(&mut self, changer_id: PlayerId, new_topic: Str) {
|
||||
#[tracing::instrument(skip(self, new_topic), name = "RoomHandle::set_topic")]
|
||||
pub async fn set_topic(&self, changer_id: &PlayerId, new_topic: Str) {
|
||||
let mut lock = self.0.write().await;
|
||||
let storage_id = lock.storage_id;
|
||||
lock.topic = new_topic.clone();
|
||||
lock.storage.set_room_topic(storage_id, &new_topic).await.unwrap();
|
||||
let update = Updates::RoomTopicChanged {
|
||||
room_id: lock.room_id.clone(),
|
||||
new_topic: new_topic.clone(),
|
||||
};
|
||||
lock.broadcast_update(update, &changer_id).await;
|
||||
lock.broadcast_update(update, changer_id).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,32 +223,34 @@ struct Room {
|
|||
room_id: RoomId,
|
||||
/// Player actors on the local node which are subscribed to this room's updates.
|
||||
subscriptions: HashMap<PlayerId, PlayerHandle>,
|
||||
/// Members of the room.
|
||||
members: HashSet<PlayerId>,
|
||||
/// The total number of messages. Used to calculate the id of the new message.
|
||||
message_count: u32,
|
||||
topic: Str,
|
||||
storage: Storage,
|
||||
}
|
||||
impl Room {
|
||||
async fn add_subscriber(&mut self, player_id: PlayerId, player_handle: PlayerHandle) {
|
||||
tracing::info!("Adding a subscriber to room");
|
||||
self.subscriptions.insert(player_id.clone(), player_handle);
|
||||
let update = Updates::RoomJoined {
|
||||
room_id: self.room_id.clone(),
|
||||
new_member_id: player_id.clone(),
|
||||
};
|
||||
self.broadcast_update(update, &player_id).await;
|
||||
}
|
||||
|
||||
async fn send_message(&mut self, author_id: PlayerId, body: Str) -> Result<()> {
|
||||
#[tracing::instrument(skip(self, body, created_at), name = "Room::send_message")]
|
||||
async fn send_message(&mut self, author_id: &PlayerId, body: Str, created_at: DateTime<Utc>) -> Result<()> {
|
||||
tracing::info!("Adding a message to room");
|
||||
self.storage.insert_message(self.storage_id, self.message_count, &body, &*author_id.as_inner()).await?;
|
||||
self.storage
|
||||
.insert_message(
|
||||
self.storage_id,
|
||||
self.message_count,
|
||||
&body,
|
||||
&*author_id.as_inner(),
|
||||
&created_at,
|
||||
)
|
||||
.await?;
|
||||
self.message_count += 1;
|
||||
let update = Updates::NewMessage {
|
||||
room_id: self.room_id.clone(),
|
||||
author_id: author_id.clone(),
|
||||
body,
|
||||
created_at,
|
||||
};
|
||||
self.broadcast_update(update, &author_id).await;
|
||||
self.broadcast_update(update, author_id).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -199,6 +258,7 @@ impl Room {
|
|||
///
|
||||
/// This is called after handling a client command.
|
||||
/// Sending the update to the player who sent the command is handled by the player actor.
|
||||
#[tracing::instrument(skip(self, update), name = "Room::broadcast_update")]
|
||||
async fn broadcast_update(&self, update: Updates, except: &PlayerId) {
|
||||
tracing::debug!("Broadcasting an update to {} subs", self.subscriptions.len());
|
||||
for (player_id, sub) in &self.subscriptions {
|
||||
|
|
|
@ -11,6 +11,11 @@ pub struct CreatePlayerRequest<'a> {
|
|||
pub name: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StopPlayerRequest<'a> {
|
||||
pub name: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ChangePasswordRequest<'a> {
|
||||
pub player_name: &'a str,
|
||||
|
@ -19,6 +24,7 @@ pub struct ChangePasswordRequest<'a> {
|
|||
|
||||
pub mod paths {
|
||||
pub const CREATE_PLAYER: &'static str = "/mgmt/create_player";
|
||||
pub const STOP_PLAYER: &'static str = "/mgmt/stop_player";
|
||||
pub const SET_PASSWORD: &'static str = "/mgmt/set_password";
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ tokio.workspace = true
|
|||
prometheus.workspace = true
|
||||
futures-util.workspace = true
|
||||
nonempty.workspace = true
|
||||
chrono.workspace = true
|
||||
bitflags = "2.4.1"
|
||||
proto-irc = { path = "../proto-irc" }
|
||||
sasl = { path = "../sasl" }
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use bitflags::bitflags;
|
||||
|
||||
bitflags! {
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Capabilities: u32 {
|
||||
const None = 0;
|
||||
const Sasl = 1 << 0;
|
||||
const ServerTime = 1 << 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use chrono::SecondsFormat;
|
||||
use futures_util::future::join_all;
|
||||
use nonempty::nonempty;
|
||||
use nonempty::NonEmpty;
|
||||
|
@ -13,17 +14,19 @@ use tokio::net::tcp::{ReadHalf, WriteHalf};
|
|||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::mpsc::channel;
|
||||
|
||||
use lavina_core::auth::{Authenticator, Verdict};
|
||||
use lavina_core::player::*;
|
||||
use lavina_core::prelude::*;
|
||||
use lavina_core::repo::Storage;
|
||||
use lavina_core::room::{RoomId, RoomInfo, RoomRegistry};
|
||||
use lavina_core::terminator::Terminator;
|
||||
use lavina_core::LavinaCore;
|
||||
use proto_irc::client::CapabilitySubcommand;
|
||||
use proto_irc::client::{client_message, ClientMessage};
|
||||
use proto_irc::server::CapSubBody;
|
||||
use proto_irc::server::{AwayStatus, ServerMessage, ServerMessageBody};
|
||||
use proto_irc::user::PrefixedNick;
|
||||
use proto_irc::{Chan, Recipient};
|
||||
use proto_irc::{Chan, Recipient, Tag};
|
||||
use sasl::AuthBody;
|
||||
mod cap;
|
||||
mod commands;
|
||||
|
@ -50,14 +53,14 @@ struct RegisteredUser {
|
|||
*/
|
||||
username: Str,
|
||||
realname: Str,
|
||||
enabled_capabilities: Capabilities,
|
||||
}
|
||||
|
||||
async fn handle_socket(
|
||||
config: ServerConfig,
|
||||
mut stream: TcpStream,
|
||||
socket_addr: &SocketAddr,
|
||||
players: PlayerRegistry,
|
||||
rooms: RoomRegistry,
|
||||
mut core: LavinaCore,
|
||||
termination: Deferred<()>, // TODO use it to stop the connection gracefully
|
||||
mut storage: Storage,
|
||||
) -> Result<()> {
|
||||
|
@ -77,7 +80,7 @@ async fn handle_socket(
|
|||
match registered_user {
|
||||
Ok(user) => {
|
||||
log::debug!("User registered");
|
||||
handle_registered_socket(config, players, rooms, &mut reader, &mut writer, user, &mut storage).await?;
|
||||
handle_registered_socket(config, core.players, core.rooms, &mut reader, &mut writer, user).await?;
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("Registration failed: {err}");
|
||||
|
@ -88,6 +91,266 @@ async fn handle_socket(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
struct RegistrationState {
|
||||
/// The last received `NICK` message.
|
||||
future_nickname: Option<Str>,
|
||||
/// The last received `USER` message.
|
||||
future_username: Option<(Str, Str)>,
|
||||
enabled_capabilities: Capabilities,
|
||||
/// `CAP LS` or `CAP REQ` was received, but not `CAP END`.
|
||||
cap_negotiation_in_progress: bool,
|
||||
/// The last received `PASS` message.
|
||||
pass: Option<Str>,
|
||||
authentication_started: bool,
|
||||
validated_user: Option<Str>,
|
||||
}
|
||||
|
||||
impl RegistrationState {
|
||||
fn new() -> RegistrationState {
|
||||
RegistrationState {
|
||||
future_nickname: None,
|
||||
future_username: None,
|
||||
enabled_capabilities: Capabilities::None,
|
||||
cap_negotiation_in_progress: false,
|
||||
pass: None,
|
||||
authentication_started: false,
|
||||
validated_user: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle an incoming message from the client during the registration process.
|
||||
///
|
||||
/// Returns `Some` if the user is fully registered, `None` if the registration is still in progress.
|
||||
async fn handle_msg(
|
||||
&mut self,
|
||||
msg: ClientMessage,
|
||||
writer: &mut BufWriter<WriteHalf<'_>>,
|
||||
storage: &mut Storage,
|
||||
config: &ServerConfig,
|
||||
) -> Result<Option<RegisteredUser>> {
|
||||
match msg {
|
||||
ClientMessage::Pass { password } => {
|
||||
self.pass = Some(password);
|
||||
Ok(None)
|
||||
}
|
||||
ClientMessage::Capability { subcommand } => match subcommand {
|
||||
CapabilitySubcommand::List { code: _ } => {
|
||||
self.cap_negotiation_in_progress = true;
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::Cap {
|
||||
target: self.future_nickname.clone().unwrap_or_else(|| "*".into()),
|
||||
subcmd: CapSubBody::Ls("sasl=PLAIN server-time".into()),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
Ok(None)
|
||||
}
|
||||
CapabilitySubcommand::Req(caps) => {
|
||||
self.cap_negotiation_in_progress = true;
|
||||
let mut acked = vec![];
|
||||
let mut naked = vec![];
|
||||
for cap in caps {
|
||||
if &*cap.name == "sasl" {
|
||||
if cap.to_disable {
|
||||
self.enabled_capabilities &= !Capabilities::Sasl;
|
||||
} else {
|
||||
self.enabled_capabilities |= Capabilities::Sasl;
|
||||
}
|
||||
acked.push(cap);
|
||||
} else if &*cap.name == "server-time" {
|
||||
if cap.to_disable {
|
||||
self.enabled_capabilities &= !Capabilities::ServerTime;
|
||||
} else {
|
||||
self.enabled_capabilities |= Capabilities::ServerTime;
|
||||
}
|
||||
acked.push(cap);
|
||||
} else {
|
||||
naked.push(cap);
|
||||
}
|
||||
}
|
||||
let mut ack_body = String::new();
|
||||
if let Some((first, tail)) = acked.split_first() {
|
||||
if first.to_disable {
|
||||
ack_body.push('-');
|
||||
}
|
||||
ack_body += &*first.name;
|
||||
for cap in tail {
|
||||
ack_body.push(' ');
|
||||
if cap.to_disable {
|
||||
ack_body.push('-');
|
||||
}
|
||||
ack_body += &*cap.name;
|
||||
}
|
||||
}
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::Cap {
|
||||
target: self.future_nickname.clone().unwrap_or_else(|| "*".into()),
|
||||
subcmd: CapSubBody::Ack(ack_body.into()),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
Ok(None)
|
||||
}
|
||||
CapabilitySubcommand::End => {
|
||||
let Some((ref username, ref realname)) = self.future_username else {
|
||||
self.cap_negotiation_in_progress = false;
|
||||
return Ok(None);
|
||||
};
|
||||
let Some(nickname) = self.future_nickname.clone() else {
|
||||
self.cap_negotiation_in_progress = false;
|
||||
return Ok(None);
|
||||
};
|
||||
let username = username.clone();
|
||||
let realname = realname.clone();
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
username,
|
||||
realname,
|
||||
enabled_capabilities: self.enabled_capabilities,
|
||||
};
|
||||
self.finalize_auth(candidate_user, writer, storage, config).await
|
||||
}
|
||||
},
|
||||
ClientMessage::Nick { nickname } => {
|
||||
if self.cap_negotiation_in_progress {
|
||||
self.future_nickname = Some(nickname);
|
||||
Ok(None)
|
||||
} else if let Some((username, realname)) = &self.future_username.clone() {
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
username: username.clone(),
|
||||
realname: realname.clone(),
|
||||
enabled_capabilities: self.enabled_capabilities,
|
||||
};
|
||||
self.finalize_auth(candidate_user, writer, storage, config).await
|
||||
} else {
|
||||
self.future_nickname = Some(nickname);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
ClientMessage::User { username, realname } => {
|
||||
if self.cap_negotiation_in_progress {
|
||||
self.future_username = Some((username, realname));
|
||||
Ok(None)
|
||||
} else if let Some(nickname) = self.future_nickname.clone() {
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
username,
|
||||
realname,
|
||||
enabled_capabilities: self.enabled_capabilities,
|
||||
};
|
||||
self.finalize_auth(candidate_user, writer, storage, config).await
|
||||
} else {
|
||||
self.future_username = Some((username, realname));
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
ClientMessage::Authenticate(body) => {
|
||||
if !self.authentication_started {
|
||||
tracing::debug!("Received authentication request");
|
||||
if &*body == "PLAIN" {
|
||||
tracing::debug!("Authentication request with method PLAIN");
|
||||
self.authentication_started = true;
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::Authenticate("+".into()),
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
Ok(None)
|
||||
} else {
|
||||
let target = self.future_nickname.clone().unwrap_or_else(|| "*".into());
|
||||
sasl_fail_message(config.server_name.clone(), target, "Unsupported mechanism".into())
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
let body = AuthBody::from_str(body.as_bytes())?;
|
||||
if let Err(e) = auth_user(storage, &body.login, &body.password).await {
|
||||
tracing::warn!("Authentication failed: {:?}", e);
|
||||
let target = self.future_nickname.clone().unwrap_or_else(|| "*".into());
|
||||
sasl_fail_message(config.server_name.clone(), target, "Bad credentials".into())
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
Ok(None)
|
||||
} else {
|
||||
let login: Str = body.login.into();
|
||||
self.validated_user = Some(login.clone());
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::N900LoggedIn {
|
||||
nick: login.clone(),
|
||||
address: login.clone(),
|
||||
account: login.clone(),
|
||||
message: format!("You are now logged in as {}", login).into(),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::N903SaslSuccess {
|
||||
nick: login.clone(),
|
||||
message: "SASL authentication successful".into(),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO handle abortion of authentication
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
async fn finalize_auth(
|
||||
&mut self,
|
||||
candidate_user: RegisteredUser,
|
||||
writer: &mut BufWriter<WriteHalf<'_>>,
|
||||
storage: &mut Storage,
|
||||
config: &ServerConfig,
|
||||
) -> Result<Option<RegisteredUser>> {
|
||||
if self.enabled_capabilities.contains(Capabilities::Sasl)
|
||||
&& self.validated_user.as_ref() == Some(&candidate_user.nickname)
|
||||
{
|
||||
Ok(Some(candidate_user))
|
||||
} else {
|
||||
let Some(candidate_password) = &self.pass else {
|
||||
sasl_fail_message(
|
||||
config.server_name.clone(),
|
||||
candidate_user.nickname.clone(),
|
||||
"User credentials was not provided".into(),
|
||||
)
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
return Ok(None);
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
Ok(Some(candidate_user))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_registration<'a>(
|
||||
reader: &mut BufReader<ReadHalf<'a>>,
|
||||
writer: &mut BufWriter<WriteHalf<'a>>,
|
||||
|
@ -96,14 +359,7 @@ async fn handle_registration<'a>(
|
|||
) -> Result<RegisteredUser> {
|
||||
let mut buffer = vec![];
|
||||
|
||||
let mut future_nickname: Option<Str> = None;
|
||||
let mut future_username: Option<(Str, Str)> = None;
|
||||
let mut enabled_capabilities = Capabilities::None;
|
||||
let mut cap_negotiation_in_progress = false; // if true, expect `CAP END` to complete registration
|
||||
|
||||
let mut pass: Option<Str> = None;
|
||||
let mut authentication_started = false;
|
||||
let mut validated_user = None;
|
||||
let mut state = RegistrationState::new();
|
||||
|
||||
let user = loop {
|
||||
let res = read_irc_message(reader, &mut buffer).await;
|
||||
|
@ -134,218 +390,8 @@ async fn handle_registration<'a>(
|
|||
}
|
||||
};
|
||||
tracing::debug!("Incoming IRC message: {msg:?}");
|
||||
match msg {
|
||||
ClientMessage::Pass { password } => {
|
||||
pass = Some(password);
|
||||
}
|
||||
ClientMessage::Capability { subcommand } => match subcommand {
|
||||
CapabilitySubcommand::List { code: _ } => {
|
||||
cap_negotiation_in_progress = true;
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::Cap {
|
||||
target: future_nickname.clone().unwrap_or_else(|| "*".into()),
|
||||
subcmd: CapSubBody::Ls("sasl=PLAIN".into()),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
}
|
||||
CapabilitySubcommand::Req(caps) => {
|
||||
cap_negotiation_in_progress = true;
|
||||
let mut acked = vec![];
|
||||
let mut naked = vec![];
|
||||
for cap in caps {
|
||||
if &*cap.name == "sasl" {
|
||||
if cap.to_disable {
|
||||
enabled_capabilities &= !Capabilities::Sasl;
|
||||
} else {
|
||||
enabled_capabilities |= Capabilities::Sasl;
|
||||
}
|
||||
acked.push(cap);
|
||||
} else {
|
||||
naked.push(cap);
|
||||
}
|
||||
}
|
||||
let mut ack_body = String::new();
|
||||
for cap in acked {
|
||||
if cap.to_disable {
|
||||
ack_body.push('-');
|
||||
}
|
||||
ack_body += &*cap.name;
|
||||
}
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::Cap {
|
||||
target: future_nickname.clone().unwrap_or_else(|| "*".into()),
|
||||
subcmd: CapSubBody::Ack(ack_body.into()),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
}
|
||||
CapabilitySubcommand::End => {
|
||||
let Some((ref username, ref realname)) = future_username else {
|
||||
todo!();
|
||||
};
|
||||
let Some(nickname) = future_nickname.clone() else {
|
||||
todo!();
|
||||
};
|
||||
let username = username.clone();
|
||||
let realname = realname.clone();
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
username,
|
||||
realname,
|
||||
};
|
||||
if enabled_capabilities.contains(Capabilities::Sasl)
|
||||
&& validated_user.as_ref() == Some(&candidate_user.nickname)
|
||||
{
|
||||
break Ok(candidate_user);
|
||||
} else {
|
||||
let Some(candidate_password) = pass else {
|
||||
sasl_fail_message(
|
||||
config.server_name.clone(),
|
||||
nickname.clone(),
|
||||
"User credentials was not provided".into(),
|
||||
)
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
continue;
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
break Ok(candidate_user);
|
||||
}
|
||||
}
|
||||
},
|
||||
ClientMessage::Nick { nickname } => {
|
||||
if cap_negotiation_in_progress {
|
||||
future_nickname = Some(nickname);
|
||||
} else if let Some((username, realname)) = future_username.clone() {
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
username,
|
||||
realname,
|
||||
};
|
||||
let Some(candidate_password) = pass else {
|
||||
sasl_fail_message(
|
||||
config.server_name.clone(),
|
||||
nickname.clone(),
|
||||
"User credentials was not provided".into(),
|
||||
)
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
continue;
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
break Ok(candidate_user);
|
||||
} else {
|
||||
future_nickname = Some(nickname);
|
||||
}
|
||||
}
|
||||
ClientMessage::User { username, realname } => {
|
||||
if cap_negotiation_in_progress {
|
||||
future_username = Some((username, realname));
|
||||
} else if let Some(nickname) = future_nickname.clone() {
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
username,
|
||||
realname,
|
||||
};
|
||||
let Some(candidate_password) = pass else {
|
||||
sasl_fail_message(
|
||||
config.server_name.clone(),
|
||||
nickname.clone(),
|
||||
"User credentials was not provided".into(),
|
||||
)
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
continue;
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
break Ok(candidate_user);
|
||||
} else {
|
||||
future_username = Some((username, realname));
|
||||
}
|
||||
}
|
||||
ClientMessage::Authenticate(body) => {
|
||||
if !authentication_started {
|
||||
tracing::debug!("Received authentication request");
|
||||
if &*body == "PLAIN" {
|
||||
tracing::debug!("Authentication request with method PLAIN");
|
||||
authentication_started = true;
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::Authenticate("+".into()),
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
} else {
|
||||
if let Some(nickname) = future_nickname.clone() {
|
||||
sasl_fail_message(
|
||||
config.server_name.clone(),
|
||||
nickname.clone(),
|
||||
"Unsupported mechanism".into(),
|
||||
)
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
} else {
|
||||
break Err(anyhow::Error::msg("Wrong authentication sequence"));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let body = AuthBody::from_str(body.as_bytes())?;
|
||||
if let Err(e) = auth_user(storage, &body.login, &body.password).await {
|
||||
tracing::warn!("Authentication failed: {:?}", e);
|
||||
if let Some(nickname) = future_nickname.clone() {
|
||||
sasl_fail_message(config.server_name.clone(), nickname.clone(), "Bad credentials".into())
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
let login: Str = body.login.into();
|
||||
validated_user = Some(login.clone());
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::N900LoggedIn {
|
||||
nick: login.clone(),
|
||||
address: login.clone(),
|
||||
account: login.clone(),
|
||||
message: format!("You are now logged in as {}", login).into(),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(config.server_name.clone().into()),
|
||||
body: ServerMessageBody::N903SaslSuccess {
|
||||
nick: login.clone(),
|
||||
message: "SASL authentication successful".into(),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO handle abortion of authentication
|
||||
}
|
||||
_ => {}
|
||||
if let Some(user) = state.handle_msg(msg, writer, storage, config).await? {
|
||||
break Ok(user);
|
||||
}
|
||||
buffer.clear();
|
||||
}?;
|
||||
|
@ -362,24 +408,13 @@ fn sasl_fail_message(sender: Str, nick: Str, text: Str) -> ServerMessage {
|
|||
}
|
||||
|
||||
async fn auth_user(storage: &mut Storage, login: &str, plain_password: &str) -> Result<()> {
|
||||
let stored_user = storage.retrieve_user_by_name(login).await?;
|
||||
|
||||
let stored_user = match stored_user {
|
||||
Some(u) => u,
|
||||
None => {
|
||||
log::info!("User '{}' not found", login);
|
||||
return Err(anyhow!("no user found"));
|
||||
}
|
||||
};
|
||||
let Some(expected_password) = stored_user.password else {
|
||||
log::info!("Password not defined for user '{}'", login);
|
||||
return Err(anyhow!("password is not defined"));
|
||||
};
|
||||
if expected_password != plain_password {
|
||||
log::info!("Incorrect password supplied for user '{}'", login);
|
||||
return Err(anyhow!("passwords do not match"));
|
||||
let verdict = Authenticator::new(storage).authenticate(login, plain_password).await?;
|
||||
// TODO properly map these onto protocol messages
|
||||
match verdict {
|
||||
Verdict::Authenticated => Ok(()),
|
||||
Verdict::UserNotFound => Err(anyhow!("no user found")),
|
||||
Verdict::InvalidPassword => Err(anyhow!("incorrect credentials")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_registered_socket<'a>(
|
||||
|
@ -395,7 +430,7 @@ async fn handle_registered_socket<'a>(
|
|||
log::info!("Handling registered user: {user:?}");
|
||||
|
||||
let player_id = PlayerId::from(user.nickname.clone())?;
|
||||
let mut connection = players.connect_to_player(player_id.clone()).await;
|
||||
let mut connection = players.connect_to_player(&player_id).await;
|
||||
let text: Str = format!("Welcome to {} Server", &config.server_name).into();
|
||||
|
||||
ServerMessage {
|
||||
|
@ -475,11 +510,18 @@ async fn handle_registered_socket<'a>(
|
|||
buffer.clear();
|
||||
},
|
||||
update = connection.receiver.recv() => {
|
||||
if let Some(update) = update {
|
||||
handle_update(&config, &user, &player_id, writer, &rooms, update).await?;
|
||||
} else {
|
||||
log::warn!("Player is terminated, must terminate the connection");
|
||||
break;
|
||||
match update {
|
||||
Some(ConnectionMessage::Update(update)) => {
|
||||
handle_update(&config, &user, &player_id, writer, &rooms, update).await?;
|
||||
}
|
||||
Some(ConnectionMessage::Stop(_)) => {
|
||||
tracing::debug!("Connection is being terminated");
|
||||
break;
|
||||
}
|
||||
None => {
|
||||
log::warn!("Player is terminated, must terminate the connection");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -564,9 +606,18 @@ async fn handle_update(
|
|||
author_id,
|
||||
room_id,
|
||||
body,
|
||||
created_at,
|
||||
} => {
|
||||
let mut tags = vec![];
|
||||
if user.enabled_capabilities.contains(Capabilities::ServerTime) {
|
||||
let tag = Tag {
|
||||
key: "time".into(),
|
||||
value: Some(created_at.to_rfc3339_opts(SecondsFormat::Millis, true).into()),
|
||||
};
|
||||
tags.push(tag);
|
||||
}
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
tags,
|
||||
sender: Some(author_id.as_inner().clone()),
|
||||
body: ServerMessageBody::PrivateMessage {
|
||||
target: Recipient::Chan(Chan::Global(room_id.as_inner().clone())),
|
||||
|
@ -602,6 +653,32 @@ async fn handle_update(
|
|||
.await?;
|
||||
writer.flush().await?
|
||||
}
|
||||
Updates::NewDialogMessage {
|
||||
sender,
|
||||
receiver,
|
||||
body,
|
||||
created_at,
|
||||
} => {
|
||||
let mut tags = vec![];
|
||||
if user.enabled_capabilities.contains(Capabilities::ServerTime) {
|
||||
let tag = Tag {
|
||||
key: "time".into(),
|
||||
value: Some(created_at.to_rfc3339_opts(SecondsFormat::Millis, true).into()),
|
||||
};
|
||||
tags.push(tag);
|
||||
}
|
||||
ServerMessage {
|
||||
tags,
|
||||
sender: Some(sender.as_inner().clone()),
|
||||
body: ServerMessageBody::PrivateMessage {
|
||||
target: Recipient::Nick(receiver.as_inner().clone()),
|
||||
body: body.clone(),
|
||||
},
|
||||
}
|
||||
.write_async(writer)
|
||||
.await?;
|
||||
writer.flush().await?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -611,6 +688,7 @@ enum HandleResult {
|
|||
Leave,
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all, name = "handle_incoming_message")]
|
||||
async fn handle_incoming_message(
|
||||
buffer: &str,
|
||||
config: &ServerConfig,
|
||||
|
@ -649,6 +727,10 @@ async fn handle_incoming_message(
|
|||
let room_id = RoomId::from(chan)?;
|
||||
user_handle.send_message(room_id, body).await?;
|
||||
}
|
||||
Recipient::Nick(nick) => {
|
||||
let receiver = PlayerId::from(nick)?;
|
||||
user_handle.send_dialog_message(receiver, body).await?;
|
||||
}
|
||||
_ => log::warn!("Unsupported target type"),
|
||||
},
|
||||
ClientMessage::Topic { chan, topic } => {
|
||||
|
@ -929,8 +1011,7 @@ impl RunningServer {
|
|||
|
||||
pub async fn launch(
|
||||
config: ServerConfig,
|
||||
players: PlayerRegistry,
|
||||
rooms: RoomRegistry,
|
||||
core: LavinaCore,
|
||||
metrics: MetricsRegistry,
|
||||
storage: Storage,
|
||||
) -> Result<RunningServer> {
|
||||
|
@ -971,13 +1052,12 @@ pub async fn launch(
|
|||
}
|
||||
|
||||
let terminator = Terminator::spawn(|termination| {
|
||||
let players = players.clone();
|
||||
let rooms = rooms.clone();
|
||||
let core = core.clone();
|
||||
let current_connections_clone = current_connections.clone();
|
||||
let stopped_tx = stopped_tx.clone();
|
||||
let storage = storage.clone();
|
||||
async move {
|
||||
match handle_socket(config, stream, &socket_addr, players, rooms, termination, storage).await {
|
||||
match handle_socket(config, stream, &socket_addr, core, termination, storage).await {
|
||||
Ok(_) => log::info!("Connection terminated"),
|
||||
Err(err) => log::warn!("Connection failed: {err}"),
|
||||
}
|
||||
|
|
|
@ -1,15 +1,18 @@
|
|||
use std::io::ErrorKind;
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use chrono::{DateTime, SecondsFormat};
|
||||
use prometheus::Registry as MetricsRegistry;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use lavina_core::auth::Authenticator;
|
||||
use lavina_core::player::{JoinResult, PlayerId, SendMessageResult};
|
||||
use lavina_core::repo::{Storage, StorageConfig};
|
||||
use lavina_core::{player::PlayerRegistry, room::RoomRegistry};
|
||||
use lavina_core::room::RoomId;
|
||||
use lavina_core::LavinaCore;
|
||||
use projection_irc::APP_VERSION;
|
||||
use projection_irc::{launch, read_irc_message, RunningServer, ServerConfig};
|
||||
|
||||
|
@ -25,7 +28,7 @@ impl<'a> TestScope<'a> {
|
|||
let (reader, writer) = stream.split();
|
||||
let reader = BufReader::new(reader);
|
||||
let buffer = vec![];
|
||||
let timeout = Duration::from_millis(100);
|
||||
let timeout = Duration::from_millis(1000);
|
||||
TestScope {
|
||||
reader,
|
||||
writer,
|
||||
|
@ -49,6 +52,31 @@ impl<'a> TestScope<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn expect_that(&mut self, validate: impl FnOnce(&str) -> bool) -> Result<()> {
|
||||
let len = tokio::time::timeout(self.timeout, read_irc_message(&mut self.reader, &mut self.buffer)).await??;
|
||||
let msg = std::str::from_utf8(&self.buffer[..len - 2])?;
|
||||
if !validate(msg) {
|
||||
return Err(anyhow!("unexpected message: {:?}", msg));
|
||||
}
|
||||
self.buffer.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn expect_server_introduction(&mut self, nick: &str) -> Result<()> {
|
||||
self.expect(&format!(":testserver 001 {nick} :Welcome to testserver Server")).await?;
|
||||
self.expect(&format!(":testserver 002 {nick} :Welcome to testserver Server")).await?;
|
||||
self.expect(&format!(":testserver 003 {nick} :Welcome to testserver Server")).await?;
|
||||
self.expect(&format!(
|
||||
":testserver 004 {nick} testserver {APP_VERSION} r CFILPQbcefgijklmnopqrstvz"
|
||||
))
|
||||
.await?;
|
||||
self.expect(&format!(
|
||||
":testserver 005 {nick} CHANTYPES=# :are supported by this server"
|
||||
))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn expect_eof(&mut self) -> Result<()> {
|
||||
let mut buf = [0; 1];
|
||||
let len = tokio::time::timeout(self.timeout, self.reader.read(&mut buf)).await??;
|
||||
|
@ -65,13 +93,17 @@ impl<'a> TestScope<'a> {
|
|||
Err(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn expect_cap_ls(&mut self) -> Result<()> {
|
||||
self.expect(":testserver CAP * LS :sasl=PLAIN server-time").await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct TestServer {
|
||||
metrics: MetricsRegistry,
|
||||
storage: Storage,
|
||||
rooms: RoomRegistry,
|
||||
players: PlayerRegistry,
|
||||
core: LavinaCore,
|
||||
server: RunningServer,
|
||||
}
|
||||
impl TestServer {
|
||||
|
@ -86,17 +118,46 @@ impl TestServer {
|
|||
db_path: ":memory:".into(),
|
||||
})
|
||||
.await?;
|
||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
||||
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
||||
let server = launch(config, players.clone(), rooms.clone(), metrics.clone(), storage.clone()).await.unwrap();
|
||||
let core = LavinaCore::new(metrics.clone(), storage.clone()).await?;
|
||||
let server = launch(config, core.clone(), metrics.clone(), storage.clone()).await.unwrap();
|
||||
Ok(TestServer {
|
||||
metrics,
|
||||
storage,
|
||||
rooms,
|
||||
players,
|
||||
core,
|
||||
server,
|
||||
})
|
||||
}
|
||||
|
||||
async fn reboot(self) -> Result<TestServer> {
|
||||
let config = ServerConfig {
|
||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||
server_name: "testserver".into(),
|
||||
};
|
||||
let TestServer {
|
||||
metrics: _,
|
||||
storage,
|
||||
mut core,
|
||||
server,
|
||||
} = self;
|
||||
server.terminate().await?;
|
||||
core.shutdown().await?;
|
||||
let metrics = MetricsRegistry::new();
|
||||
let core = LavinaCore::new(metrics.clone(), storage.clone()).await?;
|
||||
let server = launch(config, core.clone(), metrics.clone(), storage.clone()).await.unwrap();
|
||||
Ok(TestServer {
|
||||
metrics,
|
||||
storage,
|
||||
core,
|
||||
server,
|
||||
})
|
||||
}
|
||||
|
||||
async fn shutdown(self) -> Result<()> {
|
||||
self.server.terminate().await?;
|
||||
self.core.shutdown().await?;
|
||||
self.storage.close().await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
@ -106,7 +167,7 @@ async fn scenario_basic() -> Result<()> {
|
|||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
server.storage.set_password("tester", "password").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
@ -114,18 +175,7 @@ async fn scenario_basic() -> Result<()> {
|
|||
s.send("PASS password").await?;
|
||||
s.send("NICK tester").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect(":testserver 001 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 002 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 003 tester :Welcome to testserver Server").await?;
|
||||
s.expect(
|
||||
format!(
|
||||
":testserver 004 tester testserver {} r CFILPQbcefgijklmnopqrstvz",
|
||||
&APP_VERSION
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.await?;
|
||||
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
|
@ -135,7 +185,208 @@ async fn scenario_basic() -> Result<()> {
|
|||
|
||||
// wrap up
|
||||
|
||||
server.server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_join_and_reboot() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
||||
// Open a connection and join a channel
|
||||
|
||||
s.send("PASS password").await?;
|
||||
s.send("NICK tester").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("JOIN #test").await?;
|
||||
s.expect(":tester JOIN #test").await?;
|
||||
s.expect(":testserver 332 tester #test :New room").await?;
|
||||
s.expect(":testserver 353 tester = #test :tester").await?;
|
||||
s.expect(":testserver 366 tester #test :End of /NAMES list").await?;
|
||||
s.send("PRIVMSG #test :Hello").await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
s.expect_eof().await?;
|
||||
stream.shutdown().await?;
|
||||
|
||||
// Open a new connection and expect to be force-joined to the channel
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
||||
async fn test(s: &mut TestScope<'_>) -> Result<()> {
|
||||
s.send("PASS password").await?;
|
||||
s.send("NICK tester").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect(":tester JOIN #test").await?;
|
||||
s.expect(":testserver 332 tester #test :New room").await?;
|
||||
s.expect(":testserver 353 tester = #test :tester").await?;
|
||||
s.expect(":testserver 366 tester #test :End of /NAMES list").await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
s.expect_eof().await?;
|
||||
Ok(())
|
||||
}
|
||||
test(&mut s).await?;
|
||||
stream.shutdown().await?;
|
||||
|
||||
// Reboot the server
|
||||
|
||||
let server = server.reboot().await?;
|
||||
|
||||
// Open a new connection and expect to be force-joined to the channel
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
||||
test(&mut s).await?;
|
||||
stream.shutdown().await?;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_force_join_msg() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream1 = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s1 = TestScope::new(&mut stream1);
|
||||
let mut stream2 = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s2 = TestScope::new(&mut stream2);
|
||||
|
||||
s1.send("PASS password").await?;
|
||||
s1.send("NICK tester").await?;
|
||||
s1.send("USER UserName 0 * :Real Name").await?;
|
||||
s1.expect_server_introduction("tester").await?;
|
||||
s1.expect_nothing().await?;
|
||||
|
||||
s2.send("PASS password").await?;
|
||||
s2.send("NICK tester").await?;
|
||||
s2.send("USER UserName 0 * :Real Name").await?;
|
||||
s2.expect_server_introduction("tester").await?;
|
||||
s2.expect_nothing().await?;
|
||||
|
||||
// We join the channel from the first connection
|
||||
s1.send("JOIN #test").await?;
|
||||
s1.expect(":tester JOIN #test").await?;
|
||||
s1.expect(":testserver 332 tester #test :New room").await?;
|
||||
s1.expect(":testserver 353 tester = #test :tester").await?;
|
||||
s1.expect(":testserver 366 tester #test :End of /NAMES list").await?;
|
||||
|
||||
// And the second connection should receive the JOIN message (forced JOIN)
|
||||
s2.expect(":tester JOIN #test").await?;
|
||||
s2.expect(":testserver 332 tester #test :New room").await?;
|
||||
s2.expect(":testserver 353 tester = #test :tester").await?;
|
||||
s2.expect(":testserver 366 tester #test :End of /NAMES list").await?;
|
||||
|
||||
// We send a message to the channel from the second connection
|
||||
s2.send("PRIVMSG #test :Hello").await?;
|
||||
// We should not receive an acknowledgement from the server
|
||||
s2.expect_nothing().await?;
|
||||
// But we should receive this message from the first connection
|
||||
s1.expect(":tester PRIVMSG #test :Hello").await?;
|
||||
|
||||
s1.send("QUIT :Leaving").await?;
|
||||
s1.expect(":testserver ERROR :Leaving the server").await?;
|
||||
s1.expect_eof().await?;
|
||||
|
||||
// Closing a connection does not kick you from the channel on a different connection
|
||||
s2.expect_nothing().await?;
|
||||
|
||||
s2.send("QUIT :Leaving").await?;
|
||||
s2.expect(":testserver ERROR :Leaving the server").await?;
|
||||
s2.expect_eof().await?;
|
||||
|
||||
stream1.shutdown().await?;
|
||||
stream2.shutdown().await?;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_two_users() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester1").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester1", "password").await?;
|
||||
server.storage.create_user("tester2").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester2", "password").await?;
|
||||
|
||||
let mut stream1 = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s1 = TestScope::new(&mut stream1);
|
||||
let mut stream2 = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s2 = TestScope::new(&mut stream2);
|
||||
|
||||
s1.send("PASS password").await?;
|
||||
s1.send("NICK tester1").await?;
|
||||
s1.send("USER UserName 0 * :Real Name").await?;
|
||||
s1.expect_server_introduction("tester1").await?;
|
||||
s1.expect_nothing().await?;
|
||||
|
||||
s2.send("PASS password").await?;
|
||||
s2.send("NICK tester2").await?;
|
||||
s2.send("USER UserName 0 * :Real Name").await?;
|
||||
s2.expect_server_introduction("tester2").await?;
|
||||
s2.expect_nothing().await?;
|
||||
|
||||
// Join the channel from the first user
|
||||
s1.send("JOIN #test").await?;
|
||||
s1.expect(":tester1 JOIN #test").await?;
|
||||
s1.expect(":testserver 332 tester1 #test :New room").await?;
|
||||
s1.expect(":testserver 353 tester1 = #test :tester1").await?;
|
||||
s1.expect(":testserver 366 tester1 #test :End of /NAMES list").await?;
|
||||
// Then join the channel from the second user
|
||||
s2.send("JOIN #test").await?;
|
||||
s2.expect(":tester2 JOIN #test").await?;
|
||||
s2.expect(":testserver 332 tester2 #test :New room").await?;
|
||||
s2.expect_that(|msg| {
|
||||
msg == ":testserver 353 tester2 = #test :tester1 tester2"
|
||||
|| msg == ":testserver 353 tester2 = #test :tester2 tester1"
|
||||
})
|
||||
.await?;
|
||||
s2.expect(":testserver 366 tester2 #test :End of /NAMES list").await?;
|
||||
// The first user should receive the JOIN message from the second user
|
||||
s1.expect(":tester2 JOIN #test").await?;
|
||||
s1.expect_nothing().await?;
|
||||
s2.expect_nothing().await?;
|
||||
// Send a message from the second user
|
||||
s2.send("PRIVMSG #test :Hello").await?;
|
||||
// The first user should receive the message
|
||||
s1.expect(":tester2 PRIVMSG #test :Hello").await?;
|
||||
// Leave the channel from the first user
|
||||
s1.send("PART #test").await?;
|
||||
s1.expect(":tester1 PART #test").await?;
|
||||
// The second user should receive the PART message
|
||||
s2.expect(":tester1 PART #test").await?;
|
||||
|
||||
stream1.shutdown().await?;
|
||||
stream2.shutdown().await?;
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -150,7 +401,7 @@ async fn scenario_cap_full_negotiation() -> Result<()> {
|
|||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
server.storage.set_password("tester", "password").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
@ -158,7 +409,7 @@ async fn scenario_cap_full_negotiation() -> Result<()> {
|
|||
s.send("CAP LS 302").await?;
|
||||
s.send("NICK tester").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect(":testserver CAP * LS :sasl=PLAIN").await?;
|
||||
s.expect_cap_ls().await?;
|
||||
s.send("CAP REQ :sasl").await?;
|
||||
s.expect(":testserver CAP tester ACK :sasl").await?;
|
||||
s.send("AUTHENTICATE PLAIN").await?;
|
||||
|
@ -169,18 +420,7 @@ async fn scenario_cap_full_negotiation() -> Result<()> {
|
|||
|
||||
s.send("CAP END").await?;
|
||||
|
||||
s.expect(":testserver 001 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 002 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 003 tester :Welcome to testserver Server").await?;
|
||||
s.expect(
|
||||
format!(
|
||||
":testserver 004 tester testserver {} r CFILPQbcefgijklmnopqrstvz",
|
||||
&APP_VERSION
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.await?;
|
||||
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
|
@ -190,7 +430,46 @@ async fn scenario_cap_full_negotiation() -> Result<()> {
|
|||
|
||||
// wrap up
|
||||
|
||||
server.server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_cap_full_negotiation_nick_last() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
||||
s.send("CAP LS 302").await?;
|
||||
s.expect_cap_ls().await?;
|
||||
s.send("CAP REQ :sasl").await?;
|
||||
s.expect(":testserver CAP * ACK :sasl").await?;
|
||||
s.send("AUTHENTICATE PLAIN").await?;
|
||||
s.expect(":testserver AUTHENTICATE +").await?;
|
||||
s.send("AUTHENTICATE dGVzdGVyAHRlc3RlcgBwYXNzd29yZA==").await?; // base64-encoded 'tester\x00tester\x00password'
|
||||
s.expect(":testserver 900 tester tester tester :You are now logged in as tester").await?;
|
||||
s.expect(":testserver 903 tester :SASL authentication successful").await?;
|
||||
s.send("CAP END").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.send("NICK tester").await?;
|
||||
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
s.expect_eof().await?;
|
||||
|
||||
stream.shutdown().await?;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -201,7 +480,7 @@ async fn scenario_cap_short_negotiation() -> Result<()> {
|
|||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
server.storage.set_password("tester", "password").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
@ -218,18 +497,7 @@ async fn scenario_cap_short_negotiation() -> Result<()> {
|
|||
|
||||
s.send("CAP END").await?;
|
||||
|
||||
s.expect(":testserver 001 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 002 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 003 tester :Welcome to testserver Server").await?;
|
||||
s.expect(
|
||||
format!(
|
||||
":testserver 004 tester testserver {} r CFILPQbcefgijklmnopqrstvz",
|
||||
&APP_VERSION
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.await?;
|
||||
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
|
@ -239,7 +507,7 @@ async fn scenario_cap_short_negotiation() -> Result<()> {
|
|||
|
||||
// wrap up
|
||||
|
||||
server.server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -250,7 +518,7 @@ async fn scenario_cap_sasl_fail() -> Result<()> {
|
|||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
server.storage.set_password("tester", "password").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
@ -258,7 +526,7 @@ async fn scenario_cap_sasl_fail() -> Result<()> {
|
|||
s.send("CAP LS 302").await?;
|
||||
s.send("NICK tester").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect(":testserver CAP * LS :sasl=PLAIN").await?;
|
||||
s.expect_cap_ls().await?;
|
||||
s.send("CAP REQ :sasl").await?;
|
||||
s.expect(":testserver CAP tester ACK :sasl").await?;
|
||||
s.send("AUTHENTICATE SHA256").await?;
|
||||
|
@ -273,18 +541,7 @@ async fn scenario_cap_sasl_fail() -> Result<()> {
|
|||
|
||||
s.send("CAP END").await?;
|
||||
|
||||
s.expect(":testserver 001 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 002 tester :Welcome to testserver Server").await?;
|
||||
s.expect(":testserver 003 tester :Welcome to testserver Server").await?;
|
||||
s.expect(
|
||||
format!(
|
||||
":testserver 004 tester testserver {} r CFILPQbcefgijklmnopqrstvz",
|
||||
&APP_VERSION
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.await?;
|
||||
s.expect(":testserver 005 tester CHANTYPES=# :are supported by this server").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
|
@ -294,19 +551,18 @@ async fn scenario_cap_sasl_fail() -> Result<()> {
|
|||
|
||||
// wrap up
|
||||
|
||||
server.server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn terminate_socket_scenario() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
let address: SocketAddr = ("127.0.0.1:0".parse().unwrap());
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
server.storage.set_password("tester", "password").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
@ -318,8 +574,142 @@ async fn terminate_socket_scenario() -> Result<()> {
|
|||
s.send("AUTHENTICATE PLAIN").await?;
|
||||
s.expect(":testserver AUTHENTICATE +").await?;
|
||||
|
||||
server.server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
assert_eq!(stream.read_u8().await.unwrap_err().kind(), ErrorKind::UnexpectedEof);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn server_time_capability() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
||||
s.send("CAP LS 302").await?;
|
||||
s.send("NICK tester").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect_cap_ls().await?;
|
||||
s.send("CAP REQ :sasl server-time").await?;
|
||||
s.expect(":testserver CAP tester ACK :sasl server-time").await?;
|
||||
s.send("AUTHENTICATE PLAIN").await?;
|
||||
s.expect(":testserver AUTHENTICATE +").await?;
|
||||
s.send("AUTHENTICATE dGVzdGVyAHRlc3RlcgBwYXNzd29yZA==").await?; // base64-encoded 'tester\x00tester\x00password'
|
||||
s.expect(":testserver 900 tester tester tester :You are now logged in as tester").await?;
|
||||
s.expect(":testserver 903 tester :SASL authentication successful").await?;
|
||||
s.send("CAP END").await?;
|
||||
s.expect_server_introduction("tester").await?;
|
||||
s.expect_nothing().await?;
|
||||
s.send("JOIN #test").await?;
|
||||
s.expect(":tester JOIN #test").await?;
|
||||
s.expect(":testserver 332 tester #test :New room").await?;
|
||||
s.expect(":testserver 353 tester = #test :tester").await?;
|
||||
s.expect(":testserver 366 tester #test :End of /NAMES list").await?;
|
||||
|
||||
server.storage.create_user("some_guy").await?;
|
||||
let mut conn = server.core.players.connect_to_player(&PlayerId::from("some_guy").unwrap()).await;
|
||||
let res = conn.join_room(RoomId::from("test").unwrap()).await?;
|
||||
let JoinResult::Success(_) = res else {
|
||||
panic!("Failed to join room");
|
||||
};
|
||||
|
||||
s.expect(":some_guy JOIN #test").await?;
|
||||
|
||||
let SendMessageResult::Success(res) = conn.send_message(RoomId::from("test").unwrap(), "Hello".into()).await?
|
||||
else {
|
||||
panic!("Failed to send message");
|
||||
};
|
||||
s.expect(&format!(
|
||||
"@time={} :some_guy PRIVMSG #test :Hello",
|
||||
res.to_rfc3339_opts(SecondsFormat::Millis, true)
|
||||
))
|
||||
.await?;
|
||||
|
||||
// formatting check
|
||||
assert_eq!(
|
||||
DateTime::parse_from_rfc3339(&"2024-01-01T10:00:32.123Z").unwrap().to_rfc3339_opts(SecondsFormat::Millis, true),
|
||||
"2024-01-01T10:00:32.123Z"
|
||||
);
|
||||
|
||||
s.send("QUIT :Leaving").await?;
|
||||
s.expect(":testserver ERROR :Leaving the server").await?;
|
||||
s.expect_eof().await?;
|
||||
|
||||
stream.shutdown().await?;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_two_players_dialog() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester1").await?;
|
||||
server.storage.set_password("tester1", "password").await?;
|
||||
server.storage.create_user("tester2").await?;
|
||||
server.storage.set_password("tester2", "password").await?;
|
||||
|
||||
let mut stream1 = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s1 = TestScope::new(&mut stream1);
|
||||
|
||||
let mut stream2 = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s2 = TestScope::new(&mut stream2);
|
||||
|
||||
s1.send("CAP LS 302").await?;
|
||||
s1.send("NICK tester1").await?;
|
||||
s1.send("USER UserName 0 * :Real Name").await?;
|
||||
s1.expect_cap_ls().await?;
|
||||
s1.send("CAP REQ :sasl").await?;
|
||||
s1.expect(":testserver CAP tester1 ACK :sasl").await?;
|
||||
s1.send("AUTHENTICATE PLAIN").await?;
|
||||
s1.expect(":testserver AUTHENTICATE +").await?;
|
||||
s1.send("AUTHENTICATE dGVzdGVyMQB0ZXN0ZXIxAHBhc3N3b3Jk").await?; // base64-encoded 'tester1\x00tester1\x00password'
|
||||
s1.expect(":testserver 900 tester1 tester1 tester1 :You are now logged in as tester1").await?;
|
||||
s1.expect(":testserver 903 tester1 :SASL authentication successful").await?;
|
||||
s1.send("CAP END").await?;
|
||||
s1.expect_server_introduction("tester1").await?;
|
||||
s1.expect_nothing().await?;
|
||||
|
||||
s2.send("CAP LS 302").await?;
|
||||
s2.send("NICK tester2").await?;
|
||||
s2.send("USER UserName 0 * :Real Name").await?;
|
||||
s2.expect_cap_ls().await?;
|
||||
s2.send("CAP REQ :sasl").await?;
|
||||
s2.expect(":testserver CAP tester2 ACK :sasl").await?;
|
||||
s2.send("AUTHENTICATE PLAIN").await?;
|
||||
s2.expect(":testserver AUTHENTICATE +").await?;
|
||||
s2.send("AUTHENTICATE dGVzdGVyMgB0ZXN0ZXIyAHBhc3N3b3Jk").await?; // base64-encoded 'tester2\x00tester2\x00password'
|
||||
s2.expect(":testserver 900 tester2 tester2 tester2 :You are now logged in as tester2").await?;
|
||||
s2.expect(":testserver 903 tester2 :SASL authentication successful").await?;
|
||||
s2.send("CAP END").await?;
|
||||
s2.expect_server_introduction("tester2").await?;
|
||||
s2.expect_nothing().await?;
|
||||
|
||||
s1.send("PRIVMSG tester2 :Henlo! How are you?").await?;
|
||||
s1.expect_nothing().await?;
|
||||
s2.expect(":tester1 PRIVMSG tester2 :Henlo! How are you?").await?;
|
||||
s2.expect_nothing().await?;
|
||||
|
||||
s2.send("PRIVMSG tester1 good").await?;
|
||||
s2.expect_nothing().await?;
|
||||
s1.expect(":tester2 PRIVMSG tester1 :good").await?;
|
||||
s1.expect_nothing().await?;
|
||||
|
||||
stream1.shutdown().await?;
|
||||
stream2.shutdown().await?;
|
||||
|
||||
server.shutdown().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
use quick_xml::events::Event;
|
||||
|
||||
use lavina_core::room::RoomRegistry;
|
||||
use proto_xmpp::bind::{BindResponse, Jid, Name, Resource, Server};
|
||||
use proto_xmpp::client::{Iq, IqType};
|
||||
use lavina_core::room::{RoomId, RoomRegistry};
|
||||
use proto_xmpp::bind::{BindResponse, Jid, Name, Server};
|
||||
use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType};
|
||||
use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery};
|
||||
use proto_xmpp::roster::RosterQuery;
|
||||
use proto_xmpp::session::Session;
|
||||
|
@ -17,16 +17,16 @@ use proto_xmpp::xml::ToXml;
|
|||
impl<'a> XmppConnection<'a> {
|
||||
pub async fn handle_iq(&self, output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) {
|
||||
match iq.body {
|
||||
IqClientBody::Bind(b) => {
|
||||
IqClientBody::Bind(_) => {
|
||||
let req = Iq {
|
||||
from: None,
|
||||
id: iq.id,
|
||||
to: None,
|
||||
r#type: IqType::Result,
|
||||
body: BindResponse(Jid {
|
||||
name: Some(Name("darova".into())),
|
||||
server: Server("localhost".into()),
|
||||
resource: Some(Resource("kek".into())),
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
};
|
||||
req.serialize(output);
|
||||
|
@ -52,18 +52,32 @@ impl<'a> XmppConnection<'a> {
|
|||
req.serialize(output);
|
||||
}
|
||||
IqClientBody::DiscoInfo(info) => {
|
||||
let response = disco_info(iq.to.as_deref(), &info);
|
||||
let req = Iq {
|
||||
from: iq.to,
|
||||
id: iq.id,
|
||||
to: None,
|
||||
r#type: IqType::Result,
|
||||
body: response,
|
||||
};
|
||||
req.serialize(output);
|
||||
let response = self.disco_info(iq.to.as_ref(), &info).await;
|
||||
match response {
|
||||
Ok(response) => {
|
||||
let req = Iq {
|
||||
from: iq.to,
|
||||
id: iq.id,
|
||||
to: None,
|
||||
r#type: IqType::Result,
|
||||
body: response,
|
||||
};
|
||||
req.serialize(output);
|
||||
}
|
||||
Err(response) => {
|
||||
let req = Iq {
|
||||
from: iq.to,
|
||||
id: iq.id,
|
||||
to: None,
|
||||
r#type: IqType::Error,
|
||||
body: response,
|
||||
};
|
||||
req.serialize(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
IqClientBody::DiscoItem(item) => {
|
||||
let response = disco_items(iq.to.as_deref(), &item, self.rooms).await;
|
||||
let response = self.disco_items(iq.to.as_ref(), &item, self.rooms).await;
|
||||
let req = Iq {
|
||||
from: iq.to,
|
||||
id: iq.id,
|
||||
|
@ -79,84 +93,131 @@ impl<'a> XmppConnection<'a> {
|
|||
id: iq.id,
|
||||
to: None,
|
||||
r#type: IqType::Error,
|
||||
body: (),
|
||||
body: IqError {
|
||||
r#type: IqErrorType::Cancel,
|
||||
},
|
||||
};
|
||||
req.serialize(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn disco_info(to: Option<&str>, req: &InfoQuery) -> InfoQuery {
|
||||
let identity;
|
||||
let feature;
|
||||
match to {
|
||||
Some("localhost") => {
|
||||
identity = vec![Identity {
|
||||
category: "server".into(),
|
||||
async fn disco_info(&self, to: Option<&Jid>, req: &InfoQuery) -> Result<InfoQuery, IqError> {
|
||||
let identity;
|
||||
let feature;
|
||||
|
||||
match to {
|
||||
Some(Jid {
|
||||
name: None,
|
||||
r#type: "im".into(),
|
||||
}];
|
||||
feature = vec![
|
||||
Feature::new("http://jabber.org/protocol/disco#info"),
|
||||
Feature::new("http://jabber.org/protocol/disco#items"),
|
||||
Feature::new("iq"),
|
||||
Feature::new("presence"),
|
||||
]
|
||||
}
|
||||
Some("rooms.localhost") => {
|
||||
identity = vec![Identity {
|
||||
category: "conference".into(),
|
||||
name: Some("Chat rooms".into()),
|
||||
r#type: "text".into(),
|
||||
}];
|
||||
feature = vec![
|
||||
Feature::new("http://jabber.org/protocol/disco#info"),
|
||||
Feature::new("http://jabber.org/protocol/disco#items"),
|
||||
Feature::new("http://jabber.org/protocol/muc"),
|
||||
]
|
||||
}
|
||||
_ => {
|
||||
identity = vec![];
|
||||
feature = vec![];
|
||||
}
|
||||
};
|
||||
InfoQuery {
|
||||
node: None,
|
||||
identity,
|
||||
feature,
|
||||
}
|
||||
}
|
||||
|
||||
async fn disco_items(to: Option<&str>, req: &ItemQuery, rooms: &RoomRegistry) -> ItemQuery {
|
||||
let item = match to {
|
||||
Some("localhost") => {
|
||||
vec![Item {
|
||||
jid: Jid {
|
||||
server,
|
||||
resource: None,
|
||||
}) if server.0 == self.hostname => {
|
||||
identity = vec![Identity {
|
||||
category: "server".into(),
|
||||
name: None,
|
||||
server: Server("rooms.localhost".into()),
|
||||
resource: None,
|
||||
},
|
||||
r#type: "im".into(),
|
||||
}];
|
||||
feature = vec![
|
||||
Feature::new("http://jabber.org/protocol/disco#info"),
|
||||
Feature::new("http://jabber.org/protocol/disco#items"),
|
||||
Feature::new("iq"),
|
||||
Feature::new("presence"),
|
||||
]
|
||||
}
|
||||
Some(Jid {
|
||||
name: None,
|
||||
node: None,
|
||||
}]
|
||||
}
|
||||
Some("rooms.localhost") => {
|
||||
let room_list = rooms.get_all_rooms().await;
|
||||
room_list
|
||||
.into_iter()
|
||||
.map(|room_info| Item {
|
||||
server,
|
||||
resource: None,
|
||||
}) if server.0 == self.hostname_rooms => {
|
||||
identity = vec![Identity {
|
||||
category: "conference".into(),
|
||||
name: Some("Chat rooms".into()),
|
||||
r#type: "text".into(),
|
||||
}];
|
||||
feature = vec![
|
||||
Feature::new("http://jabber.org/protocol/disco#info"),
|
||||
Feature::new("http://jabber.org/protocol/disco#items"),
|
||||
Feature::new("http://jabber.org/protocol/muc"),
|
||||
]
|
||||
}
|
||||
Some(Jid {
|
||||
name: Some(room_name),
|
||||
server,
|
||||
resource: None,
|
||||
}) if server.0 == self.hostname_rooms => {
|
||||
let room_id = RoomId::from(room_name.0.clone()).unwrap();
|
||||
let Some(_) = self.rooms.get_room(&room_id).await else {
|
||||
// TODO should return item-not-found
|
||||
// example:
|
||||
// <error type="cancel">
|
||||
// <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
|
||||
// <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">Conference room does not exist</text>
|
||||
// </error>
|
||||
return Err(IqError {
|
||||
r#type: IqErrorType::Cancel,
|
||||
});
|
||||
};
|
||||
identity = vec![Identity {
|
||||
category: "conference".into(),
|
||||
name: Some(room_id.into_inner().to_string()),
|
||||
r#type: "text".into(),
|
||||
}];
|
||||
feature = vec![
|
||||
Feature::new("http://jabber.org/protocol/disco#info"),
|
||||
Feature::new("http://jabber.org/protocol/disco#items"),
|
||||
Feature::new("http://jabber.org/protocol/muc"),
|
||||
]
|
||||
}
|
||||
_ => {
|
||||
identity = vec![];
|
||||
feature = vec![];
|
||||
}
|
||||
};
|
||||
Ok(InfoQuery {
|
||||
node: None,
|
||||
identity,
|
||||
feature,
|
||||
})
|
||||
}
|
||||
|
||||
async fn disco_items(&self, to: Option<&Jid>, req: &ItemQuery, rooms: &RoomRegistry) -> ItemQuery {
|
||||
let item = match to {
|
||||
Some(Jid {
|
||||
name: None,
|
||||
server,
|
||||
resource: None,
|
||||
}) if server.0 == self.hostname => {
|
||||
vec![Item {
|
||||
jid: Jid {
|
||||
name: Some(Name(room_info.id.into_inner())),
|
||||
server: Server("rooms.localhost".into()),
|
||||
name: None,
|
||||
server: Server(self.hostname_rooms.clone()),
|
||||
resource: None,
|
||||
},
|
||||
name: None,
|
||||
node: None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
_ => vec![],
|
||||
};
|
||||
ItemQuery { item }
|
||||
}]
|
||||
}
|
||||
Some(Jid {
|
||||
name: None,
|
||||
server,
|
||||
resource: None,
|
||||
}) if server.0 == self.hostname_rooms => {
|
||||
let room_list = rooms.get_all_rooms().await;
|
||||
room_list
|
||||
.into_iter()
|
||||
.map(|room_info| Item {
|
||||
jid: Jid {
|
||||
name: Some(Name(room_info.id.into_inner())),
|
||||
server: Server(self.hostname_rooms.clone()),
|
||||
resource: None,
|
||||
},
|
||||
name: None,
|
||||
node: None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
_ => vec![],
|
||||
};
|
||||
ItemQuery { item }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,13 +22,16 @@ use tokio::sync::mpsc::channel;
|
|||
use tokio_rustls::rustls::{Certificate, PrivateKey};
|
||||
use tokio_rustls::TlsAcceptor;
|
||||
|
||||
use lavina_core::player::{PlayerConnection, PlayerId, PlayerRegistry};
|
||||
use lavina_core::auth::{Authenticator, Verdict};
|
||||
use lavina_core::player::{ConnectionMessage, PlayerConnection, PlayerId, PlayerRegistry, StopReason};
|
||||
use lavina_core::prelude::*;
|
||||
use lavina_core::repo::Storage;
|
||||
use lavina_core::room::RoomRegistry;
|
||||
use lavina_core::terminator::Terminator;
|
||||
use lavina_core::LavinaCore;
|
||||
use proto_xmpp::bind::{Name, Resource};
|
||||
use proto_xmpp::stream::*;
|
||||
use proto_xmpp::streamerror::{StreamError, StreamErrorKind};
|
||||
use proto_xmpp::xml::{Continuation, FromXml, Parser, ToXml};
|
||||
use sasl::AuthBody;
|
||||
|
||||
|
@ -44,6 +47,7 @@ pub struct ServerConfig {
|
|||
pub listen_on: SocketAddr,
|
||||
pub cert: PathBuf,
|
||||
pub key: PathBuf,
|
||||
pub hostname: Str,
|
||||
}
|
||||
|
||||
struct LoadedConfig {
|
||||
|
@ -52,9 +56,17 @@ struct LoadedConfig {
|
|||
}
|
||||
|
||||
struct Authenticated {
|
||||
/// Identifier of the authenticated player.
|
||||
///
|
||||
/// Used when communicating with lavina-core on behalf of the player.
|
||||
player_id: PlayerId,
|
||||
/// The user's XMPP name.
|
||||
///
|
||||
/// Used in `to` and `from` fields of XMPP messages.
|
||||
xmpp_name: Name,
|
||||
/// The resource given to this user by the server.
|
||||
xmpp_resource: Resource,
|
||||
/// The resource used by this user when joining MUCs.
|
||||
xmpp_muc_name: Resource,
|
||||
}
|
||||
|
||||
|
@ -71,8 +83,7 @@ impl RunningServer {
|
|||
|
||||
pub async fn launch(
|
||||
config: ServerConfig,
|
||||
players: PlayerRegistry,
|
||||
rooms: RoomRegistry,
|
||||
core: LavinaCore,
|
||||
metrics: MetricsRegistry,
|
||||
storage: Storage,
|
||||
) -> Result<RunningServer> {
|
||||
|
@ -114,14 +125,14 @@ pub async fn launch(
|
|||
// TODO kill the older connection and restart it
|
||||
continue;
|
||||
}
|
||||
let players = players.clone();
|
||||
let rooms = rooms.clone();
|
||||
let core = core.clone();
|
||||
let storage = storage.clone();
|
||||
let hostname = config.hostname.clone();
|
||||
let terminator = Terminator::spawn(|termination| {
|
||||
let stopped_tx = stopped_tx.clone();
|
||||
let loaded_config = loaded_config.clone();
|
||||
async move {
|
||||
match handle_socket(loaded_config, stream, &socket_addr, players, rooms, storage, termination).await {
|
||||
match handle_socket(loaded_config, stream, &socket_addr, core, storage, hostname, termination).await {
|
||||
Ok(_) => log::info!("Connection terminated"),
|
||||
Err(err) => log::warn!("Connection failed: {err}"),
|
||||
}
|
||||
|
@ -156,12 +167,12 @@ pub async fn launch(
|
|||
}
|
||||
|
||||
async fn handle_socket(
|
||||
config: Arc<LoadedConfig>,
|
||||
cert_config: Arc<LoadedConfig>,
|
||||
mut stream: TcpStream,
|
||||
socket_addr: &SocketAddr,
|
||||
mut players: PlayerRegistry,
|
||||
rooms: RoomRegistry,
|
||||
mut core: LavinaCore,
|
||||
mut storage: Storage,
|
||||
hostname: Str,
|
||||
termination: Deferred<()>, // TODO use it to stop the connection gracefully
|
||||
) -> Result<()> {
|
||||
log::info!("Received an XMPP connection from {socket_addr}");
|
||||
|
@ -170,12 +181,12 @@ async fn handle_socket(
|
|||
let mut buf_reader = BufReader::new(reader);
|
||||
let mut buf_writer = BufWriter::new(writer);
|
||||
|
||||
socket_force_tls(&mut buf_reader, &mut buf_writer, &mut reader_buf).await?;
|
||||
socket_force_tls(&mut buf_reader, &mut buf_writer, &mut reader_buf, &hostname).await?;
|
||||
|
||||
let mut config = tokio_rustls::rustls::ServerConfig::builder()
|
||||
.with_safe_defaults()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(vec![config.cert.clone()], config.key.clone())?;
|
||||
.with_single_cert(vec![cert_config.cert.clone()], cert_config.key.clone())?;
|
||||
config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new());
|
||||
|
||||
log::debug!("Accepting TLS connection...");
|
||||
|
@ -185,7 +196,7 @@ async fn handle_socket(
|
|||
|
||||
let (a, b) = tokio::io::split(new_stream);
|
||||
let mut xml_reader = NsReader::from_reader(BufReader::new(a));
|
||||
let mut xml_writer = Writer::new(b);
|
||||
let mut xml_writer = Writer::new(BufWriter::new(b));
|
||||
|
||||
pin!(termination);
|
||||
select! {
|
||||
|
@ -194,17 +205,18 @@ async fn handle_socket(
|
|||
log::info!("Socket handling was terminated");
|
||||
return Ok(())
|
||||
},
|
||||
authenticated = socket_auth(&mut xml_reader, &mut xml_writer, &mut reader_buf, &mut storage) => {
|
||||
authenticated = socket_auth(&mut xml_reader, &mut xml_writer, &mut reader_buf, &mut storage, &hostname) => {
|
||||
match authenticated {
|
||||
Ok(authenticated) => {
|
||||
let mut connection = players.connect_to_player(authenticated.player_id.clone()).await;
|
||||
let mut connection = core.players.connect_to_player(&authenticated.player_id).await;
|
||||
socket_final(
|
||||
&mut xml_reader,
|
||||
&mut xml_writer,
|
||||
&mut reader_buf,
|
||||
&authenticated,
|
||||
&mut connection,
|
||||
&rooms,
|
||||
&core.rooms,
|
||||
&hostname,
|
||||
)
|
||||
.await?;
|
||||
},
|
||||
|
@ -216,7 +228,7 @@ async fn handle_socket(
|
|||
}
|
||||
|
||||
let a = xml_reader.into_inner().into_inner();
|
||||
let b = xml_writer.into_inner();
|
||||
let b = xml_writer.into_inner().into_inner();
|
||||
a.unsplit(b).shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -225,17 +237,18 @@ async fn socket_force_tls(
|
|||
reader: &mut (impl AsyncBufRead + Unpin),
|
||||
writer: &mut (impl AsyncWrite + Unpin),
|
||||
reader_buf: &mut Vec<u8>,
|
||||
hostname: &Str,
|
||||
) -> Result<()> {
|
||||
use proto_xmpp::tls::*;
|
||||
let xml_reader = &mut NsReader::from_reader(reader);
|
||||
let xml_writer = &mut Writer::new(writer);
|
||||
read_xml_header(xml_reader, reader_buf).await?;
|
||||
// TODO validate the server hostname received in the stream start
|
||||
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
|
||||
|
||||
let event = Event::Decl(BytesDecl::new("1.0", None, None));
|
||||
xml_writer.write_event_async(event).await?;
|
||||
let msg = ServerStreamStart {
|
||||
from: "localhost".into(),
|
||||
from: hostname.to_string(),
|
||||
lang: "en".into(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
version: "1.0".into(),
|
||||
|
@ -260,13 +273,14 @@ async fn socket_auth(
|
|||
xml_writer: &mut Writer<(impl AsyncWrite + Unpin)>,
|
||||
reader_buf: &mut Vec<u8>,
|
||||
storage: &mut Storage,
|
||||
hostname: &Str,
|
||||
) -> Result<Authenticated> {
|
||||
read_xml_header(xml_reader, reader_buf).await?;
|
||||
// TODO validate the server hostname received in the stream start
|
||||
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
|
||||
|
||||
xml_writer.write_event_async(Event::Decl(BytesDecl::new("1.0", None, None))).await?;
|
||||
ServerStreamStart {
|
||||
from: "localhost".into(),
|
||||
from: hostname.to_string(),
|
||||
lang: "en".into(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
version: "1.0".into(),
|
||||
|
@ -283,36 +297,28 @@ async fn socket_auth(
|
|||
xml_writer.get_mut().flush().await?;
|
||||
|
||||
let auth: proto_xmpp::sasl::Auth = proto_xmpp::sasl::Auth::parse(xml_reader, reader_buf).await?;
|
||||
proto_xmpp::sasl::Success.write_xml(xml_writer).await?;
|
||||
|
||||
match AuthBody::from_str(&auth.body) {
|
||||
Ok(logopass) => {
|
||||
let name = &logopass.login;
|
||||
let stored_user = storage.retrieve_user_by_name(name).await?;
|
||||
|
||||
let stored_user = match stored_user {
|
||||
Some(u) => u,
|
||||
None => {
|
||||
log::info!("User '{}' not found", name);
|
||||
return Err(fail("no user found"));
|
||||
let verdict = Authenticator::new(storage).authenticate(name, &logopass.password).await?;
|
||||
match verdict {
|
||||
Verdict::Authenticated => {
|
||||
proto_xmpp::sasl::Success.write_xml(xml_writer).await?;
|
||||
xml_writer.get_mut().flush().await?;
|
||||
}
|
||||
Verdict::UserNotFound | Verdict::InvalidPassword => {
|
||||
proto_xmpp::sasl::Failure.write_xml(xml_writer).await?;
|
||||
xml_writer.get_mut().flush().await?;
|
||||
return Err(anyhow!("incorrect credentials"));
|
||||
}
|
||||
};
|
||||
// TODO return proper XML errors to the client
|
||||
|
||||
if stored_user.password.is_none() {
|
||||
log::info!("Password not defined for user '{}'", name);
|
||||
return Err(fail("password is not defined"));
|
||||
}
|
||||
if stored_user.password.as_deref() != Some(&logopass.password) {
|
||||
log::info!("Incorrect password supplied for user '{}'", name);
|
||||
return Err(fail("passwords do not match"));
|
||||
}
|
||||
|
||||
let name: Str = name.as_str().into();
|
||||
Ok(Authenticated {
|
||||
player_id: PlayerId::from(name.as_str())?,
|
||||
xmpp_name: Name(name.to_string().into()),
|
||||
xmpp_resource: Resource(name.to_string().into()),
|
||||
xmpp_muc_name: Resource(name.to_string().into()),
|
||||
player_id: PlayerId::from(name.clone())?,
|
||||
xmpp_name: Name(name.clone()),
|
||||
xmpp_resource: Resource(name.clone()),
|
||||
xmpp_muc_name: Resource(name.clone()),
|
||||
})
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
|
@ -326,13 +332,14 @@ async fn socket_final(
|
|||
authenticated: &Authenticated,
|
||||
user_handle: &mut PlayerConnection,
|
||||
rooms: &RoomRegistry,
|
||||
hostname: &Str,
|
||||
) -> Result<()> {
|
||||
read_xml_header(xml_reader, reader_buf).await?;
|
||||
// TODO validate the server hostname received in the stream start
|
||||
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
|
||||
|
||||
xml_writer.write_event_async(Event::Decl(BytesDecl::new("1.0", None, None))).await?;
|
||||
ServerStreamStart {
|
||||
from: "localhost".into(),
|
||||
from: hostname.to_string(),
|
||||
lang: "en".into(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
version: "1.0".into(),
|
||||
|
@ -358,13 +365,15 @@ async fn socket_final(
|
|||
user: authenticated,
|
||||
user_handle,
|
||||
rooms,
|
||||
hostname: hostname.clone(),
|
||||
hostname_rooms: format!("rooms.{}", hostname).into(),
|
||||
};
|
||||
let should_recreate_xml_future = select! {
|
||||
biased;
|
||||
res = &mut next_xml_event => 's: {
|
||||
let (ns, event) = res?;
|
||||
if let Event::Text(ref e) = event {
|
||||
if e.iter().all(|x| *x == 0xA) {
|
||||
if e.iter().all(|x| *x == b'\n' || *x == b' ') {
|
||||
break 's true;
|
||||
}
|
||||
}
|
||||
|
@ -387,16 +396,41 @@ async fn socket_final(
|
|||
true
|
||||
},
|
||||
update = conn.user_handle.receiver.recv() => {
|
||||
if let Some(update) = update {
|
||||
conn.handle_update(&mut events, update).await?;
|
||||
for i in &events {
|
||||
xml_writer.write_event_async(i).await?;
|
||||
match update {
|
||||
Some(ConnectionMessage::Update(update)) => {
|
||||
conn.handle_update(&mut events, update).await?;
|
||||
for i in &events {
|
||||
xml_writer.write_event_async(i).await?;
|
||||
}
|
||||
events.clear();
|
||||
xml_writer.get_mut().flush().await?;
|
||||
}
|
||||
Some(ConnectionMessage::Stop(reason)) => {
|
||||
tracing::debug!("Connection is being terminated: {reason:?}");
|
||||
let kind = match reason {
|
||||
StopReason::ServerShutdown => StreamErrorKind::SystemShutdown,
|
||||
StopReason::InternalError => StreamErrorKind::InternalServerError,
|
||||
};
|
||||
StreamError { kind }.serialize(&mut events);
|
||||
ServerStreamEnd.serialize(&mut events);
|
||||
for i in &events {
|
||||
xml_writer.write_event_async(i).await?;
|
||||
}
|
||||
events.clear();
|
||||
xml_writer.get_mut().flush().await?;
|
||||
break;
|
||||
}
|
||||
None => {
|
||||
log::error!("Player is terminated, must terminate the connection");
|
||||
StreamError { kind: StreamErrorKind::SystemShutdown }.serialize(&mut events);
|
||||
ServerStreamEnd.serialize(&mut events);
|
||||
for i in &events {
|
||||
xml_writer.write_event_async(i).await?;
|
||||
}
|
||||
events.clear();
|
||||
xml_writer.get_mut().flush().await?;
|
||||
break;
|
||||
}
|
||||
events.clear();
|
||||
xml_writer.get_mut().flush().await?;
|
||||
} else {
|
||||
log::warn!("Player is terminated, must terminate the connection");
|
||||
break;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
@ -414,12 +448,15 @@ struct XmppConnection<'a> {
|
|||
user: &'a Authenticated,
|
||||
user_handle: &'a mut PlayerConnection,
|
||||
rooms: &'a RoomRegistry,
|
||||
hostname: Str,
|
||||
hostname_rooms: Str,
|
||||
}
|
||||
|
||||
impl<'a> XmppConnection<'a> {
|
||||
#[tracing::instrument(skip(self, output, packet), name = "XmppConnection::handle_packet")]
|
||||
async fn handle_packet(&mut self, output: &mut Vec<Event<'static>>, packet: ClientPacket) -> Result<bool> {
|
||||
let res = match packet {
|
||||
proto::ClientPacket::Iq(iq) => {
|
||||
ClientPacket::Iq(iq) => {
|
||||
self.handle_iq(output, iq).await;
|
||||
false
|
||||
}
|
||||
|
@ -427,37 +464,16 @@ impl<'a> XmppConnection<'a> {
|
|||
self.handle_message(output, m).await?;
|
||||
false
|
||||
}
|
||||
proto::ClientPacket::Presence(p) => {
|
||||
ClientPacket::Presence(p) => {
|
||||
self.handle_presence(output, p).await?;
|
||||
false
|
||||
}
|
||||
proto::ClientPacket::StreamEnd => {
|
||||
ClientPacket::StreamEnd => {
|
||||
ServerStreamEnd.serialize(output);
|
||||
true
|
||||
}
|
||||
ClientPacket::Eos => true,
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_xml_header(
|
||||
xml_reader: &mut NsReader<(impl AsyncBufRead + Unpin)>,
|
||||
reader_buf: &mut Vec<u8>,
|
||||
) -> Result<()> {
|
||||
if let Event::Decl(bytes) = xml_reader.read_event_into_async(reader_buf).await? {
|
||||
// this is <?xml ...> header
|
||||
if let Some(encoding) = bytes.encoding() {
|
||||
let encoding = encoding?;
|
||||
if &*encoding == b"UTF-8" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Unsupported encoding: {encoding:?}"))
|
||||
}
|
||||
} else {
|
||||
// Err(fail("No XML encoding provided"))
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("Expected XML header"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
//! Handling of all client2server message stanzas
|
||||
|
||||
use lavina_core::player::PlayerId;
|
||||
use quick_xml::events::Event;
|
||||
|
||||
use lavina_core::prelude::*;
|
||||
|
@ -18,17 +19,17 @@ impl<'a> XmppConnection<'a> {
|
|||
resource: _,
|
||||
}) = m.to
|
||||
{
|
||||
if server.0.as_ref() == "rooms.localhost" && m.r#type == MessageType::Groupchat {
|
||||
if server.0.as_ref() == &*self.hostname_rooms && m.r#type == MessageType::Groupchat {
|
||||
self.user_handle.send_message(RoomId::from(name.0.clone())?, m.body.clone().into()).await?;
|
||||
Message::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server("localhost".into()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(name),
|
||||
server: Server("rooms.localhost".into()),
|
||||
server: Server(self.hostname_rooms.clone()),
|
||||
resource: Some(self.user.xmpp_muc_name.clone()),
|
||||
}),
|
||||
id: m.id,
|
||||
|
@ -40,6 +41,9 @@ impl<'a> XmppConnection<'a> {
|
|||
}
|
||||
.serialize(output);
|
||||
Ok(())
|
||||
} else if server.0.as_ref() == &*self.hostname && m.r#type == MessageType::Chat {
|
||||
self.user_handle.send_dialog_message(PlayerId::from(name.0.clone())?, m.body.clone()).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use quick_xml::events::Event;
|
|||
|
||||
use lavina_core::prelude::*;
|
||||
use lavina_core::room::RoomId;
|
||||
use proto_xmpp::bind::{Jid, Server};
|
||||
use proto_xmpp::bind::{Jid, Name, Server};
|
||||
use proto_xmpp::client::Presence;
|
||||
use proto_xmpp::xml::{Ignore, ToXml};
|
||||
|
||||
|
@ -12,42 +12,67 @@ use crate::XmppConnection;
|
|||
|
||||
impl<'a> XmppConnection<'a> {
|
||||
pub async fn handle_presence(&mut self, output: &mut Vec<Event<'static>>, p: Presence<Ignore>) -> Result<()> {
|
||||
let response = if p.to.is_none() {
|
||||
Presence::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server("localhost".into()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server("localhost".into()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
..Default::default()
|
||||
match p.to {
|
||||
None => {
|
||||
self.self_presence(output, p.r#type.as_deref()).await;
|
||||
}
|
||||
} else if let Some(Jid {
|
||||
name: Some(name),
|
||||
server,
|
||||
resource: Some(resource),
|
||||
}) = p.to
|
||||
{
|
||||
let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?;
|
||||
Presence::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server("localhost".into()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(name.clone()),
|
||||
server: Server("rooms.localhost".into()),
|
||||
resource: Some(self.user.xmpp_muc_name.clone()),
|
||||
}),
|
||||
..Default::default()
|
||||
Some(Jid {
|
||||
name: Some(name),
|
||||
server,
|
||||
// resources in MUCs are members' personas – not implemented (yet?)
|
||||
resource: Some(_),
|
||||
}) if server.0 == self.hostname_rooms => {
|
||||
self.muc_presence(name, output).await?;
|
||||
}
|
||||
} else {
|
||||
Presence::<()>::default()
|
||||
_ => {
|
||||
// TODO other presence cases
|
||||
let response = Presence::<()>::default();
|
||||
response.serialize(output);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn self_presence(&mut self, output: &mut Vec<Event<'static>>, r#type: Option<&str>) {
|
||||
match r#type {
|
||||
Some("unavailable") => {
|
||||
// do not print anything
|
||||
}
|
||||
None => {
|
||||
let response = Presence::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
response.serialize(output);
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn muc_presence(&mut self, name: Name, output: &mut Vec<Event<'static>>) -> Result<()> {
|
||||
let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?;
|
||||
// TODO handle bans
|
||||
let response = Presence::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(name.clone()),
|
||||
server: Server(self.hostname_rooms.clone()),
|
||||
resource: Some(self.user.xmpp_muc_name.clone()),
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
response.serialize(output);
|
||||
Ok(())
|
||||
|
|
|
@ -25,7 +25,7 @@ impl FromXml for IqClientBody {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let bytes = match event {
|
||||
Event::Start(bytes) => bytes,
|
||||
Event::Empty(bytes) => bytes,
|
||||
|
@ -52,13 +52,14 @@ pub enum ClientPacket {
|
|||
Message(Message<Ignore>),
|
||||
Presence(Presence<Ignore>),
|
||||
StreamEnd,
|
||||
Eos,
|
||||
}
|
||||
|
||||
impl FromXml for ClientPacket {
|
||||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
match event {
|
||||
Event::Start(bytes) | Event::Empty(bytes) => {
|
||||
let name = bytes.name();
|
||||
|
@ -83,6 +84,7 @@ impl FromXml for ClientPacket {
|
|||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
}
|
||||
}
|
||||
Event::Eof => Ok(ClientPacket::Eos),
|
||||
_ => {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
}
|
||||
|
|
|
@ -17,16 +17,17 @@ impl<'a> XmppConnection<'a> {
|
|||
room_id,
|
||||
author_id,
|
||||
body,
|
||||
created_at: _,
|
||||
} => {
|
||||
Message::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server("localhost".into()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(Name(room_id.into_inner().into())),
|
||||
server: Server("rooms.localhost".into()),
|
||||
server: Server(self.hostname_rooms.clone()),
|
||||
resource: Some(Resource(author_id.into_inner().into())),
|
||||
}),
|
||||
id: None,
|
||||
|
@ -38,6 +39,34 @@ impl<'a> XmppConnection<'a> {
|
|||
}
|
||||
.serialize(output);
|
||||
}
|
||||
Updates::NewDialogMessage {
|
||||
sender,
|
||||
receiver,
|
||||
body,
|
||||
created_at: _,
|
||||
} => {
|
||||
if receiver == self.user.player_id {
|
||||
Message::<()> {
|
||||
to: Some(Jid {
|
||||
name: Some(self.user.xmpp_name.clone()),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(self.user.xmpp_resource.clone()),
|
||||
}),
|
||||
from: Some(Jid {
|
||||
name: Some(Name(sender.as_inner().clone())),
|
||||
server: Server(self.hostname.clone()),
|
||||
resource: Some(Resource(sender.into_inner())),
|
||||
}),
|
||||
id: None,
|
||||
r#type: MessageType::Chat,
|
||||
lang: None,
|
||||
subject: None,
|
||||
body: body.into(),
|
||||
custom: vec![],
|
||||
}
|
||||
.serialize(output);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use std::io::ErrorKind;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
|
@ -17,10 +16,10 @@ use tokio_rustls::rustls::client::ServerCertVerifier;
|
|||
use tokio_rustls::rustls::{ClientConfig, ServerName};
|
||||
use tokio_rustls::TlsConnector;
|
||||
|
||||
use lavina_core::player::PlayerRegistry;
|
||||
use lavina_core::auth::Authenticator;
|
||||
use lavina_core::repo::{Storage, StorageConfig};
|
||||
use lavina_core::room::RoomRegistry;
|
||||
use projection_xmpp::{launch, ServerConfig};
|
||||
use lavina_core::LavinaCore;
|
||||
use projection_xmpp::{launch, RunningServer, ServerConfig};
|
||||
use proto_xmpp::xml::{Continuation, FromXml, Parser};
|
||||
|
||||
pub async fn read_irc_message(reader: &mut BufReader<ReadHalf<'_>>, buf: &mut Vec<u8>) -> Result<usize> {
|
||||
|
@ -83,7 +82,7 @@ impl<'a> TestScopeTls<'a> {
|
|||
fn new(stream: &'a mut TlsStream<TcpStream>, buffer: Vec<u8>) -> TestScopeTls<'a> {
|
||||
let (reader, writer) = tokio::io::split(stream);
|
||||
let reader = NsReader::from_reader(BufReader::new(reader));
|
||||
let timeout = Duration::from_millis(100);
|
||||
let timeout = Duration::from_millis(500);
|
||||
|
||||
TestScopeTls {
|
||||
reader,
|
||||
|
@ -122,29 +121,54 @@ impl ServerCertVerifier for IgnoreCertVerification {
|
|||
}
|
||||
}
|
||||
|
||||
struct TestServer {
|
||||
metrics: MetricsRegistry,
|
||||
storage: Storage,
|
||||
core: LavinaCore,
|
||||
server: RunningServer,
|
||||
}
|
||||
impl TestServer {
|
||||
async fn start() -> Result<TestServer> {
|
||||
let _ = tracing_subscriber::fmt::try_init();
|
||||
let config = ServerConfig {
|
||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||
cert: "tests/certs/xmpp.pem".parse().unwrap(),
|
||||
key: "tests/certs/xmpp.key".parse().unwrap(),
|
||||
hostname: "localhost".into(),
|
||||
};
|
||||
let metrics = MetricsRegistry::new();
|
||||
let storage = Storage::open(StorageConfig {
|
||||
db_path: ":memory:".into(),
|
||||
})
|
||||
.await?;
|
||||
let core = LavinaCore::new(metrics.clone(), storage.clone()).await?;
|
||||
let server = launch(config, core.clone(), metrics.clone(), storage.clone()).await.unwrap();
|
||||
Ok(TestServer {
|
||||
metrics,
|
||||
storage,
|
||||
core,
|
||||
server,
|
||||
})
|
||||
}
|
||||
|
||||
async fn shutdown(self) -> Result<()> {
|
||||
self.server.terminate().await?;
|
||||
self.core.shutdown().await?;
|
||||
self.storage.close().await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_basic() -> Result<()> {
|
||||
tracing_subscriber::fmt::try_init();
|
||||
let config = ServerConfig {
|
||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||
cert: "tests/certs/xmpp.pem".parse().unwrap(),
|
||||
key: "tests/certs/xmpp.key".parse().unwrap(),
|
||||
};
|
||||
let mut metrics = MetricsRegistry::new();
|
||||
let mut storage = Storage::open(StorageConfig {
|
||||
db_path: ":memory:".into(),
|
||||
})
|
||||
.await?;
|
||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
||||
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
||||
let server = launch(config, players, rooms, metrics, storage.clone()).await.unwrap();
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
storage.create_user("tester").await?;
|
||||
storage.set_password("tester", "password").await?;
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.addr).await?;
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
tracing::info!("TCP connection established");
|
||||
|
||||
|
@ -169,7 +193,7 @@ async fn scenario_basic() -> Result<()> {
|
|||
.with_no_client_auth(),
|
||||
));
|
||||
tracing::info!("Initiating TLS connection...");
|
||||
let mut stream = connector.connect(ServerName::IpAddress(server.addr.ip()), stream).await?;
|
||||
let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
|
||||
tracing::info!("TLS connection established");
|
||||
|
||||
let mut s = TestScopeTls::new(&mut stream, buffer);
|
||||
|
@ -179,37 +203,170 @@ async fn scenario_basic() -> Result<()> {
|
|||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanism"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"PLAIN"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanism"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
|
||||
// base64-encoded b"\x00tester\x00password"
|
||||
s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZA==</auth>"#)
|
||||
.await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"success"));
|
||||
s.send(r#"<?xml version="1.0"?>"#).await?;
|
||||
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"bind"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
s.send(r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>kek</resource></bind></iq>"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"iq"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"bind"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"jid"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"jid"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"bind"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"iq"));
|
||||
s.send(r#"<presence xmlns="jabber:client" type="unavailable"><status>Logged out</status></presence>"#).await?;
|
||||
|
||||
stream.shutdown().await?;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_wrong_password() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
tracing::info!("TCP connection established");
|
||||
|
||||
s.send(r#"<?xml version="1.0"?>"#).await?;
|
||||
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"starttls"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"required"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"starttls"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
s.send(r#"<starttls/>"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed"));
|
||||
let buffer = s.buffer;
|
||||
tracing::info!("TLS feature negotiation complete");
|
||||
|
||||
let connector = TlsConnector::from(Arc::new(
|
||||
ClientConfig::builder()
|
||||
.with_safe_defaults()
|
||||
.with_custom_certificate_verifier(Arc::new(IgnoreCertVerification))
|
||||
.with_no_client_auth(),
|
||||
));
|
||||
tracing::info!("Initiating TLS connection...");
|
||||
let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
|
||||
tracing::info!("TLS connection established");
|
||||
|
||||
let mut s = TestScopeTls::new(&mut stream, buffer);
|
||||
|
||||
s.send(r#"<?xml version="1.0"?>"#).await?;
|
||||
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanism"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"PLAIN"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanism"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
|
||||
// base64-encoded b"\x00tester\x00password2"
|
||||
s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZDI=</auth>"#)
|
||||
.await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"failure"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"not-authorized"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"failure"));
|
||||
|
||||
let _ = stream.shutdown().await;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scenario_basic_without_headers() -> Result<()> {
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
tracing::info!("TCP connection established");
|
||||
|
||||
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"starttls"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"required"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"starttls"));
|
||||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
s.send(r#"<starttls/>"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed"));
|
||||
let buffer = s.buffer;
|
||||
tracing::info!("TLS feature negotiation complete");
|
||||
|
||||
let connector = TlsConnector::from(Arc::new(
|
||||
ClientConfig::builder()
|
||||
.with_safe_defaults()
|
||||
.with_custom_certificate_verifier(Arc::new(IgnoreCertVerification))
|
||||
.with_no_client_auth(),
|
||||
));
|
||||
tracing::info!("Initiating TLS connection...");
|
||||
let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
|
||||
tracing::info!("TLS connection established");
|
||||
|
||||
let mut s = TestScopeTls::new(&mut stream, buffer);
|
||||
|
||||
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||
|
||||
stream.shutdown().await?;
|
||||
|
||||
// wrap up
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn terminate_socket() -> Result<()> {
|
||||
tracing_subscriber::fmt::try_init();
|
||||
let config = ServerConfig {
|
||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||
cert: "tests/certs/xmpp.pem".parse().unwrap(),
|
||||
key: "tests/certs/xmpp.key".parse().unwrap(),
|
||||
};
|
||||
let mut metrics = MetricsRegistry::new();
|
||||
let mut storage = Storage::open(StorageConfig {
|
||||
db_path: ":memory:".into(),
|
||||
})
|
||||
.await?;
|
||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone()).unwrap();
|
||||
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics).unwrap();
|
||||
let server = launch(config, players, rooms, metrics, storage.clone()).await.unwrap();
|
||||
let address: SocketAddr = ("127.0.0.1:0".parse().unwrap());
|
||||
let mut server = TestServer::start().await?;
|
||||
|
||||
// test scenario
|
||||
|
||||
storage.create_user("tester").await?;
|
||||
storage.set_password("tester", "password").await?;
|
||||
server.storage.create_user("tester").await?;
|
||||
Authenticator::new(&server.storage).set_password("tester", "password").await?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.addr).await?;
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
tracing::info!("TCP connection established");
|
||||
|
||||
|
@ -225,7 +382,6 @@ async fn terminate_socket() -> Result<()> {
|
|||
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||
s.send(r#"<starttls/>"#).await?;
|
||||
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed"));
|
||||
let buffer = s.buffer;
|
||||
|
||||
let connector = TlsConnector::from(Arc::new(
|
||||
ClientConfig::builder()
|
||||
|
@ -235,10 +391,10 @@ async fn terminate_socket() -> Result<()> {
|
|||
));
|
||||
|
||||
tracing::info!("Initiating TLS connection...");
|
||||
let mut stream = connector.connect(ServerName::IpAddress(server.addr.ip()), stream).await?;
|
||||
let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?;
|
||||
tracing::info!("TLS connection established");
|
||||
|
||||
server.terminate().await?;
|
||||
server.shutdown().await?;
|
||||
|
||||
assert_eq!(stream.read_u8().await.unwrap_err().kind(), ErrorKind::UnexpectedEof);
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ pub enum ClientMessage {
|
|||
},
|
||||
Part {
|
||||
chan: Chan,
|
||||
message: Str,
|
||||
message: Option<Str>,
|
||||
},
|
||||
/// `PRIVMSG <target> :<msg>`
|
||||
PrivateMessage {
|
||||
|
@ -235,14 +235,20 @@ fn client_message_topic(input: &str) -> IResult<&str, ClientMessage> {
|
|||
fn client_message_part(input: &str) -> IResult<&str, ClientMessage> {
|
||||
let (input, _) = tag("PART ")(input)?;
|
||||
let (input, chan) = chan(input)?;
|
||||
let (input, _) = tag(" ")(input)?;
|
||||
let (input, t) = opt(tag(" "))(input)?;
|
||||
match t {
|
||||
Some(_) => (),
|
||||
None => {
|
||||
return Ok((input, ClientMessage::Part { chan, message: None }));
|
||||
}
|
||||
}
|
||||
let (input, r) = opt(tag(":"))(input)?;
|
||||
let (input, message) = match r {
|
||||
Some(_) => token(input)?,
|
||||
None => receiver(input)?,
|
||||
};
|
||||
|
||||
let message = message.into();
|
||||
let message = Some(message.into());
|
||||
Ok((input, ClientMessage::Part { chan, message }))
|
||||
}
|
||||
|
||||
|
@ -471,7 +477,18 @@ mod test {
|
|||
let input = "PART #chan :Pokasiki !!!";
|
||||
let expected = ClientMessage::Part {
|
||||
chan: Chan::Global("chan".into()),
|
||||
message: "Pokasiki !!!".into(),
|
||||
message: Some("Pokasiki !!!".into()),
|
||||
};
|
||||
|
||||
let result = client_message(input);
|
||||
assert_matches!(result, Ok(result) => assert_eq!(expected, result));
|
||||
}
|
||||
#[test]
|
||||
fn test_client_message_part_empty() {
|
||||
let input = "PART #chan";
|
||||
let expected = ClientMessage::Part {
|
||||
chan: Chan::Global("chan".into()),
|
||||
message: None,
|
||||
};
|
||||
|
||||
let result = client_message(input);
|
||||
|
|
|
@ -19,8 +19,19 @@ use tokio::io::{AsyncWrite, AsyncWriteExt};
|
|||
/// Single message tag value.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Tag {
|
||||
key: Str,
|
||||
value: Option<u8>,
|
||||
pub key: Str,
|
||||
pub value: Option<Str>,
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
pub async fn write_async(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
|
||||
writer.write_all(self.key.as_bytes()).await?;
|
||||
if let Some(value) = &self.value {
|
||||
writer.write_all(b"=").await?;
|
||||
writer.write_all(value.as_bytes()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn receiver(input: &str) -> IResult<&str, &str> {
|
||||
|
|
|
@ -18,6 +18,13 @@ pub struct ServerMessage {
|
|||
|
||||
impl ServerMessage {
|
||||
pub async fn write_async(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
|
||||
if !self.tags.is_empty() {
|
||||
for tag in &self.tags {
|
||||
writer.write_all(b"@").await?;
|
||||
tag.write_async(writer).await?;
|
||||
writer.write_all(b" ").await?;
|
||||
}
|
||||
}
|
||||
match &self.sender {
|
||||
Some(ref sender) => {
|
||||
writer.write_all(b":").await?;
|
||||
|
@ -334,10 +341,15 @@ impl ServerMessageBody {
|
|||
writer.write_all(b" = ").await?;
|
||||
chan.write_async(writer).await?;
|
||||
writer.write_all(b" :").await?;
|
||||
for member in members {
|
||||
{
|
||||
let member = &members.head;
|
||||
writer.write_all(member.prefix.to_string().as_bytes()).await?;
|
||||
writer.write_all(member.nick.as_bytes()).await?;
|
||||
}
|
||||
for member in &members.tail {
|
||||
writer.write_all(b" ").await?;
|
||||
writer.write_all(member.prefix.to_string().as_bytes()).await?;
|
||||
writer.write_all(member.nick.as_bytes()).await?;
|
||||
}
|
||||
}
|
||||
ServerMessageBody::N366NamesReplyEnd { client, chan } => {
|
||||
|
|
|
@ -11,12 +11,15 @@ pub const XMLNS: &'static str = "urn:ietf:params:xml:ns:xmpp-bind";
|
|||
|
||||
// TODO remove `pub` in newtypes, introduce validation
|
||||
|
||||
/// Name (node identifier) of an XMPP entity. Placed before the `@` in a JID.
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub struct Name(pub Str);
|
||||
|
||||
/// Server name of an XMPP entity. Placed after the `@` and before the `/` in a JID.
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub struct Server(pub Str);
|
||||
|
||||
/// Resource of an XMPP entity. Placed after the `/` in a JID.
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub struct Resource(pub Str);
|
||||
|
||||
|
@ -45,7 +48,7 @@ impl Jid {
|
|||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
lazy_static! {
|
||||
static ref RE: Regex = Regex::new(r"^(([a-zA-Z]+)@)?([a-zA-Z.]+)(/([a-zA-Z\-]+))?$").unwrap();
|
||||
static ref RE: Regex = Regex::new(r"^(([a-zA-Z0-9]+)@)?([^@/]+)(/([a-zA-Z0-9\-]+))?$").unwrap();
|
||||
}
|
||||
let m = RE.captures(i).ok_or(anyhow!("Incorrectly format jid: {i}"))?;
|
||||
|
||||
|
@ -79,7 +82,7 @@ impl FromXml for BindRequest {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut resource: Option<Str> = None;
|
||||
let Event::Start(bytes) = event else {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
|
@ -94,15 +97,15 @@ impl FromXml for BindRequest {
|
|||
return Err(anyhow!("Incorrect namespace"));
|
||||
}
|
||||
loop {
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
match event {
|
||||
Event::Start(bytes) if bytes.name().0 == b"resource" => {
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::Text(text) = event else {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
resource = Some(std::str::from_utf8(&*text)?.into());
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::End(bytes) = event else {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
|
|
|
@ -255,11 +255,49 @@ impl MessageType {
|
|||
}
|
||||
}
|
||||
|
||||
/// Error response to an IQ request.
|
||||
///
|
||||
/// https://xmpp.org/rfcs/rfc6120.html#stanzas-error
|
||||
pub struct IqError {
|
||||
pub r#type: IqErrorType,
|
||||
}
|
||||
|
||||
pub enum IqErrorType {
|
||||
/// Retry after providing credentials
|
||||
Auth,
|
||||
/// Do not retry (the error cannot be remedied)
|
||||
Cancel,
|
||||
/// Proceed (the condition was only a warning)
|
||||
Continue,
|
||||
/// Retry after changing the data sent
|
||||
Modify,
|
||||
/// Retry after waiting (the error is temporary)
|
||||
Wait,
|
||||
}
|
||||
impl IqErrorType {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
IqErrorType::Auth => "auth",
|
||||
IqErrorType::Cancel => "cancel",
|
||||
IqErrorType::Continue => "continue",
|
||||
IqErrorType::Modify => "modify",
|
||||
IqErrorType::Wait => "wait",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToXml for IqError {
|
||||
fn serialize(&self, events: &mut Vec<Event<'static>>) {
|
||||
let bytes = BytesStart::new(format!(r#"error xmlns="{}" type="{}""#, XMLNS, self.r#type.as_str()));
|
||||
events.push(Event::Empty(bytes));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub struct Iq<T> {
|
||||
pub from: Option<String>,
|
||||
pub from: Option<Jid>,
|
||||
pub id: String,
|
||||
pub to: Option<String>,
|
||||
pub to: Option<Jid>,
|
||||
pub r#type: IqType,
|
||||
pub body: T,
|
||||
}
|
||||
|
@ -285,9 +323,9 @@ enum IqParserInner<T: FromXml> {
|
|||
Final(IqParserState<T>),
|
||||
}
|
||||
struct IqParserState<T> {
|
||||
pub from: Option<String>,
|
||||
pub from: Option<Jid>,
|
||||
pub id: Option<String>,
|
||||
pub to: Option<String>,
|
||||
pub to: Option<Jid>,
|
||||
pub r#type: Option<IqType>,
|
||||
pub body: Option<T>,
|
||||
}
|
||||
|
@ -310,13 +348,15 @@ impl<T: FromXml> Parser for IqParser<T> {
|
|||
let attr = fail_fast!(attr);
|
||||
if attr.key.0 == b"from" {
|
||||
let value = fail_fast!(std::str::from_utf8(&*attr.value));
|
||||
state.from = Some(value.to_string())
|
||||
let value = fail_fast!(Jid::from_string(value));
|
||||
state.from = Some(value)
|
||||
} else if attr.key.0 == b"id" {
|
||||
let value = fail_fast!(std::str::from_utf8(&*attr.value));
|
||||
state.id = Some(value.to_string())
|
||||
} else if attr.key.0 == b"to" {
|
||||
let value = fail_fast!(std::str::from_utf8(&*attr.value));
|
||||
state.to = Some(value.to_string())
|
||||
let value = fail_fast!(Jid::from_string(value));
|
||||
state.to = Some(value)
|
||||
} else if attr.key.0 == b"type" {
|
||||
let value = fail_fast!(IqType::from_str(&*attr.value));
|
||||
state.r#type = Some(value);
|
||||
|
@ -338,7 +378,7 @@ impl<T: FromXml> Parser for IqParser<T> {
|
|||
}
|
||||
},
|
||||
IqParserInner::Final(state) => {
|
||||
if let Event::End(ref bytes) = event {
|
||||
if let Event::End(_) = event {
|
||||
let id = fail_fast!(state.id.ok_or_else(|| ffail!("No id provided")));
|
||||
let r#type = fail_fast!(state.r#type.ok_or_else(|| ffail!("No type provided")));
|
||||
let body = fail_fast!(state.body.ok_or_else(|| ffail!("No body provided")));
|
||||
|
@ -393,15 +433,17 @@ impl<T: ToXml> ToXml for Iq<T> {
|
|||
let mut start = BytesStart::new(start);
|
||||
let mut attrs = vec![];
|
||||
if let Some(ref from) = self.from {
|
||||
let value = from.to_string().into_bytes();
|
||||
attrs.push(Attribute {
|
||||
key: QName(b"from"),
|
||||
value: from.as_bytes().into(),
|
||||
value: value.into(),
|
||||
});
|
||||
};
|
||||
if let Some(ref to) = self.to {
|
||||
let value = to.to_string().into_bytes();
|
||||
attrs.push(Attribute {
|
||||
key: QName(b"to"),
|
||||
value: to.as_bytes().into(),
|
||||
value: value.into(),
|
||||
});
|
||||
}
|
||||
attrs.push(Attribute {
|
||||
|
@ -486,7 +528,7 @@ impl<T: FromXml> FromXml for Presence<T> {
|
|||
type P = impl Parser<Output = Result<Presence<T>>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let (bytes, end) = match event {
|
||||
Event::Start(bytes) => (bytes, false),
|
||||
Event::Empty(bytes) => (bytes, true),
|
||||
|
@ -515,37 +557,37 @@ impl<T: FromXml> FromXml for Presence<T> {
|
|||
return Ok(p);
|
||||
}
|
||||
loop {
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
match event {
|
||||
Event::Start(bytes) => match bytes.name().0 {
|
||||
b"show" => {
|
||||
let (_, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::Text(bytes) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
let i = PresenceShow::from_str(bytes)?;
|
||||
p.show = Some(i);
|
||||
|
||||
let (_, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::End(_) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
}
|
||||
b"status" => {
|
||||
let (_, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::Text(bytes) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
let s = std::str::from_utf8(bytes)?;
|
||||
p.status.push(s.to_string());
|
||||
|
||||
let (_, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::End(_) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
}
|
||||
b"priority" => {
|
||||
let (_, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::Text(bytes) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
|
@ -553,7 +595,7 @@ impl<T: FromXml> FromXml for Presence<T> {
|
|||
let i = s.parse()?;
|
||||
p.priority = Some(PresencePriority(i));
|
||||
|
||||
let (_, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::End(_) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
|
|
|
@ -21,7 +21,7 @@ impl FromXml for InfoQuery {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut node = None;
|
||||
let mut identity = vec![];
|
||||
let mut feature = vec![];
|
||||
|
@ -48,7 +48,7 @@ impl FromXml for InfoQuery {
|
|||
});
|
||||
}
|
||||
loop {
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let bytes = match event {
|
||||
Event::Start(bytes) => bytes,
|
||||
Event::Empty(bytes) => bytes,
|
||||
|
@ -141,7 +141,7 @@ impl FromXml for Identity {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut category = None;
|
||||
let mut name = None;
|
||||
let mut r#type = None;
|
||||
|
@ -179,8 +179,8 @@ impl FromXml for Identity {
|
|||
return Ok(item);
|
||||
}
|
||||
|
||||
let (namespace, event) = yield;
|
||||
let Event::End(bytes) = event else {
|
||||
(namespace, event) = yield;
|
||||
let Event::End(_) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
Ok(item)
|
||||
|
@ -209,7 +209,7 @@ impl FromXml for Feature {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut var = None;
|
||||
let (bytes, end) = match event {
|
||||
Event::Start(bytes) => (bytes, false),
|
||||
|
@ -234,8 +234,8 @@ impl FromXml for Feature {
|
|||
return Ok(item);
|
||||
}
|
||||
|
||||
let (namespace, event) = yield;
|
||||
let Event::End(bytes) = event else {
|
||||
(namespace, event) = yield;
|
||||
let Event::End(_) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
Ok(item)
|
||||
|
@ -258,9 +258,9 @@ impl FromXml for ItemQuery {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut item = vec![];
|
||||
let (bytes, end) = match event {
|
||||
let (_, end) = match event {
|
||||
Event::Start(bytes) => (bytes, false),
|
||||
Event::Empty(bytes) => (bytes, true),
|
||||
_ => return Err(ffail!("Unexpected XML event: {event:?}")),
|
||||
|
@ -269,7 +269,7 @@ impl FromXml for ItemQuery {
|
|||
return Ok(ItemQuery { item });
|
||||
}
|
||||
loop {
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let bytes = match event {
|
||||
Event::Start(bytes) => bytes,
|
||||
Event::Empty(bytes) => bytes,
|
||||
|
@ -296,7 +296,7 @@ impl FromXmlTag for ItemQuery {
|
|||
|
||||
impl ToXml for ItemQuery {
|
||||
fn serialize(&self, events: &mut Vec<Event<'static>>) {
|
||||
let mut bytes = BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS_ITEM));
|
||||
let bytes = BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS_ITEM));
|
||||
let empty = self.item.is_empty();
|
||||
if empty {
|
||||
events.push(Event::Empty(bytes));
|
||||
|
@ -342,7 +342,7 @@ impl FromXml for Item {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(_, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut jid = None;
|
||||
let mut name = None;
|
||||
let mut node = None;
|
||||
|
@ -378,8 +378,8 @@ impl FromXml for Item {
|
|||
return Ok(item);
|
||||
}
|
||||
|
||||
let (namespace, event) = yield;
|
||||
let Event::End(bytes) = event else {
|
||||
(_, event) = yield;
|
||||
let Event::End(_) = event else {
|
||||
return Err(ffail!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
Ok(item)
|
||||
|
|
|
@ -10,6 +10,7 @@ pub mod sasl;
|
|||
pub mod session;
|
||||
pub mod stanzaerror;
|
||||
pub mod stream;
|
||||
pub mod streamerror;
|
||||
pub mod tls;
|
||||
pub mod xml;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ impl FromXml for History {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut history = History::default();
|
||||
let (bytes, end) = match event {
|
||||
Event::Start(bytes) if bytes.name().0 == Self::NAME.as_bytes() => (bytes, false),
|
||||
|
@ -51,7 +51,7 @@ impl FromXml for History {
|
|||
return Ok(history);
|
||||
}
|
||||
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::End(bytes) = event else {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
|
@ -73,17 +73,17 @@ impl FromXml for Password {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let bytes = match event {
|
||||
Event::Start(bytes) if bytes.name().0 == Self::NAME.as_bytes() => bytes,
|
||||
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
||||
};
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::Text(bytes) = event else {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
let s = std::str::from_utf8(bytes)?.to_string();
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let Event::End(bytes) = event else {
|
||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||
};
|
||||
|
@ -108,7 +108,7 @@ impl FromXml for X {
|
|||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut res = X::default();
|
||||
let (_, end) = match event {
|
||||
Event::Start(bytes) => (bytes, false),
|
||||
|
@ -120,7 +120,7 @@ impl FromXml for X {
|
|||
}
|
||||
|
||||
loop {
|
||||
let (namespace, event) = yield;
|
||||
(namespace, event) = yield;
|
||||
let bytes = match event {
|
||||
Event::Start(bytes) => bytes,
|
||||
Event::Empty(bytes) => bytes,
|
||||
|
|
|
@ -1,47 +1,30 @@
|
|||
use quick_xml::events::{BytesStart, Event};
|
||||
|
||||
use crate::xml::*;
|
||||
use anyhow::{anyhow as ffail, Result};
|
||||
use anyhow::{anyhow, Result};
|
||||
use quick_xml::name::ResolveResult;
|
||||
|
||||
pub const XMLNS: &'static str = "jabber:iq:roster";
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub struct RosterQuery;
|
||||
|
||||
pub struct QueryParser(QueryParserInner);
|
||||
|
||||
enum QueryParserInner {
|
||||
Initial,
|
||||
InQuery,
|
||||
}
|
||||
|
||||
impl Parser for QueryParser {
|
||||
type Output = Result<RosterQuery>;
|
||||
|
||||
fn consume<'a>(
|
||||
self: Self,
|
||||
namespace: quick_xml::name::ResolveResult,
|
||||
event: &quick_xml::events::Event<'a>,
|
||||
) -> Continuation<Self, Self::Output> {
|
||||
match self.0 {
|
||||
QueryParserInner::Initial => match event {
|
||||
Event::Start(_) => Continuation::Continue(QueryParser(QueryParserInner::InQuery)),
|
||||
Event::Empty(_) => Continuation::Final(Ok(RosterQuery)),
|
||||
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
|
||||
},
|
||||
QueryParserInner::InQuery => match event {
|
||||
Event::End(_) => Continuation::Final(Ok(RosterQuery)),
|
||||
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromXml for RosterQuery {
|
||||
type P = QueryParser;
|
||||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
QueryParser(QueryParserInner::Initial)
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
match event {
|
||||
Event::Start(_) => (),
|
||||
Event::Empty(_) => return Ok(RosterQuery),
|
||||
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
||||
}
|
||||
(namespace, event) = yield;
|
||||
match event {
|
||||
Event::End(_) => return Ok(RosterQuery),
|
||||
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::borrow::Borrow;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use quick_xml::events::{BytesStart, Event};
|
||||
use quick_xml::events::{BytesEnd, BytesStart, Event};
|
||||
use quick_xml::{NsReader, Writer};
|
||||
use tokio::io::{AsyncBufRead, AsyncWrite};
|
||||
|
||||
|
@ -74,3 +74,16 @@ impl Success {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Failure;
|
||||
impl Failure {
|
||||
pub async fn write_xml(&self, writer: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()> {
|
||||
let event = BytesStart::new(r#"failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl""#);
|
||||
writer.write_event_async(Event::Start(event)).await?;
|
||||
let event = BytesStart::new(r#"not-authorized"#);
|
||||
writer.write_event_async(Event::Empty(event)).await?;
|
||||
let event = BytesEnd::new(r#"failure"#);
|
||||
writer.write_event_async(Event::End(event)).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,46 +2,29 @@ use quick_xml::events::{BytesStart, Event};
|
|||
|
||||
use crate::xml::*;
|
||||
use anyhow::{anyhow, Result};
|
||||
use quick_xml::name::ResolveResult;
|
||||
|
||||
pub const XMLNS: &'static str = "urn:ietf:params:xml:ns:xmpp-session";
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub struct Session;
|
||||
|
||||
pub struct SessionParser(SessionParserInner);
|
||||
|
||||
enum SessionParserInner {
|
||||
Initial,
|
||||
InSession,
|
||||
}
|
||||
|
||||
impl Parser for SessionParser {
|
||||
type Output = Result<Session>;
|
||||
|
||||
fn consume<'a>(
|
||||
self: Self,
|
||||
namespace: quick_xml::name::ResolveResult,
|
||||
event: &quick_xml::events::Event<'a>,
|
||||
) -> Continuation<Self, Self::Output> {
|
||||
match self.0 {
|
||||
SessionParserInner::Initial => match event {
|
||||
Event::Start(_) => Continuation::Continue(SessionParser(SessionParserInner::InSession)),
|
||||
Event::Empty(_) => Continuation::Final(Ok(Session)),
|
||||
_ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))),
|
||||
},
|
||||
SessionParserInner::InSession => match event {
|
||||
Event::End(_) => Continuation::Final(Ok(Session)),
|
||||
_ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromXml for Session {
|
||||
type P = SessionParser;
|
||||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
SessionParser(SessionParserInner::Initial)
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
match event {
|
||||
Event::Start(_) => (),
|
||||
Event::Empty(_) => return Ok(Session),
|
||||
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
||||
}
|
||||
(namespace, event) = yield;
|
||||
match event {
|
||||
Event::End(_) => return Ok(Session),
|
||||
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,17 @@ impl ClientStreamStart {
|
|||
reader: &mut NsReader<impl AsyncBufRead + Unpin>,
|
||||
buf: &mut Vec<u8>,
|
||||
) -> Result<ClientStreamStart> {
|
||||
let incoming = skip_text!(reader, buf);
|
||||
let mut incoming = skip_text!(reader, buf);
|
||||
if let Event::Decl(bytes) = incoming {
|
||||
// this is <?xml ...> header
|
||||
if let Some(encoding) = bytes.encoding() {
|
||||
let encoding = encoding?;
|
||||
if &*encoding != b"UTF-8" {
|
||||
return Err(anyhow!("Unsupported encoding: {encoding:?}"));
|
||||
}
|
||||
}
|
||||
incoming = skip_text!(reader, buf);
|
||||
}
|
||||
if let Event::Start(e) = incoming {
|
||||
let (ns, local) = reader.resolve_element(e.name());
|
||||
if ns != ResolveResult::Bound(Namespace(XMLNS.as_bytes())) {
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
use crate::xml::ToXml;
|
||||
use quick_xml::events::{BytesEnd, BytesStart, Event};
|
||||
|
||||
/// Stream error condition
|
||||
///
|
||||
/// [Spec](https://xmpp.org/rfcs/rfc6120.html#streams-error-conditions).
|
||||
pub enum StreamErrorKind {
|
||||
/// The server has experienced a misconfiguration or other internal error that prevents it from servicing the stream.
|
||||
InternalServerError,
|
||||
/// The server is being shut down and all active streams are being closed.
|
||||
SystemShutdown,
|
||||
}
|
||||
impl StreamErrorKind {
|
||||
pub fn from_str(s: &str) -> Option<Self> {
|
||||
match s {
|
||||
"internal-server-error" => Some(Self::InternalServerError),
|
||||
"system-shutdown" => Some(Self::SystemShutdown),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::InternalServerError => "internal-server-error",
|
||||
Self::SystemShutdown => "system-shutdown",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StreamError {
|
||||
pub kind: StreamErrorKind,
|
||||
}
|
||||
impl ToXml for StreamError {
|
||||
fn serialize(&self, events: &mut Vec<Event<'static>>) {
|
||||
events.push(Event::Start(BytesStart::new("stream:error")));
|
||||
events.push(Event::Empty(BytesStart::new(format!(
|
||||
r#"{} xmlns="urn:ietf:params:xml:ns:xmpp-streams""#,
|
||||
self.kind.as_str()
|
||||
))));
|
||||
events.push(Event::End(BytesEnd::new("stream:error")));
|
||||
}
|
||||
}
|
|
@ -1,49 +1,35 @@
|
|||
use super::*;
|
||||
use derive_more::From;
|
||||
|
||||
#[derive(Default, Debug, PartialEq, Eq)]
|
||||
pub struct Ignore;
|
||||
|
||||
#[derive(From)]
|
||||
pub struct IgnoreParser(IgnoreParserInner);
|
||||
|
||||
enum IgnoreParserInner {
|
||||
Initial,
|
||||
InTag { name: Vec<u8>, depth: u8 },
|
||||
}
|
||||
|
||||
impl Parser for IgnoreParser {
|
||||
type Output = Result<Ignore>;
|
||||
|
||||
fn consume<'a>(self: Self, _: ResolveResult, event: &Event<'a>) -> Continuation<Self, Self::Output> {
|
||||
match self.0 {
|
||||
IgnoreParserInner::Initial => match event {
|
||||
Event::Start(bytes) => {
|
||||
let name = bytes.name().0.to_owned();
|
||||
Continuation::Continue(IgnoreParserInner::InTag { name, depth: 0 }.into())
|
||||
}
|
||||
Event::Empty(_) => Continuation::Final(Ok(Ignore)),
|
||||
_ => Continuation::Final(Ok(Ignore)),
|
||||
},
|
||||
IgnoreParserInner::InTag { name, depth } => match event {
|
||||
Event::End(bytes) if name == bytes.name().0 => {
|
||||
if depth == 0 {
|
||||
Continuation::Final(Ok(Ignore))
|
||||
} else {
|
||||
Continuation::Continue(IgnoreParserInner::InTag { name, depth: depth - 1 }.into())
|
||||
}
|
||||
}
|
||||
_ => Continuation::Continue(IgnoreParserInner::InTag { name, depth }.into()),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromXml for Ignore {
|
||||
type P = IgnoreParser;
|
||||
type P = impl Parser<Output = Result<Self>>;
|
||||
|
||||
fn parse() -> Self::P {
|
||||
IgnoreParserInner::Initial.into()
|
||||
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
let mut depth = match event {
|
||||
Event::Start(bytes) => 0,
|
||||
Event::Empty(_) => return Ok(Ignore),
|
||||
_ => return Ok(Ignore),
|
||||
};
|
||||
loop {
|
||||
(namespace, event) = yield;
|
||||
match event {
|
||||
Event::End(_) => {
|
||||
if depth == 0 {
|
||||
return Ok(Ignore);
|
||||
} else {
|
||||
depth -= 1;
|
||||
}
|
||||
}
|
||||
Event::Start(_) => {
|
||||
depth += 1;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,38 @@ use anyhow::Result;
|
|||
mod ignore;
|
||||
pub use ignore::Ignore;
|
||||
|
||||
/// Types which can be parsed from an XML input stream.
|
||||
///
|
||||
/// Example:
|
||||
/// ```
|
||||
/// #![feature(type_alias_impl_trait)]
|
||||
/// #![feature(impl_trait_in_assoc_type)]
|
||||
/// #![feature(coroutines)]
|
||||
/// # use proto_xmpp::xml::FromXml;
|
||||
/// # use quick_xml::events::Event;
|
||||
/// # use quick_xml::name::ResolveResult;
|
||||
/// # use proto_xmpp::xml::Parser;
|
||||
/// # use anyhow::Result;
|
||||
///
|
||||
/// struct MyStruct;
|
||||
/// impl FromXml for MyStruct {
|
||||
/// type P = impl Parser<Output = Result<Self>>;
|
||||
///
|
||||
/// fn parse() -> Self::P {
|
||||
/// |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|
||||
/// (namespace, event) = yield;
|
||||
/// Ok(MyStruct)
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub trait FromXml: Sized {
|
||||
/// The type of parser instances.
|
||||
///
|
||||
/// If the result type of the [parse] is anonymous, this type member can be defined by using `impl Trait`.
|
||||
type P: Parser<Output = Result<Self>>;
|
||||
|
||||
/// Creates a new instance of a parser with an initial state.
|
||||
fn parse() -> Self::P;
|
||||
}
|
||||
|
||||
|
@ -25,9 +54,18 @@ pub trait FromXmlTag: FromXml {
|
|||
const NS: &'static str;
|
||||
}
|
||||
|
||||
/// A stateful parser instance which consumes XML events until the parsing is complete.
|
||||
///
|
||||
/// Usually implemented with the experimental coroutine syntax, which yields to consume the next XML event,
|
||||
/// and returns the final result when the parsing is done.
|
||||
pub trait Parser: Sized {
|
||||
type Output;
|
||||
|
||||
/// Advance the parsing by one XML event.
|
||||
///
|
||||
/// This method consumes `self`, but if the parsing is incomplete,
|
||||
/// it will return the next state of the parser in the returned result.
|
||||
/// Otherwise, it will return the final result of parsing.
|
||||
fn consume<'a>(self: Self, namespace: ResolveResult, event: &Event<'a>) -> Continuation<Self, Self::Output>;
|
||||
}
|
||||
|
||||
|
@ -50,8 +88,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// The result of a single parser iteration.
|
||||
pub enum Continuation<Parser, Res> {
|
||||
/// The parsing is complete and the final result is available.
|
||||
Final(Res),
|
||||
/// The parsing is not complete and more XML events are required.
|
||||
Continue(Parser),
|
||||
}
|
||||
|
||||
|
@ -89,8 +130,8 @@ macro_rules! delegate_parsing {
|
|||
Continuation::Final(Ok(res)) => break Ok(res.into()),
|
||||
Continuation::Final(Err(err)) => break Err(err),
|
||||
Continuation::Continue(p) => {
|
||||
let (namespace, event) = yield;
|
||||
parser = p.consume(namespace, event);
|
||||
($namespace, $event) = yield;
|
||||
parser = p.consume($namespace, $event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,10 +79,6 @@ mod test {
|
|||
fn test_fail_if_size_less_then_3() {
|
||||
let orig = b"login\x00pass";
|
||||
let encoded = general_purpose::STANDARD.encode(orig);
|
||||
let expected = AuthBody {
|
||||
login: "login".to_string(),
|
||||
password: "pass".to_string(),
|
||||
};
|
||||
let result = AuthBody::from_str(encoded.as_bytes());
|
||||
|
||||
assert!(result.is_err());
|
||||
|
@ -92,10 +88,6 @@ mod test {
|
|||
fn test_fail_if_size_greater_then_3() {
|
||||
let orig = b"first\x00login\x00pass\x00other";
|
||||
let encoded = general_purpose::STANDARD.encode(orig);
|
||||
let expected = AuthBody {
|
||||
login: "login".to_string(),
|
||||
password: "pass".to_string(),
|
||||
};
|
||||
let result = AuthBody::from_str(encoded.as_bytes());
|
||||
|
||||
assert!(result.is_err());
|
||||
|
|
|
@ -8,11 +8,12 @@ Some useful commands for development and testing.
|
|||
|
||||
Following commands require `OpenSSL` to be installed. It is provided as `openssl` package in Arch Linux.
|
||||
|
||||
Generate self-signed TLS certificate:
|
||||
Generate self-signed TLS certificate. Mind the common name (CN) field, it should match the domain name of the server.
|
||||
Example for localhost:
|
||||
|
||||
openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -noenc \
|
||||
-keyout certs/xmpp.key -out certs/xmpp.pem \
|
||||
-subj "/CN=example.com"
|
||||
-subj "/CN=localhost"
|
||||
|
||||
Print content of a TLS certificate:
|
||||
|
||||
|
|
|
@ -19,9 +19,15 @@ server_name = "irc.localhost"
|
|||
listen_on = "127.0.0.1:5222"
|
||||
cert = "./certs/xmpp.pem"
|
||||
key = "./certs/xmpp.key"
|
||||
hostname = "localhost"
|
||||
|
||||
[storage]
|
||||
db_path = "db.sqlite"
|
||||
|
||||
[tracing]
|
||||
# otlp grpc endpoint
|
||||
endpoint = "http://jaeger:4317"
|
||||
service_name = "lavina"
|
||||
```
|
||||
|
||||
## With Docker Compose
|
||||
|
@ -40,6 +46,15 @@ services:
|
|||
- '5222:5222' # xmpp
|
||||
- '6667:6667' # irc non-tls
|
||||
- '127.0.0.1:1380:8080' # management http (private)
|
||||
# if you want to observe traces
|
||||
jaeger:
|
||||
image: "jaegertracing/all-in-one:1.56"
|
||||
ports:
|
||||
- "16686:16686" # web ui
|
||||
- "4317:4317" # grpc ingest endpoint
|
||||
environment:
|
||||
- COLLECTOR_OTLP_ENABLED=true
|
||||
- SPAN_STORAGE_TYPE=memory
|
||||
```
|
||||
|
||||
## With Cargo
|
||||
|
|
|
@ -1 +1 @@
|
|||
nightly-2024-03-20
|
||||
nightly-2024-04-19
|
||||
|
|
125
src/http.rs
125
src/http.rs
|
@ -12,10 +12,13 @@ use prometheus::{Encoder, Registry as MetricsRegistry, TextEncoder};
|
|||
use serde::{Deserialize, Serialize};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use lavina_core::auth::{Authenticator, UpdatePasswordResult};
|
||||
use lavina_core::player::{PlayerId, PlayerRegistry};
|
||||
use lavina_core::prelude::*;
|
||||
use lavina_core::repo::Storage;
|
||||
use lavina_core::room::RoomRegistry;
|
||||
use lavina_core::terminator::Terminator;
|
||||
use lavina_core::LavinaCore;
|
||||
|
||||
use mgmt_api::*;
|
||||
|
||||
|
@ -29,20 +32,20 @@ pub struct ServerConfig {
|
|||
pub async fn launch(
|
||||
config: ServerConfig,
|
||||
metrics: MetricsRegistry,
|
||||
rooms: RoomRegistry,
|
||||
core: LavinaCore,
|
||||
storage: Storage,
|
||||
) -> Result<Terminator> {
|
||||
log::info!("Starting the http service");
|
||||
let listener = TcpListener::bind(config.listen_on).await?;
|
||||
log::debug!("Listener started");
|
||||
let terminator = Terminator::spawn(|rx| main_loop(listener, metrics, rooms, storage, rx.map(|_| ())));
|
||||
let terminator = Terminator::spawn(|rx| main_loop(listener, metrics, core, storage, rx.map(|_| ())));
|
||||
Ok(terminator)
|
||||
}
|
||||
|
||||
async fn main_loop(
|
||||
listener: TcpListener,
|
||||
metrics: MetricsRegistry,
|
||||
rooms: RoomRegistry,
|
||||
core: LavinaCore,
|
||||
storage: Storage,
|
||||
termination: impl Future<Output = ()>,
|
||||
) -> Result<()> {
|
||||
|
@ -55,13 +58,13 @@ async fn main_loop(
|
|||
let (stream, _) = result?;
|
||||
let stream = TokioIo::new(stream);
|
||||
let metrics = metrics.clone();
|
||||
let rooms = rooms.clone();
|
||||
let core = core.clone();
|
||||
let storage = storage.clone();
|
||||
tokio::task::spawn(async move {
|
||||
let registry = metrics.clone();
|
||||
let rooms = rooms.clone();
|
||||
let core = core.clone();
|
||||
let storage = storage.clone();
|
||||
let server = http1::Builder::new().serve_connection(stream, service_fn(move |r| route(registry.clone(), rooms.clone(), storage.clone(), r)));
|
||||
let server = http1::Builder::new().serve_connection(stream, service_fn(move |r| route(registry.clone(), core.clone(), storage.clone(), r)));
|
||||
if let Err(err) = server.await {
|
||||
tracing::error!("Error serving connection: {:?}", err);
|
||||
}
|
||||
|
@ -75,16 +78,17 @@ async fn main_loop(
|
|||
|
||||
async fn route(
|
||||
registry: MetricsRegistry,
|
||||
rooms: RoomRegistry,
|
||||
core: LavinaCore,
|
||||
storage: Storage,
|
||||
request: Request<hyper::body::Incoming>,
|
||||
) -> HttpResult<Response<Full<Bytes>>> {
|
||||
let res = match (request.method(), request.uri().path()) {
|
||||
(&Method::GET, "/metrics") => endpoint_metrics(registry),
|
||||
(&Method::GET, "/rooms") => endpoint_rooms(rooms).await,
|
||||
(&Method::GET, "/rooms") => endpoint_rooms(core.rooms).await,
|
||||
(&Method::POST, paths::CREATE_PLAYER) => endpoint_create_player(request, storage).await.or5xx(),
|
||||
(&Method::POST, paths::STOP_PLAYER) => endpoint_stop_player(request, core.players).await.or5xx(),
|
||||
(&Method::POST, paths::SET_PASSWORD) => endpoint_set_password(request, storage).await.or5xx(),
|
||||
_ => not_found(),
|
||||
_ => endpoint_not_found(),
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
@ -96,6 +100,7 @@ fn endpoint_metrics(registry: MetricsRegistry) -> Response<Full<Bytes>> {
|
|||
Response::new(Full::new(Bytes::from(buffer)))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn endpoint_rooms(rooms: RoomRegistry) -> Response<Full<Bytes>> {
|
||||
// TODO introduce management API types independent from core-domain types
|
||||
// TODO remove `Serialize` implementations from all core-domain types
|
||||
|
@ -103,20 +108,14 @@ async fn endpoint_rooms(rooms: RoomRegistry) -> Response<Full<Bytes>> {
|
|||
Response::new(room_list)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn endpoint_create_player(
|
||||
request: Request<hyper::body::Incoming>,
|
||||
mut storage: Storage,
|
||||
) -> Result<Response<Full<Bytes>>> {
|
||||
let str = request.collect().await?.to_bytes();
|
||||
let Ok(res) = serde_json::from_slice::<CreatePlayerRequest>(&str[..]) else {
|
||||
let payload = ErrorResponse {
|
||||
code: errors::MALFORMED_REQUEST,
|
||||
message: "The request payload contains incorrect JSON value",
|
||||
}
|
||||
.to_body();
|
||||
let mut response = Response::new(payload);
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
return Ok(response);
|
||||
return Ok(malformed_request());
|
||||
};
|
||||
storage.create_user(&res.name).await?;
|
||||
log::info!("Player {} created", res.name);
|
||||
|
@ -125,38 +124,48 @@ async fn endpoint_create_player(
|
|||
Ok(response)
|
||||
}
|
||||
|
||||
async fn endpoint_set_password(
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn endpoint_stop_player(
|
||||
request: Request<hyper::body::Incoming>,
|
||||
mut storage: Storage,
|
||||
players: PlayerRegistry,
|
||||
) -> Result<Response<Full<Bytes>>> {
|
||||
let str = request.collect().await?.to_bytes();
|
||||
let Ok(res) = serde_json::from_slice::<ChangePasswordRequest>(&str[..]) else {
|
||||
let payload = ErrorResponse {
|
||||
code: errors::MALFORMED_REQUEST,
|
||||
message: "The request payload contains incorrect JSON value",
|
||||
}
|
||||
.to_body();
|
||||
let mut response = Response::new(payload);
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
return Ok(response);
|
||||
let Ok(res) = serde_json::from_slice::<StopPlayerRequest>(&str[..]) else {
|
||||
return Ok(malformed_request());
|
||||
};
|
||||
let Some(_) = storage.set_password(&res.player_name, &res.password).await? else {
|
||||
let payload = ErrorResponse {
|
||||
code: errors::PLAYER_NOT_FOUND,
|
||||
message: "No such player exists",
|
||||
}
|
||||
.to_body();
|
||||
let mut response = Response::new(payload);
|
||||
*response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
|
||||
return Ok(response);
|
||||
let Ok(player_id) = PlayerId::from(res.name) else {
|
||||
return Ok(player_not_found());
|
||||
};
|
||||
let Some(()) = players.stop_player(&player_id).await? else {
|
||||
return Ok(player_not_found());
|
||||
};
|
||||
log::info!("Password changed for player {}", res.player_name);
|
||||
let mut response = Response::new(Full::<Bytes>::default());
|
||||
*response.status_mut() = StatusCode::NO_CONTENT;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub fn not_found() -> Response<Full<Bytes>> {
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn endpoint_set_password(
|
||||
request: Request<hyper::body::Incoming>,
|
||||
storage: Storage,
|
||||
) -> Result<Response<Full<Bytes>>> {
|
||||
let str = request.collect().await?.to_bytes();
|
||||
let Ok(res) = serde_json::from_slice::<ChangePasswordRequest>(&str[..]) else {
|
||||
return Ok(malformed_request());
|
||||
};
|
||||
let verdict = Authenticator::new(&storage).set_password(&res.player_name, &res.password).await?;
|
||||
match verdict {
|
||||
UpdatePasswordResult::PasswordUpdated => {}
|
||||
UpdatePasswordResult::UserNotFound => {
|
||||
return Ok(player_not_found());
|
||||
}
|
||||
}
|
||||
let mut response = Response::new(Full::<Bytes>::default());
|
||||
*response.status_mut() = StatusCode::NO_CONTENT;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn endpoint_not_found() -> Response<Full<Bytes>> {
|
||||
let payload = ErrorResponse {
|
||||
code: errors::INVALID_PATH,
|
||||
message: "The path does not exist",
|
||||
|
@ -168,25 +177,47 @@ pub fn not_found() -> Response<Full<Bytes>> {
|
|||
response
|
||||
}
|
||||
|
||||
fn player_not_found() -> Response<Full<Bytes>> {
|
||||
let payload = ErrorResponse {
|
||||
code: errors::PLAYER_NOT_FOUND,
|
||||
message: "No such player exists",
|
||||
}
|
||||
.to_body();
|
||||
let mut response = Response::new(payload);
|
||||
*response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
|
||||
response
|
||||
}
|
||||
|
||||
fn malformed_request() -> Response<Full<Bytes>> {
|
||||
let payload = ErrorResponse {
|
||||
code: errors::MALFORMED_REQUEST,
|
||||
message: "The request payload contains incorrect JSON value",
|
||||
}
|
||||
.to_body();
|
||||
|
||||
let mut response = Response::new(payload);
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
return response;
|
||||
}
|
||||
|
||||
trait Or5xx {
|
||||
fn or5xx(self) -> Response<Full<Bytes>>;
|
||||
}
|
||||
|
||||
impl Or5xx for Result<Response<Full<Bytes>>> {
|
||||
fn or5xx(self) -> Response<Full<Bytes>> {
|
||||
match self {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
let mut response = Response::new(Full::new(e.to_string().into()));
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
response
|
||||
}
|
||||
}
|
||||
self.unwrap_or_else(|e| {
|
||||
let mut response = Response::new(Full::new(e.to_string().into()));
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
response
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
trait ToBody {
|
||||
fn to_body(&self) -> Full<Bytes>;
|
||||
}
|
||||
|
||||
impl<T> ToBody for T
|
||||
where
|
||||
T: Serialize,
|
||||
|
|
93
src/main.rs
93
src/main.rs
|
@ -6,13 +6,21 @@ use std::path::Path;
|
|||
use clap::Parser;
|
||||
use figment::providers::Format;
|
||||
use figment::{providers::Toml, Figment};
|
||||
use opentelemetry::KeyValue;
|
||||
use opentelemetry_otlp::WithExportConfig;
|
||||
use opentelemetry_sdk::trace::{BatchConfig, RandomIdGenerator, Sampler};
|
||||
use opentelemetry_sdk::{runtime, Resource};
|
||||
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;
|
||||
use opentelemetry_semantic_conventions::SCHEMA_URL;
|
||||
use prometheus::Registry as MetricsRegistry;
|
||||
use serde::Deserialize;
|
||||
use tracing_opentelemetry::OpenTelemetryLayer;
|
||||
use tracing_subscriber::fmt::Subscriber;
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
use lavina_core::player::PlayerRegistry;
|
||||
use lavina_core::prelude::*;
|
||||
use lavina_core::repo::Storage;
|
||||
use lavina_core::room::RoomRegistry;
|
||||
use lavina_core::LavinaCore;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ServerConfig {
|
||||
|
@ -20,6 +28,13 @@ struct ServerConfig {
|
|||
irc: projection_irc::ServerConfig,
|
||||
xmpp: projection_xmpp::ServerConfig,
|
||||
storage: lavina_core::repo::StorageConfig,
|
||||
tracing: Option<TracingConfig>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct TracingConfig {
|
||||
endpoint: String,
|
||||
service_name: String,
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
|
@ -37,9 +52,9 @@ fn load_config() -> Result<ServerConfig> {
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
set_up_logging()?;
|
||||
let sleep = ctrl_c()?;
|
||||
let config = load_config()?;
|
||||
set_up_logging(&config.tracing)?;
|
||||
|
||||
tracing::info!("Booting up");
|
||||
tracing::info!("Loaded config: {config:?}");
|
||||
|
@ -48,28 +63,14 @@ async fn main() -> Result<()> {
|
|||
irc: irc_config,
|
||||
xmpp: xmpp_config,
|
||||
storage: storage_config,
|
||||
tracing: _,
|
||||
} = config;
|
||||
let mut metrics = MetricsRegistry::new();
|
||||
let metrics = MetricsRegistry::new();
|
||||
let storage = Storage::open(storage_config).await?;
|
||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone())?;
|
||||
let mut players = PlayerRegistry::empty(rooms.clone(), &mut metrics)?;
|
||||
let telemetry_terminator = http::launch(telemetry_config, metrics.clone(), rooms.clone(), storage.clone()).await?;
|
||||
let irc = projection_irc::launch(
|
||||
irc_config,
|
||||
players.clone(),
|
||||
rooms.clone(),
|
||||
metrics.clone(),
|
||||
storage.clone(),
|
||||
)
|
||||
.await?;
|
||||
let xmpp = projection_xmpp::launch(
|
||||
xmpp_config,
|
||||
players.clone(),
|
||||
rooms.clone(),
|
||||
metrics.clone(),
|
||||
storage.clone(),
|
||||
)
|
||||
.await?;
|
||||
let core = LavinaCore::new(metrics.clone(), storage.clone()).await?;
|
||||
let telemetry_terminator = http::launch(telemetry_config, metrics.clone(), core.clone(), storage.clone()).await?;
|
||||
let irc = projection_irc::launch(irc_config, core.clone(), metrics.clone(), storage.clone()).await?;
|
||||
let xmpp = projection_xmpp::launch(xmpp_config, core.clone(), metrics.clone(), storage.clone()).await?;
|
||||
tracing::info!("Started");
|
||||
|
||||
sleep.await;
|
||||
|
@ -78,10 +79,7 @@ async fn main() -> Result<()> {
|
|||
xmpp.terminate().await?;
|
||||
irc.terminate().await?;
|
||||
telemetry_terminator.terminate().await?;
|
||||
players.shutdown_all().await?;
|
||||
drop(players);
|
||||
drop(rooms);
|
||||
storage.close().await?;
|
||||
core.shutdown().await?;
|
||||
tracing::info!("Shutdown complete");
|
||||
Ok(())
|
||||
}
|
||||
|
@ -106,7 +104,44 @@ fn ctrl_c() -> Result<impl Future<Output = ()>> {
|
|||
Ok(recv(chan))
|
||||
}
|
||||
|
||||
fn set_up_logging() -> Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
fn set_up_logging(tracing_config: &Option<TracingConfig>) -> Result<()> {
|
||||
let subscriber = tracing_subscriber::registry().with(tracing_subscriber::fmt::layer());
|
||||
|
||||
let targets = {
|
||||
use std::{env, str::FromStr};
|
||||
use tracing_subscriber::filter::Targets;
|
||||
match env::var("RUST_LOG") {
|
||||
Ok(var) => Targets::from_str(&var)
|
||||
.map_err(|e| {
|
||||
eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e);
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
Err(env::VarError::NotPresent) => Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL),
|
||||
Err(e) => {
|
||||
eprintln!("Ignoring `RUST_LOG`: {}", e);
|
||||
Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Some(config) = tracing_config {
|
||||
let trace_config = opentelemetry_sdk::trace::Config::default()
|
||||
.with_sampler(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(1.0))))
|
||||
.with_id_generator(RandomIdGenerator::default())
|
||||
.with_resource(Resource::from_schema_url(
|
||||
[KeyValue::new(SERVICE_NAME, config.service_name.to_string())],
|
||||
SCHEMA_URL,
|
||||
));
|
||||
let trace_exporter = opentelemetry_otlp::new_exporter().tonic().with_endpoint(&config.endpoint);
|
||||
let tracer = opentelemetry_otlp::new_pipeline()
|
||||
.tracing()
|
||||
.with_trace_config(trace_config)
|
||||
.with_batch_config(BatchConfig::default())
|
||||
.with_exporter(trace_exporter)
|
||||
.install_batch(runtime::Tokio)?;
|
||||
let subscriber = subscriber.with(OpenTelemetryLayer::new(tracer));
|
||||
targets.with_subscriber(subscriber).try_init()?;
|
||||
} else {
|
||||
targets.with_subscriber(subscriber).try_init()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue