integration_test.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 = "127.0.0.1:8080";
  16. const PINGPONG_SERVER_ADDR: &str = "127.0.0.1:8081";
  17. const ECHO_SERVER_ADDR_EXPOSED: &str = "127.0.0.1:2334";
  18. const PINGPONG_SERVER_ADDR_EXPOSED: &str = "127.0.0.1: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. // FIXME: Self-signed certificate on Mac requires mannual interference. Disable CI for now
  50. #[cfg(not(target_os = "macos"))]
  51. #[cfg(feature="tls")]
  52. test("tests/for_tcp/tls_transport.toml", Type::Tcp).await?;
  53. #[cfg(feature="noise")]
  54. test("tests/for_tcp/noise_transport.toml", Type::Tcp).await?;
  55. #[cfg(feature="websocket")]
  56. test("tests/for_tcp/websocket_transport.toml", Type::Tcp).await?;
  57. #[cfg(not(target_os = "macos"))]
  58. #[cfg(feature="websocket")]
  59. test("tests/for_tcp/websocket_tls_transport.toml", Type::Tcp).await?;
  60. Ok(())
  61. }
  62. #[tokio::test]
  63. async fn udp() -> Result<()> {
  64. init();
  65. // Spawn a echo server
  66. tokio::spawn(async move {
  67. if let Err(e) = common::udp::echo_server(ECHO_SERVER_ADDR).await {
  68. panic!("Failed to run the echo server for testing: {:?}", e);
  69. }
  70. });
  71. // Spawn a pingpong server
  72. tokio::spawn(async move {
  73. if let Err(e) = common::udp::pingpong_server(PINGPONG_SERVER_ADDR).await {
  74. panic!("Failed to run the pingpong server for testing: {:?}", e);
  75. }
  76. });
  77. test("tests/for_udp/tcp_transport.toml", Type::Udp).await?;
  78. // See above
  79. #[cfg(not(target_os = "macos"))]
  80. #[cfg(feature="tls")]
  81. test("tests/for_udp/tls_transport.toml", Type::Udp).await?;
  82. #[cfg(feature="noise")]
  83. test("tests/for_udp/noise_transport.toml", Type::Udp).await?;
  84. #[cfg(feature="websocket")]
  85. test("tests/for_udp/websocket_transport.toml", Type::Udp).await?;
  86. #[cfg(not(target_os = "macos"))]
  87. #[cfg(feature="websocket")]
  88. test("tests/for_udp/websocket_tls_transport.toml", Type::Udp).await?;
  89. Ok(())
  90. }
  91. #[instrument]
  92. async fn test(config_path: &'static str, t: Type) -> Result<()> {
  93. let (client_shutdown_tx, client_shutdown_rx) = broadcast::channel(1);
  94. let (server_shutdown_tx, server_shutdown_rx) = broadcast::channel(1);
  95. // Start the client
  96. info!("start the client");
  97. let client = tokio::spawn(async move {
  98. run_rathole_client(config_path, client_shutdown_rx)
  99. .await
  100. .unwrap();
  101. });
  102. // Sleep for 1 second. Expect the client keep retrying to reach the server
  103. time::sleep(Duration::from_secs(1)).await;
  104. // Start the server
  105. info!("start the server");
  106. let server = tokio::spawn(async move {
  107. run_rathole_server(config_path, server_shutdown_rx)
  108. .await
  109. .unwrap();
  110. });
  111. time::sleep(Duration::from_millis(2500)).await; // Wait for the client to retry
  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 client crash and restart
  119. info!("shutdown the client");
  120. client_shutdown_tx.send(true)?;
  121. let _ = tokio::join!(client);
  122. info!("restart the client");
  123. let client_shutdown_rx = client_shutdown_tx.subscribe();
  124. let client = tokio::spawn(async move {
  125. run_rathole_client(config_path, client_shutdown_rx)
  126. .await
  127. .unwrap();
  128. });
  129. time::sleep(Duration::from_secs(1)).await; // Wait for the client to start
  130. info!("echo");
  131. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  132. info!("pingpong");
  133. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  134. .await
  135. .unwrap();
  136. // Simulate the server crash and restart
  137. info!("shutdown the server");
  138. server_shutdown_tx.send(true)?;
  139. let _ = tokio::join!(server);
  140. info!("restart the server");
  141. let server_shutdown_rx = server_shutdown_tx.subscribe();
  142. let server = tokio::spawn(async move {
  143. run_rathole_server(config_path, server_shutdown_rx)
  144. .await
  145. .unwrap();
  146. });
  147. time::sleep(Duration::from_millis(2500)).await; // Wait for the client to retry
  148. // Simulate heavy load
  149. info!("lots of echo and pingpong");
  150. let mut v = Vec::new();
  151. for _ in 0..HITTER_NUM / 2 {
  152. v.push(tokio::spawn(async move {
  153. echo_hitter(ECHO_SERVER_ADDR_EXPOSED, t).await.unwrap();
  154. }));
  155. v.push(tokio::spawn(async move {
  156. pingpong_hitter(PINGPONG_SERVER_ADDR_EXPOSED, t)
  157. .await
  158. .unwrap();
  159. }));
  160. }
  161. for h in v {
  162. assert!(tokio::join!(h).0.is_ok());
  163. }
  164. // Shutdown
  165. info!("shutdown the server and the client");
  166. server_shutdown_tx.send(true)?;
  167. client_shutdown_tx.send(true)?;
  168. let _ = tokio::join!(server, client);
  169. Ok(())
  170. }
  171. async fn echo_hitter(addr: &'static str, t: Type) -> Result<()> {
  172. match t {
  173. Type::Tcp => tcp_echo_hitter(addr).await,
  174. Type::Udp => udp_echo_hitter(addr).await,
  175. }
  176. }
  177. async fn pingpong_hitter(addr: &'static str, t: Type) -> Result<()> {
  178. match t {
  179. Type::Tcp => tcp_pingpong_hitter(addr).await,
  180. Type::Udp => udp_pingpong_hitter(addr).await,
  181. }
  182. }
  183. async fn tcp_echo_hitter(addr: &'static str) -> Result<()> {
  184. let mut conn = TcpStream::connect(addr).await?;
  185. let mut wr = [0u8; 1024];
  186. let mut rd = [0u8; 1024];
  187. for _ in 0..100 {
  188. rand::thread_rng().fill(&mut wr);
  189. conn.write_all(&wr).await?;
  190. conn.read_exact(&mut rd).await?;
  191. assert_eq!(wr, rd);
  192. }
  193. Ok(())
  194. }
  195. async fn udp_echo_hitter(addr: &'static str) -> Result<()> {
  196. let conn = UdpSocket::bind("127.0.0.1:0").await?;
  197. conn.connect(addr).await?;
  198. let mut wr = [0u8; 128];
  199. let mut rd = [0u8; 128];
  200. for _ in 0..3 {
  201. rand::thread_rng().fill(&mut wr);
  202. conn.send(&wr).await?;
  203. debug!("send");
  204. conn.recv(&mut rd).await?;
  205. debug!("recv");
  206. assert_eq!(wr, rd);
  207. }
  208. Ok(())
  209. }
  210. async fn tcp_pingpong_hitter(addr: &'static str) -> Result<()> {
  211. let mut conn = TcpStream::connect(addr).await?;
  212. let wr = PING.as_bytes();
  213. let mut rd = [0u8; PONG.len()];
  214. for _ in 0..100 {
  215. conn.write_all(wr).await?;
  216. conn.read_exact(&mut rd).await?;
  217. assert_eq!(rd, PONG.as_bytes());
  218. }
  219. Ok(())
  220. }
  221. async fn udp_pingpong_hitter(addr: &'static str) -> Result<()> {
  222. let conn = UdpSocket::bind("127.0.0.1:0").await?;
  223. conn.connect(&addr).await?;
  224. let wr = PING.as_bytes();
  225. let mut rd = [0u8; PONG.len()];
  226. for _ in 0..3 {
  227. conn.send(wr).await?;
  228. debug!("ping");
  229. conn.recv(&mut rd).await?;
  230. debug!("pong");
  231. assert_eq!(rd, PONG.as_bytes());
  232. }
  233. Ok(())
  234. }