Skip to content

Commit

Permalink
pass &str to new_tunnel not Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
sword-jin committed Jul 22, 2024
1 parent ac39eff commit 24fc299
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 107 deletions.
7 changes: 3 additions & 4 deletions examples/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
),
Expand Down
19 changes: 5 additions & 14 deletions src/bin/castle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_shutdown::ShutdownManager;
use bytes::Bytes;
use castled::{
client::{
tunnel::{new_http_tunnel, new_tcp_tunnel, new_udp_tunnel},
Expand Down Expand Up @@ -92,23 +91,15 @@ 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,
remote_port,
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,
Expand All @@ -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),
);
Expand Down
2 changes: 1 addition & 1 deletion src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions src/client/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
))),
Expand Down
Loading

0 comments on commit 24fc299

Please sign in to comment.