From 48fb7be50caf891343779cfdc740adc83a8e2806 Mon Sep 17 00:00:00 2001 From: iverly Date: Sun, 7 Jan 2024 19:30:35 +0100 Subject: [PATCH] feat: move backend structure to shared module Signed-off-by: iverly --- proxy/src/backend.rs | 68 ------------------------------------ proxy/src/lib.rs | 21 +++++------ shared/src/models/backend.rs | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 78 deletions(-) delete mode 100644 proxy/src/backend.rs diff --git a/proxy/src/backend.rs b/proxy/src/backend.rs deleted file mode 100644 index 6434a07..0000000 --- a/proxy/src/backend.rs +++ /dev/null @@ -1,68 +0,0 @@ -/// A backend is a Minecraft server that the proxy can connect to. -/// -/// Properties: -/// -/// * `host`: The hostname of the backend server. -/// * `port`: The port that the backend server is listening on. -#[derive(Debug, Clone)] -pub struct Backend { - hostname: String, - redirect_ip: String, - redirect_port: u16, -} - -impl Backend { - /// Creates a new instance of the `Backend` struct - /// - /// Arguments: - /// - /// * `host` - The host of the backend - /// * `port` - The port of the backend - /// - /// Returns: - /// - /// A new instance of the struct. - pub fn new(hostname: String, redirect_ip: String, redirect_port: u16) -> Self { - Self { - hostname, - redirect_ip, - redirect_port, - } - } - - /// It returns the host of the backend - /// - /// Returns: - /// - /// The host of the backend - pub fn hostname(&self) -> &str { - self.hostname.as_str() - } - - /// It returns the ip of the backend - /// - /// Returns: - /// - /// The ip of the backend - pub fn redirect_ip(&self) -> &str { - self.redirect_ip.as_str() - } - - /// It returns the port of the backend - /// - /// Returns: - /// - /// The port of the backend - pub fn redirect_port(&self) -> u16 { - self.redirect_port - } - - /// It returns the address of the backend - /// - /// Returns: - /// - /// The address of the backend - pub fn addr(&self) -> String { - self.redirect_ip.clone() + ":" + &self.redirect_port.to_string() - } -} diff --git a/proxy/src/lib.rs b/proxy/src/lib.rs index d7d9198..eb80462 100644 --- a/proxy/src/lib.rs +++ b/proxy/src/lib.rs @@ -12,7 +12,6 @@ use tokio::{ use crate::stream::Stream; -pub mod backend; pub mod stream; /// The proxy is responsible for accepting connections from the client and @@ -237,11 +236,13 @@ impl Proxy { })?; } Event::PutBackend(backend, tx) => { - tx.send(storage.lock().await.add_backend(backend::Backend::new( - backend.hostname, - backend.redirect_ip, - backend.redirect_port, - ))) + tx.send(storage.lock().await.add_backend( + shared::models::backend::Backend::new( + backend.hostname, + backend.redirect_ip, + backend.redirect_port, + ), + )) .map_err(|_| { log::error!("failed to send put backend response"); anyhow!("failed to send put backend response") @@ -252,11 +253,11 @@ impl Proxy { .lock() .await .get_backends() - .into_iter() + .iter() .map(|backend| shared::models::backend::Backend { - hostname: backend.hostname().to_string(), - redirect_ip: backend.redirect_ip().to_string(), - redirect_port: backend.redirect_port(), + hostname: backend.1.hostname().to_string(), + redirect_ip: backend.1.redirect_ip().to_string(), + redirect_port: backend.1.redirect_port(), }) .collect::>())) .map_err(|_| { diff --git a/shared/src/models/backend.rs b/shared/src/models/backend.rs index d2ecc1a..4dd9b10 100644 --- a/shared/src/models/backend.rs +++ b/shared/src/models/backend.rs @@ -1,6 +1,68 @@ +/// A backend is a Minecraft server that the proxy can connect to. +/// +/// Properties: +/// +/// * `host`: The hostname of the backend server. +/// * `port`: The port that the backend server is listening on. #[derive(Debug, Clone)] pub struct Backend { pub hostname: String, pub redirect_ip: String, pub redirect_port: u16, } + +impl Backend { + /// Creates a new instance of the `Backend` struct + /// + /// Arguments: + /// + /// * `host` - The host of the backend + /// * `port` - The port of the backend + /// + /// Returns: + /// + /// A new instance of the struct. + pub fn new(hostname: String, redirect_ip: String, redirect_port: u16) -> Self { + Self { + hostname, + redirect_ip, + redirect_port, + } + } + + /// It returns the host of the backend + /// + /// Returns: + /// + /// The host of the backend + pub fn hostname(&self) -> &str { + self.hostname.as_str() + } + + /// It returns the ip of the backend + /// + /// Returns: + /// + /// The ip of the backend + pub fn redirect_ip(&self) -> &str { + self.redirect_ip.as_str() + } + + /// It returns the port of the backend + /// + /// Returns: + /// + /// The port of the backend + pub fn redirect_port(&self) -> u16 { + self.redirect_port + } + + /// It returns the address of the backend + /// + /// Returns: + /// + /// The address of the backend + pub fn addr(&self) -> String { + self.redirect_ip.clone() + ":" + &self.redirect_port.to_string() + } +}