mod.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use std::path::PathBuf;
  2. use anyhow::Result;
  3. use tokio::{
  4. io::{self, AsyncReadExt, AsyncWriteExt},
  5. net::{TcpListener, TcpStream, ToSocketAddrs},
  6. sync::broadcast,
  7. };
  8. pub const PING: &str = "ping";
  9. pub const PONG: &str = "pong";
  10. pub async fn run_rathole_server(
  11. config_path: &str,
  12. shutdown_rx: broadcast::Receiver<bool>,
  13. ) -> Result<()> {
  14. let cli = rathole::Cli {
  15. config_path: PathBuf::from(config_path),
  16. server: true,
  17. client: false,
  18. };
  19. rathole::run(&cli, shutdown_rx).await
  20. }
  21. pub async fn run_rathole_client(
  22. config_path: &str,
  23. shutdown_rx: broadcast::Receiver<bool>,
  24. ) -> Result<()> {
  25. let cli = rathole::Cli {
  26. config_path: PathBuf::from(config_path),
  27. server: false,
  28. client: true,
  29. };
  30. rathole::run(&cli, shutdown_rx).await
  31. }
  32. pub mod tcp {
  33. use super::*;
  34. pub async fn echo_server<A: ToSocketAddrs>(addr: A) -> Result<()> {
  35. let l = TcpListener::bind(addr).await?;
  36. loop {
  37. let (conn, _addr) = l.accept().await?;
  38. tokio::spawn(async move {
  39. let _ = echo(conn).await;
  40. });
  41. }
  42. }
  43. pub async fn pingpong_server<A: ToSocketAddrs>(addr: A) -> Result<()> {
  44. let l = TcpListener::bind(addr).await?;
  45. loop {
  46. let (conn, _addr) = l.accept().await?;
  47. tokio::spawn(async move {
  48. let _ = pingpong(conn).await;
  49. });
  50. }
  51. }
  52. async fn echo(conn: TcpStream) -> Result<()> {
  53. let (mut rd, mut wr) = conn.into_split();
  54. io::copy(&mut rd, &mut wr).await?;
  55. Ok(())
  56. }
  57. async fn pingpong(mut conn: TcpStream) -> Result<()> {
  58. let mut buf = [0u8; PING.len()];
  59. while conn.read_exact(&mut buf).await? != 0 {
  60. assert_eq!(buf, PING.as_bytes());
  61. conn.write_all(PONG.as_bytes()).await?;
  62. }
  63. Ok(())
  64. }
  65. }
  66. pub mod udp {
  67. use rathole::UDP_BUFFER_SIZE;
  68. use tokio::net::UdpSocket;
  69. use tracing::debug;
  70. use super::*;
  71. pub async fn echo_server<A: ToSocketAddrs>(addr: A) -> Result<()> {
  72. let l = UdpSocket::bind(addr).await?;
  73. debug!("UDP echo server listening");
  74. let mut buf = [0u8; UDP_BUFFER_SIZE];
  75. loop {
  76. let (n, addr) = l.recv_from(&mut buf).await?;
  77. debug!("Get {:?} from {}", &buf[..n], addr);
  78. l.send_to(&buf[..n], addr).await?;
  79. }
  80. }
  81. pub async fn pingpong_server<A: ToSocketAddrs>(addr: A) -> Result<()> {
  82. let l = UdpSocket::bind(addr).await?;
  83. let mut buf = [0u8; UDP_BUFFER_SIZE];
  84. loop {
  85. let (n, addr) = l.recv_from(&mut buf).await?;
  86. assert_eq!(&buf[..n], PING.as_bytes());
  87. l.send_to(PONG.as_bytes(), addr).await?;
  88. }
  89. }
  90. }