Start work on handling TLS for the RPC endpoint

This commit is contained in:
Max Brunsfeld
2021-07-06 16:27:57 -07:00
parent 59fe0549cc
commit 193c704875
6 changed files with 114 additions and 21 deletions
+15 -5
View File
@@ -115,13 +115,22 @@ impl Client {
access_token: String,
executor: &Arc<Background>,
) -> surf::Result<()> {
// TODO - If the `ZED_SERVER_URL` uses https, then wrap this stream in
// a TLS stream using `native-tls`.
let stream = smol::net::TcpStream::connect(&address).await?;
log::info!("connected to rpc address {}", address);
let (connection_id, handler) = self.peer.add_connection(stream).await;
executor.spawn(handler.run()).detach();
let connection_id = if ZED_SERVER_URL.starts_with("https") {
let host = address.split(':').next().unwrap();
let stream = async_native_tls::connect(host, stream)
.await
.context("failed to establish TLS connection")?;
let (conn_id, handler) = self.peer.add_connection(stream).await;
executor.spawn(handler.run()).detach();
conn_id
} else {
let (conn_id, handler) = self.peer.add_connection(stream).await;
executor.spawn(handler.run()).detach();
conn_id
};
log::info!("connected to rpc address {}", address);
let auth_response = self
.peer
@@ -138,6 +147,7 @@ impl Client {
Err(anyhow!("failed to authenticate with RPC server"))?;
}
log::info!("authenticated rpc connection as user {}", user_id);
self.state.write().await.connection_id = Some(connection_id);
Ok(())
}
+5 -2
View File
@@ -6,6 +6,7 @@ use crate::{
language::LanguageRegistry,
rpc,
settings::Settings,
util::SurfResultExt as _,
worktree::{File, Worktree},
AppState,
};
@@ -684,7 +685,9 @@ impl Workspace {
let platform = cx.platform();
let task = cx.spawn(|this, mut cx| async move {
rpc.log_in_and_connect(&cx).await?;
rpc.log_in_and_connect(&cx)
.await
.context("failed to establish rpc connection")?;
let share_task = this.update(&mut cx, |this, cx| {
let worktree = this.worktrees.iter().next()?;
@@ -705,7 +708,7 @@ impl Workspace {
cx.spawn(|_, _| async move {
if let Err(e) = task.await {
log::error!("sharing failed: {}", e);
log::error!("sharing failed: {:?}", e);
}
})
.detach();