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 let user_id = state.credentials.as_ref().map(|c| c.user_id);
324
325 match status {
326 Status::Connected { .. } => {
327 self.telemetry.set_user_id(user_id);
328 state._reconnect_task = None;
329 }
330 Status::ConnectionLost => {
331 let this = self.clone();
332 let reconnect_interval = state.reconnect_interval;
333 state._reconnect_task = Some(cx.spawn(|cx| async move {
334 let mut rng = StdRng::from_entropy();
335 let mut delay = Duration::from_millis(100);
336 while let Err(error) = this.authenticate_and_connect(true, &cx).await {
337 log::error!("failed to connect {}", error);
338 if matches!(*this.status().borrow(), Status::ConnectionError) {
339 this.set_status(
340 Status::ReconnectionError {
341 next_reconnection: Instant::now() + delay,
342 },
343 &cx,
344 );
345 cx.background().timer(delay).await;
346 delay = delay
347 .mul_f32(rng.gen_range(1.0..=2.0))
348 .min(reconnect_interval);
349 } else {
350 break;
351 }
352 }
353 }));
354 }
355 Status::SignedOut | Status::UpgradeRequired => {
356 self.telemetry.set_user_id(user_id);
357 state._reconnect_task.take();
358 }
359 _ => {}
360 }
361 }
362
363 pub fn add_view_for_remote_entity<T: View>(
364 self: &Arc<Self>,
365 remote_id: u64,
366 cx: &mut ViewContext<T>,
367 ) -> Subscription {
368 let id = (TypeId::of::<T>(), remote_id);
369 self.state
370 .write()
371 .entities_by_type_and_remote_id
372 .insert(id, AnyWeakEntityHandle::View(cx.weak_handle().into()));
373 Subscription::Entity {
374 client: Arc::downgrade(self),
375 id,
376 }
377 }
378
379 pub fn add_model_for_remote_entity<T: Entity>(
380 self: &Arc<Self>,
381 remote_id: u64,
382 cx: &mut ModelContext<T>,
383 ) -> Subscription {
384 let id = (TypeId::of::<T>(), remote_id);
385 self.state
386 .write()
387 .entities_by_type_and_remote_id
388 .insert(id, AnyWeakEntityHandle::Model(cx.weak_handle().into()));
389 Subscription::Entity {
390 client: Arc::downgrade(self),
391 id,
392 }
393 }
394
395 pub fn add_message_handler<M, E, H, F>(
396 self: &Arc<Self>,
397 model: ModelHandle<E>,
398 handler: H,
399 ) -> Subscription
400 where
401 M: EnvelopedMessage,
402 E: Entity,
403 H: 'static
404 + Send
405 + Sync
406 + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
407 F: 'static + Future<Output = Result<()>>,
408 {
409 let message_type_id = TypeId::of::<M>();
410
411 let mut state = self.state.write();
412 state
413 .models_by_message_type
414 .insert(message_type_id, model.downgrade().into());
415
416 let prev_handler = state.message_handlers.insert(
417 message_type_id,
418 Arc::new(move |handle, envelope, client, cx| {
419 let handle = if let AnyEntityHandle::Model(handle) = handle {
420 handle
421 } else {
422 unreachable!();
423 };
424 let model = handle.downcast::<E>().unwrap();
425 let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
426 handler(model, *envelope, client.clone(), cx).boxed_local()
427 }),
428 );
429 if prev_handler.is_some() {
430 panic!("registered handler for the same message twice");
431 }
432
433 Subscription::Message {
434 client: Arc::downgrade(self),
435 id: message_type_id,
436 }
437 }
438
439 pub fn add_view_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
440 where
441 M: EntityMessage,
442 E: View,
443 H: 'static
444 + Send
445 + Sync
446 + Fn(ViewHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
447 F: 'static + Future<Output = Result<()>>,
448 {
449 self.add_entity_message_handler::<M, E, _, _>(move |handle, message, client, cx| {
450 if let AnyEntityHandle::View(handle) = handle {
451 handler(handle.downcast::<E>().unwrap(), message, client, cx)
452 } else {
453 unreachable!();
454 }
455 })
456 }
457
458 pub fn add_model_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
459 where
460 M: EntityMessage,
461 E: Entity,
462 H: 'static
463 + Send
464 + Sync
465 + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
466 F: 'static + Future<Output = Result<()>>,
467 {
468 self.add_entity_message_handler::<M, E, _, _>(move |handle, message, client, cx| {
469 if let AnyEntityHandle::Model(handle) = handle {
470 handler(handle.downcast::<E>().unwrap(), message, client, cx)
471 } else {
472 unreachable!();
473 }
474 })
475 }
476
477 fn add_entity_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
478 where
479 M: EntityMessage,
480 E: Entity,
481 H: 'static
482 + Send
483 + Sync
484 + Fn(AnyEntityHandle, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
485 F: 'static + Future<Output = Result<()>>,
486 {
487 let model_type_id = TypeId::of::<E>();
488 let message_type_id = TypeId::of::<M>();
489
490 let mut state = self.state.write();
491 state
492 .entity_types_by_message_type
493 .insert(message_type_id, model_type_id);
494 state
495 .entity_id_extractors
496 .entry(message_type_id)
497 .or_insert_with(|| {
498 |envelope| {
499 envelope
500 .as_any()
501 .downcast_ref::<TypedEnvelope<M>>()
502 .unwrap()
503 .payload
504 .remote_entity_id()
505 }
506 });
507 let prev_handler = state.message_handlers.insert(
508 message_type_id,
509 Arc::new(move |handle, envelope, client, cx| {
510 let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
511 handler(handle, *envelope, client.clone(), cx).boxed_local()
512 }),
513 );
514 if prev_handler.is_some() {
515 panic!("registered handler for the same message twice");
516 }
517 }
518
519 pub fn add_model_request_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
520 where
521 M: EntityMessage + RequestMessage,
522 E: Entity,
523 H: 'static
524 + Send
525 + Sync
526 + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
527 F: 'static + Future<Output = Result<M::Response>>,
528 {
529 self.add_model_message_handler(move |entity, envelope, client, cx| {
530 Self::respond_to_request::<M, _>(
531 envelope.receipt(),
532 handler(entity, envelope, client.clone(), cx),
533 client,
534 )
535 })
536 }
537
538 pub fn add_view_request_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
539 where
540 M: EntityMessage + RequestMessage,
541 E: View,
542 H: 'static
543 + Send
544 + Sync
545 + Fn(ViewHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
546 F: 'static + Future<Output = Result<M::Response>>,
547 {
548 self.add_view_message_handler(move |entity, envelope, client, cx| {
549 Self::respond_to_request::<M, _>(
550 envelope.receipt(),
551 handler(entity, envelope, client.clone(), cx),
552 client,
553 )
554 })
555 }
556
557 async fn respond_to_request<T: RequestMessage, F: Future<Output = Result<T::Response>>>(
558 receipt: Receipt<T>,
559 response: F,
560 client: Arc<Self>,
561 ) -> Result<()> {
562 match response.await {
563 Ok(response) => {
564 client.respond(receipt, response)?;
565 Ok(())
566 }
567 Err(error) => {
568 client.respond_with_error(
569 receipt,
570 proto::Error {
571 message: format!("{:?}", error),
572 },
573 )?;
574 Err(error)
575 }
576 }
577 }
578
579 pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool {
580 read_credentials_from_keychain(cx).is_some()
581 }
582
583 #[async_recursion(?Send)]
584 pub async fn authenticate_and_connect(
585 self: &Arc<Self>,
586 try_keychain: bool,
587 cx: &AsyncAppContext,
588 ) -> anyhow::Result<()> {
589 let was_disconnected = match *self.status().borrow() {
590 Status::SignedOut => true,
591 Status::ConnectionError
592 | Status::ConnectionLost
593 | Status::Authenticating { .. }
594 | Status::Reauthenticating { .. }
595 | Status::ReconnectionError { .. } => false,
596 Status::Connected { .. } | Status::Connecting { .. } | Status::Reconnecting { .. } => {
597 return Ok(())
598 }
599 Status::UpgradeRequired => return Err(EstablishConnectionError::UpgradeRequired)?,
600 };
601
602 if was_disconnected {
603 self.set_status(Status::Authenticating, cx);
604 } else {
605 self.set_status(Status::Reauthenticating, cx)
606 }
607
608 let mut read_from_keychain = false;
609 let mut credentials = self.state.read().credentials.clone();
610 if credentials.is_none() && try_keychain {
611 credentials = read_credentials_from_keychain(cx);
612 read_from_keychain = credentials.is_some();
613 if read_from_keychain {
614 self.report_event("read credentials from keychain", Default::default());
615 }
616 }
617 if credentials.is_none() {
618 let mut status_rx = self.status();
619 let _ = status_rx.next().await;
620 futures::select_biased! {
621 authenticate = self.authenticate(cx).fuse() => {
622 match authenticate {
623 Ok(creds) => credentials = Some(creds),
624 Err(err) => {
625 self.set_status(Status::ConnectionError, cx);
626 return Err(err);
627 }
628 }
629 }
630 _ = status_rx.next().fuse() => {
631 return Err(anyhow!("authentication canceled"));
632 }
633 }
634 }
635 let credentials = credentials.unwrap();
636
637 if was_disconnected {
638 self.set_status(Status::Connecting, cx);
639 } else {
640 self.set_status(Status::Reconnecting, cx);
641 }
642
643 match self.establish_connection(&credentials, cx).await {
644 Ok(conn) => {
645 self.state.write().credentials = Some(credentials.clone());
646 if !read_from_keychain && IMPERSONATE_LOGIN.is_none() {
647 write_credentials_to_keychain(&credentials, cx).log_err();
648 }
649 self.set_connection(conn, cx).await;
650 Ok(())
651 }
652 Err(EstablishConnectionError::Unauthorized) => {
653 self.state.write().credentials.take();
654 if read_from_keychain {
655 cx.platform().delete_credentials(&ZED_SERVER_URL).log_err();
656 self.set_status(Status::SignedOut, cx);
657 self.authenticate_and_connect(false, cx).await
658 } else {
659 self.set_status(Status::ConnectionError, cx);
660 Err(EstablishConnectionError::Unauthorized)?
661 }
662 }
663 Err(EstablishConnectionError::UpgradeRequired) => {
664 self.set_status(Status::UpgradeRequired, cx);
665 Err(EstablishConnectionError::UpgradeRequired)?
666 }
667 Err(error) => {
668 self.set_status(Status::ConnectionError, cx);
669 Err(error)?
670 }
671 }
672 }
673
674 async fn set_connection(self: &Arc<Self>, conn: Connection, cx: &AsyncAppContext) {
675 let executor = cx.background();
676 log::info!("add connection to peer");
677 let (connection_id, handle_io, mut incoming) = self
678 .peer
679 .add_connection(conn, move |duration| executor.timer(duration))
680 .await;
681 log::info!("set status to connected {}", connection_id);
682 self.set_status(Status::Connected { connection_id }, cx);
683 cx.foreground()
684 .spawn({
685 let cx = cx.clone();
686 let this = self.clone();
687 async move {
688 let mut message_id = 0_usize;
689 while let Some(message) = incoming.next().await {
690 let mut state = this.state.write();
691 message_id += 1;
692 let type_name = message.payload_type_name();
693 let payload_type_id = message.payload_type_id();
694 let sender_id = message.original_sender_id().map(|id| id.0);
695
696 let model = state
697 .models_by_message_type
698 .get(&payload_type_id)
699 .and_then(|model| model.upgrade(&cx))
700 .map(AnyEntityHandle::Model)
701 .or_else(|| {
702 let entity_type_id =
703 *state.entity_types_by_message_type.get(&payload_type_id)?;
704 let entity_id = state
705 .entity_id_extractors
706 .get(&message.payload_type_id())
707 .map(|extract_entity_id| {
708 (extract_entity_id)(message.as_ref())
709 })?;
710
711 let entity = state
712 .entities_by_type_and_remote_id
713 .get(&(entity_type_id, entity_id))?;
714 if let Some(entity) = entity.upgrade(&cx) {
715 Some(entity)
716 } else {
717 state
718 .entities_by_type_and_remote_id
719 .remove(&(entity_type_id, entity_id));
720 None
721 }
722 });
723
724 let model = if let Some(model) = model {
725 model
726 } else {
727 log::info!("unhandled message {}", type_name);
728 continue;
729 };
730
731 if let Some(handler) = state.message_handlers.get(&payload_type_id).cloned()
732 {
733 drop(state); // Avoid deadlocks if the handler interacts with rpc::Client
734 let future = handler(model, message, &this, cx.clone());
735
736 let client_id = this.id;
737 log::debug!(
738 "rpc message received. client_id:{}, message_id:{}, sender_id:{:?}, type:{}",
739 client_id,
740 message_id,
741 sender_id,
742 type_name
743 );
744 cx.foreground()
745 .spawn(async move {
746 match future.await {
747 Ok(()) => {
748 log::debug!(
749 "rpc message handled. client_id:{}, message_id:{}, sender_id:{:?}, type:{}",
750 client_id,
751 message_id,
752 sender_id,
753 type_name
754 );
755 }
756 Err(error) => {
757 log::error!(
758 "error handling message. client_id:{}, message_id:{}, sender_id:{:?}, type:{}, error:{:?}",
759 client_id,
760 message_id,
761 sender_id,
762 type_name,
763 error
764 );
765 }
766 }
767 })
768 .detach();
769 } else {
770 log::info!("unhandled message {}", type_name);
771 }
772
773 // Don't starve the main thread when receiving lots of messages at once.
774 smol::future::yield_now().await;
775 }
776 }
777 })
778 .detach();
779
780 let handle_io = cx.background().spawn(handle_io);
781 let this = self.clone();
782 let cx = cx.clone();
783 cx.foreground()
784 .spawn(async move {
785 match handle_io.await {
786 Ok(()) => {
787 if *this.status().borrow() == (Status::Connected { connection_id }) {
788 this.set_status(Status::SignedOut, &cx);
789 }
790 }
791 Err(err) => {
792 log::error!("connection error: {:?}", err);
793 this.set_status(Status::ConnectionLost, &cx);
794 }
795 }
796 })
797 .detach();
798 }
799
800 fn authenticate(self: &Arc<Self>, cx: &AsyncAppContext) -> Task<Result<Credentials>> {
801 #[cfg(any(test, feature = "test-support"))]
802 if let Some(callback) = self.authenticate.read().as_ref() {
803 return callback(cx);
804 }
805
806 self.authenticate_with_browser(cx)
807 }
808
809 fn establish_connection(
810 self: &Arc<Self>,
811 credentials: &Credentials,
812 cx: &AsyncAppContext,
813 ) -> Task<Result<Connection, EstablishConnectionError>> {
814 #[cfg(any(test, feature = "test-support"))]
815 if let Some(callback) = self.establish_connection.read().as_ref() {
816 return callback(credentials, cx);
817 }
818
819 self.establish_websocket_connection(credentials, cx)
820 }
821
822 fn establish_websocket_connection(
823 self: &Arc<Self>,
824 credentials: &Credentials,
825 cx: &AsyncAppContext,
826 ) -> Task<Result<Connection, EstablishConnectionError>> {
827 let request = Request::builder()
828 .header(
829 "Authorization",
830 format!("{} {}", credentials.user_id, credentials.access_token),
831 )
832 .header("x-zed-protocol-version", rpc::PROTOCOL_VERSION);
833
834 let http = self.http.clone();
835 cx.background().spawn(async move {
836 let mut rpc_url = format!("{}/rpc", *ZED_SERVER_URL);
837 let rpc_response = http.get(&rpc_url, Default::default(), false).await?;
838 if rpc_response.status().is_redirection() {
839 rpc_url = rpc_response
840 .headers()
841 .get("Location")
842 .ok_or_else(|| anyhow!("missing location header in /rpc response"))?
843 .to_str()
844 .map_err(EstablishConnectionError::other)?
845 .to_string();
846 }
847 // Until we switch the zed.dev domain to point to the new Next.js app, there
848 // will be no redirect required, and the app will connect directly to
849 // wss://zed.dev/rpc.
850 else if rpc_response.status() != StatusCode::UPGRADE_REQUIRED {
851 Err(anyhow!(
852 "unexpected /rpc response status {}",
853 rpc_response.status()
854 ))?
855 }
856
857 let mut rpc_url = Url::parse(&rpc_url).context("invalid rpc url")?;
858 let rpc_host = rpc_url
859 .host_str()
860 .zip(rpc_url.port_or_known_default())
861 .ok_or_else(|| anyhow!("missing host in rpc url"))?;
862 let stream = smol::net::TcpStream::connect(rpc_host).await?;
863
864 log::info!("connected to rpc endpoint {}", rpc_url);
865
866 match rpc_url.scheme() {
867 "https" => {
868 rpc_url.set_scheme("wss").unwrap();
869 let request = request.uri(rpc_url.as_str()).body(())?;
870 let (stream, _) =
871 async_tungstenite::async_tls::client_async_tls(request, stream).await?;
872 Ok(Connection::new(
873 stream
874 .map_err(|error| anyhow!(error))
875 .sink_map_err(|error| anyhow!(error)),
876 ))
877 }
878 "http" => {
879 rpc_url.set_scheme("ws").unwrap();
880 let request = request.uri(rpc_url.as_str()).body(())?;
881 let (stream, _) = async_tungstenite::client_async(request, stream).await?;
882 Ok(Connection::new(
883 stream
884 .map_err(|error| anyhow!(error))
885 .sink_map_err(|error| anyhow!(error)),
886 ))
887 }
888 _ => Err(anyhow!("invalid rpc url: {}", rpc_url))?,
889 }
890 })
891 }
892
893 pub fn authenticate_with_browser(
894 self: &Arc<Self>,
895 cx: &AsyncAppContext,
896 ) -> Task<Result<Credentials>> {
897 let platform = cx.platform();
898 let executor = cx.background();
899 let telemetry = self.telemetry.clone();
900 executor.clone().spawn(async move {
901 // Generate a pair of asymmetric encryption keys. The public key will be used by the
902 // zed server to encrypt the user's access token, so that it can'be intercepted by
903 // any other app running on the user's device.
904 let (public_key, private_key) =
905 rpc::auth::keypair().expect("failed to generate keypair for auth");
906 let public_key_string =
907 String::try_from(public_key).expect("failed to serialize public key for auth");
908
909 // Start an HTTP server to receive the redirect from Zed's sign-in page.
910 let server = tiny_http::Server::http("127.0.0.1:0").expect("failed to find open port");
911 let port = server.server_addr().port();
912
913 // Open the Zed sign-in page in the user's browser, with query parameters that indicate
914 // that the user is signing in from a Zed app running on the same device.
915 let mut url = format!(
916 "{}/native_app_signin?native_app_port={}&native_app_public_key={}",
917 *ZED_SERVER_URL, port, public_key_string
918 );
919
920 if let Some(impersonate_login) = IMPERSONATE_LOGIN.as_ref() {
921 log::info!("impersonating user @{}", impersonate_login);
922 write!(&mut url, "&impersonate={}", impersonate_login).unwrap();
923 }
924
925 platform.open_url(&url);
926
927 // Receive the HTTP request from the user's browser. Retrieve the user id and encrypted
928 // access token from the query params.
929 //
930 // TODO - Avoid ever starting more than one HTTP server. Maybe switch to using a
931 // custom URL scheme instead of this local HTTP server.
932 let (user_id, access_token) = executor
933 .spawn(async move {
934 for _ in 0..100 {
935 if let Some(req) = server.recv_timeout(Duration::from_secs(1))? {
936 let path = req.url();
937 let mut user_id = None;
938 let mut access_token = None;
939 let url = Url::parse(&format!("http://example.com{}", path))
940 .context("failed to parse login notification url")?;
941 for (key, value) in url.query_pairs() {
942 if key == "access_token" {
943 access_token = Some(value.to_string());
944 } else if key == "user_id" {
945 user_id = Some(value.to_string());
946 }
947 }
948
949 let post_auth_url =
950 format!("{}/native_app_signin_succeeded", *ZED_SERVER_URL);
951 req.respond(
952 tiny_http::Response::empty(302).with_header(
953 tiny_http::Header::from_bytes(
954 &b"Location"[..],
955 post_auth_url.as_bytes(),
956 )
957 .unwrap(),
958 ),
959 )
960 .context("failed to respond to login http request")?;
961 return Ok((
962 user_id.ok_or_else(|| anyhow!("missing user_id parameter"))?,
963 access_token
964 .ok_or_else(|| anyhow!("missing access_token parameter"))?,
965 ));
966 }
967 }
968
969 Err(anyhow!("didn't receive login redirect"))
970 })
971 .await?;
972
973 let access_token = private_key
974 .decrypt_string(&access_token)
975 .context("failed to decrypt access token")?;
976 platform.activate(true);
977
978 telemetry.report_event("authenticate with browser", Default::default());
979
980 Ok(Credentials {
981 user_id: user_id.parse()?,
982 access_token,
983 })
984 })
985 }
986
987 pub fn disconnect(self: &Arc<Self>, cx: &AsyncAppContext) -> Result<()> {
988 let conn_id = self.connection_id()?;
989 self.peer.disconnect(conn_id);
990 self.set_status(Status::SignedOut, cx);
991 Ok(())
992 }
993
994 fn connection_id(&self) -> Result<ConnectionId> {
995 if let Status::Connected { connection_id, .. } = *self.status().borrow() {
996 Ok(connection_id)
997 } else {
998 Err(anyhow!("not connected"))
999 }
1000 }
1001
1002 pub fn send<T: EnvelopedMessage>(&self, message: T) -> Result<()> {
1003 log::debug!("rpc send. client_id:{}, name:{}", self.id, T::NAME);
1004 self.peer.send(self.connection_id()?, message)
1005 }
1006
1007 pub fn request<T: RequestMessage>(
1008 &self,
1009 request: T,
1010 ) -> impl Future<Output = Result<T::Response>> {
1011 let client_id = self.id;
1012 log::debug!(
1013 "rpc request start. client_id:{}. name:{}",
1014 client_id,
1015 T::NAME
1016 );
1017 let response = self
1018 .connection_id()
1019 .map(|conn_id| self.peer.request(conn_id, request));
1020 async move {
1021 let response = response?.await;
1022 log::debug!(
1023 "rpc request finish. client_id:{}. name:{}",
1024 client_id,
1025 T::NAME
1026 );
1027 response
1028 }
1029 }
1030
1031 fn respond<T: RequestMessage>(&self, receipt: Receipt<T>, response: T::Response) -> Result<()> {
1032 log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME);
1033 self.peer.respond(receipt, response)
1034 }
1035
1036 fn respond_with_error<T: RequestMessage>(
1037 &self,
1038 receipt: Receipt<T>,
1039 error: proto::Error,
1040 ) -> Result<()> {
1041 log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME);
1042 self.peer.respond_with_error(receipt, error)
1043 }
1044
1045 pub fn start_telemetry(&self, db: Arc<Db>) {
1046 self.telemetry.start(db);
1047 }
1048
1049 pub fn report_event(&self, kind: &str, properties: Value) {
1050 self.telemetry.report_event(kind, properties)
1051 }
1052
1053 pub fn telemetry_log_file_path(&self) -> Option<PathBuf> {
1054 self.telemetry.log_file_path()
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}