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