Use a websocket to connect to the server

Co-Authored-By: Max Brunsfeld <max@zed.dev>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-07-07 18:49:38 +02:00
parent 4466b6b76a
commit cb9002254f
4 changed files with 110 additions and 34 deletions

77
Cargo.lock generated
View File

@ -215,6 +215,18 @@ dependencies = [
"event-listener",
]
[[package]]
name = "async-native-tls"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e9e7a929bd34c68a82d58a4de7f86fffdaf97fb2af850162a7bb19dd7269b33"
dependencies = [
"async-std",
"native-tls",
"thiserror",
"url",
]
[[package]]
name = "async-net"
version = "1.5.0"
@ -1959,9 +1971,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.86"
version = "0.2.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
[[package]]
name = "libflate"
@ -2170,6 +2182,24 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a"
[[package]]
name = "native-tls"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8d96b2e1c8da3957d58100b09f102c6d9cfdfced01b7ec5a8974044bb09dbd4"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "nb-connect"
version = "1.0.3"
@ -2316,6 +2346,20 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549430950c79ae24e6d02e0b7404534ecf311d94cc9f861e9e4020187d13d885"
dependencies = [
"bitflags 1.2.1",
"cfg-if 1.0.0",
"foreign-types",
"libc",
"once_cell",
"openssl-sys",
]
[[package]]
name = "openssl-probe"
version = "0.1.4"
@ -2324,9 +2368,9 @@ checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a"
[[package]]
name = "openssl-sys"
version = "0.9.63"
version = "0.9.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6b0d6fb7d80f877617dfcb014e605e2b5ab2fb0afdf27935219bb6bd984cb98"
checksum = "7a7907e3bfa08bb85105209cdfcb6c63d109f8f6c1ed6ca318fff5c1853fbc1d"
dependencies = [
"autocfg 1.0.1",
"cc",
@ -3222,6 +3266,29 @@ version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]]
name = "security-framework"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467"
dependencies = [
"bitflags 1.2.1",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e4effb91b4b8b6fb7732e670b6cee160278ff8e6bf485c7805d9e319d76e284"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "semver"
version = "0.9.0"
@ -4379,6 +4446,8 @@ version = "0.1.0"
dependencies = [
"anyhow",
"arrayvec",
"async-native-tls",
"async-tungstenite",
"cargo-bundle",
"crossbeam-channel",
"ctor",

View File

@ -9,6 +9,7 @@ tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "d7277
cocoa = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }
cocoa-foundation = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }
core-foundation = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }
core-foundation-sys = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }
core-graphics = { git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737" }
[profile.dev]

View File

@ -19,6 +19,8 @@ test-support = ["tempdir", "serde_json"]
[dependencies]
anyhow = "1.0.38"
arrayvec = "0.5.2"
async-native-tls = "0.3"
async-tungstenite = "0.14"
crossbeam-channel = "0.5.0"
ctor = "0.1.20"
dirs = "3.0"

View File

@ -1,6 +1,6 @@
use super::util::SurfResultExt as _;
use crate::{language::LanguageRegistry, worktree::Worktree};
use anyhow::{anyhow, Context, Result};
use async_native_tls::TlsConnector;
use gpui::executor::Background;
use gpui::{AsyncAppContext, ModelHandle, Task, WeakModelHandle};
use lazy_static::lazy_static;
@ -13,12 +13,12 @@ use surf::Url;
pub use zed_rpc::{proto, ConnectionId, PeerId, TypedEnvelope};
use zed_rpc::{
proto::{EnvelopedMessage, RequestMessage},
rest, Peer, Receipt,
Peer, Receipt,
};
lazy_static! {
static ref ZED_SERVER_URL: String =
std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev".to_string());
std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev:443".to_string());
}
#[derive(Clone)]
@ -86,42 +86,46 @@ impl Client {
}
let (user_id, access_token) = Self::login(cx.platform(), &cx.background()).await?;
let mut response = surf::get(format!(
"{}{}",
*ZED_SERVER_URL,
&rest::GET_RPC_ADDRESS_PATH
))
.header(
"Authorization",
http_auth_basic::Credentials::new(&user_id, &access_token).as_http_header(),
self.connect(
&ZED_SERVER_URL,
user_id.parse()?,
access_token,
&cx.background(),
)
.await
.context("rpc address request failed")?;
let rest::GetRpcAddressResponse { address } = response
.body_json()
.await
.context("failed to parse rpc address response")?;
self.connect(&address, user_id.parse()?, access_token, &cx.background())
.await
.await?;
Ok(())
}
pub async fn connect(
&self,
address: &str,
server_url: &str,
user_id: i32,
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 let Some(host) = server_url.strip_prefix("https://") {
let stream = smol::net::TcpStream::connect(host).await?;
let stream = TlsConnector::new()
.use_sni(true)
.connect(host, stream)
.await?;
let (stream, _) =
async_tungstenite::client_async(format!("wss://{}/rpc", host), stream).await?;
log::info!("connected to rpc address {}", &*ZED_SERVER_URL);
let (connection_id, handler) = self.peer.add_connection(stream).await;
executor.spawn(handler.run()).detach();
connection_id
} else if let Some(host) = server_url.strip_prefix("http://") {
let stream = smol::net::TcpStream::connect(host).await?;
let (stream, _) =
async_tungstenite::client_async(format!("ws://{}/rpc", host), stream).await?;
log::info!("connected to rpc address {}", &*ZED_SERVER_URL);
let (connection_id, handler) = self.peer.add_connection(stream).await;
executor.spawn(handler.run()).detach();
connection_id
} else {
return Err(anyhow!("invalid server url: {}", server_url))?;
};
let auth_response = self
.peer