From dc9e45cefcd778025dda8193635436c48d4eb104 Mon Sep 17 00:00:00 2001 From: alterwain Date: Sun, 9 Mar 2025 23:35:30 +0300 Subject: [PATCH] modified: Cargo.lock modified: Cargo.toml modified: src/tor.rs --- Cargo.lock | 29 ++++++++++++++++------- Cargo.toml | 7 +++--- src/tor.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 125abea..47535cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,12 +12,14 @@ dependencies = [ "env_logger", "futures", "futures-util", + "httparse", "hyper", "hyper-util", "log", "pin-project-lite", "rusqlite", "serde", + "serde_json", "sha3", "tokio", "tor-cell", @@ -372,13 +374,14 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "axum" -version = "0.7.9" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" +checksum = "6d6fd624c75e18b3b4c6b9caf42b1afe24437daaee904069137d8bab077be8b8" dependencies = [ - "async-trait", "axum-core", + "axum-macros", "bytes", + "form_urlencoded", "futures-util", "http", "http-body", @@ -406,11 +409,10 @@ dependencies = [ [[package]] name = "axum-core" -version = "0.4.5" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" +checksum = "df1362f362fd16024ae199c1970ce98f9661bf5ef94b9808fee734bc3698b733" dependencies = [ - "async-trait", "bytes", "futures-util", "http", @@ -425,6 +427,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "axum-macros" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604fde5e028fea851ce1d8570bbdc034bec850d157f7569d10f347d06808c05c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.99", +] + [[package]] name = "backtrace" version = "0.3.74" @@ -2098,9 +2111,9 @@ dependencies = [ [[package]] name = "matchit" -version = "0.7.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" [[package]] name = "memchr" diff --git a/Cargo.toml b/Cargo.toml index 594ab02..8988bfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,16 +10,17 @@ env_logger = "0.11.6" log = "0.4.20" sha3 = "0.10.8" serde = { version = "1", features = ["derive"] } +serde_json = "1.0.140" bincode = "1.3.3" tor-cell = "0.28.0" tor-hsservice = "0.28.0" tor-proto = { version = "0.28.0", features = ["hs-service", "tokio"] } -arti-client = { version = "0.28.0", features = ["tokio", "onion-service-service"]} +arti-client = { version = "0.28.0", features = ["tokio", "onion-service-service", "onion-service-client"]} futures = "0.3" -axum = "0.7.5" +axum = { version = "0.8.1", features = ["macros"] } futures-util = "0.3.30" hyper = "1.4.1" hyper-util = { version = "0.1.6", features = ["service"] } pin-project-lite = "0.2.14" tower-service = "0.3.2" -hypertor = "0.1" \ No newline at end of file +httparse = "1.10.1" \ No newline at end of file diff --git a/src/tor.rs b/src/tor.rs index 5696151..2a73bde 100644 --- a/src/tor.rs +++ b/src/tor.rs @@ -1,15 +1,31 @@ -use std::{error::Error, net::TcpListener}; +use std::{error::Error, net::TcpListener, time::Duration}; -use arti_client::{TorClient, TorClientConfig}; -use futures::{Stream, StreamExt}; -use log::info; -use tokio::io::split; +use arti_client::{config::BoolOrAuto, StreamPrefs, TorClient, TorClientConfig}; +use futures::{AsyncReadExt, Stream, StreamExt}; +use log::{error, info}; use tor_hsservice::config::OnionServiceConfigBuilder; use tor_hsservice::handle_rend_requests; -use axum::{routing, Router}; +use axum::{ + http::StatusCode, + Json, Router, + debug_handler +}; +use axum::routing; +use serde::{Deserialize, Serialize}; +use futures::io::AsyncWriteExt; use crate::tor_axum; +#[debug_handler] +async fn test_connection_to_tor(Json(payload): Json) -> (StatusCode, Json) { + info!("Got interesting payload! {}", payload.msg); + (StatusCode::OK, Json(SimpleMessage{ msg: "World is hell indeed!".to_string() })) +} + +#[derive(Serialize, Deserialize)] +struct SimpleMessage { + msg: String +} pub async fn start() -> Result<(), Box> { let tor_client = TorClient::create_bootstrapped(TorClientConfig::default()).await?; @@ -22,10 +38,49 @@ pub async fn start() -> Result<(), Box> { let stream_requests = handle_rend_requests(rend_requests); - let app = Router::new().route("/", routing::get(|| async { "Hello, World!" })); + let app = Router::new().route("/", routing::post(test_connection_to_tor)); println!("serving at: http://{}", onion_service.onion_name().unwrap()); + let onion_name = onion_service.onion_name().unwrap(); + + tokio::spawn(async move { + tokio::time::sleep(Duration::from_secs(40)).await; + + let mut stream_prefs = StreamPrefs::new(); + stream_prefs.connect_to_onion_services(BoolOrAuto::Explicit(true)); + + match tor_client.connect_with_prefs((onion_name.to_string(), 80), &stream_prefs).await { + Ok(mut stream) => { + + let host = onion_name.to_string(); + let path = "/"; + + // Construct the HTTP GET request manually + let request = format!( + "POST {} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n{}", + path, host, serde_json::to_string(&SimpleMessage { msg: "is world hell?".to_string()}).unwrap() + ); + + if let Ok(a) = stream + .write_all(request.as_bytes()) + .await { + let _ = stream.flush().await; + let mut buf = [0; 4096]; + if let Ok(n) = stream.read(&mut buf).await { + info!("Got response!!! {}", String::from_utf8(buf[..n].to_vec()).unwrap()); + } else { + error!("No response!!!"); + } + // Flushing the stream is important; see below! + } else { + error!("GO NO MONEY"); + } + } + Err(err) => error!("Error: {}", err) + } + }); + tor_axum::serve(stream_requests, app).await; Ok(())