mod.rs 2.8 KB

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