clone from github.com/sticnarf/tokio-socks

This commit is contained in:
2022-03-18 01:16:47 +08:00
parent e5544214bf
commit dc264fbcb2
19 changed files with 1685 additions and 2 deletions

33
examples/chainproxy.rs Normal file
View File

@@ -0,0 +1,33 @@
//! Test the proxy chaining capabilities
//!
//! This example make uses of several public proxy.
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
runtime::Runtime,
};
use tokio_socks::{tcp::Socks5Stream, Error};
const PROXY_ADDR: [&str; 2] = ["184.176.166.20:4145", "90.89.205.248:1080"]; // public proxies found here : http://spys.one/en/socks-proxy-list/
const DEST_ADDR: &str = "duckduckgo.com:80";
async fn connect_chained_proxy() -> Result<(), Error> {
let proxy_stream = TcpStream::connect(PROXY_ADDR[0]).await?;
let chained_proxy_stream = Socks5Stream::connect_with_socket(proxy_stream, PROXY_ADDR[1]).await?;
let mut stream = Socks5Stream::connect_with_socket(chained_proxy_stream, DEST_ADDR).await?;
stream.write_all(b"GET /\n\n").await?;
let mut buf = Vec::new();
let n = stream.read_to_end(&mut buf).await?;
println!("{} bytes read\n\n{}", n, String::from_utf8_lossy(&buf));
Ok(())
}
fn main() {
let rt = Runtime::new().unwrap();
rt.block_on(connect_chained_proxy()).unwrap();
}

44
examples/socket.rs Normal file
View File

@@ -0,0 +1,44 @@
//! Test the tor proxy capabilities
//!
//! This example requires a running tor proxy.
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpStream, UnixStream},
runtime::Runtime,
};
use tokio_socks::{tcp::Socks5Stream, Error};
const UNIX_PROXY_ADDR: &str = "/tmp/tor/socket.s";
const TCP_PROXY_ADDR: &str = "127.0.0.1:9050";
const ONION_ADDR: &str = "3g2upl4pq6kufc4m.onion:80"; // DuckDuckGo
async fn connect() -> Result<(), Error> {
// This require Tor to listen on and Unix Domain Socket.
// You have to create a directory /tmp/tor owned by tor, and for which only tor
// has rights, and add the following line to your torrc :
// SocksPort unix:/tmp/tor/socket.s
let socket = UnixStream::connect(UNIX_PROXY_ADDR).await?;
let target = Socks5Stream::tor_resolve_with_socket(socket, "duckduckgo.com:0").await?;
eprintln!("duckduckgo.com = {:?}", target);
let socket = UnixStream::connect(UNIX_PROXY_ADDR).await?;
let target = Socks5Stream::tor_resolve_ptr_with_socket(socket, "176.34.155.23:0").await?;
eprintln!("176.34.155.23 = {:?}", target);
let socket = TcpStream::connect(TCP_PROXY_ADDR).await?;
socket.set_nodelay(true)?;
let mut conn = Socks5Stream::connect_with_socket(socket, ONION_ADDR).await?;
conn.write_all(b"GET /\n\n").await?;
let mut buf = Vec::new();
let n = conn.read_to_end(&mut buf).await?;
println!("{} bytes read\n\n{}", n, String::from_utf8_lossy(&buf));
Ok(())
}
fn main() {
let mut rt = Runtime::new().unwrap();
rt.block_on(connect()).unwrap();
}

34
examples/tor.rs Normal file
View File

@@ -0,0 +1,34 @@
//! Test the tor proxy capabilities
//!
//! This example requires a running tor proxy.
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
runtime::Runtime,
};
use tokio_socks::{tcp::Socks5Stream, Error};
const PROXY_ADDR: &str = "127.0.0.1:9050";
const ONION_ADDR: &str = "3g2upl4pq6kufc4m.onion:80"; // DuckDuckGo
async fn connect() -> Result<(), Error> {
let target = Socks5Stream::tor_resolve(PROXY_ADDR, "duckduckgo.com:0").await?;
eprintln!("duckduckgo.com = {:?}", target);
let target = Socks5Stream::tor_resolve_ptr(PROXY_ADDR, "176.34.155.23:0").await?;
eprintln!("176.34.155.23 = {:?}", target);
let mut conn = Socks5Stream::connect(PROXY_ADDR, ONION_ADDR).await?;
conn.write_all(b"GET /\n\n").await?;
let mut buf = Vec::new();
let n = conn.read_to_end(&mut buf).await?;
println!("{} bytes read\n\n{}", n, String::from_utf8_lossy(&buf));
Ok(())
}
fn main() {
let mut rt = Runtime::new().unwrap();
rt.block_on(connect()).unwrap();
}