diff --git a/maker.toml b/maker.toml index b3505a7c..a67b079e 100644 --- a/maker.toml +++ b/maker.toml @@ -7,7 +7,7 @@ min_swap_amount = 100000 # Socks port socks_port = 19050 # Directory server address -directory_server_address = 127.0.0.1:8080 +directory_server_address = bhbzkndgad52ojm75w4goii7xsi6ou73fzyvorxas7swg2snlto4c4ad.onion:8080 # Fidelity Bond amount fidelity_amount = 5000000 # Fidelity Bond timelock in Block heights diff --git a/src/maker/rpc/server.rs b/src/maker/rpc/server.rs index 07420780..5161fd80 100644 --- a/src/maker/rpc/server.rs +++ b/src/maker/rpc/server.rs @@ -1,6 +1,5 @@ use std::{ - fs::File, - io::{ErrorKind, Read}, + io::ErrorKind, net::{TcpListener, TcpStream}, sync::{atomic::Ordering::Relaxed, Arc}, thread::sleep, @@ -12,7 +11,7 @@ use bitcoin::{Address, Amount}; use super::messages::RpcMsgReq; use crate::{ maker::{error::MakerError, rpc::messages::RpcMsgResp, Maker}, - utill::{read_message, send_message, ConnectionType, HEART_BEAT_INTERVAL}, + utill::{get_tor_hostname, read_message, send_message, ConnectionType, HEART_BEAT_INTERVAL}, wallet::{Destination, SendAmount}, }; use std::str::FromStr; @@ -118,17 +117,11 @@ fn handle_request(maker: &Arc, socket: &mut TcpStream) -> Result<(), Make if maker.config.connection_type == ConnectionType::CLEARNET { RpcMsgResp::GetTorAddressResp("Maker is not running on TOR".to_string()) } else { - let maker_hs_path_str = format!( - "/tmp/tor-rust-maker{}/hs-dir/hostname", - maker.config.network_port - ); - let mut maker_file = File::open(maker_hs_path_str)?; - let mut maker_onion_addr: String = String::new(); - maker_file.read_to_string(&mut maker_onion_addr)?; - maker_onion_addr.pop(); // Remove `\n` at the end. - let maker_address = format!("{}:{}", maker_onion_addr, maker.config.network_port); - - RpcMsgResp::GetTorAddressResp(maker_address) + let hostname = get_tor_hostname(&maker.data_dir.join("tor"))?; + + let address = format!("{}:{}", hostname, maker.config.network_port); + + RpcMsgResp::GetTorAddressResp(address) } } RpcMsgReq::Stop => { diff --git a/src/maker/server.rs b/src/maker/server.rs index 7871feb7..a5e27028 100644 --- a/src/maker/server.rs +++ b/src/maker/server.rs @@ -7,7 +7,7 @@ use std::{ io::ErrorKind, net::{Ipv4Addr, TcpListener, TcpStream}, - path::PathBuf, + path::Path, process::Child, sync::{ atomic::{AtomicBool, Ordering::Relaxed}, @@ -36,7 +36,7 @@ use crate::{ rpc::start_rpc_server, }, protocol::messages::{DnsMetadata, DnsRequest, TakerToMakerMessage}, - utill::{get_tor_addrs, read_message, send_message, ConnectionType, HEART_BEAT_INTERVAL}, + utill::{get_tor_hostname, read_message, send_message, ConnectionType, HEART_BEAT_INTERVAL}, wallet::WalletError, }; @@ -114,18 +114,18 @@ fn network_bootstrap(maker: Arc) -> Result, MakerError> { log::info!("[{}] tor setup complete!", maker_port); - let maker_onion_addr = get_tor_addrs(&tor_dir)?; - let maker_address = format!("{}:{}", maker_onion_addr, maker.config.network_port); + let maker_hostname = get_tor_hostname(&tor_dir)?; + let maker_address = format!("{}:{}", maker_hostname, maker.config.network_port); - let directory_onion_address = if cfg!(feature = "integration-test") { - let directory_onion_addr = - get_tor_addrs(&PathBuf::from("/tmp/tor-rust-directory"))?; - format!("{}:{}", directory_onion_addr, 8080) + let dns_address = if cfg!(feature = "integration-test") { + let dns_tor_dir = Path::new("/tmp/coinswap/dns/tor"); + let dns_hostname = get_tor_hostname(dns_tor_dir)?; + format!("{}:{}", dns_hostname, 8080) } else { maker.config.directory_server_address.clone() }; - (maker_address, directory_onion_address, tor_handle) + (maker_address, dns_address, tor_handle) } }; diff --git a/src/market/directory.rs b/src/market/directory.rs index 1cf1fc37..45b2eb8e 100644 --- a/src/market/directory.rs +++ b/src/market/directory.rs @@ -17,7 +17,7 @@ use crate::{ }; #[cfg(feature = "tor")] -use crate::utill::{get_tor_addrs, monitor_log_for_completion}; +use crate::utill::{get_tor_hostname, monitor_log_for_completion}; use std::{ collections::HashMap, @@ -368,9 +368,10 @@ pub fn start_directory_server( ConnectionType::TOR => { #[cfg(feature = "tor")] { - let tor_log_dir = "/tmp/tor-rust-directory/log"; - if Path::new(tor_log_dir).exists() { - match fs::remove_file(tor_log_dir) { + let tor_dir = directory.data_dir.join("tor"); + let log_file = tor_dir.join("log"); + if log_file.exists() { + match fs::remove_file(&log_file) { Ok(_) => log::info!("Previous tor log file deleted successfully"), Err(_) => log::error!("Error deleting tor log file"), } @@ -381,23 +382,22 @@ pub fn start_directory_server( tor_handle = Some(crate::tor::spawn_tor( socks_port, network_port, - "/tmp/tor-rust-directory".to_string(), + tor_dir.to_str().unwrap().to_string(), )?); log::info!("waiting for tor setup completion."); - if let Err(e) = monitor_log_for_completion( - &PathBuf::from(tor_log_dir), - "Bootstrapped 100% (done): Done", - ) { + if let Err(e) = + monitor_log_for_completion(&log_file, "Bootstrapped 100% (done): Done") + { log::error!("Error monitoring tor log file: {}", e); } log::info!("tor is ready!!"); - let onion_addr = get_tor_addrs(&PathBuf::from("/tmp/tor-rust-directory"))?; + let hostname = get_tor_hostname(&tor_dir)?; - log::info!("DNS is listening at {}:{}", onion_addr, network_port); + log::info!("DNS is listening at {}:{}", hostname, network_port); } } } diff --git a/src/taker/api.rs b/src/taker/api.rs index cd787d30..2ca50a78 100644 --- a/src/taker/api.rs +++ b/src/taker/api.rs @@ -12,15 +12,12 @@ use std::{ collections::{HashMap, HashSet}, io::BufWriter, net::TcpStream, - path::PathBuf, + path::{Path, PathBuf}, process::Child, thread::sleep, time::{Duration, Instant}, }; -#[cfg(feature = "tor")] -use std::io::Read; - use bitcoind::bitcoincore_rpc::RpcApi; #[cfg(feature = "tor")] @@ -325,7 +322,7 @@ impl Taker { if tor_log_file.exists() { if let Err(e) = std::fs::remove_file(&tor_log_file) { log::error!( - "Error removing previous tor log. Please delet the file and restart. | {:?}", + "Error removing previous tor log. Please delete the file and restart. | {:?}", tor_log_file ); return Err(e.into()); @@ -2038,13 +2035,11 @@ impl Taker { #[cfg(feature = "tor")] ConnectionType::TOR => { if cfg!(feature = "integration-test") { - let directory_hs_path_str = - "/tmp/tor-rust-directory/hs-dir/hostname".to_string(); - let mut directory_file = std::fs::File::open(directory_hs_path_str)?; - let mut directory_onion_addr = String::new(); - directory_file.read_to_string(&mut directory_onion_addr)?; - directory_onion_addr.pop(); - format!("{}:{}", directory_onion_addr, 8080) + let tor_dir = Path::new("/tmp/coinswap/dns/tor"); + + let hostname = get_tor_hostname(tor_dir)?; + log::info!("---------------hostname : {:?}", hostname); + format!("{}:{}", hostname, 8080) } else { self.config.directory_server_address.clone() } diff --git a/src/utill.rs b/src/utill.rs index f4a32fbb..599d816f 100644 --- a/src/utill.rs +++ b/src/utill.rs @@ -99,10 +99,10 @@ impl fmt::Display for ConnectionType { } } -/// Read the tor address given an hidden_service directory path -pub(crate) fn get_tor_addrs(hs_dir: &Path) -> io::Result { - let hostname_file_path = hs_dir.join("hs-dir").join("hostname"); - let mut hostname_file = File::open(hostname_file_path).unwrap(); +/// Read the tor address given a tor directory path +pub(crate) fn get_tor_hostname(tor_dir: &Path) -> io::Result { + let hostname_file_path = tor_dir.join("hs-dir").join("hostname"); + let mut hostname_file = File::open(hostname_file_path)?; let mut tor_addrs: String = String::new(); hostname_file.read_to_string(&mut tor_addrs)?; tor_addrs.pop(); // Remove `\n` at the end. diff --git a/taker.toml b/taker.toml index af140627..95d497dc 100644 --- a/taker.toml +++ b/taker.toml @@ -1,10 +1,10 @@ # Network listening port -port= 8000, +port= 8000 #Socks port -socks_port= 19070, +socks_port= 19070 # Directory server address -directory_server_address=directoryhiddenserviceaddress.onion:8080 , +directory_server_address=bhbzkndgad52ojm75w4goii7xsi6ou73fzyvorxas7swg2snlto4c4ad.onion:8080 # Connection type -connection_type= TOR, +connection_type= TOR # RPC port -rpc_port= 8081, \ No newline at end of file +rpc_port= 8081 \ No newline at end of file