checkpoint

This commit is contained in:
John Smith 2022-07-11 08:37:08 -04:00
parent 7e0d7dad06
commit b73511142a
12 changed files with 128 additions and 110 deletions

34
Cargo.lock generated
View File

@ -482,9 +482,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bitvec" name = "bitvec"
version = "1.0.0" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1489fcb93a5bb47da0462ca93ad252ad6af2145cce58d10d46a83931ba9f016b" checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [ dependencies = [
"funty", "funty",
"radium", "radium",
@ -705,6 +705,12 @@ dependencies = [
"rustc_version", "rustc_version",
] ]
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.73" version = "1.0.73"
@ -995,12 +1001,12 @@ dependencies = [
[[package]] [[package]]
name = "criterion" name = "criterion"
version = "0.3.5" version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1604dafd25fba2fe2d5895a9da139f8dc9b319a5fe5354ca137cbbce4e178d10" checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f"
dependencies = [ dependencies = [
"atty", "atty",
"cast", "cast 0.3.0",
"clap 2.34.0", "clap 2.34.0",
"criterion-plot", "criterion-plot",
"csv", "csv",
@ -1025,7 +1031,7 @@ version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d00996de9f2f7559f7f4dc286073197f83e92256a59ed395f9aac01fe717da57" checksum = "d00996de9f2f7559f7f4dc286073197f83e92256a59ed395f9aac01fe717da57"
dependencies = [ dependencies = [
"cast", "cast 0.2.7",
"itertools", "itertools",
] ]
@ -3236,9 +3242,9 @@ checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]] [[package]]
name = "plotters" name = "plotters"
version = "0.3.1" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a3fd9ec30b9749ce28cd91f255d569591cdf937fe280c312143e3c4bad6f2a" checksum = "9428003b84df1496fb9d6eeee9c5f8145cb41ca375eb0dad204328888832811f"
dependencies = [ dependencies = [
"num-traits", "num-traits",
"plotters-backend", "plotters-backend",
@ -3249,15 +3255,15 @@ dependencies = [
[[package]] [[package]]
name = "plotters-backend" name = "plotters-backend"
version = "0.3.2" version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d88417318da0eaf0fdcdb51a0ee6c3bed624333bff8f946733049380be67ac1c" checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142"
[[package]] [[package]]
name = "plotters-svg" name = "plotters-svg"
version = "0.3.1" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521fa9638fa597e1dc53e9412a4f9cefb01187ee1f7413076f9e6749e2885ba9" checksum = "e0918736323d1baff32ee0eade54984f6f201ad7e97d5cfb5d6ab4a358529615"
dependencies = [ dependencies = [
"plotters-backend", "plotters-backend",
] ]
@ -4220,9 +4226,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.24.5" version = "0.24.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d80929a3b477bce3a64360ca82bfb361eacce1dcb7b1fb31e8e5e181e37c212" checksum = "0b6e19da72a8d75be4d40e4dd4686afca31507f26c3ffdf6bd3073278d9de0a0"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"core-foundation-sys 0.8.3", "core-foundation-sys 0.8.3",

View File

@ -5,13 +5,13 @@ use crate::veilid_api::*;
use crate::veilid_config::*; use crate::veilid_config::*;
use crate::xx::*; use crate::xx::*;
cfg_if! { // cfg_if! {
if #[cfg(target_arch = "wasm32")] { // if #[cfg(target_arch = "wasm32")] {
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate)>; // pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate)>;
} else { // } else {
pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) + Send + Sync>; pub type UpdateCallback = Arc<dyn Fn(VeilidUpdate) + Send + Sync>;
} // }
} // }
struct ServicesContext { struct ServicesContext {
pub config: VeilidConfig, pub config: VeilidConfig,

View File

@ -135,7 +135,7 @@ impl ProtectedStore {
{ {
Ok(v) => Ok(Some(v)), Ok(v) => Ok(Some(v)),
Err(KeyringError::NoPasswordFound) => Ok(None), Err(KeyringError::NoPasswordFound) => Ok(None),
Err(e) => Err(eyre!("Failed to load user secret")), Err(e) => Err(eyre!("Failed to load user secret: {}", e)),
} }
} }
@ -150,7 +150,7 @@ impl ProtectedStore {
{ {
Ok(_) => Ok(true), Ok(_) => Ok(true),
Err(KeyringError::NoPasswordFound) => Ok(false), Err(KeyringError::NoPasswordFound) => Ok(false),
Err(e) => Err(eyre!("Failed to remove user secret")), Err(e) => Err(eyre!("Failed to remove user secret: {}", e)),
} }
} }

View File

@ -100,12 +100,7 @@ impl TableDB {
return Ok(None); return Ok(None);
} }
}; };
let obj = match serde_cbor::from_slice::<T>(&b) { let obj = serde_cbor::from_slice::<T>(&b).wrap_err("failed to deserialize")?;
Ok(value) => value,
Err(e) => {
bail!("failed to deserialize");
}
};
Ok(Some(obj)) Ok(Some(obj))
} }

