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

20
flake.lock generated
View file

@ -2,11 +2,11 @@
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1656928814,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
@ -20,11 +20,11 @@
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"lastModified": 1662220400,
"narHash": "sha256-9o2OGQqu4xyLZP9K6kNe1pTHnyPz0Wr3raGYnr9AIgY=",
"owner": "nix-community",
"repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"rev": "6944160c19cb591eb85bbf9b2f2768a935623ed3",
"type": "github"
},
"original": {
@ -36,8 +36,8 @@
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-DWMAHXaKn3Nb2n/f7XP059sYkW+Qq9D+YjyX15wrb98=",
"path": "/nix/store/v1skd43lw11gyb85cpzx5ns1hafapsyx-source",
"narHash": "sha256-h+4F8intTl5QFb+Yv9e4xH1p5FSbHlmbCavxnuoFFSE=",
"path": "/nix/store/k0ag5ji9grl4v14xsj1hyvbdzghyr578-source",
"type": "path"
},
"original": {
@ -48,8 +48,8 @@
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-DWMAHXaKn3Nb2n/f7XP059sYkW+Qq9D+YjyX15wrb98=",
"path": "/nix/store/v1skd43lw11gyb85cpzx5ns1hafapsyx-source",
"narHash": "sha256-h+4F8intTl5QFb+Yv9e4xH1p5FSbHlmbCavxnuoFFSE=",
"path": "/nix/store/k0ag5ji9grl4v14xsj1hyvbdzghyr578-source",
"type": "path"
},
"original": {

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"))
}
}
}