From 6b39cbfd4f3c7e6b066ac15796ad1f525dad293b Mon Sep 17 00:00:00 2001 From: vanten-s Date: Mon, 20 Nov 2023 08:48:45 +0100 Subject: [PATCH] It doesn't work --- .gitignore | 2 + Cargo.toml | 12 ++++++ src/http_lib.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 26 +++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/http_lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..daeb379 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "tor-hs-clearweb-mirror" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +arti-client = "0.11.0" +eyre = "0.6.8" +tokio = { version = "1.34.0", features = ["full"] } +tor-rtcompat = "0.9.5" diff --git a/src/http_lib.rs b/src/http_lib.rs new file mode 100644 index 0000000..0abfc60 --- /dev/null +++ b/src/http_lib.rs @@ -0,0 +1,99 @@ +use arti_client::TorClient; +use eyre::Result; +use tokio::io::{AsyncWriteExt, AsyncReadExt}; + +trait Serialize { + fn serialize(&self) -> Vec; +} + +#[derive(Clone, Debug)] +pub struct HTTPRequest { + pub method: HTTPMethod, + pub location: String, + pub version: String, + pub headers: Vec, + pub body: Option>, +} + +#[derive(Clone, Copy, Debug)] +pub enum HTTPMethod { + Get, +} + +#[derive(Clone, Debug)] +pub struct HTTPHeader { + pub key: String, + pub value: String, +} + +impl Serialize for HTTPRequest { + fn serialize(&self) -> Vec { + let mut result = vec![]; + + let mut method_vec_u8 = self.method.serialize(); + let mut location_vec_u8 = self.location.as_bytes().to_vec(); + let mut version_vec_u8 = self.version.as_bytes().to_vec(); + let mut headers = self + .headers + .iter() + .map(|x| x.serialize()) + .collect::>() + .concat(); + + result.append(&mut method_vec_u8); + result.push(b' '); + result.append(&mut location_vec_u8); + result.push(b' '); + result.append(&mut version_vec_u8); + result.append(&mut headers); + result.push(b'\n'); + result.push(b'\n'); + + if let Some(body) = &self.body { + let mut body = body.clone(); + result.append(&mut body) + } + + let _ = dbg!(String::from_utf8(result.clone())); + result + } +} + +impl Serialize for HTTPMethod { + fn serialize(&self) -> Vec { + use HTTPMethod as M; + match self { + M::Get => b"GET" + }.to_vec() + } +} + +impl Serialize for HTTPHeader { + fn serialize(&self) -> Vec { + format!("\n{}: {}", self.key, self.value).into_bytes() + } +} + +pub async fn get_website( + address: &str, + port: u16, + request: HTTPRequest, + tor_client: TorClient, +) -> Result> { + let mut client_stream = tor_client.connect((address, port)).await?; + client_stream.write_all(&request.serialize()).await?; + println!("Sent request for {} to {address}", request.location); + let mut response_status: Vec = Vec::new(); + + loop { + let response = client_stream.read_u8().await?; + response_status.push(dbg!(response)); + if response == b'\n' { + break; + } + } + + let _ = dbg!(String::from_utf8(response_status)); + + todo!(); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..49702cd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,26 @@ +use arti_client::{TorClient, TorClientConfig}; +use eyre::Result; + +mod http_lib; +use http_lib::*; + +#[tokio::main] +async fn main() -> Result<()> { + let config = TorClientConfig::default(); + let tor_client = TorClient::create_bootstrapped(config).await?; + println!("Tor Client Created!"); + let test_request = HTTPRequest { + method: HTTPMethod::Get, + location: "/".to_string(), + version: "HTTP/1.1".to_string(), + headers: vec![HTTPHeader { + key: "Host".to_string(), + value: "example.com".to_string(), + }], + body: None, + }; + + dbg!(get_website("example.com", 80, test_request, tor_client).await?); + + Ok(()) +}