ssh_connections.rs

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