View File

@ -9,3 +9,4 @@ pub use block_store::*;
pub use protected_store::*; pub use protected_store::*;
pub use system::*; pub use system::*;
pub use table_store::*; pub use table_store::*;
use utils::*;

View File

@ -1,10 +1,11 @@
use super::utils; use super::*;
use crate::xx::*; use crate::xx::*;
use crate::*; use crate::*;
use data_encoding::BASE64URL_NOPAD; use data_encoding::BASE64URL_NOPAD;
use js_sys::*; use js_sys::*;
use wasm_bindgen_futures::*; use wasm_bindgen_futures::*;
use web_sys::*; use web_sys::*;
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
#[wasm_bindgen(catch, js_name = setPassword, js_namespace = ["global", "wasmhost", "keytar"])] #[wasm_bindgen(catch, js_name = setPassword, js_namespace = ["global", "wasmhost", "keytar"])]
@ -69,9 +70,10 @@ impl ProtectedStore {
} }
pub async fn save_user_secret_string(&self, key: &str, value: &str) -> EyreResult<bool> { pub async fn save_user_secret_string(&self, key: &str, value: &str) -> EyreResult<bool> {
if utils::is_nodejs() { if is_nodejs() {
let prev = match JsFuture::from( let prev = match JsFuture::from(
keytar_getPassword(self.keyring_name().as_str(), key) keytar_getPassword(self.keyring_name().as_str(), key)
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?, .wrap_err("exception thrown")?,
) )
.await .await
@ -82,6 +84,7 @@ impl ProtectedStore {
match JsFuture::from( match JsFuture::from(
keytar_setPassword(self.keyring_name().as_str(), key, value) keytar_setPassword(self.keyring_name().as_str(), key, value)
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?, .wrap_err("exception thrown")?,
) )
.await .await
@ -91,7 +94,7 @@ impl ProtectedStore {
} }
Ok(prev) Ok(prev)
} else if utils::is_browser() { } else if is_browser() {
let win = match window() { let win = match window() {
Some(w) => w, Some(w) => w,
None => { None => {
@ -101,6 +104,7 @@ impl ProtectedStore {
let ls = match win let ls = match win
.local_storage() .local_storage()
.map_err(map_jsvalue_error)
.wrap_err("exception getting local storage")? .wrap_err("exception getting local storage")?
{ {
Some(l) => l, Some(l) => l,
@ -113,6 +117,7 @@ impl ProtectedStore {
let prev = match ls let prev = match ls
.get_item(&vkey) .get_item(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")? .wrap_err("exception_thrown")?
{ {
Some(_) => true, Some(_) => true,
@ -120,6 +125,7 @@ impl ProtectedStore {
}; };
ls.set_item(&vkey, value) ls.set_item(&vkey, value)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")?; .wrap_err("exception_thrown")?;
Ok(prev) Ok(prev)
@ -129,9 +135,10 @@ impl ProtectedStore {
} }
pub async fn load_user_secret_string(&self, key: &str) -> EyreResult<Option<String>> { pub async fn load_user_secret_string(&self, key: &str) -> EyreResult<Option<String>> {
if utils::is_nodejs() { if is_nodejs() {
let prev = match JsFuture::from( let prev = match JsFuture::from(
keytar_getPassword(self.keyring_name().as_str(), key) keytar_getPassword(self.keyring_name().as_str(), key)
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?, .wrap_err("exception thrown")?,
) )
.await .await
@ -145,7 +152,7 @@ impl ProtectedStore {
} }
Ok(prev.as_string()) Ok(prev.as_string())
} else if utils::is_browser() { } else if is_browser() {
let win = match window() { let win = match window() {
Some(w) => w, Some(w) => w,
None => { None => {
@ -155,6 +162,7 @@ impl ProtectedStore {
let ls = match win let ls = match win
.local_storage() .local_storage()
.map_err(map_jsvalue_error)
.wrap_err("exception getting local storage")? .wrap_err("exception getting local storage")?
{ {
Some(l) => l, Some(l) => l,
@ -166,6 +174,7 @@ impl ProtectedStore {
let vkey = self.browser_key_name(key); let vkey = self.browser_key_name(key);
ls.get_item(&vkey) ls.get_item(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown") .wrap_err("exception_thrown")
} else { } else {
unimplemented!(); unimplemented!();
@ -173,16 +182,18 @@ impl ProtectedStore {
} }
pub async fn remove_user_secret_string(&self, key: &str) -> EyreResult<bool> { pub async fn remove_user_secret_string(&self, key: &str) -> EyreResult<bool> {
if utils::is_nodejs() { if is_nodejs() {
match JsFuture::from( match JsFuture::from(
keytar_deletePassword(self.keyring_name().as_str(), key).wrap_err("exception thrown")?, keytar_deletePassword(self.keyring_name().as_str(), key)
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?,
) )
.await .await
{ {
Ok(v) => Ok(v.is_truthy()), Ok(v) => Ok(v.is_truthy()),
Err(_) => bail!("Failed to delete"), Err(_) => bail!("Failed to delete"),
} }
} else if utils::is_browser() { } else if is_browser() {
let win = match window() { let win = match window() {
Some(w) => w, Some(w) => w,
None => { None => {
@ -192,6 +203,7 @@ impl ProtectedStore {
let ls = match win let ls = match win
.local_storage() .local_storage()
.map_err(map_jsvalue_error)
.wrap_err("exception getting local storage")? .wrap_err("exception getting local storage")?
{ {
Some(l) => l, Some(l) => l,
@ -204,10 +216,12 @@ impl ProtectedStore {
match ls match ls
.get_item(&vkey) .get_item(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")? .wrap_err("exception_thrown")?
{ {
Some(_) => { Some(_) => {
ls.delete(&vkey) ls.delete(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")?; .wrap_err("exception_thrown")?;
Ok(true) Ok(true)
} }

View File

@ -2,7 +2,6 @@ use super::utils;
use crate::xx::*; use crate::xx::*;
use crate::*; use crate::*;
use async_executors::{Bindgen, LocalSpawnHandleExt /*, SpawnHandleExt*/}; use async_executors::{Bindgen, LocalSpawnHandleExt /*, SpawnHandleExt*/};
use core::fmt;
use futures_util::future::{select, Either}; use futures_util::future::{select, Either};
use js_sys::*; use js_sys::*;
use wasm_bindgen_futures::*; use wasm_bindgen_futures::*;

View File

@ -28,7 +28,7 @@ impl TableStore {
} }
} }
pub async fn init(&self) -> Result<(), String> { pub async fn init(&self) -> EyreResult<()> {
Ok(()) Ok(())
} }
@ -49,12 +49,12 @@ impl TableStore {
} }
} }
fn get_table_name(&self, table: &str) -> Result<String, String> { fn get_table_name(&self, table: &str) -> EyreResult<String> {
if !table if !table
.chars() .chars()
.all(|c| char::is_alphanumeric(c) || c == '_' || c == '-') .all(|c| char::is_alphanumeric(c) || c == '_' || c == '-')
{ {
return Err(format!("table name '{}' is invalid", table)); bail!("table name '{}' is invalid", table);
} }
let c = self.config.get(); let c = self.config.get();
let namespace = c.namespace.clone(); let namespace = c.namespace.clone();
@ -65,7 +65,7 @@ impl TableStore {
}) })
} }
pub async fn open(&self, name: &str, column_count: u32) -> Result<TableDB, String> { pub async fn open(&self, name: &str, column_count: u32) -> EyreResult<TableDB> {
let table_name = self.get_table_name(name)?; let table_name = self.get_table_name(name)?;
let mut inner = self.inner.lock(); let mut inner = self.inner.lock();
@ -81,7 +81,7 @@ impl TableStore {
} }
let db = Database::open(table_name.clone(), column_count) let db = Database::open(table_name.clone(), column_count)
.await .await
.map_err(|e| format!("failed to open tabledb at: {} ({})", table_name, e))?; .wrap_err("failed to open tabledb")?;
info!("opened table store '{}' with table name '{:?}' with {} columns", name, table_name, column_count); info!("opened table store '{}' with table name '{:?}' with {} columns", name, table_name, column_count);
let table_db = TableDB::new(table_name.clone(), self.clone(), db); let table_db = TableDB::new(table_name.clone(), self.clone(), db);
@ -91,7 +91,7 @@ impl TableStore {
Ok(table_db) Ok(table_db)
} }
pub async fn delete(&self, name: &str) -> Result<bool, String> { pub async fn delete(&self, name: &str) -> EyreResult<bool> {
trace!("TableStore::delete {}", name); trace!("TableStore::delete {}", name);
let table_name = self.get_table_name(name)?; let table_name = self.get_table_name(name)?;
@ -101,11 +101,11 @@ impl TableStore {
"TableStore::delete {}: Not deleting, still open.", "TableStore::delete {}: Not deleting, still open.",
table_name table_name
); );
return Err("Not deleting table that is still opened".to_owned()); bail!("Not deleting table that is still opened");
} }
if utils::is_nodejs() { if utils::is_nodejs() {
Err("unimplemented".to_owned()) unimplemented!();
} else if utils::is_browser() { } else if utils::is_browser() {
let out = match Database::delete(table_name.clone()).await { let out = match Database::delete(table_name.clone()).await {
Ok(_) => true, Ok(_) => true,
@ -115,7 +115,7 @@ impl TableStore {
trace!("TableStore::deleted {}", table_name); trace!("TableStore::deleted {}", table_name);
Ok(out) Ok(out)
} else { } else {
Err("unimplemented".to_owned()) unimplemented!();
} }
} }
} }

View File

@ -77,3 +77,11 @@ pub fn node_require(module: &str) -> JsValue {
} }
} }
} }
#[derive(ThisError, Debug, Clone, Eq, PartialEq)]
#[error("JsValue error")]
pub struct JsValueError(String);
pub fn map_jsvalue_error(x: JsValue) -> JsValueError {
JsValueError(x.as_string().unwrap_or_default())
}

View File

@ -34,14 +34,17 @@ impl Network {
} }
} }
pub fn new(network_manager: NetworkManager) -> Self { pub fn new(
network_manager: NetworkManager,
routing_table: RoutingTable,
connection_manager: ConnectionManager,
) -> Self {
Self { Self {
config: network_manager.config(), config: network_manager.config(),
inner: Arc::new(Mutex::new(Self::new_inner(network_manager))), inner: Arc::new(Mutex::new(Self::new_inner(network_manager))),
} }
} }
fn network_manager(&self) -> NetworkManager { fn network_manager(&self) -> NetworkManager {
self.inner.lock().network_manager.clone() self.inner.lock().network_manager.clone()
} }
@ -61,14 +64,15 @@ impl Network {
let res = match dial_info.protocol_type() { let res = match dial_info.protocol_type() {
ProtocolType::UDP => { ProtocolType::UDP => {
return Err("no support for UDP protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for UDP protocol")
} }
ProtocolType::TCP => { ProtocolType::TCP => {
return Err("no support for TCP protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for TCP protocol")
} }
ProtocolType::WS | ProtocolType::WSS => { ProtocolType::WS | ProtocolType::WSS => {
WebsocketProtocolHandler::send_unbound_message(dial_info.clone(), data) WebsocketProtocolHandler::send_unbound_message(dial_info.clone(), data)
.await .await
.wrap_err("failed to send unbound message")
} }
}; };
if res.is_ok() { if res.is_ok() {
@ -94,10 +98,10 @@ impl Network {
let data_len = data.len(); let data_len = data.len();
let out = match dial_info.protocol_type() { let out = match dial_info.protocol_type() {
ProtocolType::UDP => { ProtocolType::UDP => {
return Err("no support for UDP protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for UDP protocol")
} }
ProtocolType::TCP => { ProtocolType::TCP => {
return Err("no support for TCP protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for TCP protocol")
} }
ProtocolType::WS | ProtocolType::WSS => { ProtocolType::WS | ProtocolType::WSS => {
WebsocketProtocolHandler::send_recv_unbound_message( WebsocketProtocolHandler::send_recv_unbound_message(
@ -128,10 +132,10 @@ impl Network {
let data_len = data.len(); let data_len = data.len();
match descriptor.protocol_type() { match descriptor.protocol_type() {
ProtocolType::UDP => { ProtocolType::UDP => {
return Err("no support for udp protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for UDP protocol")
} }
ProtocolType::TCP => { ProtocolType::TCP => {
return Err("no support for tcp protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for TCP protocol")
} }
_ => {} _ => {}
} }
@ -164,10 +168,10 @@ impl Network {
) -> EyreResult<()> { ) -> EyreResult<()> {
let data_len = data.len(); let data_len = data.len();
if dial_info.protocol_type() == ProtocolType::UDP { if dial_info.protocol_type() == ProtocolType::UDP {
return Err("no support for UDP protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for UDP protocol");
} }
if dial_info.protocol_type() == ProtocolType::TCP { if dial_info.protocol_type() == ProtocolType::TCP {
return Err("no support for TCP protocol".to_owned()).map_err(logthru_net!(error)) bail!("no support for TCP protocol");
} }
// Handle connection-oriented protocols // Handle connection-oriented protocols
@ -176,7 +180,7 @@ impl Network {
.get_or_create_connection(None, dial_info.clone()) .get_or_create_connection(None, dial_info.clone())
.await?; .await?;
let res = conn.send_async(data).await.map_err(logthru_net!(error)); let res = conn.send_async(data).await;
if res.is_ok() { if res.is_ok() {
// Network accounting // Network accounting
self.network_manager() self.network_manager()
@ -248,7 +252,6 @@ impl Network {
Ok(false) Ok(false)
} }
////////////////////////////////////////// //////////////////////////////////////////
pub fn get_network_class(&self) -> Option<NetworkClass> { pub fn get_network_class(&self) -> Option<NetworkClass> {
// xxx eventually detect tor browser? // xxx eventually detect tor browser?

View File

@ -3,6 +3,7 @@ pub mod ws;
use super::*; use super::*;
use crate::xx::*; use crate::xx::*;
use std::io;
#[derive(Debug)] #[derive(Debug)]
pub enum ProtocolNetworkConnection { pub enum ProtocolNetworkConnection {

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
use ws_stream_wasm::*; use futures_util::{SinkExt, StreamExt};
use futures_util::{StreamExt, SinkExt};
use std::io; use std::io;
use ws_stream_wasm::*;
struct WebsocketNetworkConnectionInner { struct WebsocketNetworkConnectionInner {
ws_meta: WsMeta, ws_meta: WsMeta,
@ -9,9 +9,7 @@ struct WebsocketNetworkConnectionInner {
} }
fn to_io(err: WsErr) -> io::Error { fn to_io(err: WsErr) -> io::Error {
let kind = match err { io::Error::new(io::ErrorKind::Other, err.to_string())
WsErr::InvalidWsState {supplied:_} => io::ErrorKind::
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -27,9 +25,7 @@ impl fmt::Debug for WebsocketNetworkConnection {
} }
impl WebsocketNetworkConnection { impl WebsocketNetworkConnection {
pub fn new( pub fn new(descriptor: ConnectionDescriptor, ws_meta: WsMeta, ws_stream: WsStream) -> Self {
descriptor: ConnectionDescriptor,
ws_meta: WsMeta, ws_stream: WsStream) -> Self {
Self { Self {
descriptor, descriptor,
inner: Arc::new(WebsocketNetworkConnectionInner { inner: Arc::new(WebsocketNetworkConnectionInner {
@ -53,28 +49,29 @@ impl WebsocketNetworkConnection {
if message.len() > MAX_MESSAGE_SIZE { if message.len() > MAX_MESSAGE_SIZE {
bail_io_error_other!("sending too large WS message"); bail_io_error_other!("sending too large WS message");
} }
self.inner.ws_stream.clone() self.inner
.send(WsMessage::Binary(message)).await .ws_stream
.map_err(|_| "failed to send to websocket".to_owned()) .clone()
.map_err(logthru_net!(error)) .send(WsMessage::Binary(message))
.await
.map_err(to_io)
} }
#[instrument(level = "trace", err, skip(self), fields(ret.len))] #[instrument(level = "trace", err, skip(self), fields(ret.len))]
pub async fn recv(&self) -> Result<Vec<u8>, String> { pub async fn recv(&self) -> io::Result<Vec<u8>> {
let out = match self.inner.ws_stream.clone().next().await { let out = match self.inner.ws_stream.clone().next().await {
Some(WsMessage::Binary(v)) => v, Some(WsMessage::Binary(v)) => v,
Some(x) => { Some(x) => {
return Err(format!("Unexpected WS message type: {:?}", x)); bail_io_error_other!("Unexpected WS message type");
} }
None => { None => {
return Err("WS stream closed".to_owned()).map_err(logthru_net!(error)); bail_io_error_other!("WS stream closed");
} }
}; };
if out.len() > MAX_MESSAGE_SIZE { if out.len() > MAX_MESSAGE_SIZE {
Err("sending too large WS message".to_owned()).map_err(logthru_net!(error)) bail_io_error_other!("sending too large WS message")
} else {
Ok(out)
} }
Ok(out)
} }
} }
@ -88,8 +85,7 @@ impl WebsocketProtocolHandler {
pub async fn connect( pub async fn connect(
local_address: Option<SocketAddr>, local_address: Option<SocketAddr>,
dial_info: DialInfo, dial_info: DialInfo,
) -> Result<ProtocolNetworkConnection, String> { ) -> io::Result<ProtocolNetworkConnection> {
assert!(local_address.is_none()); assert!(local_address.is_none());
// Split dial info up // Split dial info up
@ -99,32 +95,31 @@ impl WebsocketProtocolHandler {
_ => panic!("invalid dialinfo for WS/WSS protocol"), _ => panic!("invalid dialinfo for WS/WSS protocol"),
}; };
let request = dial_info.request().unwrap(); let request = dial_info.request().unwrap();
let split_url = SplitUrl::from_str(&request)?; let split_url = SplitUrl::from_str(&request).map_err(to_io_error_other)?;
if split_url.scheme != scheme { if split_url.scheme != scheme {
return Err("invalid websocket url scheme".to_string()); bail_io_error_other!("invalid websocket url scheme");
} }
let (wsmeta, wsio) = WsMeta::connect(request, None) let (wsmeta, wsio) = WsMeta::connect(request, None).await.map_err(to_io)?;
.await
.map_err(map_to_string)
.map_err(logthru_net!(error))?;
// Make our connection descriptor // Make our connection descriptor
Ok(ProtocolNetworkConnection::Ws(WebsocketNetworkConnection::new(ConnectionDescriptor::new_no_local( Ok(ProtocolNetworkConnection::Ws(
dial_info.to_peer_address(), WebsocketNetworkConnection::new(
), wsmeta, wsio))) ConnectionDescriptor::new_no_local(dial_info.to_peer_address()),
wsmeta,
wsio,
),
))
} }
#[instrument(level = "trace", err, skip(data), fields(data.len = data.len()))] #[instrument(level = "trace", err, skip(data), fields(data.len = data.len()))]
pub async fn send_unbound_message(dial_info: DialInfo, data: Vec<u8>) -> Result<(), String> { pub async fn send_unbound_message(dial_info: DialInfo, data: Vec<u8>) -> io::Result<()> {
if data.len() > MAX_MESSAGE_SIZE { if data.len() > MAX_MESSAGE_SIZE {
return Err("sending too large unbound WS message".to_owned()); bail_io_error_other!("sending too large unbound WS message");
} }
// Make the real connection // Make the real connection
let conn = Self::connect(None, dial_info) let conn = Self::connect(None, dial_info).await?;
.await
.map_err(|e| format!("failed to connect websocket for unbound message: {}", e))?;
conn.send(data).await conn.send(data).await
} }
@ -134,23 +129,19 @@ impl WebsocketProtocolHandler {
dial_info: DialInfo, dial_info: DialInfo,
data: Vec<u8>, data: Vec<u8>,
timeout_ms: u32, timeout_ms: u32,
) -> Result<Vec<u8>, String> { ) -> io::Result<Vec<u8>> {
if data.len() > MAX_MESSAGE_SIZE { if data.len() > MAX_MESSAGE_SIZE {
return Err("sending too large unbound WS message".to_owned()); bail_io_error_other!("sending too large unbound WS message");
} }
let conn = Self::connect(None, dial_info.clone()) let conn = Self::connect(None, dial_info.clone()).await?;
.await
.map_err(|e| format!("failed to connect websocket for unbound message: {}", e))?;
conn.send(data).await?; conn.send(data).await?;
let out = timeout(timeout_ms, conn.recv()) let out = timeout(timeout_ms, conn.recv())
.await .await
.map_err(map_to_string)??; .map_err(|e| e.to_io())??;
tracing::Span::current().record("ret.len", &out.len()); tracing::Span::current().record("ret.len", &out.len());
Ok(out) Ok(out)
} }
} }