Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: trojan outbound #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"format": "ip"
}
},
"password": {
"default": "",
"type": "string"
},
"port": {
"default": 0,
"type": "integer",
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct Outbound {
pub port: u16,
#[serde(default)]
pub uuid: Uuid,
#[serde(default)]
pub password: String,
}

#[derive(Default, Clone, Serialize, Deserialize, JsonSchema)]
Expand Down
1 change: 1 addition & 0 deletions src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ async fn connect_outbound(ctx: RequestContext, outbound: Outbound) -> Result<Box

let mut stream: Box<dyn Proxy> = match outbound.protocol {
Protocol::Vless => Box::new(vless::outbound::VlessStream::new(ctx, outbound, socket)),
Protocol::Trojan => Box::new(trojan::outbound::TrojanStream::new(ctx, outbound, socket)),
Protocol::RelayV1 => Box::new(relay::outbound::RelayStream::new(
ctx,
socket,
Expand Down
1 change: 1 addition & 0 deletions src/proxy/trojan/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod encoding;
pub mod inbound;
pub mod outbound;
107 changes: 107 additions & 0 deletions src/proxy/trojan/outbound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::common::encode_addr;
use crate::proxy::{Proxy, RequestContext};

use std::pin::Pin;
use std::task::{Context, Poll};

use crate::config::Outbound;

use async_trait::async_trait;
use bytes::{BufMut, BytesMut};
use sha2::{Digest, Sha224};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf};
use worker::*;

pub struct TrojanStream {
pub stream: Socket,
pub buffer: BytesMut,
pub outbound: Outbound,
context: RequestContext,
handshaked: bool,
}

impl TrojanStream {
pub fn new(context: RequestContext, outbound: Outbound, stream: Socket) -> Self {
let buffer = BytesMut::new();

Self {
context,
outbound,
stream,
buffer,
handshaked: false,
}
}
}

#[async_trait]
impl Proxy for TrojanStream {
async fn process(&mut self) -> Result<()> {
let crlf = [0xd, 0xa];

let mut cmd: Vec<u8> = vec![];

let password = {
let p = &crate::sha224!(&self.outbound.password)[..];
crate::hex!(p)
};
cmd.extend_from_slice(password.as_bytes());

cmd.extend_from_slice(&crlf);
cmd.extend_from_slice(&[
0x1, // TODO: udp
0x1, // TODO: ipv6 & domain
]);

cmd.extend_from_slice(&encode_addr(&self.context.address)?);
cmd.extend_from_slice(&self.context.port.to_be_bytes());

cmd.extend_from_slice(&crlf);

self.stream.write_all(&cmd).await?;

Ok(())
}
}

impl AsyncRead for TrojanStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut ReadBuf<'_>,
) -> Poll<tokio::io::Result<()>> {
if self.buffer.len() > 0 {
let size = std::cmp::min(buf.remaining(), self.buffer.len());
let data = self.buffer.split_to(size);
buf.put_slice(&data);
return Poll::Ready(Ok(()));
}

match Pin::new(&mut self.stream).poll_read(cx, buf) {
Poll::Ready(Ok(())) => {
self.buffer.put_slice(buf.filled());
Poll::Ready(Ok(()))
}
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
Poll::Pending => Poll::Pending,
}
}
}

impl AsyncWrite for TrojanStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8],
) -> Poll<tokio::io::Result<usize>> {
Pin::new(&mut self.stream).poll_write(cx, buf)
}

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!()
}
}