From 88b0554a2939e9e2628ca9aa3ae2e09300514c6f Mon Sep 17 00:00:00 2001 From: Alejandro Avagnina Date: Sat, 20 Apr 2024 14:07:25 -0300 Subject: [PATCH] fix: bring back socket path arg (#27) --- src/ports/mod.rs | 2 +- src/ports/tunnel.rs | 57 ++++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/ports/mod.rs b/src/ports/mod.rs index b5214b8..07cb340 100644 --- a/src/ports/mod.rs +++ b/src/ports/mod.rs @@ -36,6 +36,6 @@ pub async fn run(args: Args, cli: &crate::Cli) -> miette::Result<()> { Commands::Details(x) => details::run(x, cli).await, Commands::Create(x) => create::run(x, cli).await, Commands::Delete(x) => delete::run(x, cli).await, - Commands::Tunnel(_x) => tunnel::run(cli).await, + Commands::Tunnel(x) => tunnel::run(x, cli).await, } } diff --git a/src/ports/tunnel.rs b/src/ports/tunnel.rs index 8500a3a..dd69ccb 100644 --- a/src/ports/tunnel.rs +++ b/src/ports/tunnel.rs @@ -10,7 +10,11 @@ use tokio_rustls::TlsConnector; use tracing::{debug, info, warn}; #[derive(Parser)] -pub struct Args {} +pub struct Args { + /// local path for the unix socket + #[arg(long)] + socket: Option, +} pub async fn copy_bytes(s1: T1, s2: T2) -> miette::Result<()> where @@ -110,6 +114,18 @@ fn define_socket_path( Ok(path) } +fn define_default_socket_path( + port: &PortInfo, + dirs: &crate::dirs::Dirs, + ctx: &crate::core::Context, +) -> miette::Result { + let default = dirs + .ensure_tmp_dir(&ctx.namespace.name)? + .join(format!("{}-{}.socket", port.network, port.version)); + + Ok(default.to_string_lossy().to_string()) +} + async fn spawn_new_connection( local: UnixStream, remote_host: &str, @@ -129,7 +145,7 @@ async fn spawn_new_connection( } // #[instrument("connect", skip_all)] -pub async fn run(cli: &crate::Cli) -> miette::Result<()> { +pub async fn run(args: Args, cli: &crate::Cli) -> miette::Result<()> { let ctx = cli .context .as_ref() @@ -201,28 +217,37 @@ pub async fn run(cli: &crate::Cli) -> miette::Result<()> { } } - let default_socket_path = define_socket_path(&port_info, None, &cli.dirs, ctx) - .context("error defining unix socket path")?; + let socket_path: PathBuf; + // check if the socket path is provided + if args.socket.is_some() { + socket_path = define_socket_path(&port_info, args.socket, &cli.dirs, ctx) + .context("error defining unix socket path")?; + } else { + // define the default socket path + let default_socket_path = define_default_socket_path(&port_info, &cli.dirs, ctx) + .context("error defining unix socket path")?; - let socket_path_input = inquire::Text::new("Enter the socket path") - .with_help_message("The path to the unix socket") - .with_default(&default_socket_path.to_string_lossy().to_string()) - .prompt() - .into_diagnostic()?; + let socket_path_input = inquire::Text::new("Enter the socket path") + .with_help_message("The path to the unix socket") + .with_default(&default_socket_path) + .prompt() + .into_diagnostic()?; - let socket_path: Option = PathBuf::from(socket_path_input).into(); + socket_path = PathBuf::from(socket_path_input).into(); - debug!(path = ?socket_path, "socket path defined"); + if socket_path.exists() { + bail!("socket path already exists"); + } + } - //check socket_path is not empty - let default_socket_socket = socket_path.clone().unwrap(); + debug!(path = ?socket_path, "socket path defined"); - let server = tokio::net::UnixListener::bind(&default_socket_socket) + let server = tokio::net::UnixListener::bind(&socket_path) .into_diagnostic() .context("error creating unix socket listener")?; loop { - info!(path = ?default_socket_socket, "waiting for client connections"); + info!(path = ?socket_path, "waiting for client connections"); tokio::select! { result = server.accept() => { @@ -235,7 +260,7 @@ pub async fn run(cli: &crate::Cli) -> miette::Result<()> { } } - std::fs::remove_file(default_socket_socket) + std::fs::remove_file(socket_path) .into_diagnostic() .context("error trying to remove unix socket")?;