Skip to content

Commit 7fc7e33

Browse files
author
Yeastplume
authored
Optional Tor Send/Listen Functionality (mimblewimble#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
1 parent dfc57c1 commit 7fc7e33

File tree

34 files changed

+2311
-153
lines changed

34 files changed

+2311
-153
lines changed

Cargo.lock

Lines changed: 245 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/owner.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use chrono::prelude::*;
1818
use uuid::Uuid;
1919

20-
use crate::config::WalletConfig;
20+
use crate::config::{TorConfig, WalletConfig};
2121
use crate::core::core::Transaction;
2222
use crate::core::global;
2323
use crate::impls::create_sender;
@@ -579,7 +579,8 @@ where
579579
.into());
580580
}
581581
};
582-
let comm_adapter = create_sender(&sa.method, &sa.dest)
582+
//TODO: no TOR just now via this method, to keep compatibility for now
583+
let comm_adapter = create_sender(&sa.method, &sa.dest, None)
583584
.map_err(|e| ErrorKind::GenericError(format!("{}", e)))?;
584585
slate = comm_adapter.send_tx(&slate)?;
585586
self.tx_lock_outputs(keychain_mask, &slate, 0)?;
@@ -1361,7 +1362,7 @@ where
13611362
/// let api_owner = Owner::new(wallet.clone());
13621363
/// let _ = api_owner.set_top_level_directory(dir);
13631364
///
1364-
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None);
1365+
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None, None);
13651366
///
13661367
/// if let Ok(_) = result {
13671368
/// //...
@@ -1373,6 +1374,7 @@ where
13731374
chain_type: &global::ChainTypes,
13741375
wallet_config: Option<WalletConfig>,
13751376
logging_config: Option<LoggingConfig>,
1377+
tor_config: Option<TorConfig>,
13761378
) -> Result<(), Error> {
13771379
let mut w_lock = self.wallet_inst.lock();
13781380
let lc = w_lock.lc_provider()?;
@@ -1381,6 +1383,7 @@ where
13811383
"grin-wallet.toml",
13821384
wallet_config,
13831385
logging_config,
1386+
tor_config,
13841387
)
13851388
}
13861389

@@ -1429,7 +1432,7 @@ where
14291432
/// let _ = api_owner.set_top_level_directory(dir);
14301433
///
14311434
/// // Create configuration
1432-
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None);
1435+
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None, None);
14331436
///
14341437
/// // create new wallet wirh random seed
14351438
/// let pw = ZeroingString::from("my_password");
@@ -1496,7 +1499,7 @@ where
14961499
/// let _ = api_owner.set_top_level_directory(dir);
14971500
///
14981501
/// // Create configuration
1499-
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None);
1502+
/// let result = api_owner.create_config(&ChainTypes::Mainnet, None, None, None);
15001503
///
15011504
/// // create new wallet wirh random seed
15021505
/// let pw = ZeroingString::from("my_password");

api/src/owner_rpc_s.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! JSON-RPC Stub generation for the Owner API
1616
use uuid::Uuid;
1717

18-
use crate::config::WalletConfig;
18+
use crate::config::{TorConfig, WalletConfig};
1919
use crate::core::core::Transaction;
2020
use crate::core::global;
2121
use crate::keychain::{Identifier, Keychain};
@@ -1469,6 +1469,11 @@ pub trait OwnerRpcS {
14691469
"log_max_size": null,
14701470
"log_max_files": null,
14711471
"tui_running": null
1472+
},
1473+
"tor_config" : {
1474+
"use_tor_listener": true,
1475+
"socks_proxy_addr": "127.0.0.1:9050",
1476+
"send_config_dir": "."
14721477
}
14731478
},
14741479
"id": 1
@@ -1492,6 +1497,7 @@ pub trait OwnerRpcS {
14921497
chain_type: global::ChainTypes,
14931498
wallet_config: Option<WalletConfig>,
14941499
logging_config: Option<LoggingConfig>,
1500+
tor_config: Option<TorConfig>,
14951501
) -> Result<(), ErrorKind>;
14961502

