-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature_gssapi_auth_support_socks5
- Loading branch information
Showing
23 changed files
with
831 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#[cfg(feature = "futures-io")] | ||
mod futures; | ||
|
||
/// A compatibility layer for using non-tokio types with this crate. | ||
/// | ||
/// Example: | ||
/// ```no_run | ||
/// use smol::net::unix::UnixStream; | ||
/// use tokio_socks::{io::Compat, tcp::Socks5Stream}; | ||
/// let socket = Compat::new(UnixStream::connect(proxy_addr) | ||
/// .await | ||
/// .map_err(Error::Io)?); // Compat<UnixStream> | ||
/// let conn = | ||
/// Socks5Stream::connect_with_password_and_socket(socket, target, username, password).await?; | ||
/// // Socks5Stream has implemented futures-io AsyncRead + AsyncWrite. | ||
/// ``` | ||
pub struct Compat<S>(S); | ||
|
||
#[cfg(feature = "futures-io")] | ||
impl<S> Compat<S> { | ||
pub fn new(inner: S) -> Self { | ||
Compat(inner) | ||
} | ||
|
||
/// Consumes the `Compat``, returning the inner value. | ||
pub fn into_inner(self) -> S { | ||
self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "futures-io")] | ||
impl<S> AsRef<S> for Compat<S> { | ||
fn as_ref(&self) -> &S { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "futures-io")] | ||
impl<S> AsMut<S> for Compat<S> { | ||
fn as_mut(&mut self) -> &mut S { | ||
&mut self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{ | ||
io::Result as IoResult, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use futures_io::{AsyncRead, AsyncWrite}; | ||
|
||
use super::Compat; | ||
use crate::io::AsyncSocket; | ||
|
||
impl<S> AsyncSocket for Compat<S> | ||
where S: AsyncRead + AsyncWrite + Unpin | ||
{ | ||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<IoResult<usize>> { | ||
AsyncRead::poll_read(Pin::new(&mut self.0), cx, buf) | ||
} | ||
|
||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> { | ||
AsyncWrite::poll_write(Pin::new(&mut self.0), cx, buf) | ||
} | ||
} | ||
|
||
impl<S> AsyncRead for Compat<S> | ||
where S: AsyncRead + Unpin | ||
{ | ||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<IoResult<usize>> { | ||
AsyncRead::poll_read(Pin::new(&mut self.0), cx, buf) | ||
} | ||
} | ||
|
||
impl<S> AsyncWrite for Compat<S> | ||
where S: AsyncWrite + Unpin | ||
{ | ||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> { | ||
AsyncWrite::poll_write(Pin::new(&mut self.0), cx, buf) | ||
} | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IoResult<()>> { | ||
AsyncWrite::poll_flush(Pin::new(&mut self.0), cx) | ||
} | ||
|
||
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IoResult<()>> { | ||
AsyncWrite::poll_close(Pin::new(&mut self.0), cx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//! Asynchronous I/O abstractions for sockets. | ||
#[cfg(feature = "tokio")] | ||
mod tokio; | ||
|
||
use std::{ | ||
future::Future, | ||
io::{Error, ErrorKind}, | ||
mem, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use futures_util::ready; | ||
|
||
#[cfg(feature = "futures-io")] | ||
mod compat; | ||
#[cfg(feature = "futures-io")] | ||
pub use compat::Compat; | ||
|
||
/// A trait for asynchronous socket I/O. | ||
/// | ||
/// Any type that implements tokio's `AsyncRead` and `AsyncWrite` traits | ||
/// has implemented `AsyncSocket` trait. | ||
/// | ||
/// Use `FuturesIoCompatExt` to wrap `futures-io` types as `AsyncSocket` types. | ||
pub trait AsyncSocket { | ||
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize, Error>>; | ||
|
||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, Error>>; | ||
} | ||
|
||
pub(crate) trait AsyncSocketExt { | ||
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> | ||
where Self: Sized; | ||
|
||
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> | ||
where Self: Sized; | ||
} | ||
|
||
impl<S: AsyncSocket> AsyncSocketExt for S { | ||
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> | ||
where Self: Sized { | ||
let capacity = buf.len(); | ||
ReadExact { | ||
reader: self, | ||
buf, | ||
capacity, | ||
} | ||
} | ||
|
||
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> | ||
where Self: Sized { | ||
WriteAll { writer: self, buf } | ||
} | ||
} | ||
|
||
pub(crate) struct ReadExact<'a, R> { | ||
reader: &'a mut R, | ||
buf: &'a mut [u8], | ||
capacity: usize, | ||
} | ||
|
||
impl<R: AsyncSocket + Unpin> Future for ReadExact<'_, R> { | ||
type Output = Result<usize, Error>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = &mut *self; | ||
while !this.buf.is_empty() { | ||
let n = ready!(Pin::new(&mut *this.reader).poll_read(cx, this.buf))?; | ||
{ | ||
let (_, rest) = mem::take(&mut this.buf).split_at_mut(n); | ||
this.buf = rest; | ||
} | ||
if n == 0 { | ||
return Poll::Ready(Err(ErrorKind::UnexpectedEof.into())); | ||
} | ||
} | ||
Poll::Ready(Ok(this.capacity)) | ||
} | ||
} | ||
|
||
pub(crate) struct WriteAll<'a, W> { | ||
writer: &'a mut W, | ||
buf: &'a [u8], | ||
} | ||
|
||
impl<W: AsyncSocket + Unpin> Future for WriteAll<'_, W> { | ||
type Output = Result<(), Error>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = &mut *self; | ||
while !this.buf.is_empty() { | ||
let n = ready!(Pin::new(&mut *this.writer).poll_write(cx, this.buf))?; | ||
{ | ||
let (_, rest) = mem::take(&mut this.buf).split_at(n); | ||
this.buf = rest; | ||
} | ||
if n == 0 { | ||
return Poll::Ready(Err(ErrorKind::WriteZero.into())); | ||
} | ||
} | ||
|
||
Poll::Ready(Ok(())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! AsyncSocket trait implementation for tokio's AsyncRead + AsyncWrite | ||
//! traits. | ||
use std::{ | ||
io::Result as IoResult, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use futures_util::ready; | ||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; | ||
|
||
use super::AsyncSocket; | ||
|
||
impl<S> AsyncSocket for S | ||
where S: AsyncRead + AsyncWrite | ||
{ | ||
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<IoResult<usize>> { | ||
let mut buf = ReadBuf::new(buf); | ||
ready!(AsyncRead::poll_read(self, cx, &mut buf))?; | ||
Poll::Ready(Ok(buf.filled().len())) | ||
} | ||
|
||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> { | ||
AsyncWrite::poll_write(self, cx, buf) | ||
} | ||
} |
Oops, something went wrong.