Skip to content

Commit f1231aa

Browse files
committed
feat: ws add padding
1 parent 9fab91a commit f1231aa

File tree

7 files changed

+43
-19
lines changed

7 files changed

+43
-19
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shuttle"
3-
version = "0.6.9"
3+
version = "0.6.10"
44
edition = "2021"
55
publish = false
66

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ check:
2121

2222
publish:
2323
cargo publish --registry crates-io --manifest-path shuttle-station/Cargo.toml
24+
cargo doc
2425

shuttle-station/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shuttle-station"
3-
version = "0.6.9"
3+
version = "0.6.10"
44
edition = "2021"
55
authors = ["Born <[email protected]>"]
66
description = "shuttle-station"

shuttle-station/src/dial.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt;
33
use anyhow::Context;
44
use async_trait::async_trait;
55
use futures::SinkExt;
6+
use futures::StreamExt;
67
use socks5_proto::Address;
78
use tokio::net::TcpStream;
89
use tokio_rustls::client::TlsStream;
@@ -35,6 +36,7 @@ pub struct TrojanDial {
3536
pub struct WebSocketDial {
3637
remote_addr: String,
3738
hash: String,
39+
padding: bool,
3840
}
3941

4042
impl TrojanDial {
@@ -49,8 +51,12 @@ impl TrojanDial {
4951
}
5052

5153
impl WebSocketDial {
52-
pub fn new(remote_addr: String, hash: String) -> Self {
53-
Self { remote_addr, hash }
54+
pub fn new(remote_addr: String, hash: String, padding: bool) -> Self {
55+
Self {
56+
remote_addr,
57+
hash,
58+
padding,
59+
}
5460
}
5561
}
5662

@@ -131,15 +137,24 @@ impl Dial<WebSocketCopyStream<MaybeTlsStream<TcpStream>>> for WebSocketDial {
131137
.await
132138
.context(format!("WebSocket can't connect remote {}", remote_addr))?;
133139

134-
let mut buf: Vec<u8> = vec![];
135-
let req = trojan::Request::new(self.hash.clone(), Command::Connect, addr);
136-
req.write_to_buf(&mut buf);
137-
138-
ws.send(Message::Binary(buf))
139-
.await
140-
.context("WebSocket can't send")?;
141-
ws.flush().await?;
142-
140+
if self.padding {
141+
let mut buf: Vec<u8> = vec![];
142+
let req = trojan::Request::new(self.hash.clone(), Command::Padding, addr);
143+
req.write_to_buf(&mut buf);
144+
ws.send(Message::Binary(buf))
145+
.await
146+
.context("WebSocket can't send")?;
147+
ws.flush().await?;
148+
let _ = ws.next().await;
149+
} else {
150+
let mut buf: Vec<u8> = vec![];
151+
let req = trojan::Request::new(self.hash.clone(), Command::Connect, addr);
152+
req.write_to_buf(&mut buf);
153+
ws.send(Message::Binary(buf))
154+
.await
155+
.context("WebSocket can't send")?;
156+
ws.flush().await?;
157+
}
143158
Ok(WebSocketCopyStream::new(ws))
144159
}
145160
}

shuttle-station/src/proto/padding.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ pub struct Padding {
99
}
1010

1111
impl Padding {
12+
pub fn new(start: u16, end: u16) -> Self {
13+
let mut rng = rand::thread_rng();
14+
let len = rng.gen_range(start..end);
15+
Self { len }
16+
}
17+
1218
pub async fn read_from<R>(stream: &mut R) -> anyhow::Result<Self>
1319
where
1420
R: AsyncRead + Unpin,
@@ -52,8 +58,6 @@ impl Padding {
5258

5359
impl Default for Padding {
5460
fn default() -> Self {
55-
let mut rng = rand::thread_rng();
56-
let len = rng.gen_range(100..3000);
57-
Self { len }
61+
Self::new(2500, 4500)
5862
}
5963
}

src/client.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ async fn proxy_handle(cc: Arc<ClientConfig>, ts: TcpStream) {
8989
(ProxyMode::Websocket, _) => {
9090
ProxyConnection::new(
9191
ts,
92-
Box::new(WebSocketDial::new(cc.remote_addr.clone(), cc.hash.clone())),
92+
Box::new(WebSocketDial::new(
93+
cc.remote_addr.clone(),
94+
cc.hash.clone(),
95+
cc.padding,
96+
)),
9397
)
9498
.handle()
9599
.await;

0 commit comments

Comments
 (0)