14971503
/**
@@ -1912,8 +1918,10 @@ where
19121918
chain_type: global::ChainTypes,
19131919
wallet_config: Option<WalletConfig>,
19141920
logging_config: Option<LoggingConfig>,
1921+
tor_config: Option<TorConfig>,
19151922
) -> Result<(), ErrorKind> {
1916-
Owner::create_config(self, &chain_type, wallet_config, logging_config).map_err(|e| e.kind())
1923+
Owner::create_config(self, &chain_type, wallet_config, logging_config, tor_config)
1924+
.map_err(|e| e.kind())
19171925
}
19181926

19191927
fn create_wallet(

config/src/comments.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ fn comments() -> HashMap<String, String> {
190190
.to_string(),
191191
);
192192

193+
retval.insert(
194+
"[tor]".to_string(),
195+
"
196+
#########################################
197+
### TOR CONFIGURATION (Experimental) ###
198+
#########################################
199+
"
200+
.to_string(),
201+
);
202+
203+
retval.insert(
204+
"use_tor_listener".to_string(),
205+
"
206+
#Whether to start tor listener on listener startup (default true)
207+
"
208+
.to_string(),
209+
);
210+
211+
retval.insert(
212+
"socks_proxy_addr".to_string(),
213+
"
214+
#Address of the running TOR (SOCKS) server
215+
"
216+
.to_string(),
217+
);
218+
219+
retval.insert(
220+
"socks_proxy_addr".to_string(),
221+
"
222+
# TOR (SOCKS) proxy server address
223+
"
224+
.to_string(),
225+
);
226+
227+
retval.insert(
228+
"send_config_dir".to_string(),
229+
"
230+
#Directory to output TOR configuration to when sending
231+
"
232+
.to_string(),
233+
);
234+
193235
retval
194236
}
195237

config/src/config.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use toml;
2727

2828
use crate::comments::insert_comments;
2929
use crate::core::global;
30-
use crate::types::WalletConfig;
3130
use crate::types::{ConfigError, GlobalWalletConfig, GlobalWalletConfigMembers};
31+
use crate::types::{TorConfig, WalletConfig};
3232
use crate::util::LoggingConfig;
3333

3434
/// Wallet configuration file name
@@ -153,6 +153,7 @@ impl Default for GlobalWalletConfigMembers {
153153
fn default() -> GlobalWalletConfigMembers {
154154
GlobalWalletConfigMembers {
155155
logging: Some(LoggingConfig::default()),
156+
tor: Some(TorConfig::default()),
156157
wallet: WalletConfig::default(),
157158
}
158159
}
@@ -257,6 +258,14 @@ impl GlobalWalletConfig {
257258
.as_mut()
258259
.unwrap()
259260
.log_file_path = log_path.to_str().unwrap().to_owned();
261+
let tor_path = wallet_home.clone();
262+
self.members
263+
.as_mut()
264+
.unwrap()
265+
.tor
266+
.as_mut()
267+
.unwrap()
268+
.send_config_dir = tor_path.to_str().unwrap().to_owned();
260269
}
261270

262271
/// Serialize config

config/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ pub mod config;
3131
pub mod types;
3232

3333
pub use crate::config::{initial_setup_wallet, GRIN_WALLET_DIR, WALLET_CONFIG_FILE_NAME};
34-
pub use crate::types::{ConfigError, GlobalWalletConfig, GlobalWalletConfigMembers, WalletConfig};
34+
pub use crate::types::{
35+
ConfigError, GlobalWalletConfig, GlobalWalletConfigMembers, TorConfig, WalletConfig,
36+
};

config/src/types.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ impl fmt::Display for ConfigError {
138138
}
139139
}
140140

141+
/// Tor configuration
142+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143+
pub struct TorConfig {
144+
/// Whether to start tor listener on listener startup (default true)
145+
pub use_tor_listener: bool,
146+
/// Just the address of the socks proxy for now
147+
pub socks_proxy_addr: String,
148+
/// Send configuration directory
149+
pub send_config_dir: String,
150+
}
151+
152+
impl Default for TorConfig {
153+
fn default() -> TorConfig {
154+
TorConfig {
155+
use_tor_listener: true,
156+
socks_proxy_addr: "127.0.0.1:59050".to_owned(),
157+
send_config_dir: ".".into(),
158+
}
159+
}
160+
}
141161
impl From<io::Error> for ConfigError {
142162
fn from(error: io::Error) -> ConfigError {
143163
ConfigError::FileIOError(
@@ -162,6 +182,8 @@ pub struct GlobalWalletConfigMembers {
162182
/// Wallet configuration
163183
#[serde(default)]
164184
pub wallet: WalletConfig,
185+
/// Tor config
186+
pub tor: Option<TorConfig>,
165187
/// Logging config
166188
pub logging: Option<LoggingConfig>,
167189
}

controller/src/command.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! Grin wallet command-line function implementations
1616

1717
use crate::api::TLSConfig;
18-
use crate::config::{WalletConfig, WALLET_CONFIG_FILE_NAME};
18+
use crate::config::{TorConfig, WalletConfig, WALLET_CONFIG_FILE_NAME};
1919
use crate::core::{core, global};
2020
use crate::error::{Error, ErrorKind};
2121
use crate::impls::{create_sender, KeybaseAllChannels, SlateGetter as _, SlateReceiver as _};
@@ -75,7 +75,13 @@ where
7575
{
7676
let mut w_lock = wallet.lock();
7777
let p = w_lock.lc_provider()?;
78-
p.create_config(&g_args.chain_type, WALLET_CONFIG_FILE_NAME, None, None)?;
78+
p.create_config(
79+
&g_args.chain_type,
80+
WALLET_CONFIG_FILE_NAME,
81+
None,
82+
None,
83+
None,
84+
)?;
7985
p.create_wallet(
8086
None,
8187
args.recovery_phrase,
@@ -125,6 +131,7 @@ pub fn listen<'a, L, C, K>(
125131
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K>>>>,
126132
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
127133
config: &WalletConfig,
134+
tor_config: &TorConfig,
128135
args: &ListenArgs,
129136
g_args: &GlobalArgs,
130137
) -> Result<(), Error>
@@ -139,6 +146,7 @@ where
139146
keychain_mask,
140147
&config.api_listen_addr(),
141148
g_args.tls_conf.clone(),
149+
tor_config.use_tor_listener,
142150
),
143151
"keybase" => KeybaseAllChannels::new()?.listen(
144152
config.clone(),
@@ -251,6 +259,7 @@ pub struct SendArgs {
251259
pub fn send<'a, L, C, K>(
252260
wallet: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
253261
keychain_mask: Option<&SecretKey>,
262+
tor_config: Option<TorConfig>,
254263
args: SendArgs,
255264
dark_scheme: bool,
256265
) -> Result<(), Error>
@@ -327,7 +336,7 @@ where
327336
})?;
328337
}
329338
method => {
330-
let sender = create_sender(method, &args.dest)?;
339+
let sender = create_sender(method, &args.dest, tor_config)?;
331340
slate = sender.send_tx(&slate)?;
332341
api.tx_lock_outputs(m, &slate, 0)?;
333342
}
@@ -514,6 +523,7 @@ pub struct ProcessInvoiceArgs {
514523
pub fn process_invoice<'a, L, C, K>(
515524
wallet: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
516525
keychain_mask: Option<&SecretKey>,
526+
tor_config: Option<TorConfig>,
517527
args: ProcessInvoiceArgs,
518528
dark_scheme: bool,
519529
) -> Result<(), Error>
@@ -594,7 +604,7 @@ where
594604
})?;
595605
}
596606
method => {
597-
let sender = create_sender(method, &args.dest)?;
607+
let sender = create_sender(method, &args.dest, tor_config)?;
598608
slate = sender.send_tx(&slate)?;
599609
api.tx_lock_outputs(m, &slate, 0)?;
600610
}

0 commit comments

Comments
 (0)