Skip to content

Commit

Permalink
Tor hidden service support via libtor
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Sep 30, 2020
1 parent 14608fd commit f512201
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ electrum = []
http = [ "warp", "tokio" ]
webhooks = [ "reqwest" ]
track-spends = []
tor = [ "libtor" ]

[dependencies]
bitcoin = "0.23.0"
Expand All @@ -38,6 +39,7 @@ thiserror = "1.0.20"
tokio = { version = "0.2.20", features = ["macros"], optional = true }
warp = { version = "0.2.3", optional = true }
reqwest = { version = "0.10.6", optional = true, features = ["json", "blocking"] }
libtor = { version = "43.6.0", optional = true }


# Statically link OpenSSL when cross-compiling to ARM
Expand Down
5 changes: 5 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ impl App {
#[cfg(feature = "webhooks")]
let webhook = config.webhook_urls.clone().map(WebHookNotifier::start);

#[cfg(feature = "tor")]
if config.onion {
crate::tor::start_onion(&config);
}

Ok(App {
config,
indexer,
Expand Down
24 changes: 24 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ pub struct Config {
display_order(102)
)]
pub webhook_urls: Option<Vec<String>>,

#[cfg(feature = "tor")]
#[structopt(
long,
short = "o",
help = "Setup tor hidden service (v3)",
display_order(110)
)]
pub onion: bool,

#[cfg(feature = "tor")]
#[structopt(
long,
help = "Tor data directory [default: ~/.bwt/tor]",
display_order(110)
)]
pub tor_dir: Option<path::PathBuf>,
}

impl Config {
Expand Down Expand Up @@ -298,6 +315,13 @@ impl Config {
})
}

#[cfg(feature = "tor")]
pub fn tor_dir(&self) -> Option<path::PathBuf> {
self.tor_dir
.clone()
.or_else(|| Some(dirs::home_dir()?.join(".bwt").join("tor")))
}

pub fn setup_logger(&self) {
apply_log_env(if self.timestamp {
pretty_env_logger::formatted_timed_builder()
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub mod http;
#[cfg(feature = "webhooks")]
pub mod webhooks;

#[cfg(feature = "tor")]
pub mod tor;

pub use app::App;
pub use config::Config;
pub use error::{Error, Result};
Expand Down
46 changes: 46 additions & 0 deletions src/tor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::net;

use libtor::{HiddenServiceVersion, Tor, TorAddress, TorFlag};

use crate::error::OptionExt;
use crate::{Config, Result};

pub fn start_onion(config: &Config) -> Result<()> {
let mut tor = Tor::new();

let datadir = match config.tor_dir() {
Some(x) => x,
None => {
warn!("Cannot determine tor data dir location, provide a path via --tor-dir");
return Ok(());
}
};
let hsdir = datadir.join("hs-dir");

tor.flag(TorFlag::DataDirectory(datadir.to_str().req()?.into()))
.flag(TorFlag::SocksPort(0))
.flag(TorFlag::HiddenServiceDir(hsdir.to_str().req()?.into()))
.flag(TorFlag::HiddenServiceVersion(HiddenServiceVersion::V3));

#[cfg(feature = "electrum")]
tor.flag(TorFlag::HiddenServicePort(
TorAddress::Port(50001),
Some(to_tor_addr(&config.electrum_rpc_addr())).into(),
));

#[cfg(feature = "http")]
tor.flag(TorFlag::HiddenServicePort(
TorAddress::Port(80),
Some(to_tor_addr(&config.http_server_addr)).into(),
));

let handle = tor.start_background();

// https://docs.rs/notify/4.0.15/notify/

Ok(())
}

fn to_tor_addr(addr: &net::SocketAddr) -> TorAddress {
TorAddress::AddressPort(addr.ip().to_string(), addr.port())
}

0 comments on commit f512201

Please sign in to comment.