login to start

This commit is contained in:
2026-06-10 13:19:10 +02:00
parent 17032a1940
commit 35ac5453d9
8 changed files with 68 additions and 30 deletions

View File

@ -1,6 +1,7 @@
use bollard::Docker;
use clap::{Arg, ArgMatches, Command}; use clap::{Arg, ArgMatches, Command};
use crate::{Config, DbPool}; use crate::{Config, DbPool, dockermgr, models::Servers};
use std::fs; use std::fs;
@ -12,7 +13,7 @@ pub fn create_subcommand() -> Command {
.arg(Arg::new("NAME").required(true)) .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::*; use crate::schema::servers::dsl::*;
let server_name = arguments.get_one::<String>("NAME").unwrap(); let server_name = arguments.get_one::<String>("NAME").unwrap();
@ -20,7 +21,7 @@ pub async fn subcommand(arguments: &ArgMatches, pool: DbPool) -> Result<(), Stri
let conn = &mut pool.get().unwrap(); 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())?; let deleted = diesel::delete(servers.filter(name.eq(server_name))).execute(conn).map_err(|_| "failed to delete from db".to_string())?;
if deleted != 1 { if deleted != 1 {

View File

@ -15,7 +15,7 @@ pub fn create_subcommand() -> Command {
.arg(Arg::new("NAME").required(true)) .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::*; use schema::servers::dsl::*;
let server_name = arguments.get_one::<String>("NAME").unwrap(); let server_name = arguments.get_one::<String>("NAME").unwrap();
dockermgr::start_server(&docker, pool, server_name).await?; dockermgr::start_server(&docker, pool, server_name).await?;

View File

@ -12,7 +12,7 @@ pub fn create_subcommand() -> Command {
.arg(Arg::new("NAME").required(true)) .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::<String>("NAME").unwrap(); let server_name = arguments.get_one::<String>("NAME").unwrap();
println!("Stopping container"); println!("Stopping container");
dockermgr::stop_server(&docker, pool, server_name).await?; dockermgr::stop_server(&docker, pool, server_name).await?;

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/29 21:22:17 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"); let docker = Docker::connect_with_defaults().expect("Failed to connect to local docker");
match arg.subcommand() { 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(("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(("ls", sub_matches)) => { ls::subcommand(sub_matches, pool).await?; },
Some(("edit", sub_matches)) => { edit::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(("start", sub_matches)) => { start::subcommand(sub_matches, &pool, &docker).await?; },
Some(("stop", sub_matches)) => { stop::subcommand(sub_matches, &pool, docker).await?; }, Some(("stop", sub_matches)) => { stop::subcommand(sub_matches, &pool, &docker).await?; },
_ => {panic!("subcommand not implemented")} _ => {panic!("subcommand not implemented")}
} }
Ok(()) Ok(())

View File

@ -6,16 +6,26 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/07 17:23:09 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 tokio::net::TcpStream;
use crate::minecraft::handshake::Handshake; use crate::minecraft::handshake::Handshake;
use std::fmt; use std::fmt;
use crate::models::Servers; use crate::models::Servers;
use crate::DbPool;
use crate::schema;
use crate::models;
use diesel::prelude::*;
use regex::Regex;
pub struct Client { pub struct Client {
pub in_stream: TcpStream, pub in_stream: TcpStream,
@ -28,19 +38,12 @@ pub struct Client {
pub server: Option<Result<Servers,diesel::result::Error>>, pub server: Option<Result<Servers,diesel::result::Error>>,
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 { impl Client {
pub fn create(stream: TcpStream, pool: DbPool) -> Self { pub fn create(stream: TcpStream, pool: DbPool, docker: Docker) -> Self {
Self { Self {
in_stream: stream, in_stream: stream,
@ -53,7 +56,9 @@ impl Client {
server: None, server: None,
db_pool: pool db_pool: pool,
docker: docker
} }
} }

View File

@ -1,16 +1,44 @@
use crate::minecraft::client::client::Client; use crate::minecraft::client::client::Client;
use diesel::result::Error::NotFound;
use json::object; use json::object;
use crate::minecraft::varint::varint_write; use crate::minecraft::varint::varint_write;
use std::collections::VecDeque; use std::collections::VecDeque;
use crate::dockermgr;
impl Client { impl Client {
pub async fn login_intent_handle(&mut self, mut _packet: &mut VecDeque<u8>, _packet_id: i32) -> Result<(),String> { pub async fn login_intent_handle(&mut self, mut _packet: &mut VecDeque<u8>, _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! { let reason_json = object! {
"type": "text", "type": "text",
"text": "login to start not implemented yet", "text": *message.clone(),
"color": "red" "color": *color.clone()
}; };
let mut response: Vec<u8> = vec![]; let mut response: Vec<u8> = vec![];
let response_json = json::stringify(reason_json); let response_json = json::stringify(reason_json);
let mut response_json_len = varint_write(response_json.len() as i32); let mut response_json_len = varint_write(response_json.len() as i32);

View File

@ -1,4 +1,5 @@
use crate::minecraft::client::client::Client; use crate::minecraft::client::client::Client;
use bollard::Docker;
use tokio::net::TcpStream; use tokio::net::TcpStream;
use crate::DbPool; use crate::DbPool;
@ -7,13 +8,15 @@ use tokio::net::TcpListener;
use std::io; 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?; let mc_listener = TcpListener::bind("0.0.0.0:25565").await?;
loop { loop {
if let Ok((socket, _)) = mc_listener.accept().await { 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 { 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); 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); println!("new client {:?}", stream);
let mut client = Client::create(stream, pool); let mut client = Client::create(stream, pool, docker);
loop { loop {
tokio::select! { tokio::select! {

View File

@ -7,5 +7,6 @@ diesel::table! {
status -> SmallInt, status -> SmallInt,
is_default -> Bool, is_default -> Bool,
redirect_ip -> Nullable<Text>, redirect_ip -> Nullable<Text>,
simple_redirect -> Bool,
} }
} }