Skip to content

Commit

Permalink
Added proxy to maker socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Jan 8, 2024
1 parent c41ea99 commit 1eef248
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub mod scripts;
pub mod taker;
#[cfg(feature = "integration-test")]
pub mod test_framework;
pub mod tor;
pub mod utill;
pub mod wallet;
pub mod tor;
// Disable watchtower for now. Handle contract watching
// individually for maker and Taker.
//pub mod watchtower;
24 changes: 22 additions & 2 deletions src/maker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ use bitcoin::Network;
use bitcoind::bitcoincore_rpc::RpcApi;
use tokio::{
io::{AsyncBufReadExt, BufReader},
net::TcpListener,
net::{TcpListener, TcpStream},
select,
sync::mpsc,
time::sleep,
};
use tokio_socks::tcp::Socks5Stream;

pub use api::{Maker, MakerBehavior};

Expand All @@ -48,6 +49,9 @@ pub async fn start_maker_server(maker: Arc<Maker>) -> Result<(), MakerError> {
maker.wallet.write()?.refresh_offer_maxsize_cache()?;

let network = maker.get_wallet().read()?.store.network;
log::info!("Network: {:?}", network);

let onion_addr = maker.config.onion_addrs.clone();

if maker.wallet.read()?.store.network != Network::Regtest {
if maker.config.onion_addrs == "myhiddenserviceaddress.onion:6102" {
Expand Down Expand Up @@ -95,7 +99,23 @@ pub async fn start_maker_server(maker: Arc<Maker>) -> Result<(), MakerError> {
break Ok(());
}
let (mut socket, addr) = select! {
new_client = listener.accept() => new_client?,

new_client = listener.accept() => {
if onion_addr == "myhiddenserviceaddress.onion".to_string() {
new_client?

Check warning on line 105 in src/maker/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/mod.rs#L105

Added line #L105 was not covered by tests
} else {
let (_,addr) = new_client?;

Check warning on line 107 in src/maker/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/mod.rs#L107

Added line #L107 was not covered by tests
let proxy_address = "socks5://127.0.0.1:9050";
let proxy_stream = TcpStream::connect(proxy_address).await?;
match Socks5Stream::connect_with_socket(proxy_stream,addr).await {
Ok(proxy_socket) => (proxy_socket.into_inner(),addr),
Err(e)=>{
println!("{:?}",e);
panic!("Something went wrong with proxy stuff")
}

Check warning on line 115 in src/maker/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/mod.rs#L109-L115

Added lines #L109 - L115 were not covered by tests
}
}
},
client_err = server_loop_comms_rx.recv() => {
//unwrap the option here because we'll never close the mscp so it will always work
match client_err.as_ref().unwrap() {
Expand Down
83 changes: 43 additions & 40 deletions src/tor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use libtor::{HiddenServiceVersion,Tor,TorAddress,TorFlag};

use libtor::{HiddenServiceVersion, Tor, TorAddress, TorFlag};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
const SOCKS_PORT: u16 = 19_050;
const CHECK_URL: &str = "https://check.torproject.org";

Expand All @@ -16,47 +19,47 @@ fn tor_instance() {
.start()
{
Ok(r) => println!("tor exit result: {}", r),
Err(e) => eprintln!("tor error: {}", e),
Err(e) => println!("tor error: {}", e),
};
}

Check warning on line 24 in src/tor.rs

View check run for this annotation

Codecov / codecov/patch

src/tor.rs#L21-L24

Added lines #L21 - L24 were not covered by tests

#[cfg(test)]
mod test {
use std::{thread, time::Duration};
// #[cfg(test)]
// mod test {
// use std::{thread, time::Duration};

// use tokio::task;
// use crate::tor::*;

// #[tokio::test]
// async fn test_instance() {
// task::spawn_blocking(|| tor_instance());

use tokio::task;
use crate::tor::*;
// let proxy = reqwest::Proxy::all(format!("socks5://127.0.0.1:{}", SOCKS_PORT)).expect("Where is proxy");
// let client = reqwest::Client::builder().proxy(proxy).build().expect("Client should we build");

#[tokio::test]
async fn test_instance() {
task::spawn_blocking(|| tor_instance());

let proxy = reqwest::Proxy::all(format!("socks5://127.0.0.1:{}", SOCKS_PORT)).expect("Where is proxy");
let client = reqwest::Client::builder().proxy(proxy).build().expect("Client should we build");
// thread::sleep(Duration::from_millis(2500));
// println!("---------------------------------request--------------------------------------------");
// let response = client
// .get(CHECK_URL.to_string())
// .send()
// .await;
// match response {
// Ok(r) => {
// let html = r.text().await.unwrap();
// println!("{:?}",html);
// match html.find("Congratulations") {
// Some(_) => {
// println!("Tor is online!");
// }
// None => {
// println!("Received a response but not through Tor...");
// }
// }
// }
// Err(e) => {
// println!("error: {}", e.to_string());
// }
// };

thread::sleep(Duration::from_millis(2500));
println!("---------------------------------request--------------------------------------------");
let response = client
.get(CHECK_URL.to_string())
.send()
.await;
match response {
Ok(r) => {
let html = r.text().await.unwrap();
println!("{:?}",html);
match html.find("Congratulations") {
Some(_) => {
println!("Tor is online!");
}
None => {
println!("Received a response but not through Tor...");
}
}
}
Err(e) => {
println!("error: {}", e.to_string());
}
};

}
}
// }
// }
2 changes: 1 addition & 1 deletion src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn setup_logger() {
.default_filter_or("coinswap=info")
.default_write_style_or("always"),
)
.is_test(true)
// .is_test(true)
.init();
});
}
Expand Down

0 comments on commit 1eef248

Please sign in to comment.