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