From 1003f918c5f7b46a94cb07620b82afc411917a05 Mon Sep 17 00:00:00 2001 From: David Main <51991544+StriderDM@users.noreply.github.com> Date: Fri, 26 Nov 2021 09:50:13 +0200 Subject: [PATCH] feat!: add tcp bypass settings for tor in wallet_ffi (#3615) Description --- This PR allows the TCP bypass settings for Tor to be set via wallet_ffi instead of being hard-coded. Motivation and Context --- As above. How Has This Been Tested? --- cargo test --all nvm use 12.22.6 && node_modules/.bin/cucumber-js --profile "ci" --tags "not @long-running and not @broken and @wallet-ffi" --- base_layer/wallet_ffi/src/lib.rs | 35 ++++++++++++++----- base_layer/wallet_ffi/wallet.h | 1 + comms/dht/src/macros.rs | 1 + comms/src/tor/hidden_service/proxy_opts.rs | 1 + integration_tests/helpers/ffi/ffiInterface.js | 2 ++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/base_layer/wallet_ffi/src/lib.rs b/base_layer/wallet_ffi/src/lib.rs index ad0153f422..26ec4fdbc4 100644 --- a/base_layer/wallet_ffi/src/lib.rs +++ b/base_layer/wallet_ffi/src/lib.rs @@ -2576,7 +2576,8 @@ pub unsafe extern "C" fn transport_tcp_create( /// `control_server_address` - The pointer to a char array /// `tor_cookie` - The pointer to a ByteVector containing the contents of the tor cookie file, can be null /// `tor_port` - The tor port -/// `socks_username` - The pointer to a char array containing the socks username, can be null +/// `tor_proxy_bypass_for_outbound` - Whether tor will use a direct tcp connection for a given bypass address instead of +/// the tor proxy if tcp is available, if not it has no effect /// `socks_password` - The pointer to a char array containing the socks password, can be null /// `error_out` - Pointer to an int which will be modified to an error code should one occur, may not be null. Functions /// as an out parameter. @@ -2592,6 +2593,7 @@ pub unsafe extern "C" fn transport_tor_create( control_server_address: *const c_char, tor_cookie: *const ByteVector, tor_port: c_ushort, + tor_proxy_bypass_for_outbound: bool, socks_username: *const c_char, socks_password: *const c_char, error_out: *mut c_int, @@ -2665,8 +2667,7 @@ pub unsafe extern "C" fn transport_tor_create( socks_address_override: None, socks_auth: authentication, tor_proxy_bypass_addresses: vec![], - // Prefer performance - tor_proxy_bypass_for_outbound_tcp: true, + tor_proxy_bypass_for_outbound_tcp: tor_proxy_bypass_for_outbound, }; let transport = TariTransportType::Tor(tor_config); @@ -5974,6 +5975,7 @@ mod test { let transport = transport_memory_create(); let _address = transport_memory_get_address(transport, error_ptr); assert_eq!(error, 0); + transport_type_destroy(transport); } } @@ -5984,8 +5986,9 @@ mod test { let error_ptr = &mut error as *mut c_int; let address_listener = CString::new("/ip4/127.0.0.1/tcp/0").unwrap(); let address_listener_str: *const c_char = CString::into_raw(address_listener) as *const c_char; - let _transport = transport_tcp_create(address_listener_str, error_ptr); + let transport = transport_tcp_create(address_listener_str, error_ptr); assert_eq!(error, 0); + transport_type_destroy(transport); } } @@ -5995,16 +5998,32 @@ mod test { let mut error = 0; let error_ptr = &mut error as *mut c_int; let address_control = CString::new("/ip4/127.0.0.1/tcp/8080").unwrap(); + let mut bypass = false; let address_control_str: *const c_char = CString::into_raw(address_control) as *const c_char; - let _transport = transport_tor_create( + let mut transport = transport_tor_create( address_control_str, - ptr::null_mut(), + ptr::null(), + 8080, + bypass, + ptr::null(), + ptr::null(), + error_ptr, + ); + assert_eq!(error, 0); + transport_type_destroy(transport); + + bypass = true; + transport = transport_tor_create( + address_control_str, + ptr::null(), 8080, - ptr::null_mut(), - ptr::null_mut(), + bypass, + ptr::null(), + ptr::null(), error_ptr, ); assert_eq!(error, 0); + transport_type_destroy(transport); } } diff --git a/base_layer/wallet_ffi/wallet.h b/base_layer/wallet_ffi/wallet.h index d7266603e2..661d861103 100644 --- a/base_layer/wallet_ffi/wallet.h +++ b/base_layer/wallet_ffi/wallet.h @@ -90,6 +90,7 @@ struct TariTransportType *transport_tor_create( const char *control_server_address, struct ByteVector *tor_cookie, unsigned short tor_port, + bool tor_proxy_bypass_for_outbound, const char *socks_username, const char *socks_password, int *error_out); diff --git a/comms/dht/src/macros.rs b/comms/dht/src/macros.rs index 3911e96e99..a4303dd37d 100644 --- a/comms/dht/src/macros.rs +++ b/comms/dht/src/macros.rs @@ -23,6 +23,7 @@ /// This macro unlocks a Mutex or RwLock. If the lock is /// poisoned (i.e. panic while unlocked) the last value /// before the panic is used. +#[allow(unused_macros)] macro_rules! acquire_lock { ($e:expr, $m:ident) => { match $e.$m() { diff --git a/comms/src/tor/hidden_service/proxy_opts.rs b/comms/src/tor/hidden_service/proxy_opts.rs index bc71c449df..ed252f0124 100644 --- a/comms/src/tor/hidden_service/proxy_opts.rs +++ b/comms/src/tor/hidden_service/proxy_opts.rs @@ -63,6 +63,7 @@ fn is_tcp_address(addr: &Multiaddr) -> bool { let protocol = iter.next(); matches!(protocol, Some(Tcp(_))) } + #[cfg(test)] mod test { use super::*; diff --git a/integration_tests/helpers/ffi/ffiInterface.js b/integration_tests/helpers/ffi/ffiInterface.js index 607252a07e..9507ad18f9 100644 --- a/integration_tests/helpers/ffi/ffiInterface.js +++ b/integration_tests/helpers/ffi/ffiInterface.js @@ -86,6 +86,7 @@ class InterfaceFFI { this.string, this.ptr, this.ushort, + this.bool, this.string, this.string, this.intPtr, @@ -608,6 +609,7 @@ class InterfaceFFI { control_server_address, tor_cookie, tor_port, + true, socks_username, socks_password, error