integration_test.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. use anyhow::Result;
  2. use common::{run_rathole_client, PING, PONG};
  3. use rand::Rng;
  4. use std::time::Duration;
  5. use tokio::{
  6. io::{AsyncReadExt, AsyncWriteExt},
  7. net::{TcpStream, UdpSocket},
  8. sync::broadcast,
  9. time,
  10. };
  11. use tracing::{debug, info, instrument};
  12. use tracing_subscriber::EnvFilter;
  13. use crate::common::run_rathole_server;
  14. mod common;
  15. const ECHO_SERVER_ADDR: &str = "0.0.0.0:8080";
  16. const PINGPONG_SERVER_ADDR: &str = "0.0.0.0:8081";
  17. const ECHO_SERVER_ADDR_EXPOSED: &str = "0.0.0.0:2334";
  18. const PINGPONG_SERVER_ADDR_EXPOSED: &str = "0.0.0.0:2335";
  19. const HITTER_NUM: usize = 4;
  20. #[derive(Clone, Copy, Debug)]
  21. enum Type {
  22. Tcp,
  23. Udp,
  24. }
  25. fn init() {
  26. let level = "info";
  27. let _ = tracing_subscriber::fmt()
  28. .with_env_filter(
  29. EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::from(level)),
  30. )
  31. .try_init();
  32. }
  33. #[tokio::test]
  34. async fn tcp() -> Result<()> {
  35. init();
  36. // Spawn a echo server
  37. tokio::spawn(async move {
  38. if let Err(e) = common::tcp::echo_server(ECHO_SERVER_ADDR).await {
  39. panic!("Failed to run the echo server for testing: {:?}", e);
  40. }
  41. });
  42. // Spawn a pingpong server
  43. tokio::spawn(async move {
  44. if let Err(e) = common::tcp::pingpong_server(PINGPONG_SERVER_ADDR).await {
  45. panic!("Failed to run the pingpong server for testing: {:?}", e);
  46. }
  47. });
  48. test("tests/for_tcp/tcp_transport.toml", Type::Tcp).await?;
  49. test("tests/for_tcp/tls_transport.toml", Type::Tcp).await?;
  50. test("tests/for_tcp/noise_transport.toml", Type::Tcp).await?;
  51. Ok(())
  52. }
  53. #[tokio::test]
  54. async fn udp() -> Result<()> {
  55. init();
  56. // Spawn a echo server
  57. tokio::spawn(async move {
  58. if let Err(e) = common::udp::echo_server(ECHO_SERVER_ADDR).await {
  59. panic!("Failed to run the echo server for testing: {:?}", e);
  60. }
  61. });
  62. // Spawn a pingpong server
  63. tokio::spawn(async move {
  64. if let Err(e) = common::udp::pingpong_server(PINGPONG_SERVER_ADDR).await {
  65. panic!("Failed to run the pingpong server for testing: {:?}", e);
  66. }
  67. });
  68. test("tests/for_udp/tcp_transport.toml", Type::Udp).await?;
  69. test("tests/for_udp/tls_transport.toml", Type::Udp).await?;
  70. test("tests/for_udp/noise_transport.toml", Type::Udp).await?;
  71. Ok(())
  72. }
  73. #[instrument]
  74. async fn test(config_path: &'static str, t: Type) -> Result<()> {
  75. let (client_shutdown_tx, client_shutdown_rx) = broadcast::channel(1);
  76. let (server_shutdown_tx, server_shutdown_rx) = broadcast::channel(1);
  77. // Start the client
  78. info!("start the client");
  79. let client = tokio::spawn(async move {
  80. run_rathole_client(config_path, client_shutdown_rx)
  81. .await
  82. .unwrap();
  83. });
  84. // Sleep for 1 second. Expect the client keep retrying to reach the server
  85. time::sleep(Duration::from_secs(1)).await;
  86. // Start the server
  87. info!("start the server");
  88. let server = tokio::spawn(async move {
  89. run_rathole_server(config_path, server_shutdown_rx)
  90. .await
  91. .unwrap();
  92. });
  93. time::sleep(Duration::from_millis(2000)).await; // Wait for the client to retry
  94. info!("echo");
  95. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  96. info!("pingpong");
  97. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  98. .await
  99. .unwrap();
  100. // Simulate the client crash and restart
  101. info!("shutdown the client");
  102. client_shutdown_tx.send(true)?;
  103. let _ = tokio::join!(client);
  104. info!("restart the client");
  105. let client_shutdown_rx = client_shutdown_tx.subscribe();
  106. let client = tokio::spawn(async move {
  107. run_rathole_client(config_path, client_shutdown_rx)
  108. .await
  109. .unwrap();
  110. });
  111. time::sleep(Duration::from_secs(1)).await; // Wait for the client to start
  112. info!("echo");
  113. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  114. info!("pingpong");
  115. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  116. .await
  117. .unwrap();
  118. // Simulate the server crash and restart
  119. info!("shutdown the server");
  120. server_shutdown_tx.send(true)?;
  121. let _ = tokio::join!(server);
  122. info!("restart the server");
  123. let server_shutdown_rx = server_shutdown_tx.subscribe();
  124. let server = tokio::spawn(async move {
  125. run_rathole_server(config_path, server_shutdown_rx)
  126. .await
  127. .unwrap();
  128. });
  129. time::sleep(Duration::from_millis(2000)).await; // Wait for the client to retry
  130. // Simulate heavy load
  131. info!("lots of echo and pingpong");
  132. let mut v = Vec::new();
  133. for _ in 0..HITTER_NUM / 2 {
  134. v.push(tokio::spawn(async move {
  135. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  136. }));
  137. v.push(tokio::spawn(async move {
  138. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  139. .await
  140. .unwrap();
  141. }));
  142. }
  143. for h in v {
  144. assert!(tokio::join!(h).0.is_ok());
  145. }
  146. // Shutdown
  147. info!("shutdown the server and the client");
  148. server_shutdown_tx.send(true)?;
  149. client_shutdown_tx.send(true)?;
  150. let _ = tokio::join!(server, client);
  151. Ok(())
  152. }
  153. async fn echo_hitter(addr: &'static str, t: Type) -> Result<()> {
  154. match t {
  155. Type::Tcp => tcp_echo_hitter(addr).await,
  156. Type::Udp => udp_echo_hitter(addr).await,
  157. }
  158. }
  159. async fn pingpong_hitter(addr: &'static str, t: Type) -> Result<()> {
  160. match t {
  161. Type::Tcp => tcp_pingpong_hitter(addr).await,
  162. Type::Udp => udp_pingpong_hitter(addr).await,
  163. }
  164. }
  165. async fn tcp_echo_hitter(addr: &'static str) -> Result<()> {
  166. let mut conn = TcpStream::connect(addr).await?;
  167. let mut wr = [0u8; 1024];
  168. let mut rd = [0u8; 1024];
  169. for _ in 0..100 {
  170. rand::thread_rng().fill(&mut wr);
  171. conn.write_all(&wr).await?;
  172. conn.read_exact(&mut rd).await?;
  173. assert_eq!(wr, rd);
  174. }
  175. Ok(())
  176. }
  177. async fn udp_echo_hitter(addr: &'static str) -> Result<()> {
  178. let conn = UdpSocket::bind("0.0.0.0:0").await?;
  179. conn.connect(addr).await?;
  180. let mut wr = [0u8; 128];
  181. let mut rd = [0u8; 128];
  182. for _ in 0..3 {
  183. rand::thread_rng().fill(&mut wr);
  184. conn.send(&wr).await?;
  185. debug!("send");
  186. conn.recv(&mut rd).await?;
  187. debug!("recv");
  188. assert_eq!(wr, rd);
  189. }
  190. Ok(())
  191. }
  192. async fn tcp_pingpong_hitter(addr: &'static str) -> Result<()> {
  193. let mut conn = TcpStream::connect(addr).await?;
  194. let wr = PING.as_bytes();
  195. let mut rd = [0u8; PONG.len()];
  196. for _ in 0..100 {
  197. conn.write_all(wr).await?;
  198. conn.read_exact(&mut rd).await?;
  199. assert_eq!(rd, PONG.as_bytes());
  200. }
  201. Ok(())
  202. }
  203. async fn udp_pingpong_hitter(addr: &'static str) -> Result<()> {
  204. let conn = UdpSocket::bind("0.0.0.0:0").await?;
  205. conn.connect(&addr).await?;
  206. let wr = PING.as_bytes();
  207. let mut rd = [0u8; PONG.len()];
  208. for _ in 0..3 {
  209. conn.send(wr).await?;
  210. debug!("ping");
  211. conn.recv(&mut rd).await?;
  212. debug!("pong");
  213. assert_eq!(rd, PONG.as_bytes());
  214. }
  215. Ok(())
  216. }