wip: Starting down the bind for the effects

todo: complete a ip todo

chore: Fix the result type on something

todo: Address returning

chore: JS with callbacks

chore: Add in the chown and permissions

chore: Add in the binds and unbinds in
This commit is contained in:
BluJ
2023-02-06 12:14:48 -07:00
committed by Aiden McClelland
parent 550b17552b
commit 9366dbb96e
10 changed files with 1237 additions and 460 deletions

View File

@@ -1,8 +1,11 @@
use helpers::{Callback, OsApi};
use models::PackageId;
use color_eyre::{eyre::eyre, Report};
use helpers::{AddressSchemaLocal, AddressSchemaOnion, Callback, OsApi};
use models::{InterfaceId, PackageId};
use sqlx::Acquire;
use crate::manager::Manager;
use crate::Error;
use crate::{manager::Manager, net::keys::Key};
use super::try_get_running_ip;
#[async_trait::async_trait]
impl OsApi for Manager {
@@ -11,7 +14,103 @@ impl OsApi for Manager {
id: PackageId,
path: &str,
callback: Callback,
) -> Result<serde_json::Value, Error> {
todo!()
) -> Result<serde_json::Value, Report> {
todo!("BLUJ")
}
async fn bind_local(
&self,
internal_port: u16,
address_schema: AddressSchemaLocal,
) -> Result<helpers::Address, Report> {
let ip = try_get_running_ip(&self.seed)
.await?
.ok_or_else(|| eyre!("No ip available"))?;
let AddressSchemaLocal { id, external_port } = address_schema;
let mut svc = self
.seed
.ctx
.net_controller
.create_service(self.seed.manifest.id.clone(), ip)
.await
.map_err(|e| eyre!("Could not get to net controller: {e:?}"))?;
let mut secrets = self.seed.ctx.secret_store.acquire().await?;
let mut tx = secrets.begin().await?;
svc.add_lan(&mut tx, id.clone(), external_port, internal_port, false)
.await
.map_err(|e| eyre!("Could not add to local: {e:?}"))?;
let key = Key::for_interface(&mut tx, Some((self.seed.manifest.id.clone(), id)))
.await
.map_err(|e| eyre!("Could not get network name: {e:?}"))?
.local_address();
tx.commit().await?;
Ok(helpers::Address(key))
}
async fn bind_onion(
&self,
internal_port: u16,
address_schema: AddressSchemaOnion,
) -> Result<helpers::Address, Report> {
let AddressSchemaOnion { id, external_port } = address_schema;
let ip = try_get_running_ip(&self.seed)
.await?
.ok_or_else(|| eyre!("No ip available"))?;
let mut svc = self
.seed
.ctx
.net_controller
.create_service(self.seed.manifest.id.clone(), ip)
.await
.map_err(|e| eyre!("Could not get to net controller: {e:?}"))?;
let mut secrets = self.seed.ctx.secret_store.acquire().await?;
let mut tx = secrets.begin().await?;
svc.add_tor(&mut tx, id.clone(), external_port, internal_port)
.await
.map_err(|e| eyre!("Could not add to tor: {e:?}"))?;
let key = Key::for_interface(&mut tx, Some((self.seed.manifest.id.clone(), id)))
.await
.map_err(|e| eyre!("Could not get network name: {e:?}"))?
.tor_address()
.to_string();
tx.commit().await?;
Ok(helpers::Address(key))
}
async fn unbind_onion(&self, id: InterfaceId, external: u16) -> Result<(), Report> {
let ip = try_get_running_ip(&self.seed)
.await?
.ok_or_else(|| eyre!("No ip available"))?;
let mut svc = self
.seed
.ctx
.net_controller
.create_service(self.seed.manifest.id.clone(), ip)
.await
.map_err(|e| eyre!("Could not get to net controller: {e:?}"))?;
let mut secrets = self.seed.ctx.secret_store.acquire().await?;
svc.remove_tor(id, external)
.await
.map_err(|e| eyre!("Could not add to tor: {e:?}"))?;
Ok(())
}
async fn unbind_local(&self, id: InterfaceId, external: u16) -> Result<(), Report> {
let ip = try_get_running_ip(&self.seed)
.await?
.ok_or_else(|| eyre!("No ip available"))?;
let mut svc = self
.seed
.ctx
.net_controller
.create_service(self.seed.manifest.id.clone(), ip)
.await
.map_err(|e| eyre!("Could not get to net controller: {e:?}"))?;
let mut secrets = self.seed.ctx.secret_store.acquire().await?;
svc.remove_lan(id, external)
.await
.map_err(|e| eyre!("Could not add to local: {e:?}"))?;
Ok(())
}
}

View File

@@ -251,7 +251,10 @@ async fn run_main_log_result(result: RunMainResult, seed: Arc<manager_seed::Mana
}
}
pub(super) async fn get_status(db: &mut PatchDbHandle, manifest: &Manifest) -> Result<MainStatus, Error> {
pub(super) async fn get_status(
db: &mut PatchDbHandle,
manifest: &Manifest,
) -> Result<MainStatus, Error> {
Ok(crate::db::DatabaseModel::new()
.package_data()
.idx_model(&manifest.id)
@@ -283,7 +286,6 @@ async fn set_status(
.status()
.main()
.put(db, main_status)
.await?
.clone();
.await?;
Ok(())
}

View File

@@ -4,7 +4,7 @@ use std::sync::Arc;
use std::task::Poll;
use std::time::Duration;
use color_eyre::eyre::eyre;
use color_eyre::{eyre::eyre, Report};
use embassy_container_init::ProcessGroupId;
use futures::future::BoxFuture;
use futures::{FutureExt, TryFutureExt};
@@ -833,6 +833,18 @@ async fn main_health_check_daemon(seed: Arc<ManagerSeed>) {
type RuntimeOfCommand = NonDetachingJoinHandle<Result<Result<NoOutput, (i32, String)>, Error>>;
async fn try_get_running_ip(seed: &ManagerSeed) -> Result<Option<Ipv4Addr>, Report> {
Ok(container_inspect(seed)
.await
.map(|x| x.network_settings)?
.and_then(|ns| ns.networks)
.and_then(|mut n| n.remove("start9"))
.and_then(|es| es.ip_address)
.filter(|ip| !ip.is_empty())
.map(|ip| ip.parse())
.transpose()?)
}
async fn get_running_ip(seed: &ManagerSeed, mut runtime: &mut RuntimeOfCommand) -> GetRunningIp {
loop {
match container_inspect(seed).await {