1#[cfg(any(test, feature = "test-support"))]
2pub mod test;
3
4pub mod channel;
5pub mod http;
6pub mod telemetry;
7pub mod user;
8
9use anyhow::{anyhow, Context, Result};
10use async_recursion::async_recursion;
11use async_tungstenite::tungstenite::{
12 error::Error as WebsocketError,
13 http::{Request, StatusCode},
14};
15use db::Db;
16use futures::{future::LocalBoxFuture, FutureExt, SinkExt, StreamExt, TryStreamExt};
17use gpui::{
18 actions, serde_json::Value, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle,
19 AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
20 MutableAppContext, Task, View, ViewContext, ViewHandle,
21};
22use http::HttpClient;
23use lazy_static::lazy_static;
24use parking_lot::RwLock;
25use postage::watch;
26use rand::prelude::*;
27use rpc::proto::{AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage};
28use std::{
29 any::TypeId,
30 collections::HashMap,
31 convert::TryFrom,
32 fmt::Write as _,
33 future::Future,
34 path::PathBuf,
35 sync::{Arc, Weak},
36 time::{Duration, Instant},
37};
38use telemetry::Telemetry;
39use thiserror::Error;
40use url::Url;
41use util::{ResultExt, TryFutureExt};
42
43pub use channel::*;
44pub use rpc::*;
45pub use user::*;
46
47lazy_static! {
48 pub static ref ZED_SERVER_URL: String =
49 std::env::var("ZED_SERVER_URL").unwrap_or_else(|_| "https://zed.dev".to_string());
50 pub static ref IMPERSONATE_LOGIN: Option<String> = std::env::var("ZED_IMPERSONATE")
51 .ok()
52 .and_then(|s| if s.is_empty() { None } else { Some(s) });
53}
54
55pub const ZED_SECRET_CLIENT_TOKEN: &str = "618033988749894";
56
57actions!(client, [Authenticate]);
58
59pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) {
60 cx.add_global_action({
61 let client = client.clone();
62 move |_: &Authenticate, cx| {
63 let client = client.clone();
64 cx.spawn(
65 |cx| async move { client.authenticate_and_connect(true, &cx).log_err().await },
66 )
67 .detach();
68 }
69 });
70}
71
72pub struct Client {
73 id: usize,
74 peer: Arc<Peer>,
75 http: Arc<dyn HttpClient>,
76 telemetry: Arc<Telemetry>,
77 state: RwLock<ClientState>,
78
79 #[allow(clippy::type_complexity)]
80 #[cfg(any(test, feature = "test-support"))]
81 authenticate: RwLock<
82 Option<Box<dyn 'static + Send + Sync + Fn(&AsyncAppContext) -> Task<Result<Credentials>>>>,
83 >,
84
85 #[allow(clippy::type_complexity)]
86 #[cfg(any(test, feature = "test-support"))]
87 establish_connection: RwLock<
88 Option<
89 Box<
90 dyn 'static
91 + Send
92 + Sync
93 + Fn(
94 &Credentials,
95 &AsyncAppContext,
96 ) -> Task<Result<Connection, EstablishConnectionError>>,
97 >,
98 >,
99 >,
100}
101
102#[derive(Error, Debug)]
103pub enum EstablishConnectionError {
104 #[error("upgrade required")]
105 UpgradeRequired,
106 #[error("unauthorized")]
107 Unauthorized,
108 #[error("{0}")]
109 Other(#[from] anyhow::Error),
110 #[error("{0}")]
111 Http(#[from] http::Error),
112 #[error("{0}")]
113 Io(#[from] std::io::Error),
114 #[error("{0}")]
115 Websocket(#[from] async_tungstenite::tungstenite::http::Error),
116}
117
118impl From<WebsocketError> for EstablishConnectionError {
119 fn from(error: WebsocketError) -> Self {
120 if let WebsocketError::Http(response) = &error {
121 match response.status() {
122 StatusCode::UNAUTHORIZED => return EstablishConnectionError::Unauthorized,
123 StatusCode::UPGRADE_REQUIRED => return EstablishConnectionError::UpgradeRequired,
124 _ => {}
125 }
126 }
127 EstablishConnectionError::Other(error.into())
128 }
129}
130
131impl EstablishConnectionError {
132 pub fn other(error: impl Into<anyhow::Error> + Send + Sync) -> Self {
133 Self::Other(error.into())
134 }
135}
136
137#[derive(Copy, Clone, Debug, Eq, PartialEq)]
138pub enum Status {
139 SignedOut,
140 UpgradeRequired,
141 Authenticating,
142 Connecting,
143 ConnectionError,
144 Connected { connection_id: ConnectionId },
145 ConnectionLost,
146 Reauthenticating,
147 Reconnecting,
148 ReconnectionError { next_reconnection: Instant },
149}
150
151impl Status {
152 pub fn is_connected(&self) -> bool {
153 matches!(self, Self::Connected { .. })
154 }
155}
156
157struct ClientState {
158 credentials: Option<Credentials>,
159 status: (watch::Sender<Status>, watch::Receiver<Status>),
160 entity_id_extractors: HashMap<TypeId, fn(&dyn AnyTypedEnvelope) -> u64>,
161 _reconnect_task: Option<Task<()>>,
162 reconnect_interval: Duration,
163 entities_by_type_and_remote_id: HashMap<(TypeId, u64), AnyWeakEntityHandle>,
164 models_by_message_type: HashMap<TypeId, AnyWeakModelHandle>,
165 entity_types_by_message_type: HashMap<TypeId, TypeId>,
166 #[allow(clippy::type_complexity)]
167 message_handlers: HashMap<
168 TypeId,
169 Arc<
170 dyn Send
171 + Sync
172 + Fn(
173 AnyEntityHandle,
174 Box<dyn AnyTypedEnvelope>,
175 &Arc<Client>,
176 AsyncAppContext,
177 ) -> LocalBoxFuture<'static, Result<()>>,
178 >,
179 >,
180}
181
182enum AnyWeakEntityHandle {
183 Model(AnyWeakModelHandle),
184 View(AnyWeakViewHandle),
185}
186
187enum AnyEntityHandle {
188 Model(AnyModelHandle),
189 View(AnyViewHandle),
190}
191
192#[derive(Clone, Debug)]
193pub struct Credentials {
194 pub user_id: u64,
195 pub access_token: String,
196}
197
198impl Default for ClientState {
199 fn default() -> Self {
200 Self {
201 credentials: None,
202 status: watch::channel_with(Status::SignedOut),
203 entity_id_extractors: Default::default(),
204 _reconnect_task: None,
205 reconnect_interval: Duration::from_secs(5),
206 models_by_message_type: Default::default(),
207 entities_by_type_and_remote_id: Default::default(),
208 entity_types_by_message_type: Default::default(),
209 message_handlers: Default::default(),
210 }
211 }
212}
213
214pub enum Subscription {
215 Entity {
216 client: Weak<Client>,
217 id: (TypeId, u64),
218 },
219 Message {
220 client: Weak<Client>,
221 id: TypeId,
222 },
223}
224
225impl Drop for Subscription {
226 fn drop(&mut self) {
227 match self {
228 Subscription::Entity { client, id } => {
229 if let Some(client) = client.upgrade() {
230 let mut state = client.state.write();
231 let _ = state.entities_by_type_and_remote_id.remove(id);
232 }
233 }
234 Subscription::Message { client, id } => {
235 if let Some(client) = client.upgrade() {
236 let mut state = client.state.write();
237 let _ = state.entity_types_by_message_type.remove(id);
238 let _ = state.message_handlers.remove(id);
239 }
240 }
241 }
242 }
243}
244
245impl Client {
246 pub fn new(http: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
247 Arc::new(Self {
248 id: 0,
249 peer: Peer::new(),
250 telemetry: Telemetry::new(http.clone(), cx),
251 http,
252 state: Default::default(),
253
254 #[cfg(any(test, feature = "test-support"))]
255 authenticate: Default::default(),
256 #[cfg(any(test, feature = "test-support"))]
257 establish_connection: Default::default(),
258 })
259 }
260
261 pub fn id(&self) -> usize {
262 self.id
263 }
264
265 pub fn http_client(&self) -> Arc<dyn HttpClient> {
266 self.http.clone()
267 }
268
269 #[cfg(any(test, feature = "test-support"))]
270 pub fn set_id(&mut self, id: usize) -> &Self {
271 self.id = id;
272 self
273 }
274
275 #[cfg(any(test, feature = "test-support"))]
276 pub fn tear_down(&self) {
277 let mut state = self.state.write();
278 state._reconnect_task.take();
279 state.message_handlers.clear();
280 state.models_by_message_type.clear();
281 state.entities_by_type_and_remote_id.clear();
282 state.entity_id_extractors.clear();
283 self.peer.reset();
284 }
285
286 #[cfg(any(test, feature = "test-support"))]
287 pub fn override_authenticate<F>(&self, authenticate: F) -> &Self
288 where
289 F: 'static + Send + Sync + Fn(&AsyncAppContext) -> Task<Result<Credentials>>,
290 {
291 *self.authenticate.write() = Some(Box::new(authenticate));
292 self
293 }
294
295 #[cfg(any(test, feature = "test-support"))]
296 pub fn override_establish_connection<F>(&self, connect: F) -> &Self
297 where
298 F: 'static
299 + Send
300 + Sync
301 + Fn(&Credentials, &AsyncAppContext) -> Task<Result<Connection, EstablishConnectionError>>,
302 {
303 *self.establish_connection.write() = Some(Box::new(connect));
304 self
305 }
306
307 pub fn user_id(&self) -> Option<u64> {
308 self.state
309 .read()
310 .credentials
311 .as_ref()
312 .map(|credentials| credentials.user_id)
313 }
314
315 pub fn status(&self) -> watch::Receiver<Status> {
316 self.state.read().status.1.clone()
317 }
318
319 fn set_status(self: &Arc<Self>, status: Status, cx: &AsyncAppContext) {
320 log::info!("set status on client {}: {:?}", self.id, status);
321 let mut state = self.state.write();
322 *state.status.0.borrow_mut() = status;
323
324 match status {
325 Status::Connected { .. } => {
326 state._reconnect_task = None;
327 }
328 Status::ConnectionLost => {
329 let this = self.clone();
330 let reconnect_interval = state.reconnect_interval;
331 state._reconnect_task = Some(cx.spawn(|cx| async move {
332 let mut rng = StdRng::from_entropy();
333 let mut delay = Duration::from_millis(100);
334 while let Err(error) = this.authenticate_and_connect(true, &cx).await {
335 log::error!("failed to connect {}", error);
336 if matches!(*this.status().borrow(), Status::ConnectionError) {
337 this.set_status(
338 Status::ReconnectionError {
339 next_reconnection: Instant::now() + delay,
340 },
341 &cx,
342 );
343 cx.background().timer(delay).await;
344 delay = delay
345 .mul_f32(rng.gen_range(1.0..=2.0))
346 .min(reconnect_interval);
347 } else {
348 break;
349 }
350 }
351 }));
352 }
353 Status::SignedOut | Status::UpgradeRequired => {
354 self.telemetry.set_authenticated_user_info(None, false);
355 state._reconnect_task.take();
356 }
357 _ => {}
358 }
359 }
360
361 pub fn add_view_for_remote_entity<T: View>(
362 self: &Arc<Self>,
363 remote_id: u64,
364 cx: &mut ViewContext<T>,
365 ) -> Subscription {
366 let id = (TypeId::of::<T>(), remote_id);
367 self.state
368 .write()
369 .entities_by_type_and_remote_id
370 .insert(id, AnyWeakEntityHandle::View(cx.weak_handle().into()));
371 Subscription::Entity {
372 client: Arc::downgrade(self),
373 id,
374 }
375 }
376
377 pub fn add_model_for_remote_entity<T: Entity>(
378 self: &Arc<Self>,
379 remote_id: u64,
380 cx: &mut ModelContext<T>,
381 ) -> Subscription {
382 let id = (TypeId::of::<T>(), remote_id);
383 self.state
384 .write()
385 .entities_by_type_and_remote_id
386 .insert(id, AnyWeakEntityHandle::Model(cx.weak_handle().into()));
387 Subscription::Entity {
388 client: Arc::downgrade(self),
389 id,
390 }
391 }
392
393 pub fn add_message_handler<M, E, H, F>(
394 self: &Arc<Self>,
395 model: ModelHandle<E>,
396 handler: H,
397 ) -> Subscription
398 where
399 M: EnvelopedMessage,
400 E: Entity,
401 H: 'static
402 + Send
403 + Sync
404 + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
405 F: 'static + Future<Output = Result<()>>,
406 {
407 let message_type_id = TypeId::of::<M>();
408
409 let mut state = self.state.write();
410 state
411 .models_by_message_type
412 .insert(message_type_id, model.downgrade().into());
413
414 let prev_handler = state.message_handlers.insert(
415 message_type_id,
416 Arc::new(move |handle, envelope, client, cx| {
417 let handle = if let AnyEntityHandle::Model(handle) = handle {
418 handle
419 } else {
420 unreachable!();
421 };
422 let model = handle.downcast::<E>().unwrap();
423 let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
424 handler(model, *envelope, client.clone(), cx).boxed_local()
425 }),
426 );
427 if prev_handler.is_some() {
428 panic!("registered handler for the same message twice");
429 }
430
431 Subscription::Message {
432 client: Arc::downgrade(self),
433 id: message_type_id,
434 }
435 }
436
437 pub fn add_view_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
438 where
439 M: EntityMessage,
440 E: View,
441 H: 'static
442 + Send
443 + Sync
444 + Fn(ViewHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
445 F: 'static + Future<Output = Result<()>>,
446 {
447 self.add_entity_message_handler::<M, E, _, _>(move |handle, message, client, cx| {
448 if let AnyEntityHandle::View(handle) = handle {
449 handler(handle.downcast::<E>().unwrap(), message, client, cx)
450 } else {
451 unreachable!();
452 }
453 })
454 }
455
456 pub fn add_model_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
457 where
458 M: EntityMessage,
459 E: Entity,
460 H: 'static
461 + Send
462 + Sync
463 + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
464 F: 'static + Future<Output = Result<()>>,
465 {
466 self.add_entity_message_handler::<M, E, _, _>(move |handle, message, client, cx| {
467 if let AnyEntityHandle::Model(handle) = handle {
468 handler(handle.downcast::<E>().unwrap(), message, client, cx)
469 } else {
470 unreachable!();
471 }
472 })
473 }
474
475 fn add_entity_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
476 where
477 M: EntityMessage,
478 E: Entity,
479 H: 'static
480 + Send
481 + Sync
482 + Fn(AnyEntityHandle, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
483 F: 'static + Future<Output = Result<()>>,
484 {
485 let model_type_id = TypeId::of::<E>();
486 let message_type_id = TypeId::of::<M>();
487
488 let mut state = self.state.write();
489 state
490 .entity_types_by_message_type
491 .insert(message_type_id, model_type_id);
492 state
493 .entity_id_extractors
494 .entry(message_type_id)
495 .or_insert_with(|| {
496 |envelope| {
497 envelope
498 .as_any()
499 .downcast_ref::<TypedEnvelope<M>>()
500 .unwrap()
501 .payload
502 .remote_entity_id()
503 }
504 });
505 let prev_handler = state.message_handlers.insert(
506 message_type_id,
507 Arc::new(move |handle, envelope, client, cx| {
508 let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
509 handler(handle, *envelope, client.clone(), cx).boxed_local()
510 }),
511 );
512 if prev_handler.is_some() {
513 panic!("registered handler for the same message twice");
514 }
515 }
516
517 pub fn add_model_request_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
518 where
519 M: EntityMessage + RequestMessage,
520 E: Entity,
521 H: 'static
522 + Send
523 + Sync
524 + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
525 F: 'static + Future<Output = Result<M::Response>>,
526 {
527 self.add_model_message_handler(move |entity, envelope, client, cx| {
528 Self::respond_to_request::<M, _>(
529 envelope.receipt(),
530 handler(entity, envelope, client.clone(), cx),
531 client,
532 )
533 })
534 }
535
536 pub fn add_view_request_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
537 where
538 M: EntityMessage + RequestMessage,
539 E: View,
540 H: 'static
541 + Send
542 + Sync
543 + Fn(ViewHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
544 F: 'static + Future<Output = Result<M::Response>>,
545 {
546 self.add_view_message_handler(move |entity, envelope, client, cx| {
547 Self::respond_to_request::<M, _>(
548 envelope.receipt(),
549 handler(entity, envelope, client.clone(), cx),
550 client,
551 )
552 })
553 }
554
555 async fn respond_to_request<T: RequestMessage, F: Future<Output = Result<T::Response>>>(
556 receipt: Receipt<T>,
557 response: F,
558 client: Arc<Self>,
559 ) -> Result<()> {
560 match response.await {
561 Ok(response) => {
562 client.respond(receipt, response)?;
563 Ok(())
564 }
565 Err(error) => {
566 client.respond_with_error(
567 receipt,
568 proto::Error {
569 message: format!("{:?}", error),
570 },
571 )?;
572 Err(error)
573 }
574 }
575 }
576
577 pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool {
578 read_credentials_from_keychain(cx).is_some()
579 }
580
581 #[async_recursion(?Send)]
582 pub async fn authenticate_and_connect(
583 self: &Arc<Self>,
584 try_keychain: bool,
585 cx: &AsyncAppContext,
586 ) -> anyhow::Result<()> {
587 let was_disconnected = match *self.status().borrow() {
588 Status::SignedOut => true,
589 Status::ConnectionError
590 | Status::ConnectionLost
591 | Status::Authenticating { .. }
592 | Status::Reauthenticating { .. }
593 | Status::ReconnectionError { .. } => false,
594 Status::Connected { .. } | Status::Connecting { .. } | Status::Reconnecting { .. } => {
595 return Ok(())
596 }
597 Status::UpgradeRequired => return Err(EstablishConnectionError::UpgradeRequired)?,
598 };
599
600 if was_disconnected {
601 self.set_status(Status::Authenticating, cx);
602 } else {
603 self.set_status(Status::Reauthenticating, cx)
604 }
605
606 let mut read_from_keychain = false;
607 let mut credentials = self.state.read().credentials.clone();
608 if credentials.is_none() && try_keychain {
609 credentials = read_credentials_from_keychain(cx);
610 read_from_keychain = credentials.is_some();
611 if read_from_keychain {
612 self.report_event("read credentials from keychain", Default::default());
613 }
614 }
615 if credentials.is_none() {
616 let mut status_rx = self.status();
617 let _ = status_rx.next().await;
618 futures::select_biased! {
619 authenticate = self.authenticate(cx).fuse() => {
620 match authenticate {
621 Ok(creds) => credentials = Some(creds),
622 Err(err) => {
623 self.set_status(Status::ConnectionError, cx);
624 return Err(err);
625 }
626 }
627 }
628 _ = status_rx.next().fuse() => {
629 return Err(anyhow!("authentication canceled"));
630 }
631 }
632 }
633 let credentials = credentials.unwrap();
634
635 if was_disconnected {
636 self.set_status(Status::Connecting, cx);
637 } else {
638 self.set_status(Status::Reconnecting, cx);
639 }
640
641 match self.establish_connection(&credentials, cx).await {
642 Ok(conn) => {
643 self.state.write().credentials = Some(credentials.clone());
644 if !read_from_keychain && IMPERSONATE_LOGIN.is_none() {
645 write_credentials_to_keychain(&credentials, cx).log_err();
646 }
647 self.set_connection(conn, cx).await;
648 Ok(())
649 }
650 Err(EstablishConnectionError::Unauthorized) => {
651 self.state.write().credentials.take();
652 if read_from_keychain {
653 cx.platform().delete_credentials(&ZED_SERVER_URL).log_err();
654 self.set_status(Status::SignedOut, cx);
655 self.authenticate_and_connect(false, cx).await
656 } else {
657 self.set_status(Status::ConnectionError, cx);
658 Err(EstablishConnectionError::Unauthorized)?
659 }
660 }
661 Err(EstablishConnectionError::UpgradeRequired) => {
662 self.set_status(Status::UpgradeRequired, cx);
663 Err(EstablishConnectionError::UpgradeRequired)?
664 }
665 Err(error) => {
666 self.set_status(Status::ConnectionError, cx);
667 Err(error)?
668 }
669 }
670 }
671
672 async fn set_connection(self: &Arc<Self>, conn: Connection, cx: &AsyncAppContext) {
673 let executor = cx.background();
674 log::info!("add connection to peer");
675 let (connection_id, handle_io, mut incoming) = self
676 .peer
677 .add_connection(conn, move |duration| executor.timer(duration))
678 .await;
679 log::info!("set status to connected {}", connection_id);
680 self.set_status(Status::Connected { connection_id }, cx);
681 cx.foreground()
682 .spawn({
683 let cx = cx.clone();
684 let this = self.clone();
685 async move {
686 let mut message_id = 0_usize;
687 while let Some(message) = incoming.next().await {
688 let mut state = this.state.write();
689 message_id += 1;
690 let type_name = message.payload_type_name();
691 let payload_type_id = message.payload_type_id();
692 let sender_id = message.original_sender_id().map(|id| id.0);
693
694 let model = state
695 .models_by_message_type
696 .get(&payload_type_id)
697 .and_then(|model| model.upgrade(&cx))
698 .map(AnyEntityHandle::Model)
699 .or_else(|| {
700 let entity_type_id =
701 *state.entity_types_by_message_type.get(&payload_type_id)?;
702 let entity_id = state
703 .entity_id_extractors
704 .get(&message.payload_type_id())
705 .map(|extract_entity_id| {
706 (extract_entity_id)(message.as_ref())
707 })?;
708
709 let entity = state
710 .entities_by_type_and_remote_id
711 .get(&(entity_type_id, entity_id))?;
712 if let Some(entity) = entity.upgrade(&cx) {
713 Some(entity)
714 } else {
715 state
716 .entities_by_type_and_remote_id
717 .remove(&(entity_type_id, entity_id));
718 None
719 }
720 });
721
722 let model = if let Some(model) = model {
723 model
724 } else {
725 log::info!("unhandled message {}", type_name);
726 continue;
727 };
728
729 if let Some(handler) = state.message_handlers.get(&payload_type_id).cloned()
730 {
731 drop(state); // Avoid deadlocks if the handler interacts with rpc::Client
732 let future = handler(model, message, &this, cx.clone());
733
734 let client_id = this.id;
735 log::debug!(
736 "rpc message received. client_id:{}, message_id:{}, sender_id:{:?}, type:{}",
737 client_id,
738 message_id,
739 sender_id,
740 type_name
741 );
742 cx.foreground()
743 .spawn(async move {
744 match future.await {
745 Ok(()) => {
746 log::debug!(
747 "rpc message handled. client_id:{}, message_id:{}, sender_id:{:?}, type:{}",
748 client_id,
749 message_id,
750 sender_id,
751 type_name
752 );
753 }
754 Err(error) => {
755 log::error!(
756 "error handling message. client_id:{}, message_id:{}, sender_id:{:?}, type:{}, error:{:?}",
757 client_id,
758 message_id,
759 sender_id,
760 type_name,
761 error
762 );
763 }
764 }
765 })
766 .detach();
767 } else {
768 log::info!("unhandled message {}", type_name);
769 }
770
771 // Don't starve the main thread when receiving lots of messages at once.
772 smol::future::yield_now().await;
773 }
774 }
775 })
776 .detach();
777
778 let handle_io = cx.background().spawn(handle_io);
779 let this = self.clone();
780 let cx = cx.clone();
781 cx.foreground()
782 .spawn(async move {
783 match handle_io.await {
784 Ok(()) => {
785 if *this.status().borrow() == (Status::Connected { connection_id }) {
786 this.set_status(Status::SignedOut, &cx);
787 }
788 }
789 Err(err) => {
790 log::error!("connection error: {:?}", err);
791 this.set_status(Status::ConnectionLost, &cx);
792 }
793 }
794 })
795 .detach();
796 }
797
798 fn authenticate(self: &Arc<Self>, cx: &AsyncAppContext) -> Task<Result<Credentials>> {
799 #[cfg(any(test, feature = "test-support"))]
800 if let Some(callback) = self.authenticate.read().as_ref() {
801 return callback(cx);
802 }
803
804 self.authenticate_with_browser(cx)
805 }
806
807 fn establish_connection(
808 self: &Arc<Self>,
809 credentials: &Credentials,
810 cx: &AsyncAppContext,
811 ) -> Task<Result<Connection, EstablishConnectionError>> {
812 #[cfg(any(test, feature = "test-support"))]
813 if let Some(callback) = self.establish_connection.read().as_ref() {
814 return callback(credentials, cx);
815 }
816
817 self.establish_websocket_connection(credentials, cx)
818 }
819
820 fn establish_websocket_connection(
821 self: &Arc<Self>,
822 credentials: &Credentials,
823 cx: &AsyncAppContext,
824 ) -> Task<Result<Connection, EstablishConnectionError>> {
825 let request = Request::builder()
826 .header(
827 "Authorization",
828 format!("{} {}", credentials.user_id, credentials.access_token),
829 )
830 .header("x-zed-protocol-version", rpc::PROTOCOL_VERSION);
831
832 let http = self.http.clone();
833 cx.background().spawn(async move {
834 let mut rpc_url = format!("{}/rpc", *ZED_SERVER_URL);
835 let rpc_response = http.get(&rpc_url, Default::default(), false).await?;
836 if rpc_response.status().is_redirection() {
837 rpc_url = rpc_response
838 .headers()
839 .get("Location")
840 .ok_or_else(|| anyhow!("missing location header in /rpc response"))?
841 .to_str()
842 .map_err(EstablishConnectionError::other)?
843 .to_string();
844 }
845 // Until we switch the zed.dev domain to point to the new Next.js app, there
846 // will be no redirect required, and the app will connect directly to
847 // wss://zed.dev/rpc.
848 else if rpc_response.status() != StatusCode::UPGRADE_REQUIRED {
849 Err(anyhow!(
850 "unexpected /rpc response status {}",
851 rpc_response.status()
852 ))?
853 }
854
855 let mut rpc_url = Url::parse(&rpc_url).context("invalid rpc url")?;
856 let rpc_host = rpc_url
857 .host_str()
858 .zip(rpc_url.port_or_known_default())
859 .ok_or_else(|| anyhow!("missing host in rpc url"))?;
860 let stream = smol::net::TcpStream::connect(rpc_host).await?;
861
862 log::info!("connected to rpc endpoint {}", rpc_url);
863
864 match rpc_url.scheme() {
865 "https" => {
866 rpc_url.set_scheme("wss").unwrap();
867 let request = request.uri(rpc_url.as_str()).body(())?;
868 let (stream, _) =
869 async_tungstenite::async_tls::client_async_tls(request, stream).await?;
870 Ok(Connection::new(
871 stream
872 .map_err(|error| anyhow!(error))
873 .sink_map_err(|error| anyhow!(error)),
874 ))
875 }
876 "http" => {
877 rpc_url.set_scheme("ws").unwrap();
878 let request = request.uri(rpc_url.as_str()).body(())?;
879 let (stream, _) = async_tungstenite::client_async(request, stream).await?;
880 Ok(Connection::new(
881 stream
882 .map_err(|error| anyhow!(error))
883 .sink_map_err(|error| anyhow!(error)),
884 ))
885 }
886 _ => Err(anyhow!("invalid rpc url: {}", rpc_url))?,
887 }
888 })
889 }
890
891 pub fn authenticate_with_browser(
892 self: &Arc<Self>,
893 cx: &AsyncAppContext,
894 ) -> Task<Result<Credentials>> {
895 let platform = cx.platform();
896 let executor = cx.background();
897 let telemetry = self.telemetry.clone();
898 executor.clone().spawn(async move {
899 // Generate a pair of asymmetric encryption keys. The public key will be used by the
900 // zed server to encrypt the user's access token, so that it can'be intercepted by
901 // any other app running on the user's device.
902 let (public_key, private_key) =
903 rpc::auth::keypair().expect("failed to generate keypair for auth");
904 let public_key_string =
905 String::try_from(public_key).expect("failed to serialize public key for auth");
906
907 // Start an HTTP server to receive the redirect from Zed's sign-in page.
908 let server = tiny_http::Server::http("127.0.0.1:0").expect("failed to find open port");
909 let port = server.server_addr().port();
910
911 // Open the Zed sign-in page in the user's browser, with query parameters that indicate
912 // that the user is signing in from a Zed app running on the same device.
913 let mut url = format!(
914 "{}/native_app_signin?native_app_port={}&native_app_public_key={}",
915 *ZED_SERVER_URL, port, public_key_string
916 );
917
918 if let Some(impersonate_login) = IMPERSONATE_LOGIN.as_ref() {
919 log::info!("impersonating user @{}", impersonate_login);
920 write!(&mut url, "&impersonate={}", impersonate_login).unwrap();
921 }
922
923 platform.open_url(&url);
924
925 // Receive the HTTP request from the user's browser. Retrieve the user id and encrypted
926 // access token from the query params.
927 //
928 // TODO - Avoid ever starting more than one HTTP server. Maybe switch to using a
929 // custom URL scheme instead of this local HTTP server.
930 let (user_id, access_token) = executor
931 .spawn(async move {
932 for _ in 0..100 {
933 if let Some(req) = server.recv_timeout(Duration::from_secs(1))? {
934 let path = req.url();
935 let mut user_id = None;
936 let mut access_token = None;
937 let url = Url::parse(&format!("http://example.com{}", path))
938 .context("failed to parse login notification url")?;
939 for (key, value) in url.query_pairs() {
940 if key == "access_token" {
941 access_token = Some(value.to_string());
942 } else if key == "user_id" {
943 user_id = Some(value.to_string());
944 }
945 }
946
947 let post_auth_url =
948 format!("{}/native_app_signin_succeeded", *ZED_SERVER_URL);
949 req.respond(
950 tiny_http::Response::empty(302).with_header(
951 tiny_http::Header::from_bytes(
952 &b"Location"[..],
953 post_auth_url.as_bytes(),
954 )
955 .unwrap(),
956 ),
957 )
958 .context("failed to respond to login http request")?;
959 return Ok((
960 user_id.ok_or_else(|| anyhow!("missing user_id parameter"))?,
961 access_token
962 .ok_or_else(|| anyhow!("missing access_token parameter"))?,
963 ));
964 }
965 }
966
967 Err(anyhow!("didn't receive login redirect"))
968 })
969 .await?;
970
971 let access_token = private_key
972 .decrypt_string(&access_token)
973 .context("failed to decrypt access token")?;
974 platform.activate(true);
975
976 telemetry.report_event("authenticate with browser", Default::default());
977
978 Ok(Credentials {
979 user_id: user_id.parse()?,
980 access_token,
981 })
982 })
983 }
984
985 pub fn disconnect(self: &Arc<Self>, cx: &AsyncAppContext) -> Result<()> {
986 let conn_id = self.connection_id()?;
987 self.peer.disconnect(conn_id);
988 self.set_status(Status::SignedOut, cx);
989 Ok(())
990 }
991
992 fn connection_id(&self) -> Result<ConnectionId> {
993 if let Status::Connected { connection_id, .. } = *self.status().borrow() {
994 Ok(connection_id)
995 } else {
996 Err(anyhow!("not connected"))
997 }
998 }
999
1000 pub fn send<T: EnvelopedMessage>(&self, message: T) -> Result<()> {
1001 log::debug!("rpc send. client_id:{}, name:{}", self.id, T::NAME);
1002 self.peer.send(self.connection_id()?, message)
1003 }
1004
1005 pub fn request<T: RequestMessage>(
1006 &self,
1007 request: T,
1008 ) -> impl Future<Output = Result<T::Response>> {
1009 let client_id = self.id;
1010 log::debug!(
1011 "rpc request start. client_id:{}. name:{}",
1012 client_id,
1013 T::NAME
1014 );
1015 let response = self
1016 .connection_id()
1017 .map(|conn_id| self.peer.request(conn_id, request));
1018 async move {
1019 let response = response?.await;
1020 log::debug!(
1021 "rpc request finish. client_id:{}. name:{}",
1022 client_id,
1023 T::NAME
1024 );
1025 response
1026 }
1027 }
1028
1029 fn respond<T: RequestMessage>(&self, receipt: Receipt<T>, response: T::Response) -> Result<()> {
1030 log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME);
1031 self.peer.respond(receipt, response)
1032 }
1033
1034 fn respond_with_error<T: RequestMessage>(
1035 &self,
1036 receipt: Receipt<T>,
1037 error: proto::Error,
1038 ) -> Result<()> {
1039 log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME);
1040 self.peer.respond_with_error(receipt, error)
1041 }
1042
1043 pub fn start_telemetry(&self, db: Arc<Db>) {
1044 self.telemetry.start(db);
1045 }
1046
1047 pub fn report_event(&self, kind: &str, properties: Value) {
1048 self.telemetry.report_event(kind, properties)
1049 }
1050
1051 pub fn telemetry_log_file_path(&self) -> Option<PathBuf> {
1052 self.telemetry.log_file_path()
1053 }
1054}
1055
1056impl AnyWeakEntityHandle {
1057 fn upgrade(&self, cx: &AsyncAppContext) -> Option<AnyEntityHandle> {
1058 match self {
1059 AnyWeakEntityHandle::Model(handle) => handle.upgrade(cx).map(AnyEntityHandle::Model),
1060 AnyWeakEntityHandle::View(handle) => handle.upgrade(cx).map(AnyEntityHandle::View),
1061 }
1062 }
1063}
1064
1065fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option<Credentials> {
1066 if IMPERSONATE_LOGIN.is_some() {
1067 return None;
1068 }
1069
1070 let (user_id, access_token) = cx
1071 .platform()
1072 .read_credentials(&ZED_SERVER_URL)
1073 .log_err()
1074 .flatten()?;
1075 Some(Credentials {
1076 user_id: user_id.parse().ok()?,
1077 access_token: String::from_utf8(access_token).ok()?,
1078 })
1079}
1080
1081fn write_credentials_to_keychain(credentials: &Credentials, cx: &AsyncAppContext) -> Result<()> {
1082 cx.platform().write_credentials(
1083 &ZED_SERVER_URL,
1084 &credentials.user_id.to_string(),
1085 credentials.access_token.as_bytes(),
1086 )
1087}
1088
1089const WORKTREE_URL_PREFIX: &str = "zed://worktrees/";
1090
1091pub fn encode_worktree_url(id: u64, access_token: &str) -> String {
1092 format!("{}{}/{}", WORKTREE_URL_PREFIX, id, access_token)
1093}
1094
1095pub fn decode_worktree_url(url: &str) -> Option<(u64, String)> {
1096 let path = url.trim().strip_prefix(WORKTREE_URL_PREFIX)?;
1097 let mut parts = path.split('/');
1098 let id = parts.next()?.parse::<u64>().ok()?;
1099 let access_token = parts.next()?;
1100 if access_token.is_empty() {
1101 return None;
1102 }
1103 Some((id, access_token.to_string()))
1104}
1105
1106#[cfg(test)]
1107mod tests {
1108 use super::*;
1109 use crate::test::{FakeHttpClient, FakeServer};
1110 use gpui::{executor::Deterministic, TestAppContext};
1111 use parking_lot::Mutex;
1112 use std::future;
1113
1114 #[gpui::test(iterations = 10)]
1115 async fn test_reconnection(cx: &mut TestAppContext) {
1116 cx.foreground().forbid_parking();
1117
1118 let user_id = 5;
1119 let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1120 let server = FakeServer::for_client(user_id, &client, cx).await;
1121 let mut status = client.status();
1122 assert!(matches!(
1123 status.next().await,
1124 Some(Status::Connected { .. })
1125 ));
1126 assert_eq!(server.auth_count(), 1);
1127
1128 server.forbid_connections();
1129 server.disconnect();
1130 while !matches!(status.next().await, Some(Status::ReconnectionError { .. })) {}
1131
1132 server.allow_connections();
1133 cx.foreground().advance_clock(Duration::from_secs(10));
1134 while !matches!(status.next().await, Some(Status::Connected { .. })) {}
1135 assert_eq!(server.auth_count(), 1); // Client reused the cached credentials when reconnecting
1136
1137 server.forbid_connections();
1138 server.disconnect();
1139 while !matches!(status.next().await, Some(Status::ReconnectionError { .. })) {}
1140
1141 // Clear cached credentials after authentication fails
1142 server.roll_access_token();
1143 server.allow_connections();
1144 cx.foreground().advance_clock(Duration::from_secs(10));
1145 while !matches!(status.next().await, Some(Status::Connected { .. })) {}
1146 assert_eq!(server.auth_count(), 2); // Client re-authenticated due to an invalid token
1147 }
1148
1149 #[gpui::test(iterations = 10)]
1150 async fn test_authenticating_more_than_once(
1151 cx: &mut TestAppContext,
1152 deterministic: Arc<Deterministic>,
1153 ) {
1154 cx.foreground().forbid_parking();
1155
1156 let auth_count = Arc::new(Mutex::new(0));
1157 let dropped_auth_count = Arc::new(Mutex::new(0));
1158 let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1159 client.override_authenticate({
1160 let auth_count = auth_count.clone();
1161 let dropped_auth_count = dropped_auth_count.clone();
1162 move |cx| {
1163 let auth_count = auth_count.clone();
1164 let dropped_auth_count = dropped_auth_count.clone();
1165 cx.foreground().spawn(async move {
1166 *auth_count.lock() += 1;
1167 let _drop = util::defer(move || *dropped_auth_count.lock() += 1);
1168 future::pending::<()>().await;
1169 unreachable!()
1170 })
1171 }
1172 });
1173
1174 let _authenticate = cx.spawn(|cx| {
1175 let client = client.clone();
1176 async move { client.authenticate_and_connect(false, &cx).await }
1177 });
1178 deterministic.run_until_parked();
1179 assert_eq!(*auth_count.lock(), 1);
1180 assert_eq!(*dropped_auth_count.lock(), 0);
1181
1182 let _authenticate = cx.spawn(|cx| {
1183 let client = client.clone();
1184 async move { client.authenticate_and_connect(false, &cx).await }
1185 });
1186 deterministic.run_until_parked();
1187 assert_eq!(*auth_count.lock(), 2);
1188 assert_eq!(*dropped_auth_count.lock(), 1);
1189 }
1190
1191 #[test]
1192 fn test_encode_and_decode_worktree_url() {
1193 let url = encode_worktree_url(5, "deadbeef");
1194 assert_eq!(decode_worktree_url(&url), Some((5, "deadbeef".to_string())));
1195 assert_eq!(
1196 decode_worktree_url(&format!("\n {}\t", url)),
1197 Some((5, "deadbeef".to_string()))
1198 );
1199 assert_eq!(decode_worktree_url("not://the-right-format"), None);
1200 }
1201
1202 #[gpui::test]
1203 async fn test_subscribing_to_entity(cx: &mut TestAppContext) {
1204 cx.foreground().forbid_parking();
1205
1206 let user_id = 5;
1207 let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1208 let server = FakeServer::for_client(user_id, &client, cx).await;
1209
1210 let (done_tx1, mut done_rx1) = smol::channel::unbounded();
1211 let (done_tx2, mut done_rx2) = smol::channel::unbounded();
1212 client.add_model_message_handler(
1213 move |model: ModelHandle<Model>, _: TypedEnvelope<proto::JoinProject>, _, cx| {
1214 match model.read_with(&cx, |model, _| model.id) {
1215 1 => done_tx1.try_send(()).unwrap(),
1216 2 => done_tx2.try_send(()).unwrap(),
1217 _ => unreachable!(),
1218 }
1219 async { Ok(()) }
1220 },
1221 );
1222 let model1 = cx.add_model(|_| Model {
1223 id: 1,
1224 subscription: None,
1225 });
1226 let model2 = cx.add_model(|_| Model {
1227 id: 2,
1228 subscription: None,
1229 });
1230 let model3 = cx.add_model(|_| Model {
1231 id: 3,
1232 subscription: None,
1233 });
1234
1235 let _subscription1 = model1.update(cx, |_, cx| client.add_model_for_remote_entity(1, cx));
1236 let _subscription2 = model2.update(cx, |_, cx| client.add_model_for_remote_entity(2, cx));
1237 // Ensure dropping a subscription for the same entity type still allows receiving of
1238 // messages for other entity IDs of the same type.
1239 let subscription3 = model3.update(cx, |_, cx| client.add_model_for_remote_entity(3, cx));
1240 drop(subscription3);
1241
1242 server.send(proto::JoinProject { project_id: 1 });
1243 server.send(proto::JoinProject { project_id: 2 });
1244 done_rx1.next().await.unwrap();
1245 done_rx2.next().await.unwrap();
1246 }
1247
1248 #[gpui::test]
1249 async fn test_subscribing_after_dropping_subscription(cx: &mut TestAppContext) {
1250 cx.foreground().forbid_parking();
1251
1252 let user_id = 5;
1253 let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1254 let server = FakeServer::for_client(user_id, &client, cx).await;
1255
1256 let model = cx.add_model(|_| Model::default());
1257 let (done_tx1, _done_rx1) = smol::channel::unbounded();
1258 let (done_tx2, mut done_rx2) = smol::channel::unbounded();
1259 let subscription1 = client.add_message_handler(
1260 model.clone(),
1261 move |_, _: TypedEnvelope<proto::Ping>, _, _| {
1262 done_tx1.try_send(()).unwrap();
1263 async { Ok(()) }
1264 },
1265 );
1266 drop(subscription1);
1267 let _subscription2 =
1268 client.add_message_handler(model, move |_, _: TypedEnvelope<proto::Ping>, _, _| {
1269 done_tx2.try_send(()).unwrap();
1270 async { Ok(()) }
1271 });
1272 server.send(proto::Ping {});
1273 done_rx2.next().await.unwrap();
1274 }
1275
1276 #[gpui::test]
1277 async fn test_dropping_subscription_in_handler(cx: &mut TestAppContext) {
1278 cx.foreground().forbid_parking();
1279
1280 let user_id = 5;
1281 let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1282 let server = FakeServer::for_client(user_id, &client, cx).await;
1283
1284 let model = cx.add_model(|_| Model::default());
1285 let (done_tx, mut done_rx) = smol::channel::unbounded();
1286 let subscription = client.add_message_handler(
1287 model.clone(),
1288 move |model, _: TypedEnvelope<proto::Ping>, _, mut cx| {
1289 model.update(&mut cx, |model, _| model.subscription.take());
1290 done_tx.try_send(()).unwrap();
1291 async { Ok(()) }
1292 },
1293 );
1294 model.update(cx, |model, _| {
1295 model.subscription = Some(subscription);
1296 });
1297 server.send(proto::Ping {});
1298 done_rx.next().await.unwrap();
1299 }
1300
1301 #[derive(Default)]
1302 struct Model {
1303 id: usize,
1304 subscription: Option<Subscription>,
1305 }
1306
1307 impl Entity for Model {
1308 type Event = ();
1309 }
1310}