1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::borrow::Cow;
use std::convert::TryFrom;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use actix_connect::{Connect, ConnectError, Connection};
use actix_http::http::Uri;
use actix_service::Service;
use tokio_socks::tcp::Socks5Stream;
use tokio_socks::{Error as SocksConnectError, TargetAddr, ToProxyAddrs};
fn to_connect_error(e: SocksConnectError) -> ConnectError {
ConnectError::Io(std::io::Error::new(std::io::ErrorKind::Other, e))
}
async fn connect_socks<P: ToProxyAddrs + 'static>(
proxy: P,
req: Connect<Uri>,
) -> Result<Connection<Uri, Socks5Stream>, ConnectError> {
let res = tokio_socks::tcp::Socks5Stream::connect(
proxy,
TargetAddr::Domain(Cow::Borrowed(req.host()), req.port()),
)
.await
.map_err(to_connect_error)?;
Ok(Connection::new(
res,
Uri::try_from(format!("{}:{}", req.host(), req.port())).unwrap(),
))
}
#[derive(Clone, Debug)]
pub struct SocksConnector<P: ToProxyAddrs>(P);
impl<P> Service for SocksConnector<P>
where
P: ToProxyAddrs + Copy + 'static,
{
type Request = Connect<Uri>;
type Response = Connection<Uri, Socks5Stream>;
type Error = ConnectError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + 'static>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
Box::pin(connect_socks(self.0, req))
}
}
#[cfg(test)]
mod test {
#[actix_rt::test]
async fn test() {
let client = actix_web::client::ClientBuilder::new()
.connector(
actix_web::client::Connector::new()
.connector(crate::SocksConnector("127.0.0.1:9050"))
.timeout(std::time::Duration::from_secs(60))
.finish(),
)
.finish();
let res = client
.get("http://facebookcorewwwi.onion")
.send()
.await
.unwrap();
println!("{:?}", res);
}
}