epoxy rename

This commit is contained in:
CoolElectronics 2024-01-14 22:50:17 -05:00
parent a175b666a5
commit fc17bacb9d
No known key found for this signature in database
GPG key ID: F63593D168636C50

View file

@ -35,25 +35,25 @@ use web_sys::TextEncoder;
type HttpBody = http_body_util::Full<Bytes>; type HttpBody = http_body_util::Full<Bytes>;
#[derive(Debug)] #[derive(Debug)]
enum WsTcpResponse { enum EpxResponse {
Success(Response<Incoming>), Success(Response<Incoming>),
Redirect((Response<Incoming>, http::Request<HttpBody>, Uri)), Redirect((Response<Incoming>, http::Request<HttpBody>, Uri)),
} }
enum WsTcpCompression { enum EpxCompression {
Brotli, Brotli,
Gzip, Gzip,
} }
type WsTcpTlsStream = TlsStream<MuxStream<WsStreamWrapper>>; type EpxTlsStream = TlsStream<MuxStream<WsStreamWrapper>>;
type WsTcpUnencryptedStream = MuxStream<WsStreamWrapper>; type EpxUnencryptedStream = MuxStream<WsStreamWrapper>;
type WsTcpStream = Either<WsTcpTlsStream, WsTcpUnencryptedStream>; type EpxStream = Either<WsTcpTlsStream, WsTcpUnencryptedStream>;
async fn send_req( async fn send_req(
req: http::Request<HttpBody>, req: http::Request<HttpBody>,
should_redirect: bool, should_redirect: bool,
io: WsTcpStream, io: EpxStream,
) -> Result<WsTcpResponse, JsError> { ) -> Result<EpxResponse, JsError> {
let (mut req_sender, conn) = Builder::new() let (mut req_sender, conn) = Builder::new()
.title_case_headers(true) .title_case_headers(true)
.preserve_header_case(true) .preserve_header_case(true)
@ -101,9 +101,9 @@ async fn send_req(
"Host", "Host",
HeaderValue::from_str(redirect_url_authority.as_str())?, HeaderValue::from_str(redirect_url_authority.as_str())?,
); );
Ok(WsTcpResponse::Redirect((res, new_req, new_url))) Ok(EpxResponse::Redirect((res, new_req, new_url)))
} else { } else {
Ok(WsTcpResponse::Success(res)) Ok(EpxResponse::Success(res))
} }
} }
Err(err) => Err(err), Err(err) => Err(err),
@ -121,10 +121,10 @@ pub struct WsWebSocket {
onclose: Function, onclose: Function,
onerror: Function, onerror: Function,
onmessage: Function, onmessage: Function,
ws: Option<WebSocket<WsTcpStream>>, ws: Option<WebSocket<EpxStream>>,
} }
async fn wtf(iop: *mut WsTcpStream) { async fn wtf(iop: *mut EpxStream) {
let mut t = false; let mut t = false;
unsafe { unsafe {
let io = &mut *iop; let io = &mut *iop;
@ -172,7 +172,7 @@ impl WsWebSocket {
#[wasm_bindgen] #[wasm_bindgen]
pub async fn connect( pub async fn connect(
&mut self, &mut self,
tcp: &mut WsTcp, tcp: &mut EpoxyClient,
url: String, url: String,
protocols: Vec<String>, protocols: Vec<String>,
origin: String, origin: String,
@ -202,7 +202,7 @@ impl WsWebSocket {
.await; .await;
io.write(b"\r\n").await; io.write(b"\r\n").await;
let iop: *mut WsTcpStream = &mut io; let iop: *mut EpxStream = &mut io;
wtf(iop).await; wtf(iop).await;
let mut ws = WebSocket::after_handshake(io, fastwebsockets::Role::Client); let mut ws = WebSocket::after_handshake(io, fastwebsockets::Role::Client);
@ -281,7 +281,7 @@ pub async fn send(pointer: *mut WsWebSocket, payload: String) -> Result<(), JsEr
} }
#[wasm_bindgen] #[wasm_bindgen]
pub struct WsTcp { pub struct EpoxyClient {
rustls_config: Arc<rustls::ClientConfig>, rustls_config: Arc<rustls::ClientConfig>,
mux: Multiplexor<WsStreamWrapper>, mux: Multiplexor<WsStreamWrapper>,
useragent: String, useragent: String,
@ -289,13 +289,13 @@ pub struct WsTcp {
} }
#[wasm_bindgen] #[wasm_bindgen]
impl WsTcp { impl EpoxyClient {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub async fn new( pub async fn new(
ws_url: String, ws_url: String,
useragent: String, useragent: String,
redirect_limit: usize, redirect_limit: usize,
) -> Result<WsTcp, JsError> { ) -> Result<EpoxyClient, JsError> {
let ws_uri = ws_url let ws_uri = ws_url
.parse::<uri::Uri>() .parse::<uri::Uri>()
.replace_err("Failed to parse websocket url")?; .replace_err("Failed to parse websocket url")?;
@ -323,7 +323,7 @@ impl WsTcp {
.with_no_client_auth(), .with_no_client_auth(),
); );
Ok(WsTcp { Ok(EpoxyClient {
mux, mux,
rustls_config, rustls_config,
useragent, useragent,
@ -331,11 +331,11 @@ impl WsTcp {
}) })
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn ptr(&mut self) -> *mut WsTcp { pub fn ptr(&mut self) -> *mut EpoxyClient {
self as *mut Self self as *mut Self
} }
async fn get_http_io(&self, url: &Uri) -> Result<WsTcpStream, JsError> { async fn get_http_io(&self, url: &Uri) -> Result<EpxStream, JsError> {
let url_host = url.host().replace_err("URL must have a host")?; let url_host = url.host().replace_err("URL must have a host")?;
let url_port = utils::get_url_port(url)?; let url_port = utils::get_url_port(url)?;
let channel = self let channel = self
@ -356,9 +356,9 @@ impl WsTcp {
) )
.await .await
.replace_err("Failed to perform TLS handshake")?; .replace_err("Failed to perform TLS handshake")?;
Ok(WsTcpStream::Left(io)) Ok(EpxStream::Left(io))
} else { } else {
Ok(WsTcpStream::Right(channel)) Ok(EpxStream::Right(channel))
} }
} }
@ -369,12 +369,12 @@ impl WsTcp {
) -> Result<(hyper::Response<Incoming>, Uri, bool), JsError> { ) -> Result<(hyper::Response<Incoming>, Uri, bool), JsError> {
let mut redirected = false; let mut redirected = false;
let uri = req.uri().clone(); let uri = req.uri().clone();
let mut current_resp: WsTcpResponse = let mut current_resp: EpxResponse =
send_req(req, should_redirect, self.get_http_io(&uri).await?).await?; send_req(req, should_redirect, self.get_http_io(&uri).await?).await?;
for _ in 0..self.redirect_limit - 1 { for _ in 0..self.redirect_limit - 1 {
match current_resp { match current_resp {
WsTcpResponse::Success(_) => break, EpxResponse::Success(_) => break,
WsTcpResponse::Redirect((_, req, new_url)) => { EpxResponse::Redirect((_, req, new_url)) => {
redirected = true; redirected = true;
current_resp = current_resp =
send_req(req, should_redirect, self.get_http_io(&new_url).await?).await? send_req(req, should_redirect, self.get_http_io(&new_url).await?).await?
@ -383,8 +383,8 @@ impl WsTcp {
} }
match current_resp { match current_resp {
WsTcpResponse::Success(resp) => Ok((resp, uri, redirected)), EpxResponse::Success(resp) => Ok((resp, uri, redirected)),
WsTcpResponse::Redirect((resp, _, new_url)) => Ok((resp, new_url, redirected)), EpxResponse::Redirect((resp, _, new_url)) => Ok((resp, new_url, redirected)),
} }
} }
@ -500,18 +500,18 @@ impl WsTcp {
.and_then(|val| val.to_str().ok()) .and_then(|val| val.to_str().ok())
.unwrap_or_default() .unwrap_or_default()
{ {
"gzip" => Some(WsTcpCompression::Gzip), "gzip" => Some(EpxCompression::Gzip),
"br" => Some(WsTcpCompression::Brotli), "br" => Some(EpxCompression::Brotli),
_ => None, _ => None,
}; };
let incoming_body = IncomingBody::new(resp.into_body()); let incoming_body = IncomingBody::new(resp.into_body());
let decompressed_body = match compression { let decompressed_body = match compression {
Some(alg) => match alg { Some(alg) => match alg {
WsTcpCompression::Gzip => Either::Left(Either::Left(ReaderStream::new( EpxCompression::Gzip => Either::Left(Either::Left(ReaderStream::new(
async_comp::GzipDecoder::new(StreamReader::new(incoming_body)), async_comp::GzipDecoder::new(StreamReader::new(incoming_body)),
))), ))),
WsTcpCompression::Brotli => Either::Left(Either::Right(ReaderStream::new( EpxCompression::Brotli => Either::Left(Either::Right(ReaderStream::new(
async_comp::BrotliDecoder::new(StreamReader::new(incoming_body)), async_comp::BrotliDecoder::new(StreamReader::new(incoming_body)),
))), ))),
}, },