Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optional Tor Send/Listen Functionality (#226)
* udpate for beta release

* initial tor explorations

* rustfmt

* basic tor tx send working

* rustfmt

* add tor proxy info to config file

* rustfmt

* add utilities to output tor hidden service configuration files

* output tor config as part of listener startup

* rustfmt

* fully automate config and startup of tor process

* rustfmt

* remove unnecessary process kill commands from listener

* rustfmt

* assume defaults for tor sending config if section doesn't exist in grin-wallet.toml

* rustfmt

* ignore tor dev test

* update default paths output by config, compilation + confirmed working on windows

* rustfmt

* fix on osx/unix

* add timeout to tor connector, remove unwrap in client

* allow specifiying tor address without 'http://[].onion' on the command line

* fix api test

* rustfmt

* update address derivation path as per spec

* rustfmt

* move tor init to separate function

* rustfmt

* re-ignore tor dev test

* listen on tor by default if tor available

* rustfmt

* test fix

* remove explicit send via tor flag, and assume tor if address fits

* rustfmt
  • Loading branch information
yeastplume committed Oct 14, 2019
1 parent c603019 commit b4eeb50
Show file tree
Hide file tree
Showing 34 changed files with 2,311 additions and 153 deletions.
289 changes: 245 additions & 44 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions api/src/owner.rs
Expand Up @@ -17,7 +17,7 @@
use chrono::prelude::*;
use uuid::Uuid;

use crate::config::WalletConfig;
use crate::config::{TorConfig, WalletConfig};
use crate::core::core::Transaction;
use crate::core::global;
use crate::impls::create_sender;
Expand Down Expand Up @@ -579,7 +579,8 @@ where
.into());
}
};
let comm_adapter = create_sender(&sa.method, &sa.dest)
//TODO: no TOR just now via this method, to keep compatibility for now
let comm_adapter = create_sender(&sa.method, &sa.dest, None)
.map_err(|e| ErrorKind::GenericError(format!("{}", e)))?;
slate = comm_adapter.send_tx(&slate)?;
self.tx_lock_outputs(keychain_mask, &slate, 0)?;
Expand Down Expand Up @@ -1361,7 +1362,7 @@ where
/// let api_owner = Owner::new(wallet.clone());
/// let _ = api_owner.set_top_level_directory(dir);
///
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None);
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None, None);
///
/// if let Ok(_) = result {
/// //...
Expand All @@ -1373,6 +1374,7 @@ where
chain_type: &global::ChainTypes,
wallet_config: Option<WalletConfig>,
logging_config: Option<LoggingConfig>,
tor_config: Option<TorConfig>,
) -> Result<(), Error> {
let mut w_lock = self.wallet_inst.lock();
let lc = w_lock.lc_provider()?;
Expand All @@ -1381,6 +1383,7 @@ where
"grin-wallet.toml",
wallet_config,
logging_config,
tor_config,
)
}

Expand Down Expand Up @@ -1429,7 +1432,7 @@ where
/// let _ = api_owner.set_top_level_directory(dir);
///
/// // Create configuration
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None);
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None, None);
///
/// // create new wallet wirh random seed
/// let pw = ZeroingString::from("my_password");
Expand Down Expand Up @@ -1496,7 +1499,7 @@ where
/// let _ = api_owner.set_top_level_directory(dir);
///
/// // Create configuration
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None);
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None, None);
///
/// // create new wallet wirh random seed
/// let pw = ZeroingString::from("my_password");
Expand Down
12 changes: 10 additions & 2 deletions api/src/owner_rpc_s.rs
Expand Up @@ -15,7 +15,7 @@
//! JSON-RPC Stub generation for the Owner API
use uuid::Uuid;

