-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Uncle Jack <[email protected]>
- Loading branch information
1 parent
2a1ce02
commit a257134
Showing
10 changed files
with
282 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::proxy::Network; | ||
|
||
use sha2::{Digest, Sha224}; | ||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite}; | ||
use worker::*; | ||
|
||
pub struct Header { | ||
pub network: Network, | ||
pub address: String, | ||
pub port: u16, | ||
} | ||
|
||
pub async fn decode_request_header<S: AsyncRead + AsyncWrite + Unpin>( | ||
stream: &mut S, | ||
password: &str, | ||
) -> Result<Header> { | ||
// TODO: using BufReader instead of reading directly from the stream | ||
|
||
// +-----------------------+---------+----------------+---------+----------+ | ||
// | hex(SHA224(password)) | CRLF | Trojan Request | CRLF | Payload | | ||
// +-----------------------+---------+----------------+---------+----------+ | ||
// | 56 | X'0D0A' | Variable | X'0D0A' | Variable | | ||
// +-----------------------+---------+----------------+---------+----------+ | ||
let mut crlf = [0u8; 2]; | ||
|
||
let mut header_pass = [0u8; 56]; | ||
stream.read_exact(&mut header_pass).await?; | ||
{ | ||
let header_pass = String::from_utf8_lossy(&header_pass); | ||
let password = { | ||
let p = &crate::sha224!(&password)[..]; | ||
crate::hex!(p) | ||
}; | ||
|
||
if password != header_pass { | ||
return Err(Error::RustError("invalid password".to_string())); | ||
} | ||
} | ||
|
||
stream.read_exact(&mut crlf).await?; | ||
|
||
// +-----+------+----------+----------+ | ||
// | CMD | ATYP | DST.ADDR | DST.PORT | | ||
// +-----+------+----------+----------+ | ||
// | 1 | 1 | Variable | 2 | | ||
// +-----+------+----------+----------+ | ||
let network = match stream.read_u8().await? { | ||
0x01 => Network::Tcp, | ||
0x03 => Network::Udp, | ||
_ => return Err(Error::RustError("invalid network type".to_string())), | ||
}; | ||
|
||
let address = match stream.read_u8().await? { | ||
0x01 => crate::common::parse_ipv4(stream).await?, | ||
0x03 => crate::common::parse_domain(stream).await?, | ||
0x04 => crate::common::parse_ipv6(stream).await?, | ||
_ => return Err(Error::RustError("invalid address".to_string())), | ||
}; | ||
let port = { | ||
let mut p = [0u8; 2]; | ||
stream.read_exact(&mut p).await?; | ||
u16::from_be_bytes(p) | ||
}; | ||
|
||
// UDP | ||
// +--------+ | ||
// | Length | | ||
// +--------+ | ||
// | 2 | | ||
// +--------+ | ||
match network { | ||
Network::Udp => { | ||
stream.read_exact(&mut [0u8; 2]).await?; | ||
} | ||
_ => {} | ||
} | ||
stream.read_exact(&mut crlf).await?; | ||
|
||
Ok(Header { | ||
network, | ||
address, | ||
port, | ||
}) | ||
} |
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,115 @@ | ||
use crate::config::{Config, Inbound}; | ||
use crate::proxy::{trojan::encoding, Proxy}; | ||
|
||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use async_trait::async_trait; | ||
use bytes::{BufMut, BytesMut}; | ||
use futures_util::Stream; | ||
use pin_project_lite::pin_project; | ||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; | ||
use worker::*; | ||
|
||
pin_project! { | ||
pub struct TrojanStream<'a> { | ||
pub config: Config, | ||
pub inbound: Inbound, | ||
pub ws: &'a WebSocket, | ||
pub buffer: BytesMut, | ||
#[pin] | ||
pub events: EventStream<'a>, | ||
} | ||
} | ||
|
||
unsafe impl<'a> Send for TrojanStream<'a> {} | ||
|
||
impl<'a> TrojanStream<'a> { | ||
pub fn new( | ||
config: Config, | ||
inbound: Inbound, | ||
events: EventStream<'a>, | ||
ws: &'a WebSocket, | ||
) -> Self { | ||
let buffer = BytesMut::new(); | ||
|
||
Self { | ||
config, | ||
inbound, | ||
ws, | ||
buffer, | ||
events, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<'a> Proxy for TrojanStream<'a> { | ||
async fn process(&mut self) -> Result<()> { | ||
let password = self.inbound.password.clone(); | ||
let header = encoding::decode_request_header(&mut self, &password).await?; | ||
|
||
let mut context = self.inbound.context.clone(); | ||
{ | ||
context.address = header.address; | ||
context.port = header.port; | ||
context.network = header.network; | ||
} | ||
|
||
let outbound = self.config.dispatch_outbound(&context); | ||
let mut upstream = crate::proxy::connect_outbound(context, outbound).await?; | ||
|
||
tokio::io::copy_bidirectional(self, &mut upstream).await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'a> AsyncRead for TrojanStream<'a> { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<tokio::io::Result<()>> { | ||
let mut this = self.project(); | ||
|
||
loop { | ||
let size = std::cmp::min(this.buffer.len(), buf.remaining()); | ||
if size > 0 { | ||
buf.put_slice(&this.buffer.split_to(size)); | ||
return Poll::Ready(Ok(())); | ||
} | ||
|
||
match this.events.as_mut().poll_next(cx) { | ||
Poll::Ready(Some(Ok(WebsocketEvent::Message(msg)))) => { | ||
msg.bytes().iter().for_each(|x| this.buffer.put_slice(&x)); | ||
} | ||
Poll::Pending => return Poll::Pending, | ||
_ => return Poll::Ready(Ok(())), | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a> AsyncWrite for TrojanStream<'a> { | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
_: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<tokio::io::Result<usize>> { | ||
return Poll::Ready( | ||
self.ws | ||
.send_with_bytes(buf) | ||
.map(|_| buf.len()) | ||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string())), | ||
); | ||
} | ||
|
||
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<tokio::io::Result<()>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<tokio::io::Result<()>> { | ||
unimplemented!() | ||
} | ||
} |
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,2 @@ | ||
mod encoding; | ||
pub mod inbound; |
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
Oops, something went wrong.