From 1cf9fe6870628a1c9d65b9ce9fe68834aff16791 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Sun, 6 Sep 2020 14:19:40 -0400 Subject: [PATCH] WIP. spawning ssh --- src/admin.rs | 36 ++++++++++++++++++++++++++++++++---- src/net.rs | 17 +++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/admin.rs b/src/admin.rs index 1f269e0..0409c06 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -5,6 +5,7 @@ */ use std::net::SocketAddr; +use std::process::{Command, Stdio}; use std::sync::Arc; use log::{debug, info, error}; @@ -18,6 +19,7 @@ use warp; use warp::{Filter}; use crate::pass::is_correct_password; +use crate::net::ssh_command; #[derive(Debug, Deserialize, Serialize, Clone)] struct AdminRegistration { @@ -131,10 +133,36 @@ async fn handle_register(registration : AdminRegistration, let port = port_opt.unwrap(); info!("handle_register: received port: {}", port); // todo: start or restart ssh service - Ok(warp::reply::with_status( - "successfully registered with bubble", - http::StatusCode::OK, - )) + let tunnel = format!("{}:127.0.0.1:{}", port, proxy_port); + let target = format!("bubble-flex@{}", registration.bubble); + let ssh = Command::new(ssh_command()) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .arg("-Nn") + .arg("-R") + .arg(tunnel) + .arg(target) + .spawn(); + if ssh.is_err() { + let err = ssh.err(); + if err.is_none() { + error!("handle_register: error spawning ssh"); + } else { + error!("handle_register: error spawning ssh: {:?}", err.unwrap()); + } + Ok(warp::reply::with_status( + "error spawning ssh", + http::StatusCode::PRECONDITION_FAILED, + )) + } else { + let mut child = ssh.unwrap(); + // child.kill(); + Ok(warp::reply::with_status( + "successfully registered with bubble", + http::StatusCode::OK, + )) + } } } }, diff --git a/src/net.rs b/src/net.rs index bb2d0fc..e94b9fc 100644 --- a/src/net.rs +++ b/src/net.rs @@ -189,3 +189,20 @@ pub fn remove_static_route(ip_string: &String) -> bool { } ok } + +const SSH_WINDOWS: &'static str = "C:\\Windows\\System32\\OpenSSH\\ssh.exe"; +const SSH_MACOS: &'static str = "/usr/bin/ssh"; +const SSH_LINUX: &'static str = "/usr/bin/ssh"; + +pub fn ssh_command() -> &'static str { + let platform: Platform = platform(); + return match platform { + Platform::Windows => SSH_WINDOWS, + Platform::MacOS => SSH_MACOS, + Platform::Linux => SSH_LINUX, + _ => { + error!("ssh_command: unsupported platform: {:?}", platform); + exit(2); + } + } +}