use async_proxy::clients::socks4::no_ident::Socks4NoIdent;
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, SocketAddrV4};
use std::time::Duration;
use std::process::exit;
#[tokio::main]
async fn main() {
let proxy_addr: SocketAddr = "104.248.63.15:30588".parse().unwrap();
let dest_addr: SocketAddrV4 = "52.20.16.20:30000".parse().unwrap();
let timeouts = ConnectionTimeouts::new(
Duration::from_secs(8),
Duration::from_secs(8),
Duration::from_secs(8)
);
let mut socks4_proxy = Socks4NoIdent::new(dest_addr, timeouts);
println!("Starting connection to the Socks4 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 socks4 proxy `{}`",
dest_addr, proxy_addr);
let mut stream = match socks4_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);
}