Run the SSH proxy, connecting stdin/stdout to the gateway.
(
gateway_url: &str,
sandbox_id: &str,
token: &str,
tls: &TlsOptions,
)
| 1320 | |
| 1321 | /// Run the SSH proxy, connecting stdin/stdout to the gateway. |
| 1322 | pub async fn sandbox_ssh_proxy( |
| 1323 | gateway_url: &str, |
| 1324 | sandbox_id: &str, |
| 1325 | token: &str, |
| 1326 | tls: &TlsOptions, |
| 1327 | ) -> Result<()> { |
| 1328 | let server = grpc_server_from_ssh_gateway_url(gateway_url)?; |
| 1329 | let mut client = grpc_client(&server, tls).await?; |
| 1330 | |
| 1331 | let (tx, rx) = tokio::sync::mpsc::channel::<TcpForwardFrame>(16); |
| 1332 | tx.send(TcpForwardFrame { |
| 1333 | payload: Some(openshell_core::proto::tcp_forward_frame::Payload::Init( |
| 1334 | TcpForwardInit { |
| 1335 | sandbox_id: sandbox_id.to_string(), |
| 1336 | service_id: format!("ssh-proxy:{sandbox_id}"), |
| 1337 | target: Some(tcp_forward_init::Target::Ssh(SshRelayTarget {})), |
| 1338 | authorization_token: token.to_string(), |
| 1339 | }, |
| 1340 | )), |
| 1341 | }) |
| 1342 | .await |
| 1343 | .map_err(|_| miette::miette!("failed to initialize SSH forward stream"))?; |
| 1344 | |
| 1345 | let mut response = client |
| 1346 | .forward_tcp(ReceiverStream::new(rx)) |
| 1347 | .await |
| 1348 | .into_diagnostic()? |
| 1349 | .into_inner(); |
| 1350 | |
| 1351 | let stdin = tokio::io::stdin(); |
| 1352 | let stdout = tokio::io::stdout(); |
| 1353 | |
| 1354 | let to_remote = tokio::spawn(async move { |
| 1355 | let mut stdin = stdin; |
| 1356 | let mut buf = vec![0u8; 64 * 1024]; |
| 1357 | while let Ok(n) = stdin.read(&mut buf).await { |
| 1358 | if n == 0 { |
| 1359 | break; |
| 1360 | } |
| 1361 | if tx |
| 1362 | .send(TcpForwardFrame { |
| 1363 | payload: Some(openshell_core::proto::tcp_forward_frame::Payload::Data( |
| 1364 | buf[..n].to_vec(), |
| 1365 | )), |
| 1366 | }) |
| 1367 | .await |
| 1368 | .is_err() |
| 1369 | { |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | }); |
| 1374 | let from_remote = tokio::spawn(async move { |
| 1375 | let mut stdout = stdout; |
| 1376 | loop { |
| 1377 | let Ok(Some(frame)) = response.message().await else { |
| 1378 | break; |
| 1379 | }; |
no test coverage detected