use async_proxy::clients::socks5::{
Destination, no_auth::TcpNoAuth
};
use async_proxy::general::ConnectionTimeouts;
use async_proxy::proxy::ProxyConstructor;
use tokio::net::TcpStream;
use tokio::time::timeout;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::net::{
SocketAddr, Ipv4Addr
};
use std::time::Duration;
use std::process::exit;
#[tokio::main]
async fn main() {
let proxy_addr: SocketAddr = "72.11.148.222:56533".parse().unwrap();
let timeouts = ConnectionTimeouts::new(
Duration::from_secs(8),
Duration::from_secs(8),
Duration::from_secs(8)
);
let dest_ipaddr: Ipv4Addr = Ipv4Addr::new(52, 20, 16, 20);
const DEST_PORT: u16 = 30_000;
let mut socks5_proxy = TcpNoAuth::new(Destination::Ipv4Addr(dest_ipaddr),
DEST_PORT, timeouts);
println!("Starting connection to the socks5 proxy server `{}`", proxy_addr);
let stream = TcpStream::connect(proxy_addr)
.await
.expect("Unable to connect to the proxy server");
println!("Starting connection to the destination `{}:{}` throught socks5 proxy `{}`",
dest_ipaddr, DEST_PORT, proxy_addr);
let mut stream = match socks5_proxy.connect(stream).await {
Ok(stream) => {
println!("Successfully connected to the service through the proxy");
stream
},
Err(e) => {
println!("Cannot connect to the service: {}", e);
exit(1);
}
};
println!("Please inter a message to be sent. Message: ");
let mut input = String::new();
std::io::stdin().read_line(&mut input)
.expect("Unable to read a line from stdin");
let future = stream.write_all(input.as_bytes());
let future = timeout(Duration::from_secs(8), future);
future.await.expect("Timeout of 8 seconds reached")
.expect("Unable to send the message");
let future = stream.read_to_string(&mut input);
let future = timeout(Duration::from_secs(8), future);
future.await.expect("Timeout of 8 seconds reached")
.expect("Unable to receive a string from the service");
println!("Received message from the service: {}", input);
}