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