fix tls close_notify error, empty data packet spam, compilation errors

This commit is contained in:
Toshit Chawda 2024-08-24 22:29:16 -07:00
parent 6f0e1e7feb
commit 028c0c1332
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
4 changed files with 111 additions and 23 deletions

View file

@ -1,11 +1,13 @@
use std::{
io::ErrorKind,
pin::Pin,
task::{Context, Poll},
};
use async_trait::async_trait;
use bytes::{buf::UninitSlice, BufMut, Bytes, BytesMut};
use futures_util::{ready, AsyncRead, Future, Stream, StreamExt, TryStreamExt};
use futures_rustls::TlsStream;
use futures_util::{ready, AsyncRead, AsyncWrite, Future, Stream, StreamExt, TryStreamExt};
use http::{HeaderValue, Uri};
use hyper::{body::Body, rt::Executor};
use js_sys::{Array, ArrayBuffer, JsString, Object, Uint8Array};
@ -20,7 +22,7 @@ use wisp_mux::{
WispError,
};
use crate::EpoxyError;
use crate::{stream_provider::ProviderUnencryptedAsyncRW, EpoxyError};
#[wasm_bindgen]
extern "C" {
@ -230,6 +232,83 @@ impl WebSocketWrite for WispTransportWrite {
}
}
fn map_close_notify(x: std::io::Result<usize>) -> std::io::Result<usize> {
match x {
Ok(x) => Ok(x),
Err(x) => {
// hacky way to find if it's actually a rustls close notify error
if x.kind() == ErrorKind::UnexpectedEof
&& format!("{:?}", x).contains("TLS close_notify")
{
Ok(0)
} else {
Err(x)
}
}
}
}
pin_project! {
pub struct IgnoreCloseNotify {
#[pin]
pub inner: TlsStream<ProviderUnencryptedAsyncRW>,
}
}
impl AsyncRead for IgnoreCloseNotify {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
self.project()
.inner
.poll_read(cx, buf)
.map(map_close_notify)
}
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [std::io::IoSliceMut<'_>],
) -> Poll<std::io::Result<usize>> {
self.project()
.inner
.poll_read_vectored(cx, bufs)
.map(map_close_notify)
}
}
impl AsyncWrite for IgnoreCloseNotify {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
self.project()
.inner
.poll_write(cx, buf)
}
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[std::io::IoSlice<'_>],
) -> Poll<std::io::Result<usize>> {
self.project()
.inner
.poll_write_vectored(cx, bufs)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
self.project().inner.poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
self.project().inner.poll_close(cx)
}
}
pub fn is_redirect(code: u16) -> bool {
[301, 302, 303, 307, 308].contains(&code)
}