1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

push proxy wip

This commit is contained in:
Robin Appelman 2022-10-25 14:10:54 +02:00
commit 58918942fe
4 changed files with 84 additions and 58 deletions

View file

@ -1,3 +1,5 @@
extern crate core;
use crate::args::{ExecService, HazeArgs};
use crate::cloud::{Cloud, CloudOptions};
use crate::config::HazeConfig;

View file

@ -1,12 +1,12 @@
use crate::Result;
use crate::{Cloud, HazeConfig};
use crate::{Result, Service};
use bollard::Docker;
use futures_util::future::Either;
use futures_util::FutureExt;
use miette::{miette, Context, IntoDiagnostic};
use std::collections::HashMap;
use std::fs::{create_dir_all, remove_file, set_permissions};
use std::net::{IpAddr, SocketAddr};
use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::str::FromStr;
@ -24,7 +24,7 @@ use warp_reverse_proxy::{
};
struct ActiveInstances {
known: Mutex<HashMap<String, IpAddr>>,
known: Mutex<HashMap<String, SocketAddr>>,
docker: Docker,
config: HazeConfig,
}
@ -38,23 +38,39 @@ impl ActiveInstances {
}
}
pub async fn get(&self, name: &str) -> Option<IpAddr> {
pub async fn get(&self, name: &str) -> Option<SocketAddr> {
if let Some(ip) = self.known.lock().unwrap().get(name).cloned() {
return Some(ip);
}
let cloud = Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)
.await
.ok()?;
let addr = if let Some(name) = name.strip_suffix("-push") {
let cloud = Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)
.await
.ok()?;
let push = cloud
.services
.iter()
.filter_map(|service| match service {
Service::Push(push) => Some(push),
_ => None,
})
.next()?;
let ip = push.get_ip(&self.docker, &cloud.id).await.ok()?;
SocketAddr::new(ip, 7867)
} else {
SocketAddr::new(
Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)
.await
.ok()?
.ip?,
80,
)
};
if let Some(ip) = cloud.ip {
println!("{name} => {ip}");
println!("{name} => {addr}");
self.known.lock().unwrap().insert(name.into(), ip);
return Some(ip);
}
None
self.known.lock().unwrap().insert(name.into(), addr);
Some(addr)
}
}

View file

@ -6,6 +6,7 @@ use bollard::models::{ContainerState, EndpointSettings, HostConfig};
use bollard::Docker;
use maplit::hashmap;
use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::net::IpAddr;
use std::time::Duration;
use tokio::time::{sleep, timeout};
@ -85,38 +86,7 @@ impl ServiceTrait for NotifyPush {
}
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>> {
docker
.start_container::<String>(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
self.wait_for_push(docker, cloud_id).await?;
sleep(Duration::from_millis(100)).await;
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
let ip = if matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
) {
info.network_settings
.unwrap()
.networks
.unwrap()
.values()
.next()
.unwrap()
.ip_address
.clone()
.unwrap()
} else {
return Err(Report::msg("notify_push not started"));
};
let ip = self.get_ip(docker, cloud_id).await?;
Ok(vec![
format!("occ config:system:set trusted_proxies 1 --value {}", ip),
format!("occ notify_push:setup http://{}:7867", ip),
@ -150,4 +120,42 @@ impl NotifyPush {
.into_diagnostic()
.wrap_err("Timeout after 30 seconds")?
}
pub async fn get_ip(&self, docker: &Docker, cloud_id: &str) -> Result<IpAddr> {
docker
.start_container::<String>(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
self.wait_for_push(docker, cloud_id).await?;
sleep(Duration::from_millis(100)).await;
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
if matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
) {
info.network_settings
.unwrap()
.networks
.unwrap()
.values()
.next()
.unwrap()
.ip_address
.clone()
.unwrap()
.parse()
.into_diagnostic()
.wrap_err("Invalid ip address")
} else {
Err(Report::msg("notify_push not started"))
}
}
}