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