From 78ff6d0c61e017cdf6b14900ed6e2e88b88ccf3d Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 25 Dec 2021 10:58:43 -0500 Subject: [PATCH] tests pass --- veilid-core/src/intf/wasm/network/mod.rs | 2 +- .../src/intf/wasm/network/protocol/ws.rs | 28 ++++++++++++------- veilid-core/src/routing_table/node_ref.rs | 6 ++-- veilid-core/src/veilid_api/mod.rs | 8 ++++++ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/veilid-core/src/intf/wasm/network/mod.rs b/veilid-core/src/intf/wasm/network/mod.rs index 157a1be6..a9b55578 100644 --- a/veilid-core/src/intf/wasm/network/mod.rs +++ b/veilid-core/src/intf/wasm/network/mod.rs @@ -121,7 +121,7 @@ impl Network { } pub async fn send_data(&self, node_ref: NodeRef, data: Vec) -> Result<(), String> { - let dial_info = node_ref.dial_info(); + let dial_info = node_ref.best_dial_info(); let descriptor = node_ref.last_connection(); // First try to send data to the last socket we've seen this peer on diff --git a/veilid-core/src/intf/wasm/network/protocol/ws.rs b/veilid-core/src/intf/wasm/network/protocol/ws.rs index 7499e960..5be89f4b 100644 --- a/veilid-core/src/intf/wasm/network/protocol/ws.rs +++ b/veilid-core/src/intf/wasm/network/protocol/ws.rs @@ -91,21 +91,29 @@ impl WebsocketProtocolHandler { network_manager: NetworkManager, dial_info: &DialInfo, ) -> Result { - let url = dial_info.to_url_string(None); - let (tls, host, port, protocol_type) = match dial_info { - DialInfo::WS(ws) => (false, ws.host.clone(), ws.port, ProtocolType::WS), - DialInfo::WSS(wss) => (true, wss.host.clone(), wss.port, ProtocolType::WSS), + let url = dial_info + .request() + .ok_or_else(|| format!("missing url in websocket dialinfo: {:?}", dial_info))?; + let split_url = SplitUrl::from_str(&url)?; + let tls = match dial_info { + DialInfo::WS(ws) => { + if split_url.scheme.to_ascii_lowercase() != "ws" { + return Err(format!("wrong scheme for WS websocket url: {}", url)); + } + false + } + DialInfo::WSS(wss) => { + if split_url.scheme.to_ascii_lowercase() != "wss" { + return Err(format!("wrong scheme for WSS websocket url: {}", url)); + } + true + } _ => { return Err("wrong protocol for WebsocketProtocolHandler".to_owned()) .map_err(logthru_net!(error)) } }; - - let peer_addr = PeerAddress::new( - Address::from_str(&host).map_err(logthru_net!(error))?, - port, - protocol_type, - ); + let peer_addr = dial_info.to_peer_address(); let (ws, wsio) = WsMeta::connect(url, None) .await diff --git a/veilid-core/src/routing_table/node_ref.rs b/veilid-core/src/routing_table/node_ref.rs index 70cb61d3..47ad5f17 100644 --- a/veilid-core/src/routing_table/node_ref.rs +++ b/veilid-core/src/routing_table/node_ref.rs @@ -35,9 +35,9 @@ impl NodeRef { self.node_id } - pub fn dial_info_filter(&self) -> DialInfoFilter { - self.dial_info_filter.clone() - } + // pub fn dial_info_filter(&self) -> DialInfoFilter { + // self.dial_info_filter.clone() + // } pub fn operate(&self, f: F) -> T where diff --git a/veilid-core/src/veilid_api/mod.rs b/veilid-core/src/veilid_api/mod.rs index 31dca076..e09767b9 100644 --- a/veilid-core/src/veilid_api/mod.rs +++ b/veilid-core/src/veilid_api/mod.rs @@ -585,6 +585,14 @@ impl DialInfo { Self::WSS(di) => di.socket_address.to_socket_addr(), } } + pub fn to_peer_address(&self) -> PeerAddress { + match self { + Self::UDP(di) => PeerAddress::new(di.socket_address, ProtocolType::UDP), + Self::TCP(di) => PeerAddress::new(di.socket_address, ProtocolType::TCP), + Self::WS(di) => PeerAddress::new(di.socket_address, ProtocolType::WS), + Self::WSS(di) => PeerAddress::new(di.socket_address, ProtocolType::WSS), + } + } pub fn request(&self) -> Option { match self { Self::UDP(_) => None,