Skip to content

Commit

Permalink
move DNS tor directory inside its data directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
KnowWhoami committed Jan 16, 2025
1 parent 58a3446 commit 6ee6dd6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 56 deletions.
2 changes: 1 addition & 1 deletion maker.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 7 additions & 14 deletions src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
fs::File,
io::{ErrorKind, Read},
io::ErrorKind,
net::{TcpListener, TcpStream},
sync::{atomic::Ordering::Relaxed, Arc},
thread::sleep,
Expand All @@ -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;
Expand Down Expand Up @@ -118,17 +117,11 @@ fn handle_request(maker: &Arc<Maker>, 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"))?;

Check warning on line 120 in src/maker/rpc/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/rpc/server.rs#L120

Added line #L120 was not covered by tests

let address = format!("{}:{}", hostname, maker.config.network_port);

Check warning on line 122 in src/maker/rpc/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/rpc/server.rs#L122

Added line #L122 was not covered by tests

RpcMsgResp::GetTorAddressResp(address)

Check warning on line 124 in src/maker/rpc/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/rpc/server.rs#L124

Added line #L124 was not covered by tests
}
}
RpcMsgReq::Stop => {
Expand Down
18 changes: 9 additions & 9 deletions src/maker/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::{
io::ErrorKind,
net::{Ipv4Addr, TcpListener, TcpStream},
path::PathBuf,
path::Path,
process::Child,
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -114,18 +114,18 @@ fn network_bootstrap(maker: Arc<Maker>) -> Result<Option<Child>, 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);

Check warning on line 118 in src/maker/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/server.rs#L117-L118

Added lines #L117 - L118 were not covered by tests

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)

Check warning on line 123 in src/maker/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/server.rs#L121-L123

Added lines #L121 - L123 were not covered by tests
} else {
maker.config.directory_server_address.clone()
};

(maker_address, directory_onion_address, tor_handle)
(maker_address, dns_address, tor_handle)

Check warning on line 128 in src/maker/server.rs

View check run for this annotation

Codecov / codecov/patch

src/maker/server.rs#L128

Added line #L128 was not covered by tests
}
};

Expand Down
22 changes: 11 additions & 11 deletions src/market/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {

Check warning on line 374 in src/market/directory.rs

View check run for this annotation

Codecov / codecov/patch

src/market/directory.rs#L371-L374

Added lines #L371 - L374 were not covered by tests
Ok(_) => log::info!("Previous tor log file deleted successfully"),
Err(_) => log::error!("Error deleting tor log file"),
}
Expand All @@ -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(),

Check warning on line 385 in src/market/directory.rs

View check run for this annotation

Codecov / codecov/patch

src/market/directory.rs#L385

Added line #L385 was not covered by tests
)?);

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) =

Check warning on line 390 in src/market/directory.rs

View check run for this annotation

Codecov / codecov/patch

src/market/directory.rs#L390

Added line #L390 was not covered by tests
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)?;

Check warning on line 398 in src/market/directory.rs

View check run for this annotation

Codecov / codecov/patch

src/market/directory.rs#L398

Added line #L398 was not covered by tests

log::info!("DNS is listening at {}:{}", onion_addr, network_port);
log::info!("DNS is listening at {}:{}", hostname, network_port);

Check warning on line 400 in src/market/directory.rs

View check run for this annotation

Codecov / codecov/patch

src/market/directory.rs#L400

Added line #L400 was not covered by tests
}
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/taker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");

Check warning on line 2038 in src/taker/api.rs

View check run for this annotation

Codecov / codecov/patch

src/taker/api.rs#L2038

Added line #L2038 was not covered by tests

let hostname = get_tor_hostname(tor_dir)?;
log::info!("---------------hostname : {:?}", hostname);
format!("{}:{}", hostname, 8080)

Check warning on line 2042 in src/taker/api.rs

View check run for this annotation

Codecov / codecov/patch

src/taker/api.rs#L2040-L2042

Added lines #L2040 - L2042 were not covered by tests
} else {
self.config.directory_server_address.clone()
}
Expand Down
8 changes: 4 additions & 4 deletions src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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<String> {
let hostname_file_path = tor_dir.join("hs-dir").join("hostname");
let mut hostname_file = File::open(hostname_file_path)?;

Check warning on line 105 in src/utill.rs

View check run for this annotation

Codecov / codecov/patch

src/utill.rs#L103-L105

Added lines #L103 - L105 were not covered by tests
let mut tor_addrs: String = String::new();
hostname_file.read_to_string(&mut tor_addrs)?;
tor_addrs.pop(); // Remove `\n` at the end.
Expand Down
10 changes: 5 additions & 5 deletions taker.toml
Original file line number Diff line number Diff line change
@@ -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,
rpc_port= 8081

0 comments on commit 6ee6dd6

Please sign in to comment.