Skip to content

Commit

Permalink
fix: fix mux error loop
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Mar 3, 2021
1 parent ee3078d commit ce98829
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
11 changes: 8 additions & 3 deletions config/client.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ log_level = "debug" # optional
[trojan]
password = "password"

[socks5]
addr = "127.0.0.1:1080"

[tls]
addr = "example.com:443"
sni = "example.com"
cert = "cert.pem" # optional
cipher = ["TLS13_AES_128_GCM_SHA256"] # optional
#cipher = ["TLS13_CHACHA20_POLY1305_SHA256","TLS13_AES_256_GCM_SHA384","TLS13_AES_128_GCM_SHA256","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA25"]

#optional
# optional
#[websocket]
#uri = "wss://example.com/trojanpath"

[socks5]
addr = "127.0.0.1:1080"
# optional
#[mux]
#concurrent = 8
#timeout = 30
8 changes: 4 additions & 4 deletions config/forward.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ addr = "example.com:443"
sni = "example.com"
cert = "cert.pem" # optional
cipher = ["TLS13_AES_128_GCM_SHA256"] # optional
#cipher = ["TLS13_CHACHA20_POLY1305_SHA256","TLS13_AES_256_GCM_SHA384","TLS13_AES_128_GCM_SHA256","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA25"]
# cipher = ["TLS13_CHACHA20_POLY1305_SHA256","TLS13_AES_256_GCM_SHA384","TLS13_AES_128_GCM_SHA256","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA25"]

#optional
#[websocket]
#uri = "wss://example.com/trojanpath"
# optional
# [websocket]
# uri = "wss://example.com/trojanpath"
13 changes: 10 additions & 3 deletions config/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ sni = "example.com"
cert = "cert.pem"
key = "key.pem"

#optional
#[websocket]
#path = "/trojanpath"
# optional
# [plaintext]
# addr = "127.0.0.1:12345"

# optional
# [websocket]
# path = "/trojanpath"

# optional
# [mux]
8 changes: 7 additions & 1 deletion src/protocol/mux/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ impl MuxAcceptor {
let (accept_stream_tx, accept_stream_rx) = channel(0x40);
let handle: JoinHandle<io::Result<()>> = tokio::spawn(async move {
loop {
let result = inner.accept().await?;
let result = match inner.accept().await {
Ok(r) => r,
Err(e) => {
log::error!("mux accept err: {}", e);
continue;
}
};
match result {
AcceptResult::Tcp((stream, addr)) => {
let accept_stream_tx = accept_stream_tx.clone();
Expand Down
1 change: 1 addition & 0 deletions src/protocol/trojan/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl<T: ProxyAcceptor> ProxyAcceptor for TrojanAcceptor<T> {
}
}
Err(e) => {
log::debug!("first packet {:x?}", first_packet);
let fallback_addr = self.fallback_addr.clone();
log::warn!("fallback to {}", fallback_addr);
tokio::spawn(async move {
Expand Down
21 changes: 12 additions & 9 deletions src/protocol/trojan/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::Error;
use async_trait::async_trait;
use bytes::BufMut;
use sha2::{Digest, Sha224};
use std::{fmt::Write, io};
use tokio::io::{split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadHalf, WriteHalf};
Expand Down Expand Up @@ -104,8 +105,6 @@ impl RequestHeader {
where
R: AsyncRead + Unpin,
{
// TODO dirty code

let mut hash_buf = [0u8; 56];
let size = stream.read(&mut hash_buf).await?;
if size != 56 {
Expand Down Expand Up @@ -143,14 +142,18 @@ impl RequestHeader {
where
W: AsyncWrite + Unpin,
{
// TODO use write buffer
w.write(self.hash.as_bytes()).await?;
let header_len = 56 + 2 + 1 + self.address.serialized_len() + 2;
let mut buf = Vec::with_capacity(header_len);

let cursor = &mut buf;
let crlf = b"\r\n";
w.write(crlf).await?;
let cmd = [self.command.as_u8()];
w.write(&cmd).await?;
self.address.write_to_stream(w).await?;
w.write(crlf).await?;
cursor.put_slice(self.hash.as_bytes());
cursor.put_slice(crlf);
cursor.put_u8(self.command.as_u8());
self.address.write_to_buf(cursor);
cursor.put_slice(crlf);

w.write(&buf).await?;
Ok(())
}
}
Expand Down

0 comments on commit ce98829

Please sign in to comment.