Add SSH port forwards to settings (#24474)

Closes #6920

Release Notes:

- Added ability to specify port forwarding settings for remote
connections
This commit is contained in:
Richard Weber 2025-02-14 23:52:24 +02:00 committed by GitHub
parent 58b0a6c4af
commit 5f6311171f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 158 additions and 6 deletions

1
Cargo.lock generated
View file

@ -10886,6 +10886,7 @@ dependencies = [
"prost 0.9.0", "prost 0.9.0",
"release_channel", "release_channel",
"rpc", "rpc",
"schemars",
"serde", "serde",
"serde_json", "serde_json",
"shlex", "shlex",

View file

@ -878,6 +878,7 @@ impl RemoteServerProjects {
nickname: None, nickname: None,
args: connection_options.args.unwrap_or_default(), args: connection_options.args.unwrap_or_default(),
upload_binary_over_ssh: None, upload_binary_over_ssh: None,
port_forwards: connection_options.port_forwards,
}) })
}); });
} }

View file

@ -15,7 +15,7 @@ use gpui::{
use language::CursorShape; use language::CursorShape;
use markdown::{Markdown, MarkdownStyle}; use markdown::{Markdown, MarkdownStyle};
use release_channel::ReleaseChannel; use release_channel::ReleaseChannel;
use remote::ssh_session::ConnectionIdentifier; use remote::ssh_session::{ConnectionIdentifier, SshPortForwardOption};
use remote::{SshConnectionOptions, SshPlatform, SshRemoteClient}; use remote::{SshConnectionOptions, SshPlatform, SshRemoteClient};
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -52,6 +52,7 @@ impl SshSettings {
host, host,
port, port,
username, username,
port_forwards: conn.port_forwards,
password: None, password: None,
}; };
} }
@ -86,6 +87,9 @@ pub struct SshConnection {
// limited outbound internet access. // limited outbound internet access.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub upload_binary_over_ssh: Option<bool>, pub upload_binary_over_ssh: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub port_forwards: Option<Vec<SshPortForwardOption>>,
} }
impl From<SshConnection> for SshConnectionOptions { impl From<SshConnection> for SshConnectionOptions {
@ -98,6 +102,7 @@ impl From<SshConnection> for SshConnectionOptions {
args: Some(val.args), args: Some(val.args),
nickname: val.nickname, nickname: val.nickname,
upload_binary_over_ssh: val.upload_binary_over_ssh.unwrap_or_default(), upload_binary_over_ssh: val.upload_binary_over_ssh.unwrap_or_default(),
port_forwards: val.port_forwards,
} }
} }
} }

View file

@ -30,6 +30,7 @@ paths.workspace = true
parking_lot.workspace = true parking_lot.workspace = true
prost.workspace = true prost.workspace = true
rpc = { workspace = true, features = ["gpui"] } rpc = { workspace = true, features = ["gpui"] }
schemars.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
shlex.workspace = true shlex.workspace = true

View file

