cli: allow command-shell to listen on a prescribed port (#193028)

Fixes https://github.com/microsoft/vscode-internalbacklog/issues/4623
This commit is contained in:
Connor Peet
2023-09-13 09:02:12 -07:00
committed by GitHub
parent 3941870f19
commit 491ddd816e
2 changed files with 10 additions and 5 deletions

View File

@@ -220,8 +220,8 @@ pub struct CommandShellArgs {
#[clap(long)] #[clap(long)]
pub on_socket: bool, pub on_socket: bool,
/// Listen on a port instead of stdin/stdout. /// Listen on a port instead of stdin/stdout.
#[clap(long)] #[clap(long, num_args = 0..=1, default_missing_value = "0")]
pub on_port: bool, pub on_port: Option<u16>,
/// Require the given token string to be given in the handshake. /// Require the given token string to be given in the handshake.
#[clap(long)] #[clap(long)]
pub require_token: Option<String>, pub require_token: Option<String>,

View File

@@ -8,7 +8,11 @@ use base64::{engine::general_purpose as b64, Engine as _};
use futures::{stream::FuturesUnordered, StreamExt}; use futures::{stream::FuturesUnordered, StreamExt};
use serde::Serialize; use serde::Serialize;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::{str::FromStr, time::Duration}; use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
str::FromStr,
time::Duration,
};
use sysinfo::Pid; use sysinfo::Pid;
use tokio::{ use tokio::{
io::{AsyncBufReadExt, BufReader}, io::{AsyncBufReadExt, BufReader},
@@ -157,8 +161,9 @@ pub async fn command_shell(ctx: CommandContext, args: CommandShellArgs) -> Resul
Box::new(listener) Box::new(listener)
} }
(true, _) => { (Some(p), _) => {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0") let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), p);
let listener = tokio::net::TcpListener::bind(addr)
.await .await
.map_err(|e| wrap(e, "error listening on port"))?; .map_err(|e| wrap(e, "error listening on port"))?;