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