Compare commits

..

1 Commits

Author SHA1 Message Date
Antun Krasic
c580239c33 Use the proper exector 2024-04-11 14:21:04 +01:00

View File

@@ -1,11 +1,14 @@
use hyper::body::Bytes; use core::panic;
use http_body_util::{combinators::BoxBody, BodyExt, Empty}; use http_body_util::{combinators::BoxBody, BodyExt, Empty};
use hyper::body::Bytes;
use hyper::Request; use hyper::Request;
use hyper_util::rt::TokioIo; use hyper_util::rt::TokioIo;
use tokio::net::TcpStream;
use log::debug; use log::debug;
use std::{net::ToSocketAddrs, sync::Arc};
use tokio::net::TcpStream;
use tokio_rustls::{rustls, TlsConnector}; use tokio_rustls::{rustls, TlsConnector};
use std::sync::Arc;
// empty
pub fn empty_body() -> BoxBody<Bytes, hyper::Error> { pub fn empty_body() -> BoxBody<Bytes, hyper::Error> {
Empty::<Bytes>::new() Empty::<Bytes>::new()
@@ -20,8 +23,8 @@ pub fn empty_body() -> BoxBody<Bytes, hyper::Error> {
struct MyExecutor; struct MyExecutor;
impl<F> hyper::rt::Executor<F> for MyExecutor impl<F> hyper::rt::Executor<F> for MyExecutor
where where
F: std::future::Future + Send + 'static, F: std::future::Future + Send + 'static,
F::Output: Send + 'static, F::Output: Send + 'static,
{ {
fn execute(&self, future: F) { fn execute(&self, future: F) {
@@ -31,24 +34,31 @@ F: std::future::Future + Send + 'static,
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set log level to TRACE to see detailed information
tracing_subscriber::fmt() tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE) .with_max_level(tracing::Level::INFO)
.init(); .init();
let target_host = "167.235.156.145:443";
let domain = "g.s0c.in";
let domain = "www.google.com";
let uri = format!("https://{}/", domain);
let target_host = match format!("{}:443", domain).to_socket_addrs() {
Ok(socket_ip) => socket_ip.into_iter().next().unwrap(),
Err(e) => {
panic!("DNS resolution error: {}", e);
}
};
let mut root_cert_store = rustls::RootCertStore::empty(); let mut root_cert_store = rustls::RootCertStore::empty();
root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let mut config = rustls::ClientConfig::builder().with_root_certificates(root_cert_store).with_no_client_auth(); let mut config = rustls::ClientConfig::builder()
.with_root_certificates(root_cert_store)
.with_no_client_auth();
// Available protocols: http/1.0, http/1.1, h2 // Available protocols: http/1.0, http/1.1, h2
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec(), b"http/1.0".to_vec()]; config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec(), b"http/1.0".to_vec()];
let connector = TlsConnector::from(Arc::new(config)); let connector = TlsConnector::from(Arc::new(config));
// Look into adding tokiotls there working with setting up a client TLS connection // Look into adding tokiotls there working with setting up a client TLS connection
// //
// Connection established with the remote host // Connection established with the remote host
@@ -65,16 +75,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Or establish a HTTP/2 type connection to stream over the TLS connection. // Or establish a HTTP/2 type connection to stream over the TLS connection.
// //
let tcp_stream = TcpStream::connect(target_host).await?; let tcp_stream = TcpStream::connect(target_host).await?;
let tls_domain = rustls_pki_types::ServerName::try_from(domain).map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid dnsname"))?.to_owned(); let tls_domain = rustls_pki_types::ServerName::try_from(domain)
.map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid dnsname"))?
.to_owned();
// let io = IOTypeNotSend::new(hyper_util::rt::TokioIo::new(tcp_stream)); // let io = IOTypeNotSend::new(hyper_util::rt::TokioIo::new(tcp_stream));
let stream = connector.connect(tls_domain, tcp_stream).await?; let stream = connector.connect(tls_domain, tcp_stream).await?;
let io = TokioIo::new(stream); let io = TokioIo::new(stream);
let executor = hyper_util::rt::tokio::TokioExecutor::new();
// let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await.unwrap(); // let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await.unwrap();
let (mut sender, conn) = hyper::client::conn::http2::handshake(MyExecutor, io).await?; let (mut sender, conn) = hyper::client::conn::http2::handshake(executor, io).await?;
tokio::task::spawn(async move { tokio::task::spawn(async move {
if let Err(e) = conn.await { if let Err(e) = conn.await {
@@ -83,18 +95,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}); });
let upstream_request = Request::builder() let upstream_request = Request::builder()
.uri("https://g.s0c.in/") .uri(uri)
// .header("host", domain)
.header("user-agent", "lolza-0.2") .header("user-agent", "lolza-0.2")
.version(hyper::Version::HTTP_2) .version(hyper::Version::HTTP_2)
.body(empty_body())?; .body(Empty::<Bytes>::new())?;
debug!("Request: {:#?}", upstream_request); debug!("Request: {:#?}", upstream_request);
let res = sender.send_request(upstream_request).await?; let res = sender.send_request(upstream_request).await?;
debug!("Response: {:#?}", res); debug!("Response: {:#?}", res);
let body = res.collect().await?.to_bytes();
println!("{}", String::from_utf8_lossy(&body));
Ok(()) Ok(())
} }