Add TLS support to WS connection

This commit is contained in:
bitfl0wer 2023-04-30 11:56:14 +02:00
parent 9f76679a35
commit 8e6daeff84
1 changed files with 19 additions and 18 deletions

View File

@ -1,26 +1,25 @@
use std::sync::Arc; use std::sync::{mpsc, Arc};
use std::sync::Mutex; use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
use crate::api::types::*; use crate::api::types::*;
use crate::api::WebSocketEvent; use crate::api::WebSocketEvent;
use crate::errors::ObserverError; use crate::errors::ObserverError;
use crate::gateway::events::Events; use crate::gateway::events::Events;
use crate::URLBundle; use crate::URLBundle;
use futures_util::SinkExt;
use futures_util::stream::{FilterMap, SplitSink, SplitStream};
use futures_util::StreamExt; use futures_util::StreamExt;
use native_tls::TlsConnector;
use reqwest::Url; use reqwest::Url;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use serde_json::from_str;
use serde_json::to_string;
use tokio::io; use tokio::io;
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio_tungstenite::connect_async; use tokio::sync::Mutex;
use tokio_tungstenite::tungstenite::error::UrlError; use tokio_tungstenite::tungstenite::error::UrlError;
use tokio_tungstenite::MaybeTlsStream;
use tokio_tungstenite::WebSocketStream; use tokio_tungstenite::WebSocketStream;
use tokio_tungstenite::{connect_async, connect_async_tls_with_config};
use tokio_tungstenite::{Connector, MaybeTlsStream};
/** /**
Represents a Gateway connection. A Gateway connection will create observable Represents a Gateway connection. A Gateway connection will create observable
@ -31,13 +30,6 @@ pub struct Gateway<'a> {
pub url: String, pub url: String,
pub token: String, pub token: String,
pub events: Events<'a>, pub events: Events<'a>,
socket: WebSocketStream<MaybeTlsStream<TcpStream>>,
}
#[derive(Deserialize, Serialize, Debug)]
enum GatewayIntervalEvent {
Heartbeat(Option<u64>),
Hello(u64),
} }
impl<'a> Gateway<'a> { impl<'a> Gateway<'a> {
@ -51,7 +43,15 @@ impl<'a> Gateway<'a> {
UrlError::UnsupportedUrlScheme, UrlError::UnsupportedUrlScheme,
)); ));
} }
let (ws_stream, _) = match connect_async(websocket_url.clone()).await { let (ws_stream, _) = match connect_async_tls_with_config(
websocket_url.clone(),
None,
Some(Connector::NativeTls(
TlsConnector::builder().build().unwrap(),
)),
)
.await
{
Ok(ws_stream) => ws_stream, Ok(ws_stream) => ws_stream,
Err(_) => { Err(_) => {
return Err(tokio_tungstenite::tungstenite::Error::Io( return Err(tokio_tungstenite::tungstenite::Error::Io(
@ -60,11 +60,12 @@ impl<'a> Gateway<'a> {
} }
}; };
let (mut write, read) = ws_stream.split();
return Ok(Gateway { return Ok(Gateway {
url: websocket_url, url: websocket_url,
token, token,
events: Events::default(), events: Events::default(),
socket: ws_stream,
}); });
} }
} }