It doesn't work

This commit is contained in:
vanten-s 2023-11-20 08:48:45 +01:00
commit 6b39cbfd4f
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
4 changed files with 139 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

12
Cargo.toml Normal file
View file

@ -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"

99
src/http_lib.rs Normal file
View file

@ -0,0 +1,99 @@
use arti_client::TorClient;
use eyre::Result;
use tokio::io::{AsyncWriteExt, AsyncReadExt};
trait Serialize {
fn serialize(&self) -> Vec<u8>;
}
#[derive(Clone, Debug)]
pub struct HTTPRequest {
pub method: HTTPMethod,
pub location: String,
pub version: String,
pub headers: Vec<HTTPHeader>,
pub body: Option<Vec<u8>>,
}
#[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<u8> {
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::<Vec<_>>()
.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<u8> {
use HTTPMethod as M;
match self {
M::Get => b"GET"
}.to_vec()
}
}
impl Serialize for HTTPHeader {
fn serialize(&self) -> Vec<u8> {
format!("\n{}: {}", self.key, self.value).into_bytes()
}
}
pub async fn get_website(
address: &str,
port: u16,
request: HTTPRequest,
tor_client: TorClient<tor_rtcompat::PreferredRuntime>,
) -> Result<Vec<u8>> {
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<u8> = 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!();
}

26
src/main.rs Normal file
View file

@ -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(())
}