use crate::error::Error;
use crate::error::Result;
use crate::onion::https::OnionHttpsCall;
use crate::onion::https::OnionHttpsClient;
use crate::onion::https::OnionHttpsResponse;
use crate::onion::proxy::OnionProxyProtocol;
use crate::onion::proxy::OnionProxyRoute;
use crate::onion::runtime::OnionRuntime;
use crate::onion::session::client::OnionCreditWindow;
use crate::onion::session::dial::OnionSessionRequest;
use crate::onion::tcp::NativeOnionOpenStream;
use crate::onion::OnionLoopClass;
use crate::onion::OnionProxyTarget;
use crate::onion::OnionRoute;
use crate::onion::OnionRouteError;
use crate::onion::OnionServiceName;
use crate::processor::Processor;
#[derive(Clone)]
pub struct NativeOnionCircuitHandle {
runtime: OnionRuntime,
}
impl NativeOnionCircuitHandle {
pub(crate) const fn new(runtime: OnionRuntime) -> Self {
Self { runtime }
}
pub async fn open_tcp_target(
&self,
processor: &Processor,
target: OnionProxyTarget,
) -> Result<NativeOnionOpenStream> {
self.open_tcp_target_with_class(processor, target, OnionLoopClass::DEFAULT)
.await
}
pub async fn open_tcp_target_with_class(
&self,
processor: &Processor,
target: OnionProxyTarget,
class: OnionLoopClass,
) -> Result<NativeOnionOpenStream> {
let route = processor
.build_onion_proxy_route(OnionProxyProtocol::TcpConnect, target)
.await?;
self.open_tcp_stream_with_class(route.route, route.target, class)
.await
}
pub async fn open_tcp_stream(
&self,
route: OnionRoute,
target: OnionProxyTarget,
) -> Result<NativeOnionOpenStream> {
self.open_tcp_stream_with_class(route, target, OnionLoopClass::DEFAULT)
.await
}
pub async fn open_tcp_stream_with_class(
&self,
route: OnionRoute,
target: OnionProxyTarget,
class: OnionLoopClass,
) -> Result<NativeOnionOpenStream> {
if route.service_name() != &OnionServiceName::tcp() {
return Err(Error::OnionRouteError(
OnionRouteError::PayloadServiceMismatch {
payload_service: OnionServiceName::tcp().as_str().to_string(),
route_service: route.service().to_string(),
},
));
}
self.runtime
.open(OnionSessionRequest {
route,
symbol: OnionServiceName::tcp(),
target,
class,
window: OnionCreditWindow::DEFAULT,
})
.await
.map(NativeOnionOpenStream::new)
}
pub async fn request_https(
&self,
route: &OnionProxyRoute,
call: OnionHttpsCall,
) -> Result<OnionHttpsResponse> {
OnionHttpsClient::new(self.runtime.clone())
.request(route, call)
.await
}
pub async fn request_https_with_class(
&self,
route: &OnionProxyRoute,
call: OnionHttpsCall,
class: OnionLoopClass,
) -> Result<OnionHttpsResponse> {
OnionHttpsClient::new(self.runtime.clone())
.request_with_class(route, call, class)
.await
}
}
#[cfg(test)]
mod tests;