This commit is contained in:
Antun Krasic
2024-04-10 21:59:04 +01:00
commit 2af2ad132a
4 changed files with 905 additions and 0 deletions

132
src/main.rs Normal file
View File

@@ -0,0 +1,132 @@
use hyper::body::{Body as HttpBody, Bytes, Frame};
use hyper::{Error, Request, Response};
use hyper_util::rt::TokioIo;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::net::TcpStream;
use tracing::{debug, Value};
#[derive(Clone, Copy, Debug)]
struct LocalExec;
impl<F> hyper::rt::Executor<F> for LocalExec
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
fn execute(&self, fut: F) {
tokio::task::spawn(fut);
}
}
struct IOTypeNotSend {
_marker: PhantomData<()>,
stream: hyper_util::rt::TokioIo<tokio::net::TcpStream>,
}
impl IOTypeNotSend {
fn new(stream: TokioIo<TcpStream>) -> Self {
Self {
_marker: PhantomData,
stream,
}
}
}
impl hyper::rt::Write for IOTypeNotSend {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>> {
Pin::new(&mut self.stream).poll_write(cx, buf)
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
Pin::new(&mut self.stream).poll_flush(cx)
}
fn poll_shutdown(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> std::task::Poll<Result<(), std::io::Error>> {
Pin::new(&mut self.stream).poll_shutdown(cx)
}
}
impl hyper::rt::Read for IOTypeNotSend {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: hyper::rt::ReadBufCursor<'_>,
) -> std::task::Poll<Result<(), std::io::Error>> {
Pin::new(&mut self.stream).poll_read(cx, buf)
}
}
#[derive(Debug)]
struct Body {
_marker: PhantomData<()>,
data: Option<Bytes>,
}
impl From<String> for Body {
fn from(a: String) -> Self {
Body {
_marker: PhantomData,
data: Some(a.into()),
}
}
}
impl HttpBody for Body {
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Error>>> {
Poll::Ready(self.get_mut().data.take().map(|d| Ok(Frame::data(d))))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
let target_host = "167.235.156.145:443";
let tcp_stream = tokio::net::TcpStream::connect(target_host).await?;
let io = IOTypeNotSend::new(hyper_util::rt::TokioIo::new(tcp_stream));
let (mut sender, conn) =
hyper::client::conn::http2::handshake::<LocalExec, IOTypeNotSend, Body>(LocalExec, io)
.await?;
tokio::task::spawn(async move {
if let Err(e) = conn.await {
println!("Error: {:?}", e);
}
});
let upstream = Request::builder()
.uri("/")
.header("host", "g.s0c.in")
// .version(hyper::Version::HTTP_2)
.body(Body::from("".to_string()))?;
debug!("Sending out the request -----");
println!("{:#?}", upstream);
let res = sender.send_request(upstream).await?;
debug!("---------- REQUEST SENT");
println!("{:#?}", res);
Ok(())
}