improve proxy functionality and configuration

This commit is contained in:
2025-12-25 09:53:46 +00:00
parent 5045c05f9f
commit 208b7f4598
13 changed files with 293 additions and 161 deletions

View File

@@ -1,14 +1,13 @@
use crate::config::{DockerConnectionConfig, MrproxyConnectionData};
use crate::config::{docker::DockerConnectionConfig, mrproxy::MrproxyConnectionConfig};
use crate::database::exposer::MemStorage;
use crate::database::instance::Instance;
use crate::deploy::container::Container;
use crate::deploy::container_options::Options;
use crate::mcproxy_client::client;
use crate::{deploy, mcproxy_client};
use bollard::errors::Error;
use bollard::Docker;
use log::error;
use std::os::unix::net::UnixStream;
use std::error::Error as GenericError;
use std::sync::Mutex;
pub struct Controller {
@@ -17,7 +16,7 @@ pub struct Controller {
storage: Mutex<MemStorage>,
started: bool,
docker_config: DockerConnectionConfig,
mrproxy_config: MrproxyConnectionData,
mrproxy_config: MrproxyConnectionConfig,
}
impl Controller {
@@ -26,7 +25,7 @@ impl Controller {
network: String,
range: String,
docker_config: DockerConnectionConfig,
mrproxy_config: MrproxyConnectionData,
mrproxy_config: MrproxyConnectionConfig,
) -> Result<Self, bollard::errors::Error> {
deploy::network::Network::new(driver.clone(), network.clone(), range).await?;
let cont = Self {
@@ -49,31 +48,16 @@ impl Controller {
) -> String {
let is_stored = self.storage.lock().unwrap().search_instance(domain.clone());
match is_stored {
Some(c) => {
let mut mrcp_controller =
mcproxy_client::controller::Controller::new(&self.mrproxy_config).unwrap();
_ = mrcp_controller.insert_new_domain(
&domain,
&ip.unwrap_or(c.get_ip(&self.driver).await.unwrap()),
);
match c.docker_id {
Some(id) => id,
None => "Container without docker_id".to_string(),
}
}
Some(c) => match c.docker_id {
Some(id) => id,
None => "Container without docker_id".to_string(),
},
None => {
match self
.load_container(None, domain.clone(), ip.clone(), image.clone(), ops)
.load_container_and_bind(None, domain.clone(), ip.clone(), image.clone(), ops)
.await
{
Ok(c) => {
let mut mrcp_controller =
mcproxy_client::controller::Controller::new(&self.mrproxy_config)
.unwrap();
_ = mrcp_controller.insert_new_domain(
&domain,
&ip.unwrap_or(c.get_ip(&self.driver).await.unwrap()),
);
self.storage
.try_lock()
.unwrap()
@@ -87,6 +71,57 @@ impl Controller {
}
}
async fn load_container_and_bind(
&self,
docker_id: Option<String>,
domain: String,
ip: Option<String>,
image: String,
ops: Options,
) -> Result<deploy::container::Container, Box<dyn GenericError>> {
match self
.load_container(docker_id, domain.clone(), ip.clone(), image, ops)
.await
{
Ok(c) => {
match self
.bind_container_in_proxy(domain.clone(), ip, c.clone())
.await
{
Ok(_) => Ok(c),
Err(e) => {
error!("failed in the bind process with the proxy, deleting the container");
self.delete_container(domain).await?;
return Err(e);
}
}
}
Err(e) => return Err(Box::new(e)),
}
}
async fn bind_container_in_proxy(
&self,
domain: String,
ip: Option<String>,
container: Container,
) -> Result<(), Box<dyn GenericError>> {
let mut mrcp_controller =
mcproxy_client::controller::Controller::new(&self.mrproxy_config)?;
mrcp_controller.insert_new_domain(
&domain,
&ip.unwrap_or(container.get_ip(&self.driver).await?),
)?;
Ok(())
}
async fn unbind_container_in_proxy(&self, domain: &str) -> Result<(), Box<dyn GenericError>> {
let mut mrcp_controller =
mcproxy_client::controller::Controller::new(&self.mrproxy_config)?;
mrcp_controller.remove_domain(&domain)?;
Ok(())
}
pub async fn start_container_from_instance(&self, instance: Instance) {
let image = match self
.storage
@@ -101,7 +136,7 @@ impl Controller {
}
};
match self
.load_container(
.load_container_and_bind(
Some(instance.docker_id.clone()),
instance.domain.clone(),
instance.ip.clone(),
@@ -163,10 +198,13 @@ impl Controller {
}
}
async fn prune_given_container(&self, container: Container) -> Result<String, Error> {
async fn prune_given_container(
&self,
container: Container,
) -> Result<String, Box<dyn GenericError>> {
match container.remove(&self.driver).await {
Ok(_i) => Ok(container.get_id()),
Err(e) => Err(e),
Err(e) => Err(Box::new(e)),
}
}
@@ -180,16 +218,19 @@ impl Controller {
}
}
pub async fn delete_container(&self, domain: String) -> Result<String, Error> {
match self.storage.lock().unwrap().remove_instance(domain) {
pub async fn delete_container(&self, domain: String) -> Result<String, Box<dyn GenericError>> {
match self.storage.lock().unwrap().remove_instance(&domain) {
Some(data) => match data.1 {
Some(c) => self.prune_given_container(c).await,
Some(c) => {
self.unbind_container_in_proxy(&domain).await?;
self.prune_given_container(c).await
}
None => Ok(data.0.docker_id),
},
None => Err(Error::DockerResponseServerError {
None => Err(Box::new(Error::DockerResponseServerError {
status_code: 404,
message: "container not found".to_string(),
}),
})),
}
}