ssh_connections.rs

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