From 27570a6c6019beed4c9f8465ff01bad9320b79fe Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Mon, 9 Jan 2023 20:23:29 +0000 Subject: [PATCH] Set user's hostname to their IP address --- src/client.rs | 4 +++- src/connection.rs | 13 +++++++++++-- src/main.rs | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index 7639e55..d8aaf32 100644 --- a/src/client.rs +++ b/src/client.rs @@ -66,7 +66,7 @@ impl Actor for Client { ctx.run_interval(Duration::from_secs(30), |this, ctx| { let _span = info_span!(parent: &this.span, "ping").entered(); - if Instant::now().duration_since(this.last_active) > Duration::from_secs(120) { + if Instant::now().duration_since(this.last_active) >= Duration::from_secs(120) { this.server_leave_reason = Some("Ping timeout: 120 seconds".to_string()); ctx.stop(); } @@ -78,6 +78,8 @@ impl Actor for Client { }); }); + // join the user to all the channels they were previously in before disconnecting from + // the server ctx.spawn( self.persistence .send(FetchUserChannels { diff --git a/src/connection.rs b/src/connection.rs index 07d8bf9..4c6b339 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,5 +1,6 @@ use std::{ io::{Error, ErrorKind}, + net::SocketAddr, str::FromStr, }; @@ -30,6 +31,7 @@ pub struct UserId(pub i64); #[derive(Default)] pub struct ConnectionRequest { + host: Option, nick: Option, user: Option, mode: Option, @@ -38,6 +40,7 @@ pub struct ConnectionRequest { #[derive(Clone)] pub struct InitiatedConnection { + pub host: SocketAddr, pub nick: String, pub user: String, pub mode: String, @@ -51,7 +54,7 @@ impl InitiatedConnection { Prefix::Nickname( self.nick.to_string(), self.user.to_string(), - "my-host".to_string(), + self.host.ip().to_string(), ) } } @@ -61,6 +64,7 @@ impl TryFrom for InitiatedConnection { fn try_from(value: ConnectionRequest) -> Result { let ConnectionRequest { + host: Some(host), nick: Some(nick), user: Some(user), mode: Some(mode), @@ -70,6 +74,7 @@ impl TryFrom for InitiatedConnection { }; Ok(Self { + host, nick, user, mode, @@ -85,9 +90,13 @@ impl TryFrom for InitiatedConnection { pub async fn negotiate_client_connection( s: &mut MessageStream, write: &mut tokio_util::codec::FramedWrite, IrcCodec>, + host: SocketAddr, database: sqlx::Pool, ) -> Result, ProtocolError> { - let mut request = ConnectionRequest::default(); + let mut request = ConnectionRequest { + host: Some(host), + ..ConnectionRequest::default() + }; let mut capabilities_requested = false; diff --git a/src/main.rs b/src/main.rs index faaad24..6bd8a3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ async fn start_tcp_acceptor_loop( // ensure we have all the details required to actually connect the client to the server // (ie. we have a nick, user, etc) - let Some(connection) = connection::negotiate_client_connection(&mut read, &mut write, database).await.unwrap() else { + let Some(connection) = connection::negotiate_client_connection(&mut read, &mut write, addr, database).await.unwrap() else { error!("Failed to fully handshake with client, dropping connection"); return; }; -- libgit2 1.7.2