1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
use anyhow::Result;
use common::{run_rathole_client, PING, PONG};
use rand::Rng;
use std::time::Duration;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpStream, UdpSocket},
sync::broadcast,
time,
};
use tracing::{debug, info, instrument};
use tracing_subscriber::EnvFilter;
use crate::common::run_rathole_server;
mod common;
const ECHO_SERVER_ADDR: &str = "127.0.0.1:8080";
const PINGPONG_SERVER_ADDR: &str = "127.0.0.1:8081";
const ECHO_SERVER_ADDR_EXPOSED: &str = "127.0.0.1:2334";
const PINGPONG_SERVER_ADDR_EXPOSED: &str = "127.0.0.1:2335";
const HITTER_NUM: usize = 4;
#[derive(Clone, Copy, Debug)]
enum Type {
Tcp,
Udp,
}
fn init() {
let level = "info";
let _ = tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::from(level)),
)
.try_init();
}
#[tokio::test]
async fn tcp() -> Result<()> {
init();
// Spawn a echo server
tokio::spawn(async move {
if let Err(e) = common::tcp::echo_server(ECHO_SERVER_ADDR).await {
panic!("Failed to run the echo server for testing: {:?}", e);
}
});
// Spawn a pingpong server
tokio::spawn(async move {
if let Err(e) = common::tcp::pingpong_server(PINGPONG_SERVER_ADDR).await {
panic!("Failed to run the pingpong server for testing: {:?}", e);
}
});
test("tests/for_tcp/tcp_transport.toml", Type::Tcp).await?;
// FIXME: Self-signed certificate on Mac requires mannual interference. Disable CI for now
#[cfg(not(target_os = "macos"))]
#[cfg(feature="tls")]
test("tests/for_tcp/tls_transport.toml", Type::Tcp).await?;
#[cfg(feature="noise")]
test("tests/for_tcp/noise_transport.toml", Type::Tcp).await?;
#[cfg(feature="websocket")]
test("tests/for_tcp/websocket_transport.toml", Type::Tcp).await?;
#[cfg(not(target_os = "macos"))]
#[cfg(feature="websocket")]
test("tests/for_tcp/websocket_tls_transport.toml", Type::Tcp).await?;
Ok(())
}
#[tokio::test]
async fn udp() -> Result<()> {
init();
// Spawn a echo server
tokio::spawn(async move {
if let Err(e) = common::udp::echo_server(ECHO_SERVER_ADDR).await {
panic!("Failed to run the echo server for testing: {:?}", e);
}
});
// Spawn a pingpong server
tokio::spawn(async move {
if let Err(e) = common::udp::pingpong_server(PINGPONG_SERVER_ADDR).await {
panic!("Failed to run the pingpong server for testing: {:?}", e);
}
});
test("tests/for_udp/tcp_transport.toml", Type::Udp).await?;
// See above
#[cfg(not(target_os = "macos"))]
#[cfg(feature="tls")]
test("tests/for_udp/tls_transport.toml", Type::Udp).await?;
#[cfg(feature="noise")]
test("tests/for_udp/noise_transport.toml", Type::Udp).await?;
#[cfg(feature="websocket")]
test("tests/for_udp/websocket_transport.toml", Type::Udp).await?;
#[cfg(not(target_os = "macos"))]
#[cfg(feature="websocket")]
test("tests/for_udp/websocket_tls_transport.toml", Type::Udp).await?;
Ok(())
}
#[instrument]
async fn test(config_path: &'static str, t: Type) -> Result<()> {
let (client_shutdown_tx, client_shutdown_rx) = broadcast::channel(1);
let (server_shutdown_tx, server_shutdown_rx) = broadcast::channel(1);
// Start the client
info!("start the client");
let client = tokio::spawn(async move {
run_rathole_client(config_path, client_shutdown_rx)
.await
.unwrap();
});
// Sleep for 1 second. Expect the client keep retrying to reach the server
time::sleep(Duration::from_secs(1)).await;
// Start the server
info!("start the server");
let server = tokio::spawn(async move {
run_rathole_server(config_path, server_shutdown_rx)
.await
.unwrap();
});
time::sleep(Duration::from_millis(2500)).await; // Wait for the client to retry
info!("echo");
echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
info!("pingpong");
pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
.await
.unwrap();
// Simulate the client crash and restart
info!("shutdown the client");
client_shutdown_tx.send(true)?;
let _ = tokio::join!(client);
info!("restart the client");
let client_shutdown_rx = client_shutdown_tx.subscribe();
let client = tokio::spawn(async move {
run_rathole_client(config_path, client_shutdown_rx)
.await
.unwrap();
});
time::sleep(Duration::from_secs(1)).await; // Wait for the client to start
info!("echo");
echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
info!("pingpong");
pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
.await
.unwrap();
// Simulate the server crash and restart
info!("shutdown the server");
server_shutdown_tx.send(true)?;
let _ = tokio::join!(server);
info!("restart the server");
let server_shutdown_rx = server_shutdown_tx.subscribe();
let server = tokio::spawn(async move {
run_rathole_server(config_path, server_shutdown_rx)
.await
.unwrap();
});
time::sleep(Duration::from_millis(2500)).await; // Wait for the client to retry
// Simulate heavy load
info!("lots of echo and pingpong");
let mut v = Vec::new();
for _ in 0..HITTER_NUM / 2 {
v.push(tokio::spawn(async move {
echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
}));
v.push(tokio::spawn(async move {
pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
.await
.unwrap();
}));
}
for h in v {
assert!(tokio::join!(h).0.is_ok());
}
// Shutdown
info!("shutdown the server and the client");
server_shutdown_tx.send(true)?;
client_shutdown_tx.send(true)?;
let _ = tokio::join!(server, client);
Ok(())
}
async fn echo_hitter(addr: &'static str, t: Type) -> Result<()> {
match t {
Type::Tcp => tcp_echo_hitter(addr).await,
Type::Udp => udp_echo_hitter(addr).await,
}
}
async fn pingpong_hitter(addr: &'static str, t: Type) -> Result<()> {
match t {
Type::Tcp => tcp_pingpong_hitter(addr).await,
Type::Udp => udp_pingpong_hitter(addr).await,
}
}
async fn tcp_echo_hitter(addr: &'static str) -> Result<()> {
let mut conn = TcpStream::connect(addr).await?;
let mut wr = [0u8; 1024];
let mut rd = [0u8; 1024];
for _ in 0..100 {
rand::thread_rng().fill(&mut wr);
conn.write_all(&wr).await?;
conn.read_exact(&mut rd).await?;
assert_eq!(wr, rd);
}
Ok(())
}
async fn udp_echo_hitter(addr: &'static str) -> Result<()> {
let conn = UdpSocket::bind("127.0.0.1:0").await?;
conn.connect(addr).await?;
let mut wr = [0u8; 128];
let mut rd = [0u8; 128];
for _ in 0..3 {
rand::thread_rng().fill(&mut wr);
conn.send(&wr).await?;
debug!("send");
conn.recv(&mut rd).await?;
debug!("recv");
assert_eq!(wr, rd);
}
Ok(())
}
async fn tcp_pingpong_hitter(addr: &'static str) -> Result<()> {
let mut conn = TcpStream::connect(addr).await?;
let wr = PING.as_bytes();
let mut rd = [0u8; PONG.len()];
for _ in 0..100 {
conn.write_all(wr).await?;
conn.read_exact(&mut rd).await?;
assert_eq!(rd, PONG.as_bytes());
}
Ok(())
}
async fn udp_pingpong_hitter(addr: &'static str) -> Result<()> {
let conn = UdpSocket::bind("127.0.0.1:0").await?;
conn.connect(&addr).await?;
let wr = PING.as_bytes();
let mut rd = [0u8; PONG.len()];
for _ in 0..3 {
conn.send(wr).await?;
debug!("ping");
conn.recv(&mut rd).await?;
debug!("pong");
assert_eq!(rd, PONG.as_bytes());
}
Ok(())
}
|