ssh_connections.rs

  1use std::collections::BTreeSet;
  2use std::{path::PathBuf, sync::Arc, time::Duration};
  3
  4use anyhow::{Context as _, Result, anyhow};
  5use auto_update::AutoUpdater;
  6use editor::Editor;
  7use extension_host::ExtensionStore;
  8use futures::channel::oneshot;
  9use gpui::{
 10    Animation, AnimationExt, AnyWindowHandle, App, AsyncApp, DismissEvent, Entity, EventEmitter,
 11    Focusable, FontFeatures, ParentElement as _, PromptLevel, Render, SemanticVersion,
 12    SharedString, Task, TextStyleRefinement, Transformation, WeakEntity, percentage,
 13};
 14
 15use language::CursorShape;
 16use markdown::{Markdown, MarkdownElement, MarkdownStyle};
 17use release_channel::ReleaseChannel;
 18use remote::ssh_session::{ConnectionIdentifier, SshPortForwardOption};
 19use remote::{SshConnectionOptions, SshPlatform, SshRemoteClient};
 20use schemars::JsonSchema;
 21use serde::{Deserialize, Serialize};
 22use settings::{Settings, SettingsSources};
 23use theme::ThemeSettings;
 24use ui::{
 25    ActiveTheme, Color, Context, Icon, IconName, IconSize, InteractiveElement, IntoElement, Label,
 26    LabelCommon, Styled, Window, prelude::*,
 27};
 28use util::serde::default_true;
 29use workspace::{AppState, ModalView, Workspace};
 30
 31#[derive(Deserialize)]
 32pub struct SshSettings {
 33    pub ssh_connections: Option<Vec<SshConnection>>,
 34    /// Whether to read ~/.ssh/config for ssh connection sources.
 35    #[serde(default = "default_true")]
 36    pub read_ssh_config: bool,
 37}
 38
 39impl SshSettings {
 40    pub fn ssh_connections(&self) -> impl Iterator<Item = SshConnection> + use<> {
 41        self.ssh_connections.clone().into_iter().flatten()
 42    }
 43
 44    pub fn connection_options_for(
 45        &self,
 46        host: String,
 47        port: Option<u16>,
 48        username: Option<String>,
 49    ) -> SshConnectionOptions {
 50        for conn in self.ssh_connections() {
 51            if conn.host == host && conn.username == username && conn.port == port {
 52                return SshConnectionOptions {
 53                    nickname: conn.nickname,
 54                    upload_binary_over_ssh: conn.upload_binary_over_ssh.unwrap_or_default(),
 55                    args: Some(conn.args),
 56                    host,
 57                    port,
 58                    username,
 59                    port_forwards: conn.port_forwards,
 60                    password: None,
 61                };
 62            }
 63        }
 64        SshConnectionOptions {
 65            host,
 66            port,
 67            username,
 68            ..Default::default()
 69        }
 70    }
 71}
 72
 73#[derive(Clone, Default, Serialize, Deserialize, PartialEq, JsonSchema)]
 74pub struct SshConnection {
 75    pub host: SharedString,
 76    #[serde(skip_serializing_if = "Option::is_none")]
 77    pub username: Option<String>,
 78    #[serde(skip_serializing_if = "Option::is_none")]
 79    pub port: Option<u16>,
 80    #[serde(skip_serializing_if = "Vec::is_empty")]
 81    #[serde(default)]
 82    pub args: Vec<String>,
 83    #[serde(default)]
 84    pub projects: BTreeSet<SshProject>,
 85    /// Name to use for this server in UI.
 86    #[serde(skip_serializing_if = "Option::is_none")]
 87    pub nickname: Option<String>,
 88    // By default Zed will download the binary to the host directly.
 89    // If this is set to true, Zed will download the binary to your local machine,
 90    // and then upload it over the SSH connection. Useful if your SSH server has
 91    // limited outbound internet access.
 92    #[serde(skip_serializing_if = "Option::is_none")]
 93    pub upload_binary_over_ssh: Option<bool>,
 94
 95    #[serde(skip_serializing_if = "Option::is_none")]
 96    pub port_forwards: Option<Vec<SshPortForwardOption>>,
 97}
 98
 99impl From<SshConnection> for SshConnectionOptions {
100    fn from(val: SshConnection) -> Self {
101        SshConnectionOptions {
102            host: val.host.into(),
103            username: val.username,
104            port: val.port,
105            password: None,
106            args: Some(val.args),
107            nickname: val.nickname,
108            upload_binary_over_ssh: val.upload_binary_over_ssh.unwrap_or_default(),
109            port_forwards: val.port_forwards,
110        }
111    }
112}
113
114#[derive(Clone, Default, Serialize, PartialEq, Eq, PartialOrd, Ord, Deserialize, JsonSchema)]
115pub struct SshProject {
116    pub paths: Vec<String>,
117}
118
119#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
120pub struct RemoteSettingsContent {
121    pub ssh_connections: Option<Vec<SshConnection>>,
122    pub read_ssh_config: Option<bool>,
123}
124
125impl Settings for SshSettings {
126    const KEY: Option<&'static str> = None;
127
128    type FileContent = RemoteSettingsContent;
129
130    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
131        sources.json_merge()
132    }
133
134    fn import_from_vscode(_vscode: &settings::VsCodeSettings, _current: &mut Self::FileContent) {}
135}
136
137pub struct SshPrompt {
138    connection_string: SharedString,
139    nickname: Option<SharedString>,
140    status_message: Option<SharedString>,
141    prompt: Option<(Entity<Markdown>, oneshot::Sender<String>)>,
142    cancellation: Option<oneshot::Sender<()>>,
143    editor: Entity<Editor>,
144}
145
146impl Drop for SshPrompt {
147    fn drop(&mut self) {
148        if let Some(cancel) = self.cancellation.take() {
149            cancel.send(()).ok();
150        }
151    }
152}
153
154pub struct SshConnectionModal {
155    pub(crate) prompt: Entity<SshPrompt>,
156    paths: Vec<PathBuf>,
157    finished: bool,
158}
159
160impl SshPrompt {
161    pub(crate) fn new(
162        connection_options: &SshConnectionOptions,
163        window: &mut Window,
164        cx: &mut Context<Self>,
165    ) -> Self {
166        let connection_string = connection_options.connection_string().into();
167        let nickname = connection_options.nickname.clone().map(|s| s.into());
168
169        Self {
170            connection_string,
171            nickname,
172            editor: cx.new(|cx| Editor::single_line(window, cx)),
173            status_message: None,
174            cancellation: None,
175            prompt: None,
176        }
177    }
178
179    pub fn set_cancellation_tx(&mut self, tx: oneshot::Sender<()>) {
180        self.cancellation = Some(tx);
181    }
182
183    pub fn set_prompt(
184        &mut self,
185        prompt: String,
186        tx: oneshot::Sender<String>,
187        window: &mut Window,
188        cx: &mut Context<Self>,
189    ) {
190        let theme = ThemeSettings::get_global(cx);
191
192        let refinement = TextStyleRefinement {
193            font_family: Some(theme.buffer_font.family.clone()),
194            font_features: Some(FontFeatures::disable_ligatures()),
195            font_size: Some(theme.buffer_font_size(cx).into()),
196            color: Some(cx.theme().colors().editor_foreground),
197            background_color: Some(gpui::transparent_black()),
198            ..Default::default()
199        };
200
201        self.editor.update(cx, |editor, cx| {
202            if prompt.contains("yes/no") {
203                editor.set_masked(false, cx);
204            } else {
205                editor.set_masked(true, cx);
206            }
207            editor.set_text_style_refinement(refinement);
208            editor.set_cursor_shape(CursorShape::Block, cx);
209        });
210
211        let markdown = cx.new(|cx| Markdown::new_text(prompt.into(), cx));
212        self.prompt = Some((markdown, tx));
213        self.status_message.take();
214        window.focus(&self.editor.focus_handle(cx));
215        cx.notify();
216    }
217
218    pub fn set_status(&mut self, status: Option<String>, cx: &mut Context<Self>) {
219        self.status_message = status.map(|s| s.into());
220        cx.notify();
221    }
222
223    pub fn confirm(&mut self, window: &mut Window, cx: &mut Context<Self>) {
224        if let Some((_, tx)) = self.prompt.take() {
225            self.status_message = Some("Connecting".into());
226            self.editor.update(cx, |editor, cx| {
227                tx.send(editor.text(cx)).ok();
228                editor.clear(window, cx);
229            });
230        }
231    }
232}
233
234impl Render for SshPrompt {
235    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
236        let theme = ThemeSettings::get_global(cx);
237
238        let mut text_style = window.text_style();
239        let refinement = TextStyleRefinement {
240            font_family: Some(theme.buffer_font.family.clone()),
241            font_features: Some(FontFeatures::disable_ligatures()),
242            font_size: Some(theme.buffer_font_size(cx).into()),
243            color: Some(cx.theme().colors().editor_foreground),
244            background_color: Some(gpui::transparent_black()),
245            ..Default::default()
246        };
247
248        text_style.refine(&refinement);
249        let markdown_style = MarkdownStyle {
250            base_text_style: text_style,
251            selection_background_color: cx.theme().players().local().selection,
252            ..Default::default()
253        };
254
255        v_flex()
256            .key_context("PasswordPrompt")
257            .py_2()
258            .px_3()
259            .size_full()
260            .text_buffer(cx)
261            .when_some(self.status_message.clone(), |el, status_message| {
262                el.child(
263                    h_flex()
264                        .gap_1()
265                        .child(
266                            Icon::new(IconName::ArrowCircle)
267                                .size(IconSize::Medium)
268                                .with_animation(
269                                    "arrow-circle",
270                                    Animation::new(Duration::from_secs(2)).repeat(),
271                                    |icon, delta| {
272                                        icon.transform(Transformation::rotate(percentage(delta)))
273                                    },
274                                ),
275                        )
276                        .child(
277                            div()
278                                .text_ellipsis()
279                                .overflow_x_hidden()
280                                .child(format!("{}", status_message)),
281                        ),
282                )
283            })
284            .when_some(self.prompt.as_ref(), |el, prompt| {
285                el.child(
286                    div()
287                        .size_full()
288                        .overflow_hidden()
289                        .child(MarkdownElement::new(prompt.0.clone(), markdown_style))
290                        .child(self.editor.clone()),
291                )
292            })
293    }
294}
295
296impl SshConnectionModal {
297    pub(crate) fn new(
298        connection_options: &SshConnectionOptions,
299        paths: Vec<PathBuf>,
300        window: &mut Window,
301        cx: &mut Context<Self>,
302    ) -> Self {
303        Self {
304            prompt: cx.new(|cx| SshPrompt::new(connection_options, window, cx)),
305            finished: false,
306            paths,
307        }
308    }
309
310    fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
311        self.prompt
312            .update(cx, |prompt, cx| prompt.confirm(window, cx))
313    }
314
315    pub fn finished(&mut self, cx: &mut Context<Self>) {
316        self.finished = true;
317        cx.emit(DismissEvent);
318    }
319
320    fn dismiss(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
321        if let Some(tx) = self
322            .prompt
323            .update(cx, |prompt, _cx| prompt.cancellation.take())
324        {
325            tx.send(()).ok();
326        }
327        self.finished(cx);
328    }
329}
330
331pub(crate) struct SshConnectionHeader {
332    pub(crate) connection_string: SharedString,
333    pub(crate) paths: Vec<PathBuf>,
334    pub(crate) nickname: Option<SharedString>,
335}
336
337impl RenderOnce for SshConnectionHeader {
338    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
339        let theme = cx.theme();
340
341        let mut header_color = theme.colors().text;
342        header_color.fade_out(0.96);
343
344        let (main_label, meta_label) = if let Some(nickname) = self.nickname {
345            (nickname, Some(format!("({})", self.connection_string)))
346        } else {
347            (self.connection_string, None)
348        };
349
350        h_flex()
351            .px(DynamicSpacing::Base12.rems(cx))
352            .pt(DynamicSpacing::Base08.rems(cx))
353            .pb(DynamicSpacing::Base04.rems(cx))
354            .rounded_t_sm()
355            .w_full()
356            .gap_1p5()
357            .child(Icon::new(IconName::Server).size(IconSize::XSmall))
358            .child(
359                h_flex()
360                    .gap_1()
361                    .overflow_x_hidden()
362                    .child(
363                        div()
364                            .max_w_96()
365                            .overflow_x_hidden()
366                            .text_ellipsis()
367                            .child(Headline::new(main_label).size(HeadlineSize::XSmall)),
368                    )
369                    .children(
370                        meta_label.map(|label| {
371                            Label::new(label).color(Color::Muted).size(LabelSize::Small)
372                        }),
373                    )
374                    .child(div().overflow_x_hidden().text_ellipsis().children(
375                        self.paths.into_iter().map(|path| {
376                            Label::new(path.to_string_lossy().to_string())
377                                .size(LabelSize::Small)
378                                .color(Color::Muted)
379                        }),
380                    )),
381            )
382    }
383}
384
385impl Render for SshConnectionModal {
386    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
387        let nickname = self.prompt.read(cx).nickname.clone();
388        let connection_string = self.prompt.read(cx).connection_string.clone();
389
390        let theme = cx.theme().clone();
391        let body_color = theme.colors().editor_background;
392
393        v_flex()
394            .elevation_3(cx)
395            .w(rems(34.))
396            .border_1()
397            .border_color(theme.colors().border)
398            .key_context("SshConnectionModal")
399            .track_focus(&self.focus_handle(cx))
400            .on_action(cx.listener(Self::dismiss))
401            .on_action(cx.listener(Self::confirm))
402            .child(
403                SshConnectionHeader {
404                    paths: self.paths.clone(),
405                    connection_string,
406                    nickname,
407                }
408                .render(window, cx),
409            )
410            .child(
411                div()
412                    .w_full()
413                    .rounded_b_lg()
414                    .bg(body_color)
415                    .border_t_1()
416                    .border_color(theme.colors().border_variant)
417                    .child(self.prompt.clone()),
418            )
419    }
420}
421
422impl Focusable for SshConnectionModal {
423    fn focus_handle(&self, cx: &gpui::App) -> gpui::FocusHandle {
424        self.prompt.read(cx).editor.focus_handle(cx)
425    }
426}
427
428impl EventEmitter<DismissEvent> for SshConnectionModal {}
429
430impl ModalView for SshConnectionModal {
431    fn on_before_dismiss(
432        &mut self,
433        _window: &mut Window,
434        _: &mut Context<Self>,
435    ) -> workspace::DismissDecision {
436        return workspace::DismissDecision::Dismiss(self.finished);
437    }
438
439    fn fade_out_background(&self) -> bool {
440        true
441    }
442}
443
444#[derive(Clone)]
445pub struct SshClientDelegate {
446    window: AnyWindowHandle,
447    ui: WeakEntity<SshPrompt>,
448    known_password: Option<String>,
449}
450
451impl remote::SshClientDelegate for SshClientDelegate {
452    fn ask_password(&self, prompt: String, tx: oneshot::Sender<String>, cx: &mut AsyncApp) {
453        let mut known_password = self.known_password.clone();
454        if let Some(password) = known_password.take() {
455            tx.send(password).ok();
456        } else {
457            self.window
458                .update(cx, |_, window, cx| {
459                    self.ui.update(cx, |modal, cx| {
460                        modal.set_prompt(prompt, tx, window, cx);
461                    })
462                })
463                .ok();
464        }
465    }
466
467    fn set_status(&self, status: Option<&str>, cx: &mut AsyncApp) {
468        self.update_status(status, cx)
469    }
470
471    fn download_server_binary_locally(
472        &self,
473        platform: SshPlatform,
474        release_channel: ReleaseChannel,
475        version: Option<SemanticVersion>,
476        cx: &mut AsyncApp,
477    ) -> Task<anyhow::Result<PathBuf>> {
478        cx.spawn(async move |cx| {
479            let binary_path = AutoUpdater::download_remote_server_release(
480                platform.os,
481                platform.arch,
482                release_channel,
483                version,
484                cx,
485            )
486            .await
487            .map_err(|e| {
488                anyhow!(
489                    "Failed to download remote server binary (version: {}, os: {}, arch: {}): {}",
490                    version
491                        .map(|v| format!("{}", v))
492                        .unwrap_or("unknown".to_string()),
493                    platform.os,
494                    platform.arch,
495                    e
496                )
497            })?;
498            Ok(binary_path)
499        })
500    }
501
502    fn get_download_params(
503        &self,
504        platform: SshPlatform,
505        release_channel: ReleaseChannel,
506        version: Option<SemanticVersion>,
507        cx: &mut AsyncApp,
508    ) -> Task<Result<Option<(String, String)>>> {
509        cx.spawn(async move |cx| {
510            AutoUpdater::get_remote_server_release_url(
511                platform.os,
512                platform.arch,
513                release_channel,
514                version,
515                cx,
516            )
517            .await
518        })
519    }
520}
521
522impl SshClientDelegate {
523    fn update_status(&self, status: Option<&str>, cx: &mut AsyncApp) {
524        self.window
525            .update(cx, |_, _, cx| {
526                self.ui.update(cx, |modal, cx| {
527                    modal.set_status(status.map(|s| s.to_string()), cx);
528                })
529            })
530            .ok();
531    }
532}
533
534pub fn is_connecting_over_ssh(workspace: &Workspace, cx: &App) -> bool {
535    workspace.active_modal::<SshConnectionModal>(cx).is_some()
536}
537
538pub fn connect_over_ssh(
539    unique_identifier: ConnectionIdentifier,
540    connection_options: SshConnectionOptions,
541    ui: Entity<SshPrompt>,
542    window: &mut Window,
543    cx: &mut App,
544) -> Task<Result<Option<Entity<SshRemoteClient>>>> {
545    let window = window.window_handle();
546    let known_password = connection_options.password.clone();
547    let (tx, rx) = oneshot::channel();
548    ui.update(cx, |ui, _cx| ui.set_cancellation_tx(tx));
549
550    remote::SshRemoteClient::new(
551        unique_identifier,
552        connection_options,
553        rx,
554        Arc::new(SshClientDelegate {
555            window,
556            ui: ui.downgrade(),
557            known_password,
558        }),
559        cx,
560    )
561}
562
563pub async fn open_ssh_project(
564    connection_options: SshConnectionOptions,
565    paths: Vec<PathBuf>,
566    app_state: Arc<AppState>,
567    open_options: workspace::OpenOptions,
568    cx: &mut AsyncApp,
569) -> Result<()> {
570    let window = if let Some(window) = open_options.replace_window {
571        window
572    } else {
573        let workspace_position = cx
574            .update(|cx| {
575                workspace::ssh_workspace_position_from_db(
576                    connection_options.host.clone(),
577                    connection_options.port,
578                    connection_options.username.clone(),
579                    &paths,
580                    cx,
581                )
582            })?
583            .await
584            .context("fetching ssh workspace position from db")?;
585
586        let mut options =
587            cx.update(|cx| (app_state.build_window_options)(workspace_position.display, cx))?;
588        options.window_bounds = workspace_position.window_bounds;
589
590        cx.open_window(options, |window, cx| {
591            let project = project::Project::local(
592                app_state.client.clone(),
593                app_state.node_runtime.clone(),
594                app_state.user_store.clone(),
595                app_state.languages.clone(),
596                app_state.fs.clone(),
597                None,
598                cx,
599            );
600            cx.new(|cx| {
601                let mut workspace = Workspace::new(None, project, app_state.clone(), window, cx);
602                workspace.centered_layout = workspace_position.centered_layout;
603                workspace
604            })
605        })?
606    };
607
608    loop {
609        let (cancel_tx, cancel_rx) = oneshot::channel();
610        let delegate = window.update(cx, {
611            let connection_options = connection_options.clone();
612            let paths = paths.clone();
613            move |workspace, window, cx| {
614                window.activate_window();
615                workspace.toggle_modal(window, cx, |window, cx| {
616                    SshConnectionModal::new(&connection_options, paths, window, cx)
617                });
618
619                let ui = workspace
620                    .active_modal::<SshConnectionModal>(cx)?
621                    .read(cx)
622                    .prompt
623                    .clone();
624
625                ui.update(cx, |ui, _cx| {
626                    ui.set_cancellation_tx(cancel_tx);
627                });
628
629                Some(Arc::new(SshClientDelegate {
630                    window: window.window_handle(),
631                    ui: ui.downgrade(),
632                    known_password: connection_options.password.clone(),
633                }))
634            }
635        })?;
636
637        let Some(delegate) = delegate else { break };
638
639        let did_open_ssh_project = cx
640            .update(|cx| {
641                workspace::open_ssh_project_with_new_connection(
642                    window,
643                    connection_options.clone(),
644                    cancel_rx,
645                    delegate.clone(),
646                    app_state.clone(),
647                    paths.clone(),
648                    cx,
649                )
650            })?
651            .await;
652
653        window
654            .update(cx, |workspace, _, cx| {
655                if let Some(ui) = workspace.active_modal::<SshConnectionModal>(cx) {
656                    ui.update(cx, |modal, cx| modal.finished(cx))
657                }
658            })
659            .ok();
660
661        if let Err(e) = did_open_ssh_project {
662            log::error!("Failed to open project: {e:?}");
663            let response = window
664                .update(cx, |_, window, cx| {
665                    window.prompt(
666                        PromptLevel::Critical,
667                        "Failed to connect over SSH",
668                        Some(&e.to_string()),
669                        &["Retry", "Ok"],
670                        cx,
671                    )
672                })?
673                .await;
674
675            if response == Ok(0) {
676                continue;
677            }
678        }
679
680        window
681            .update(cx, |workspace, _, cx| {
682                if let Some(client) = workspace.project().read(cx).ssh_client().clone() {
683                    ExtensionStore::global(cx)
684                        .update(cx, |store, cx| store.register_ssh_client(client, cx));
685                }
686            })
687            .ok();
688
689        break;
690    }
691
692    // Already showed the error to the user
693    Ok(())
694}