complete get_service_port_forward fn (#2579)

* complete get_service_port_forward fn

* refactor get_service_port_forward and get_container_ip

* remove unused function

* move host_id to GetServicePortForwardParams

* replace match with deref

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>

* refactor get_container_ip to use deref

---------

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>
This commit is contained in:
Dominion5254
2024-04-05 13:20:49 -06:00
committed by GitHub
parent 056cab23e0
commit 75ff541aec
2 changed files with 26 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ use imbl::OrdMap;
use lazy_format::lazy_format;
use models::{HostId, OptionExt, PackageId};
use patch_db::PatchDb;
use tokio::sync::Mutex;
use torut::onion::{OnionAddressV3, TorSecretKeyV3};
use tracing::instrument;
@@ -387,7 +388,21 @@ impl NetService {
}
pub fn get_ip(&self) -> Ipv4Addr {
self.ip.to_owned()
self.ip
}
pub fn get_ext_port(&self, host_id: HostId, internal_port: u16) -> Result<u16, Error> {
let host_id_binds = self.binds.get_key_value(&host_id);
match host_id_binds {
Some((id, binds)) => {
if let Some(ext_port_info) = binds.lan.get(&internal_port) {
Ok(ext_port_info.0)
} else {
Err(Error::new(eyre!("Internal Port {} not found in NetService binds", internal_port), crate::ErrorKind::NotFound))
}
},
None => Err(Error::new(eyre!("HostID {} not found in NetService binds", host_id), crate::ErrorKind::NotFound))
}
}
}

View File

@@ -186,6 +186,7 @@ struct GetServicePortForwardParams {
#[ts(type = "string | null")]
package_id: Option<PackageId>,
internal_port: u32,
host_id: HostId,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, TS)]
@@ -314,22 +315,19 @@ async fn get_system_smtp(
todo!()
}
async fn get_container_ip(context: EffectContext, _: Empty) -> Result<Ipv4Addr, Error> {
match context.0.upgrade() {
Some(c) => {
let net_service = c.persistent_container.net_service.lock().await;
Ok(net_service.get_ip())
}
None => Err(Error::new(
eyre!("Upgrade on Weak<ServiceActorSeed> resulted in a None variant"),
crate::ErrorKind::NotFound,
)),
}
let context = context.deref()?;
let net_service = context.persistent_container.net_service.lock().await;
Ok(net_service.get_ip())
}
async fn get_service_port_forward(
context: EffectContext,
data: GetServicePortForwardParams,
) -> Result<Value, Error> {
todo!()
) -> Result<u16, Error> {
let internal_port = data.internal_port as u16;
let context = context.deref()?;
let net_service = context.persistent_container.net_service.lock().await;
net_service.get_ext_port(data.host_id, internal_port)
}
async fn clear_network_interfaces(context: EffectContext, _: Empty) -> Result<Value, Error> {
todo!()