From c75286bfe6a7ce6428874350e13c4aa2a8623a7d Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Wed, 18 Dec 2024 21:47:27 +0800 Subject: [PATCH] Implement connect_by_proxy and connect_onion for async_runtime Signed-off-by: Eval EXEC --- tentacle/src/runtime/async_runtime/os.rs | 33 ++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tentacle/src/runtime/async_runtime/os.rs b/tentacle/src/runtime/async_runtime/os.rs index 3c3bbe58..c58a6aed 100644 --- a/tentacle/src/runtime/async_runtime/os.rs +++ b/tentacle/src/runtime/async_runtime/os.rs @@ -1,6 +1,9 @@ use crate::{ - runtime::CompatStream2, - service::config::{TcpSocket, TcpSocketConfig}, + runtime::{proxy::socks5_config, CompatStream2}, + service::{ + config::{TcpSocket, TcpSocketConfig, TcpSocketTransformer}, + ProxyConfig, + }, }; use async_io::Async; use async_std::net::{TcpListener as AsyncListener, TcpStream as AsyncStream, ToSocketAddrs}; @@ -12,6 +15,7 @@ use futures::{ future::select, FutureExt, SinkExt, StreamExt, }; +use multiaddr::MultiAddr; use std::{ pin::Pin, task::{Context, Poll}, @@ -207,3 +211,28 @@ pub(crate) async fn connect( Some(err) => Err(err), } } + +async fn connect_by_proxy( + target_addr: A, + tcp_socket_transformer: TcpSocketTransformer, + proxy_config: ProxyConfig, +) -> io::Result +where + A: Into, +{ + let socks5_config = socks5_config::parse(&proxy_config.proxy_url)?; + + let dial_addr: SocketAddr = socks5_config.proxy_url.parse().map_err(io::Error::other)?; + let stream = connect_direct(dial_addr, tcp_socket_transformer).await?; + + crate::runtime::proxy::socks5::establish_connection(stream, target_addr, socks5_config) + .await + .map_err(io::Error::other) +} + +pub(crate) async fn connect_onion( + onion_addr: MultiAddr, + tcp_config: TcpSocketConfig, +) -> io::Result { + todo!() +}