forked from lavina/lavina
Compare commits
No commits in common. "809ddaac258897319c853a4cb3d37f49d622b307" and "1905ac1977b2d177266f1b583e9886aa7e34f10d" have entirely different histories.
809ddaac25
...
1905ac1977
File diff suppressed because it is too large
Load Diff
|
@ -54,7 +54,7 @@ async fn handle_socket(
|
|||
socket_addr: &SocketAddr,
|
||||
players: PlayerRegistry,
|
||||
rooms: RoomRegistry,
|
||||
termination: Deferred<()>,
|
||||
termination: Deferred<()>, // TODO use it to stop the connection gracefully
|
||||
mut storage: Storage,
|
||||
) -> Result<()> {
|
||||
log::info!("Received an IRC connection from {socket_addr}");
|
||||
|
@ -62,24 +62,19 @@ async fn handle_socket(
|
|||
let mut reader: BufReader<ReadHalf> = BufReader::new(reader);
|
||||
let mut writer = BufWriter::new(writer);
|
||||
|
||||
pin!(termination);
|
||||
select! {
|
||||
biased;
|
||||
_ = &mut termination =>{
|
||||
log::info!("Socket handling was terminated");
|
||||
return Ok(())
|
||||
},
|
||||
registered_user = handle_registration(&mut reader, &mut writer, &mut storage, &config) =>
|
||||
match registered_user {
|
||||
Ok(user) => {
|
||||
log::debug!("User registered");
|
||||
handle_registered_socket(config, players, rooms, &mut reader, &mut writer, user).await?;
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("Registration failed: {err}");
|
||||
}
|
||||
}
|
||||
let registered_user: Result<RegisteredUser> =
|
||||
handle_registration(&mut reader, &mut writer, &mut storage, &config).await;
|
||||
|
||||
match registered_user {
|
||||
Ok(user) => {
|
||||
log::debug!("User registered");
|
||||
handle_registered_socket(config, players, rooms, &mut reader, &mut writer, user).await?;
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("Registration failed: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
stream.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -185,16 +180,14 @@ async fn handle_registration<'a>(
|
|||
writer.flush().await?;
|
||||
}
|
||||
CapabilitySubcommand::End => {
|
||||
let Some((ref username, ref realname)) = future_username else {
|
||||
todo!();
|
||||
let Some((username, realname)) = future_username else {
|
||||
todo!()
|
||||
};
|
||||
let Some(nickname) = future_nickname.clone() else {
|
||||
todo!();
|
||||
todo!()
|
||||
};
|
||||
let username = username.clone();
|
||||
let realname = realname.clone();
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
nickname,
|
||||
username,
|
||||
realname,
|
||||
};
|
||||
|
@ -204,15 +197,7 @@ async fn handle_registration<'a>(
|
|||
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;
|
||||
todo!();
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
break Ok(candidate_user);
|
||||
|
@ -224,20 +209,12 @@ async fn handle_registration<'a>(
|
|||
future_nickname = Some(nickname);
|
||||
} else if let Some((username, realname)) = future_username.clone() {
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
nickname,
|
||||
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;
|
||||
todo!();
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
break Ok(candidate_user);
|
||||
|
@ -250,20 +227,12 @@ async fn handle_registration<'a>(
|
|||
future_username = Some((username, realname));
|
||||
} else if let Some(nickname) = future_nickname.clone() {
|
||||
let candidate_user = RegisteredUser {
|
||||
nickname: nickname.clone(),
|
||||
nickname,
|
||||
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;
|
||||
todo!();
|
||||
};
|
||||
auth_user(storage, &*candidate_user.nickname, &*candidate_password).await?;
|
||||
break Ok(candidate_user);
|
||||
|
@ -286,59 +255,38 @@ async fn handle_registration<'a>(
|
|||
.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"));
|
||||
}
|
||||
// TODO respond with 904
|
||||
todo!();
|
||||
}
|
||||
} 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?;
|
||||
auth_user(storage, &body.login, &body.password).await?;
|
||||
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
|
||||
}
|
||||
_ => {}
|
||||
|
@ -349,14 +297,6 @@ async fn handle_registration<'a>(
|
|||
Ok(user)
|
||||
}
|
||||
|
||||
fn sasl_fail_message(sender: Str, nick: Str, text: Str) -> ServerMessage {
|
||||
ServerMessage {
|
||||
tags: vec![],
|
||||
sender: Some(sender),
|
||||
body: ServerMessageBody::N904SaslFail { nick, text },
|
||||
}
|
||||
}
|
||||
|
||||
async fn auth_user(storage: &mut Storage, login: &str, plain_password: &str) -> Result<()> {
|
||||
let stored_user = storage.retrieve_user_by_name(login).await?;
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
@ -219,30 +218,3 @@ async fn scenario_cap_short_negotiation() -> Result<()> {
|
|||
server.server.terminate().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?;
|
||||
|
||||
let mut stream = TcpStream::connect(server.server.addr).await?;
|
||||
let mut s = TestScope::new(&mut stream);
|
||||
|
||||
s.send("NICK tester").await?;
|
||||
s.send("CAP REQ :sasl").await?;
|
||||
s.send("USER UserName 0 * :Real Name").await?;
|
||||
s.expect(":testserver CAP tester ACK :sasl").await?;
|
||||
s.send("AUTHENTICATE PLAIN").await?;
|
||||
s.expect(":testserver AUTHENTICATE +").await?;
|
||||
|
||||
stream.shutdown().await?;
|
||||
server.server.terminate().await?;
|
||||
|
||||
assert!(TcpStream::connect(&address).await.is_err());
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ async fn handle_socket(
|
|||
mut players: PlayerRegistry,
|
||||
rooms: RoomRegistry,
|
||||
mut storage: Storage,
|
||||
termination: Deferred<()>,
|
||||
termination: Deferred<()>, // TODO use it to stop the connection gracefully
|
||||
) -> Result<()> {
|
||||
log::info!("Received an XMPP connection from {socket_addr}");
|
||||
let mut reader_buf = vec![];
|
||||
|
@ -190,13 +190,11 @@ async fn handle_socket(
|
|||
pin!(termination);
|
||||
select! {
|
||||
biased;
|
||||
_ = &mut termination =>{
|
||||
log::info!("Socket handling was terminated");
|
||||
return Ok(())
|
||||
},
|
||||
_ = &mut termination => return Ok(()),
|
||||
authenticated = socket_auth(&mut xml_reader, &mut xml_writer, &mut reader_buf, &mut storage) => {
|
||||
match authenticated {
|
||||
Ok(authenticated) => {
|
||||
log::debug!("User authenticated");
|
||||
let mut connection = players.connect_to_player(authenticated.player_id.clone()).await;
|
||||
socket_final(
|
||||
&mut xml_reader,
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
|
@ -203,7 +202,7 @@ async fn terminate_socket() -> Result<()> {
|
|||
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());
|
||||
|
||||
// test scenario
|
||||
|
||||
storage.create_user("tester").await?;
|
||||
|
@ -214,7 +213,6 @@ async fn terminate_socket() -> Result<()> {
|
|||
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"));
|
||||
|
@ -226,6 +224,7 @@ async fn terminate_socket() -> Result<()> {
|
|||
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()
|
||||
|
@ -237,17 +236,23 @@ async fn terminate_socket() -> Result<()> {
|
|||
let mut stream = connector.connect(ServerName::IpAddress(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"));
|
||||
|
||||
stream.shutdown().await?;
|
||||
server.terminate().await?;
|
||||
|
||||
assert!(TcpStream::connect(&address).await.is_err());
|
||||
let mut s = TestScopeTls::new(&mut stream, buffer);
|
||||
|
||||
println!("--------------------------------");
|
||||
let res = s.send(r#"<?xml version="1.0"?>"#).await;
|
||||
println!();
|
||||
assert!(res.is_ok());
|
||||
|
||||
// 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.terminate().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -30,10 +30,6 @@ fn token(input: &str) -> IResult<&str, &str> {
|
|||
take_while(|i| i != '\n' && i != '\r')(input)
|
||||
}
|
||||
|
||||
fn params(input: &str) -> IResult<&str, &str> {
|
||||
take_while(|i| i != '\n' && i != '\r' && i != ':')(input)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Chan {
|
||||
/// #<name> — network-global channel, available from any server in the network.
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use nonempty::NonEmpty;
|
||||
use tokio::io::AsyncWrite;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
@ -154,10 +152,6 @@ pub enum ServerMessageBody {
|
|||
N903SaslSuccess {
|
||||
nick: Str,
|
||||
message: Str,
|
||||
},
|
||||
N904SaslFail {
|
||||
nick: Str,
|
||||
text: Str,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -375,13 +369,6 @@ impl ServerMessageBody {
|
|||
writer.write_all(b" :").await?;
|
||||
writer.write_all(message.as_bytes()).await?;
|
||||
}
|
||||
ServerMessageBody::N904SaslFail { nick, text } => {
|
||||
writer.write_all(b"904").await?;
|
||||
writer.write_all(b" ").await?;
|
||||
writer.write_all(nick.as_bytes()).await?;
|
||||
writer.write_all(b" :").await?;
|
||||
writer.write_all(text.as_bytes()).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -404,7 +391,7 @@ fn server_message_body(input: &str) -> IResult<&str, ServerMessageBody> {
|
|||
server_message_body_notice,
|
||||
server_message_body_ping,
|
||||
server_message_body_pong,
|
||||
server_message_body_cap
|
||||
server_message_body_cap,
|
||||
))(input)
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
nightly-2024-02-08
|
||||
nightly-2023-12-07
|
||||
|
|
Loading…
Reference in New Issue