diff --git a/src/cli/remove.rs b/src/cli/remove.rs index 5f785be..31c7d9a 100644 --- a/src/cli/remove.rs +++ b/src/cli/remove.rs @@ -1,6 +1,7 @@ +use bollard::Docker; use clap::{Arg, ArgMatches, Command}; -use crate::{Config, DbPool}; +use crate::{Config, DbPool, dockermgr, models::Servers}; use std::fs; @@ -12,15 +13,15 @@ pub fn create_subcommand() -> Command { .arg(Arg::new("NAME").required(true)) } -pub async fn subcommand(arguments: &ArgMatches, pool: DbPool) -> Result<(), String> { +pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker) -> Result<(), String> { use crate::schema::servers::dsl::*; let server_name = arguments.get_one::("NAME").unwrap(); let config = Config::load(); let conn = &mut pool.get().unwrap(); - - //todo stop the server if it's started + + let _ = dockermgr::stop_server(docker, pool, server_name).await; let deleted = diesel::delete(servers.filter(name.eq(server_name))).execute(conn).map_err(|_| "failed to delete from db".to_string())?; if deleted != 1 { diff --git a/src/cli/start.rs b/src/cli/start.rs index 795b769..330d57b 100644 --- a/src/cli/start.rs +++ b/src/cli/start.rs @@ -15,7 +15,7 @@ pub fn create_subcommand() -> Command { .arg(Arg::new("NAME").required(true)) } -pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: Docker) -> Result<(), String> { +pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker) -> Result<(), String> { use schema::servers::dsl::*; let server_name = arguments.get_one::("NAME").unwrap(); dockermgr::start_server(&docker, pool, server_name).await?; diff --git a/src/cli/stop.rs b/src/cli/stop.rs index 3d940af..f7d9705 100644 --- a/src/cli/stop.rs +++ b/src/cli/stop.rs @@ -12,7 +12,7 @@ pub fn create_subcommand() -> Command { .arg(Arg::new("NAME").required(true)) } -pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: Docker) -> Result<(), String> { +pub async fn subcommand(arguments: &ArgMatches, pool: &DbPool, docker: &Docker) -> Result<(), String> { let server_name = arguments.get_one::("NAME").unwrap(); println!("Stopping container"); dockermgr::stop_server(&docker, pool, server_name).await?; diff --git a/src/main.rs b/src/main.rs index c12d985..26a4022 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */ -/* Updated: 2026/06/09 17:04:32 by tomoron ### ########.fr */ +/* Updated: 2026/06/10 13:16:50 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,14 +74,14 @@ async fn main() -> Result<(), String> { let docker = Docker::connect_with_defaults().expect("Failed to connect to local docker"); match arg.subcommand() { - Some(("serverMode", _)) => { mc_socket_listen(pool).await.map_err(|_| "socket error".to_string())?; }, + Some(("serverMode", _)) => { mc_socket_listen(pool, docker).await.map_err(|_| "socket error".to_string())?; }, Some(("add", sub_matches)) => { add::subcommand(sub_matches, pool).await?; }, - Some(("remove", sub_matches)) => { remove::subcommand(sub_matches, pool).await?; }, + Some(("remove", sub_matches)) => { remove::subcommand(sub_matches, &pool, &docker).await?; }, Some(("ls", sub_matches)) => { ls::subcommand(sub_matches, pool).await?; }, Some(("edit", sub_matches)) => { edit::subcommand(sub_matches, pool).await?; }, - Some(("start", sub_matches)) => { start::subcommand(sub_matches, &pool, docker).await?; }, - Some(("stop", sub_matches)) => { stop::subcommand(sub_matches, &pool, docker).await?; }, + Some(("start", sub_matches)) => { start::subcommand(sub_matches, &pool, &docker).await?; }, + Some(("stop", sub_matches)) => { stop::subcommand(sub_matches, &pool, &docker).await?; }, _ => {panic!("subcommand not implemented")} } Ok(()) diff --git a/src/minecraft/client/client.rs b/src/minecraft/client/client.rs index 49e99dd..daed318 100644 --- a/src/minecraft/client/client.rs +++ b/src/minecraft/client/client.rs @@ -6,16 +6,26 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/07 17:23:09 by tomoron #+# #+# */ -/* Updated: 2026/06/05 12:36:46 by tomoron ### ########.fr */ +/* Updated: 2026/06/10 12:29:08 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ +use bollard::Docker; use tokio::net::TcpStream; use crate::minecraft::handshake::Handshake; use std::fmt; use crate::models::Servers; +use crate::DbPool; + +use crate::schema; +use crate::models; +use diesel::prelude::*; + +use regex::Regex; + + pub struct Client { pub in_stream: TcpStream, @@ -28,19 +38,12 @@ pub struct Client { pub server: Option>, - pub db_pool: DbPool + pub db_pool: DbPool, + pub docker: Docker } -use crate::DbPool; - -use crate::schema; -use crate::models; -use diesel::prelude::*; - -use regex::Regex; - impl Client { - pub fn create(stream: TcpStream, pool: DbPool) -> Self { + pub fn create(stream: TcpStream, pool: DbPool, docker: Docker) -> Self { Self { in_stream: stream, @@ -53,7 +56,9 @@ impl Client { server: None, - db_pool: pool + db_pool: pool, + + docker: docker } } diff --git a/src/minecraft/client/login.rs b/src/minecraft/client/login.rs index 0c28ddf..a581005 100644 --- a/src/minecraft/client/login.rs +++ b/src/minecraft/client/login.rs @@ -1,16 +1,44 @@ use crate::minecraft::client::client::Client; +use diesel::result::Error::NotFound; use json::object; use crate::minecraft::varint::varint_write; use std::collections::VecDeque; +use crate::dockermgr; + impl Client { pub async fn login_intent_handle(&mut self, mut _packet: &mut VecDeque, _packet_id: i32) -> Result<(),String> { + match self.login_start_server().await { + Ok(_) => { + self.login_send_disconnect(&"Start request successful, the server will start soon".to_string(), &"green".to_string()).await?; + return Ok(()); + } + Err(e) => { + self.login_send_disconnect(&e, &"red".to_string()).await?; + return Err(e); + }, + } + } + + async fn login_start_server(&mut self) -> Result<(), String> { + let server = match self.server.as_ref().unwrap().as_ref() { + Ok(server) => { server }, + Err(NotFound) => { return Err("This server was not found".to_string()); }, + Err(e) => { return Err(format!("db fetch error : {}", e)); } + }; + + dockermgr::start_server(&self.docker, &self.db_pool, &server.name).await?; + Ok(()) + } + + async fn login_send_disconnect(&mut self, message: &String, color: &String) -> Result<(), String> { let reason_json = object! { "type": "text", - "text": "login to start not implemented yet", - "color": "red" + "text": *message.clone(), + "color": *color.clone() }; + let mut response: Vec = vec![]; let response_json = json::stringify(reason_json); let mut response_json_len = varint_write(response_json.len() as i32); diff --git a/src/minecraft/socket.rs b/src/minecraft/socket.rs index 8c9493d..101c30f 100644 --- a/src/minecraft/socket.rs +++ b/src/minecraft/socket.rs @@ -1,4 +1,5 @@ use crate::minecraft::client::client::Client; +use bollard::Docker; use tokio::net::TcpStream; use crate::DbPool; @@ -7,13 +8,15 @@ use tokio::net::TcpListener; use std::io; -pub async fn mc_socket_listen(pool: DbPool) -> io::Result<()> { +pub async fn mc_socket_listen(pool: DbPool, docker: Docker) -> io::Result<()> { let mc_listener = TcpListener::bind("0.0.0.0:25565").await?; loop { if let Ok((socket, _)) = mc_listener.accept().await { - let cloned = pool.clone(); + let cloned_pool = pool.clone(); + let cloned_docker = docker.clone(); + tokio::spawn(async move { - if let Err(e) = process_mc_socket(socket, cloned).await { + if let Err(e) = process_mc_socket(socket, cloned_pool, cloned_docker).await { eprintln!("mc error: {:?}", e); } }); @@ -21,10 +24,10 @@ pub async fn mc_socket_listen(pool: DbPool) -> io::Result<()> { } } -async fn process_mc_socket(stream: TcpStream, pool: DbPool) -> Result<(), String> { +async fn process_mc_socket(stream: TcpStream, pool: DbPool, docker: Docker) -> Result<(), String> { println!("new client {:?}", stream); - let mut client = Client::create(stream, pool); + let mut client = Client::create(stream, pool, docker); loop { tokio::select! { diff --git a/src/schema.rs b/src/schema.rs index 10534b7..9e4532e 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -7,5 +7,6 @@ diesel::table! { status -> SmallInt, is_default -> Bool, redirect_ip -> Nullable, + simple_redirect -> Bool, } }