@ -29,6 +29,8 @@ use rpc::{
AnyProtoClient, EntityMessageSubscriber, ErrorExt, ProtoClient, ProtoMessageHandlerSet, AnyProtoClient, EntityMessageSubscriber, ErrorExt, ProtoClient, ProtoMessageHandlerSet,
RpcError, RpcError,
}; };
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use smol::{ use smol::{
fs, fs,
process::{self, Child, Stdio}, process::{self, Child, Stdio},
@ -59,6 +61,16 @@ pub struct SshSocket {
socket_path: PathBuf, socket_path: PathBuf,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, JsonSchema)]
pub struct SshPortForwardOption {
#[serde(skip_serializing_if = "Option::is_none")]
pub local_host: Option<String>,
pub local_port: u16,
#[serde(skip_serializing_if = "Option::is_none")]
pub remote_host: Option<String>,
pub remote_port: u16,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct SshConnectionOptions { pub struct SshConnectionOptions {
pub host: String, pub host: String,
@ -66,6 +78,7 @@ pub struct SshConnectionOptions {
pub port: Option<u16>, pub port: Option<u16>,
pub password: Option<String>, pub password: Option<String>,
pub args: Option<Vec<String>>, pub args: Option<Vec<String>>,
pub port_forwards: Option<Vec<SshPortForwardOption>>,
pub nickname: Option<String>, pub nickname: Option<String>,
pub upload_binary_over_ssh: bool, pub upload_binary_over_ssh: bool,
@ -83,6 +96,42 @@ macro_rules! shell_script {
}}; }};
} }
fn parse_port_number(port_str: &str) -> Result<u16> {
port_str
.parse()
.map_err(|e| anyhow!("Invalid port number: {}: {}", port_str, e))
}
fn parse_port_forward_spec(spec: &str) -> Result<SshPortForwardOption> {
let parts: Vec<&str> = spec.split(':').collect();
match parts.len() {
4 => {
let local_port = parse_port_number(parts[1])?;
let remote_port = parse_port_number(parts[3])?;
Ok(SshPortForwardOption {
local_host: Some(parts[0].to_string()),
local_port,
remote_host: Some(parts[2].to_string()),
remote_port,
})
}
3 => {
let local_port = parse_port_number(parts[0])?;
let remote_port = parse_port_number(parts[2])?;
Ok(SshPortForwardOption {
local_host: None,
local_port,
remote_host: Some(parts[1].to_string()),
remote_port,
})
}
_ => anyhow::bail!("Invalid port forward format"),
}
}
impl SshConnectionOptions { impl SshConnectionOptions {
pub fn parse_command_line(input: &str) -> Result<Self> { pub fn parse_command_line(input: &str) -> Result<Self> {
let input = input.trim_start_matches("ssh "); let input = input.trim_start_matches("ssh ");
@ -90,14 +139,14 @@ impl SshConnectionOptions {
let mut username: Option<String> = None; let mut username: Option<String> = None;
let mut port: Option<u16> = None; let mut port: Option<u16> = None;
let mut args = Vec::new(); let mut args = Vec::new();
let mut port_forwards: Vec<SshPortForwardOption> = Vec::new();
// disallowed: -E, -e, -F, -f, -G, -g, -M, -N, -n, -O, -q, -S, -s, -T, -t, -V, -v, -W // disallowed: -E, -e, -F, -f, -G, -g, -M, -N, -n, -O, -q, -S, -s, -T, -t, -V, -v, -W
const ALLOWED_OPTS: &[&str] = &[ const ALLOWED_OPTS: &[&str] = &[
"-4", "-6", "-A", "-a", "-C", "-K", "-k", "-X", "-x", "-Y", "-y", "-4", "-6", "-A", "-a", "-C", "-K", "-k", "-X", "-x", "-Y", "-y",
]; ];
const ALLOWED_ARGS: &[&str] = &[ const ALLOWED_ARGS: &[&str] = &[
"-B", "-b", "-c", "-D", "-I", "-i", "-J", "-L", "-l", "-m", "-o", "-P", "-p", "-R", "-B", "-b", "-c", "-D", "-I", "-i", "-J", "-l", "-m", "-o", "-P", "-p", "-R", "-w",
"-w",
]; ];
let mut tokens = shlex::split(input) let mut tokens = shlex::split(input)
@ -123,6 +172,20 @@ impl SshConnectionOptions {
username = Some(l.to_string()); username = Some(l.to_string());
continue; continue;
} }
if arg == "-L" || arg.starts_with("-L") {
let forward_spec = if arg == "-L" {
tokens.next()
} else {
Some(arg.strip_prefix("-L").unwrap().to_string())
};
if let Some(spec) = forward_spec {
port_forwards.push(parse_port_forward_spec(&spec)?);
} else {
anyhow::bail!("Missing port forward format");
}
}
for a in ALLOWED_ARGS { for a in ALLOWED_ARGS {
if arg == *a { if arg == *a {
args.push(arg); args.push(arg);
@ -154,10 +217,16 @@ impl SshConnectionOptions {
anyhow::bail!("missing hostname"); anyhow::bail!("missing hostname");
}; };
let port_forwards = match port_forwards.len() {
0 => None,
_ => Some(port_forwards),
};
Ok(Self { Ok(Self {
host: hostname.to_string(), host: hostname.to_string(),
username: username.clone(), username: username.clone(),
port, port,
port_forwards,
args: Some(args), args: Some(args),
password: None, password: None,
nickname: None, nickname: None,
@ -179,8 +248,28 @@ impl SshConnectionOptions {
result result
} }
pub fn additional_args(&self) -> Option<&Vec<String>> { pub fn additional_args(&self) -> Vec<String> {
self.args.as_ref() let mut args = self.args.iter().flatten().cloned().collect::<Vec<String>>();
if let Some(forwards) = &self.port_forwards {
args.extend(forwards.iter().map(|pf| {
let local_host = match &pf.local_host {
Some(host) => host,
None => "localhost",
};
let remote_host = match &pf.remote_host {
Some(host) => host,
None => "localhost",
};
format!(
"-L{}:{}:{}:{}",
local_host, pf.local_port, remote_host, pf.remote_port
)
}));
}
args
} }
fn scp_url(&self) -> String { fn scp_url(&self) -> String {
@ -1454,7 +1543,7 @@ impl SshRemoteConnection {
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.env("SSH_ASKPASS_REQUIRE", "force") .env("SSH_ASKPASS_REQUIRE", "force")
.env("SSH_ASKPASS", &askpass_script_path) .env("SSH_ASKPASS", &askpass_script_path)
.args(connection_options.additional_args().unwrap_or(&Vec::new())) .args(connection_options.additional_args())
.args([ .args([
"-N", "-N",
"-o", "-o",

View file

@ -89,6 +89,61 @@ If you use the command line to open a connection to a host by doing `zed ssh://1
Additionally it's worth noting that while you can pass a password on the command line `zed ssh://user:password@host/~`, we do not support writing a password to your settings file. If you're connecting repeatedly to the same host, you should configure key-based authentication. Additionally it's worth noting that while you can pass a password on the command line `zed ssh://user:password@host/~`, we do not support writing a password to your settings file. If you're connecting repeatedly to the same host, you should configure key-based authentication.
## Port forwarding
If you'd like to be able to connect to ports on your remote server from your local machine, you can configure port forwarding in your settings file. This is particularly useful for developing websites so you can load the site in your browser while working.
```json
{
"ssh_connections": [
{
"host": "192.168.1.10",
"port_forwards": [{ "local_port": 8080, "remote_port": 80 }]
}
]
}
```
This will cause requests from your local machine to `localhost:8080` to be forwarded to the remote machine's port 80. Under the hood this uses the `-L` argument to ssh.
By default these ports are bound to localhost, so other computers in the same network as your development machine cannot access them. You can set the local_host to bind to a different interface, for example, 0.0.0.0 will bind to all local interfaces.
```json
{
"ssh_connections": [
{
"host": "192.168.1.10",
"port_forwards": [
{
"local_port": 8080,
"remote_port": 80,
"local_host": "0.0.0.0"
}
]
}
]
}
```
These ports also default to the `localhost` interface on the remote host. If you need to change this, you can also set the remote host:
```json
{
"ssh_connections": [
{
"host": "192.168.1.10",
"port_forwards": [
{
"local_port": 8080,
"remote_port": 80,
"remote_host": "docker-host"
}
]
}
]
}
```
## Zed settings ## Zed settings
When opening a remote project there are three relevant settings locations: When opening a remote project there are three relevant settings locations: