Skip to content

Commit

Permalink
Merge pull request #20 from trinity-1686a/generic-socket
Browse files Browse the repository at this point in the history
Allow to take arbitrary socket instead of address to estaplish connexion to proxy
  • Loading branch information
sticnarf authored Jul 15, 2020
2 parents d88e7da + 1af569a commit 55f95ad
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: |
sudo add-apt-repository ppa:artyom.h31/3proxy -y
sudo apt-get update
sudo apt-get install 3proxy -y
sudo apt-get install 3proxy socat -y
- name: Run tests
run: |
cargo build --verbose --all
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ either = "1"
thiserror = "1.0"

[dev-dependencies]
tokio = { version = "0.2", features = ["io-util", "rt-threaded"] }
tokio = { version = "0.2", features = ["io-util", "rt-threaded", "uds", "dns"] }
once_cell = "1.2.0"
hyper = "0.13"
44 changes: 44 additions & 0 deletions examples/socket.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 0 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,5 @@ pub enum Error {
PasswordAuthFailure(u8),
}


///// Result type of `tokio-socks`
// pub type Result<T> = std::result::Result<T, Error>;
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Stream for ProxyAddrsStream {
Some(Err(_)) => {
let err = self.0.take().unwrap().unwrap_err();
Poll::Ready(Some(Err(err.into())))
}
},
None => unreachable!(),
}
}
Expand Down Expand Up @@ -235,8 +235,7 @@ impl IntoTargetAddr<'static> for (String, u16) {
}

impl<'a, T> IntoTargetAddr<'a> for &'a T
where
T: IntoTargetAddr<'a> + Copy,
where T: IntoTargetAddr<'a> + Copy
{
fn into_target_addr(self) -> Result<TargetAddr<'a>> {
(*self).into_target_addr()
Expand Down Expand Up @@ -299,9 +298,7 @@ mod tests {
}

fn into_target_addr<'a, T>(t: T) -> Result<TargetAddr<'a>>
where
T: IntoTargetAddr<'a>,
{
where T: IntoTargetAddr<'a> {
t.into_target_addr()
}

Expand Down
Loading

0 comments on commit 55f95ad

Please sign in to comment.