rpc/websockets: Port to rustls.

This commit is contained in:
Luther Blissett
2022-09-24 14:29:30 +02:00
parent 76c25f06a1
commit 5f56936668
5 changed files with 29 additions and 41 deletions

2
Cargo.lock generated
View File

@@ -1178,7 +1178,6 @@ dependencies = [
"arrayvec",
"async-channel",
"async-executor",
"async-native-tls",
"async-std",
"async-trait",
"async-tungstenite",
@@ -1213,7 +1212,6 @@ dependencies = [
"libc",
"libsqlite3-sys",
"log",
"native-tls",
"num-bigint",
"num-traits",
"pasta_curves",

View File

@@ -56,10 +56,6 @@ async-trait = {version = "0.1.57", optional = true}
async-channel = {version = "1.7.1", optional = true}
async-executor = {version = "1.4.1", optional = true}
# async-net
async-native-tls = {version = "0.4.0", optional = true}
native-tls = {version = "0.2.10", optional = true}
# Networking
socket2 = {version = "0.4.7", optional = true}
futures-rustls = {version = "0.22.2", features = ["dangerous_configuration"], optional = true}
@@ -150,35 +146,30 @@ async-runtime = [
"smol",
]
async-net = [
"async-native-tls",
"native-tls",
]
websockets = [
"async-tungstenite",
"tungstenite",
"futures-rustls",
]
util = [
"blake3",
"bs58",
"hex",
"bincode",
"serde",
"toml",
"url",
"simplelog",
"serde_json",
"dirs",
"fxhash",
"chrono",
"indicatif",
"termion",
"bs58",
"hex",
"bincode",
"serde",
"toml",
"url",
"simplelog",
"serde_json",
"dirs",
"fxhash",
"chrono",
"indicatif",
"termion",
"async-net",
"async-runtime",
"rpc",
"rpc",
"serial",
]
@@ -198,9 +189,7 @@ rpc = [
"url",
"fast-socks5",
"async-net",
"async-runtime",
"websockets",
"net",
]

View File

@@ -141,10 +141,6 @@ pub enum Error {
#[error("tungstenite error: {0}")]
TungsteniteError(String),
#[cfg(feature = "async-native-tls")]
#[error("async_native_tls error: {0}")]
AsyncNativeTlsError(String),
#[error("Tor error: {0}")]
TorError(String),
@@ -183,6 +179,10 @@ pub enum Error {
#[error(transparent)]
RustlsError(#[from] futures_rustls::rustls::Error),
#[cfg(feature = "futures-rustls")]
#[error("Invalid DNS Name {0}")]
RustlsInvalidDns(String),
// =======================
// Protocol-related errors
// =======================
@@ -457,13 +457,6 @@ impl From<async_channel::RecvError> for Error {
}
}
#[cfg(feature = "async-native-tls")]
impl From<async_native_tls::Error> for Error {
fn from(err: async_native_tls::Error) -> Self {
Self::AsyncNativeTlsError(err.to_string())
}
}
impl From<log::SetLoggerError> for Error {
fn from(err: log::SetLoggerError) -> Self {
Self::SetLoggerError(err.to_string())
@@ -491,6 +484,13 @@ impl From<tungstenite::Error> for Error {
}
}
#[cfg(feature = "futures-rustls")]
impl From<futures_rustls::rustls::client::InvalidDnsNameError> for Error {
fn from(err: futures_rustls::rustls::client::InvalidDnsNameError) -> Self {
Self::RustlsInvalidDns(err.to_string())
}
}
#[cfg(feature = "bincode")]
impl From<bincode::error::DecodeError> for Error {
fn from(err: bincode::error::DecodeError) -> Self {

View File

@@ -7,5 +7,6 @@ pub mod client;
/// Server-side JSON-RPC implementation
pub mod server;
#[cfg(feature = "websockets")]
/// Websockets client
pub mod websockets;

View File

@@ -4,9 +4,9 @@ use std::{
task::{Context, Poll},
};
use async_native_tls::{TlsConnector, TlsStream};
use async_tungstenite::WebSocketStream;
use futures::sink::Sink;
use futures_rustls::{client::TlsStream, rustls::ServerName, TlsConnector};
use smol::{prelude::*, Async};
use tungstenite::{handshake::client::Response, Message};
use url::Url;
@@ -88,7 +88,7 @@ pub async fn connect(addr: &str, tls: TlsConnector) -> DrkResult<(WsStream, Resp
}
"wss" => {
let stream = Async::<TcpStream>::connect(socket_addr).await?;
let stream = tls.connect(host, stream).await?;
let stream = tls.connect(ServerName::try_from(host.as_str())?, stream).await?;
let (stream, resp) = async_tungstenite::client_async(addr, stream).await?;
Ok((WsStream::Tls(stream), resp))
}