diff --git a/src/lol.rs b/src/lol.rs new file mode 100644 index 0000000..32d4a2b --- /dev/null +++ b/src/lol.rs @@ -0,0 +1,151 @@ +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 hyper::rt::Executor 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, +} + +impl IOTypeNotSend { + fn new(stream: TokioIo) -> 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> { + Pin::new(&mut self.stream).poll_write(cx, buf) + } + + fn poll_flush( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + Pin::new(&mut self.stream).poll_flush(cx) + } + + fn poll_shutdown( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> std::task::Poll> { + 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> { + Pin::new(&mut self.stream).poll_read(cx, buf) + } +} + +#[derive(Debug)] +struct Body { + _marker: PhantomData<()>, + data: Option, +} + +impl From 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, Error>>> { + Poll::Ready(self.get_mut().data.take().map(|d| Ok(Frame::data(d)))) + } +} + +// #[tokio::main] +async fn old_main() -> Result<(), Box> { + tracing_subscriber::fmt() + .with_max_level(tracing::Level::TRACE) + .init(); + let target_host = "167.235.156.145:443"; + let domain = "www.google.com"; + + // Look into adding tokiotls there working with setting up a client TLS connection + // + // Connection established with the remote host + // Establish valid Client TLS negotiation process + // Craft the request - HTTP/1.1 or HTTP/2 needs to be looked at + // Send the request. + // + // From the intiial look, it might seem that the needed action here is to do the + // actual TLS negtiation based on the `TcpStream` that was opened. + // Established a valid TLS connection. + // + // Once a TLS connection is estasblished either a HTTP/1.1 request should be sent + // over the line. + // Or establish a HTTP/2 type connection to stream over the TLS connection. + // + 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, io) + .await?; + + tokio::task::spawn(async move { + if let Err(e) = conn.await { + println!("Error: {:?}", e); + } + }); + + let upstream = Request::builder() + .uri("/") + .header("host", domain) + .header("user-agent", "lolza-0.2") + .version(hyper::Version::HTTP_2) + .body(Body::from("".to_string()))?; + + sender. + + debug!("Sending out the request -----"); + println!("{:#?}", upstream); + let res = sender.send_request(upstream).await?; + + debug!("---------- REQUEST SENT"); + + println!("{:#?}", res); + + Ok(()) +}