From 24fc2990f5f66a32912d83c16626bc47216dfa76 Mon Sep 17 00:00:00 2001 From: sword-jin Date: Mon, 22 Jul 2024 02:09:15 +0000 Subject: [PATCH] pass &str to new_tunnel not Bytes --- examples/crawler.rs | 7 ++- src/bin/castle.rs | 19 ++----- src/client/client.rs | 2 +- src/client/tunnel.rs | 22 ++++---- tests/lib.rs | 121 ++++++++++++++++--------------------------- 5 files changed, 64 insertions(+), 107 deletions(-) diff --git a/examples/crawler.rs b/examples/crawler.rs index 1152b52..d3c8eb1 100644 --- a/examples/crawler.rs +++ b/examples/crawler.rs @@ -5,7 +5,6 @@ use std::{ }; use async_shutdown::ShutdownManager; -use bytes::Bytes; use castled::{client::tunnel::new_http_tunnel, util}; use serde::Deserialize; use tokio::process::Child; @@ -80,10 +79,10 @@ async fn main() -> anyhow::Result<()> { let _ = castled_client .start_tunnel( new_http_tunnel( - "foo".to_string(), + "foo", in_memory_server.address().to_owned(), - Bytes::from(mock_host), - Bytes::from(""), + mock_host, + "", false, 0, ), diff --git a/src/bin/castle.rs b/src/bin/castle.rs index d3cc1fa..6c4eb3d 100644 --- a/src/bin/castle.rs +++ b/src/bin/castle.rs @@ -1,5 +1,4 @@ use async_shutdown::ShutdownManager; -use bytes::Bytes; use castled::{ client::{ tunnel::{new_http_tunnel, new_tcp_tunnel, new_udp_tunnel}, @@ -92,11 +91,7 @@ async fn main() -> anyhow::Result<()> { local_addr, } => { let local_endpoint = parse_socket_addr(&local_addr, port).await?; - tunnel = new_tcp_tunnel( - DEFAULT_TCP_TUNNEL_NAME.to_string(), - local_endpoint, - remote_port, - ); + tunnel = new_tcp_tunnel(DEFAULT_TCP_TUNNEL_NAME, local_endpoint, remote_port); } Commands::Udp { port, @@ -104,11 +99,7 @@ async fn main() -> anyhow::Result<()> { local_addr, } => { let local_endpoint = parse_socket_addr(&local_addr, port).await?; - tunnel = new_udp_tunnel( - DEFAULT_UDP_TUNNEL_NAME.to_string(), - local_endpoint, - remote_port, - ); + tunnel = new_udp_tunnel(DEFAULT_UDP_TUNNEL_NAME, local_endpoint, remote_port); } Commands::Http { port, @@ -120,10 +111,10 @@ async fn main() -> anyhow::Result<()> { } => { let local_endpoint = parse_socket_addr(&local_addr, port).await?; tunnel = new_http_tunnel( - DEFAULT_HTTP_TUNNEL_NAME.to_string(), + DEFAULT_HTTP_TUNNEL_NAME, local_endpoint, - Bytes::from(domain.unwrap_or_default()), - Bytes::from(subdomain.unwrap_or_default()), + &domain.unwrap_or_default(), + &subdomain.unwrap_or_default(), random_subdomain, remote_port.unwrap_or(0), ); diff --git a/src/client/client.rs b/src/client/client.rs index a2bac82..eadff9c 100644 --- a/src/client/client.rs +++ b/src/client/client.rs @@ -52,7 +52,7 @@ impl Client { /// /// async fn run() { /// let client = Client::new("127.0.0.1:6100".parse().unwrap()); - /// let tunnel = new_tcp_tunnel(String::from("my-tunnel"), SocketAddr::from(([127, 0, 0, 1], 8971)), 8080); + /// let tunnel = new_tcp_tunnel("my-tunnel", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080); /// let shutdown = ShutdownManager::new(); /// let entrypoint = client.start_tunnel(tunnel, shutdown.clone()).await.unwrap(); /// println!("entrypoint: {:?}", entrypoint); diff --git a/src/client/tunnel.rs b/src/client/tunnel.rs index 6a70729..33754cc 100644 --- a/src/client/tunnel.rs +++ b/src/client/tunnel.rs @@ -13,10 +13,10 @@ pub struct Tunnel { pub(crate) local_endpoint: SocketAddr, } -pub fn new_tcp_tunnel(name: String, local_endpoint: SocketAddr, remote_port: u16) -> Tunnel { +pub fn new_tcp_tunnel(name: &str, local_endpoint: SocketAddr, remote_port: u16) -> Tunnel { Tunnel { inner: pb::Tunnel { - name, + name: name.to_string(), r#type: Type::Tcp as i32, config: Some(tunnel::Config::Tcp(TcpConfig { remote_port: remote_port as i32, @@ -27,10 +27,10 @@ pub fn new_tcp_tunnel(name: String, local_endpoint: SocketAddr, remote_port: u16 } } -pub fn new_udp_tunnel(name: String, local_endpoint: SocketAddr, remote_port: u16) -> Tunnel { +pub fn new_udp_tunnel(name: &str, local_endpoint: SocketAddr, remote_port: u16) -> Tunnel { Tunnel { inner: pb::Tunnel { - name, + name: name.to_string(), r#type: Type::Udp as i32, config: Some(tunnel::Config::Udp(UdpConfig { remote_port: remote_port as i32, @@ -41,21 +41,21 @@ pub fn new_udp_tunnel(name: String, local_endpoint: SocketAddr, remote_port: u16 } } -pub fn new_http_tunnel( - name: String, +pub fn new_http_tunnel<'a>( + name: &'a str, local_endpoint: SocketAddr, - domain: Bytes, - subdomain: Bytes, + domain: &'a str, + subdomain: &'a str, random_subdomain: bool, remote_port: u16, ) -> Tunnel { Tunnel { inner: pb::Tunnel { - name, + name: name.to_string(), r#type: Type::Http as i32, config: Some(tunnel::Config::Http(get_http_config( - domain, - subdomain, + Bytes::copy_from_slice(domain.as_bytes()), + Bytes::copy_from_slice(subdomain.as_bytes()), random_subdomain, remote_port, ))), diff --git a/tests/lib.rs b/tests/lib.rs index 1d44846..62af569 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -5,7 +5,6 @@ use crate::common::is_port_listening; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use async_shutdown::ShutdownManager; -use bytes::Bytes; use castled::{ client::{ tunnel::{new_http_tunnel, new_tcp_tunnel, new_udp_tunnel, Tunnel}, @@ -45,7 +44,7 @@ async fn client_register_tcp() { let entrypoint = client .start_tunnel( new_tcp_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8971)), /* no matter */ remote_port, ), @@ -92,7 +91,7 @@ async fn client_register_and_close_then_register_again() { let _ = client .start_tunnel( new_tcp_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8971)), remote_port, ), @@ -118,7 +117,7 @@ async fn client_register_and_close_then_register_again() { let _ = client .start_tunnel( new_tcp_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8971)), /* no matter */ remote_port, ), @@ -163,10 +162,10 @@ async fn register_http_tunnel_with_subdomain() { let _ = client .start_tunnel( new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], local_port)), - Bytes::from(""), - Bytes::from("foo"), + "", + "foo", false, 0, ), @@ -218,27 +217,19 @@ async fn test_assigned_entrypoint() { entrypoint: Default::default(), tunnels: vec![ TestTunnel { - tunnel: new_tcp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_tcp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec![], }, TestTunnel { - tunnel: new_udp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_udp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec![], }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from(""), + "", + "", false, 0, ), @@ -253,27 +244,19 @@ async fn test_assigned_entrypoint() { }, tunnels: vec![ TestTunnel { - tunnel: new_tcp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_tcp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec!["tcp://example.com:8080"], }, TestTunnel { - tunnel: new_udp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_udp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec!["udp://example.com:8080"], }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from(""), + "", + "", false, 9999, ), @@ -281,10 +264,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from("mydomain.com"), - Bytes::from(""), + "mydomain.com", + "", false, 9999, ), @@ -292,10 +275,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from("foo"), + "", + "foo", false, 9999, ), @@ -310,27 +293,19 @@ async fn test_assigned_entrypoint() { }, tunnels: vec![ TestTunnel { - tunnel: new_tcp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_tcp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec!["tcp://127.0.0.1:8080"], }, TestTunnel { - tunnel: new_udp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_udp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec!["udp://127.0.0.1:8080"], }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from(""), + "", + "", false, 9999, ), @@ -338,10 +313,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from("mydomain.com"), - Bytes::from(""), + "mydomain.com", + "", false, 9999, ), @@ -349,10 +324,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from("foo"), + "", + "foo", false, 9999, ), @@ -369,11 +344,7 @@ async fn test_assigned_entrypoint() { }, tunnels: vec![ TestTunnel { - tunnel: new_tcp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_tcp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec![ "tcp://127.0.0.1:8080", "tcp://example.com:8080", @@ -381,11 +352,7 @@ async fn test_assigned_entrypoint() { ], }, TestTunnel { - tunnel: new_udp_tunnel( - "test".to_string(), - SocketAddr::from(([127, 0, 0, 1], 8971)), - 8080, - ), + tunnel: new_udp_tunnel("test", SocketAddr::from(([127, 0, 0, 1], 8971)), 8080), expected: vec![ "udp://127.0.0.1:8080", "udp://example.com:8080", @@ -394,10 +361,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from(""), + "", + "", false, 9999, ), @@ -409,10 +376,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from("mydomain.com"), - Bytes::from(""), + "mydomain.com", + "", false, 9999, ), @@ -420,10 +387,10 @@ async fn test_assigned_entrypoint() { }, TestTunnel { tunnel: new_http_tunnel( - "test".to_string(), + "test", SocketAddr::from(([127, 0, 0, 1], 8080)), - Bytes::from(""), - Bytes::from("bar"), + "", + "bar", false, 9999, ),