use crate::config::WalletConfig;
use crate::config::{TorConfig, WalletConfig};
use crate::core::core::Transaction;
use crate::core::global;
use crate::keychain::{Identifier, Keychain};
Expand Down Expand Up @@ -1469,6 +1469,11 @@ pub trait OwnerRpcS {
"log_max_size": null,
"log_max_files": null,
"tui_running": null
},
"tor_config" : {
"use_tor_listener": true,
"socks_proxy_addr": "127.0.0.1:9050",
"send_config_dir": "."
}
},
"id": 1
Expand All @@ -1492,6 +1497,7 @@ pub trait OwnerRpcS {
chain_type: global::ChainTypes,
wallet_config: Option<WalletConfig>,
logging_config: Option<LoggingConfig>,
tor_config: Option<TorConfig>,
) -> Result<(), ErrorKind>;

/**
Expand Down Expand Up @@ -1912,8 +1918,10 @@ where
chain_type: global::ChainTypes,
wallet_config: Option<WalletConfig>,
logging_config: Option<LoggingConfig>,
tor_config: Option<TorConfig>,
) -> Result<(), ErrorKind> {
Owner::create_config(self, &chain_type, wallet_config, logging_config).map_err(|e| e.kind())
Owner::create_config(self, &chain_type, wallet_config, logging_config, tor_config)
.map_err(|e| e.kind())
}

fn create_wallet(
Expand Down
42 changes: 42 additions & 0 deletions config/src/comments.rs
Expand Up @@ -190,6 +190,48 @@ fn comments() -> HashMap<String, String> {
.to_string(),
);

retval.insert(
"[tor]".to_string(),
"
#########################################
### TOR CONFIGURATION (Experimental) ###
#########################################
"
.to_string(),
);

retval.insert(
"use_tor_listener".to_string(),
"
#Whether to start tor listener on listener startup (default true)
"
.to_string(),
);

retval.insert(
"socks_proxy_addr".to_string(),
"
#Address of the running TOR (SOCKS) server
"
.to_string(),
);

retval.insert(
"socks_proxy_addr".to_string(),
"
# TOR (SOCKS) proxy server address
"
.to_string(),
);

retval.insert(
"send_config_dir".to_string(),
"
#Directory to output TOR configuration to when sending
"
.to_string(),
);

retval
}

Expand Down
11 changes: 10 additions & 1 deletion config/src/config.rs
Expand Up @@ -27,8 +27,8 @@ use toml;

use crate::comments::insert_comments;
use crate::core::global;
use crate::types::WalletConfig;
use crate::types::{ConfigError, GlobalWalletConfig, GlobalWalletConfigMembers};
use crate::types::{TorConfig, WalletConfig};
use crate::util::LoggingConfig;

/// Wallet configuration file name
Expand Down Expand Up @@ -153,6 +153,7 @@ impl Default for GlobalWalletConfigMembers {
fn default() -> GlobalWalletConfigMembers {
GlobalWalletConfigMembers {
logging: Some(LoggingConfig::default()),
tor: Some(TorConfig::default()),
wallet: WalletConfig::default(),
}
}
Expand Down Expand Up @@ -257,6 +258,14 @@ impl GlobalWalletConfig {
.as_mut()
.unwrap()
.log_file_path = log_path.to_str().unwrap().to_owned();
let tor_path = wallet_home.clone();
self.members
.as_mut()
.unwrap()
.tor
.as_mut()
.unwrap()
.send_config_dir = tor_path.to_str().unwrap().to_owned();
}

/// Serialize config
Expand Down
4 changes: 3 additions & 1 deletion config/src/lib.rs
Expand Up @@ -31,4 +31,6 @@ pub mod config;
pub mod types;

pub use crate::config::{initial_setup_wallet, GRIN_WALLET_DIR, WALLET_CONFIG_FILE_NAME};
pub use crate::types::{ConfigError, GlobalWalletConfig, GlobalWalletConfigMembers, WalletConfig};
pub use crate::types::{
ConfigError, GlobalWalletConfig, GlobalWalletConfigMembers, TorConfig, WalletConfig,
};
22 changes: 22 additions & 0 deletions config/src/types.rs
Expand Up @@ -138,6 +138,26 @@ impl fmt::Display for ConfigError {
}
}

/// Tor configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TorConfig {
/// Whether to start tor listener on listener startup (default true)
pub use_tor_listener: bool,
/// Just the address of the socks proxy for now
pub socks_proxy_addr: String,
/// Send configuration directory
pub send_config_dir: String,
}

impl Default for TorConfig {
fn default() -> TorConfig {
TorConfig {
use_tor_listener: true,
socks_proxy_addr: "127.0.0.1:59050".to_owned(),
send_config_dir: ".".into(),
}
}
}
impl From<io::Error> for ConfigError {
fn from(error: io::Error) -> ConfigError {
ConfigError::FileIOError(
Expand All @@ -162,6 +182,8 @@ pub struct GlobalWalletConfigMembers {
/// Wallet configuration
#[serde(default)]
pub wallet: WalletConfig,
/// Tor config
pub tor: Option<TorConfig>,
/// Logging config
pub logging: Option<LoggingConfig>,
}
18 changes: 14 additions & 4 deletions controller/src/command.rs
Expand Up @@ -15,7 +15,7 @@
//! Grin wallet command-line function implementations

use crate::api::TLSConfig;
use crate::config::{WalletConfig, WALLET_CONFIG_FILE_NAME};
use crate::config::{TorConfig, WalletConfig, WALLET_CONFIG_FILE_NAME};
use crate::core::{core, global};
use crate::error::{Error, ErrorKind};
use crate::impls::{create_sender, KeybaseAllChannels, SlateGetter as _, SlateReceiver as _};
Expand Down Expand Up @@ -75,7 +75,13 @@ where
{
let mut w_lock = wallet.lock();
let p = w_lock.lc_provider()?;
p.create_config(&g_args.chain_type, WALLET_CONFIG_FILE_NAME, None, None)?;
p.create_config(
&g_args.chain_type,
WALLET_CONFIG_FILE_NAME,
None,
None,
None,
)?;
p.create_wallet(
None,
args.recovery_phrase,
Expand Down Expand Up @@ -125,6 +131,7 @@ pub fn listen<'a, L, C, K>(
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K>>>>,
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
config: &WalletConfig,
tor_config: &TorConfig,
args: &ListenArgs,
g_args: &GlobalArgs,
) -> Result<(), Error>
Expand All @@ -139,6 +146,7 @@ where
keychain_mask,
&config.api_listen_addr(),
g_args.tls_conf.clone(),
tor_config.use_tor_listener,
),
"keybase" => KeybaseAllChannels::new()?.listen(
config.clone(),
Expand Down Expand Up @@ -251,6 +259,7 @@ pub struct SendArgs {
pub fn send<'a, L, C, K>(
wallet: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
keychain_mask: Option<&SecretKey>,
tor_config: Option<TorConfig>,
args: SendArgs,
dark_scheme: bool,
) -> Result<(), Error>
Expand Down Expand Up @@ -327,7 +336,7 @@ where
})?;
}
method => {
let sender = create_sender(method, &args.dest)?;
let sender = create_sender(method, &args.dest, tor_config)?;
slate = sender.send_tx(&slate)?;
api.tx_lock_outputs(m, &slate, 0)?;
}
Expand Down Expand Up @@ -514,6 +523,7 @@ pub struct ProcessInvoiceArgs {
pub fn process_invoice<'a, L, C, K>(
wallet: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
keychain_mask: Option<&SecretKey>,
tor_config: Option<TorConfig>,
args: ProcessInvoiceArgs,
dark_scheme: bool,
) -> Result<(), Error>
Expand Down Expand Up @@ -594,7 +604,7 @@ where
})?;
}
method => {
let sender = create_sender(method, &args.dest)?;
let sender = create_sender(method, &args.dest, tor_config)?;
slate = sender.send_tx(&slate)?;
api.tx_lock_outputs(m, &slate, 0)?;
}
Expand Down

0 comments on commit b4eeb50

Please sign in to comment.