use tonic::transport::{Channel, ClientTlsConfig}; use crate::{grpc::CopyRequest, state::State}; #[derive(Default)] pub struct RemoteCopier { host: String, } impl RemoteCopier { pub fn new(remote_host: impl Into) -> Self { Self { host: remote_host.into(), } } pub async fn copy(&self, input: &[u8]) -> anyhow::Result<()> { let tls = ClientTlsConfig::new(); let channel = Channel::from_shared(self.host.clone())? .tls_config(if self.host.starts_with("https") { tls.with_native_roots() } else { tls })? .connect() .await?; tracing::debug!("establishing connection to remote"); let mut client = crate::grpc::void_pin_client::VoidPinClient::new(channel); tracing::info!("sending copy request"); client .copy(CopyRequest { content: input.into(), }) .await?; tracing::info!("sent copy request"); Ok(()) } } pub trait RemoteCopierState { fn remote_copier(&self, host: impl Into) -> RemoteCopier; } impl RemoteCopierState for State { fn remote_copier(&self, host: impl Into) -> RemoteCopier { RemoteCopier::new(host) } }