mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-12-03 12:24:27 +01:00
Global config instance (#769)
* config: global instance * fix: cli tests * fix: owner api tests * config: save tor config * fix: owner rpc tests * fix: set config file path on init and owner api call * tor: store launched services, get global config at command loop, remove unused listen args, check for empty global config to set default, do not save empty tor config at owner * api: update config in memory after successful file write * arti: restart proxy in loop * fix: comment for tor config save * config: set or init, update without error * config: scoped tor config * build: update lock file * config: ability to store multiple configurations at memory from config path * tor: restart service on config change * fix: owner api test * foreign: pass tor config from args, stop service, error handling * arti: move runtime wrapper to single file * config: read existing config from file at create_config * config: handle error on read * arti: single dir for cache * foreign: register config listener before launch * config: save temp * config: remove unused error type * config: make path and members not optional, return an error on config creation if it exists, pass default tor config to send if not set * arti: non public runtime wrapper * config: migrate comments * fix: rpc tests * config: return from cache if exists * config: save disabled tor config when null provided at owner and tor section was not set * config: update tor with args at listener * config: pass pathbuf directly on creation * config: unique tmp file name, setting permissions and providing config version * fix global config handling * fix config persistence * fix tor service restart * fix config synchronization * require wallet close before changing data directory * unify global config updates --------- Co-authored-by: wiesche <wiesche89@googlemail.com>
This commit is contained in:
+18
-24
@@ -17,9 +17,9 @@ use crate::libwallet::slate_versions::{SlateVersion, VersionedSlate};
|
||||
use crate::libwallet::{Error, Slate};
|
||||
use crate::tor::arti::{start_tor_client, tor_post};
|
||||
use crate::tor::bridge::TorBridge;
|
||||
use crate::tor::config as tor_config;
|
||||
use crate::tor::process::TorProcess;
|
||||
use crate::tor::proxy::TorProxy;
|
||||
use crate::tor::{config as tor_config, Tor};
|
||||
use crate::SlateSender;
|
||||
use grin_wallet_config::TorConfig;
|
||||
use serde::Serialize;
|
||||
@@ -34,7 +34,7 @@ use std::sync::Arc;
|
||||
pub struct TorSlateSender {
|
||||
base_url: String,
|
||||
config: TorConfig,
|
||||
tor: Arc<Tor>,
|
||||
tor_process: Arc<Option<TorProcess>>,
|
||||
}
|
||||
|
||||
impl TorSlateSender {
|
||||
@@ -43,27 +43,29 @@ impl TorSlateSender {
|
||||
if !base_url.starts_with("http") && !base_url.starts_with("https") {
|
||||
Err(Error::GenericError("Scheme must be http".to_string()))
|
||||
} else {
|
||||
let tor_dir = {
|
||||
let mut path = PathBuf::from(&config.send_config_dir);
|
||||
path.push("tor");
|
||||
path.push("sender");
|
||||
path
|
||||
};
|
||||
let tor = if config.use_integrated.unwrap_or(false) {
|
||||
start_tor_client(tor_dir.to_str().unwrap(), config.clone())?
|
||||
start_tor_client(config.clone())?;
|
||||
None
|
||||
} else {
|
||||
Self::launch_tor_process(&config, &tor_dir)?
|
||||
let tor_dir = {
|
||||
let mut path = PathBuf::from(&config.send_config_dir);
|
||||
path.push("tor");
|
||||
path.push("sender");
|
||||
path
|
||||
};
|
||||
let p = Self::launch_tor_process(&config, &tor_dir)?;
|
||||
Some(p)
|
||||
};
|
||||
Ok(TorSlateSender {
|
||||
base_url: base_url.to_owned(),
|
||||
config,
|
||||
tor: Arc::new(tor),
|
||||
tor_process: Arc::new(tor),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Launch external Tor process.
|
||||
fn launch_tor_process(config: &TorConfig, tor_dir: &PathBuf) -> Result<Tor, Error> {
|
||||
fn launch_tor_process(config: &TorConfig, tor_dir: &PathBuf) -> Result<TorProcess, Error> {
|
||||
let mut tor = TorProcess::new();
|
||||
let socks_proxy_addr = SocketAddr::V4(
|
||||
config
|
||||
@@ -107,11 +109,7 @@ impl TorSlateSender {
|
||||
.completion_percent(100)
|
||||
.launch()
|
||||
.map_err(|e| Error::TorProcess(format!("{:?}", e)))?;
|
||||
Ok(Tor {
|
||||
process: Some(tor),
|
||||
service: None,
|
||||
client: None,
|
||||
})
|
||||
Ok(tor)
|
||||
}
|
||||
|
||||
/// Check version of the listening wallet
|
||||
@@ -177,7 +175,7 @@ impl TorSlateSender {
|
||||
where
|
||||
IN: Serialize,
|
||||
{
|
||||
let res = if self.tor.process.is_some() {
|
||||
let res = if self.tor_process.is_some() {
|
||||
let socks_proxy_addr =
|
||||
SocketAddr::V4(self.config.socks_proxy_addr.parse().map_err(|_| {
|
||||
ClientError::Internal("Socks proxy address is not set".to_string())
|
||||
@@ -189,12 +187,8 @@ impl TorSlateSender {
|
||||
let res = client.send_request(req)?;
|
||||
res
|
||||
} else {
|
||||
if let Some(client) = &self.tor.client {
|
||||
tor_post(client.clone(), &self.config, &input, url)
|
||||
.map_err(|e| ClientError::RequestError(format!("{:?}", e)))?
|
||||
} else {
|
||||
return Err(ClientError::Internal("Tor is not configured".to_string()));
|
||||
}
|
||||
tor_post(&self.config, &input, url)
|
||||
.map_err(|e| ClientError::RequestError(format!("{:?}", e)))?
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ use crate::lifecycle::seed::WalletSeed;
|
||||
use crate::util::secp::key::SecretKey;
|
||||
use crate::util::ZeroingString;
|
||||
use grin_util::logger::LoggingConfig;
|
||||
use grin_wallet_config::config::reload_global_config;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::path::MAIN_SEPARATOR;
|
||||
@@ -89,48 +90,11 @@ where
|
||||
logging_config: Option<LoggingConfig>,
|
||||
tor_config: Option<TorConfig>,
|
||||
) -> Result<(), Error> {
|
||||
let mut default_config = GlobalWalletConfig::for_chain(&chain_type);
|
||||
let config_file_version = match default_config.members.as_ref() {
|
||||
Some(m) => m.clone().config_file_version,
|
||||
None => None,
|
||||
};
|
||||
let logging = match logging_config {
|
||||
Some(l) => Some(l),
|
||||
None => match default_config.members.as_ref() {
|
||||
Some(m) => m.clone().logging,
|
||||
None => None,
|
||||
},
|
||||
};
|
||||
// Check if config was provided, if not load default and set update to "true"
|
||||
let (wallet, update) = match wallet_config {
|
||||
Some(w) => (w, false),
|
||||
None => match default_config.members.as_ref() {
|
||||
Some(m) => (m.clone().wallet, true),
|
||||
None => (WalletConfig::default(), true),
|
||||
},
|
||||
};
|
||||
let tor = match tor_config {
|
||||
Some(t) => Some(t),
|
||||
None => match default_config.members.as_ref() {
|
||||
Some(m) => m.clone().tor,
|
||||
None => Some(TorConfig::default()),
|
||||
},
|
||||
};
|
||||
default_config = GlobalWalletConfig {
|
||||
members: Some(GlobalWalletConfigMembers {
|
||||
config_file_version,
|
||||
wallet,
|
||||
tor,
|
||||
logging,
|
||||
}),
|
||||
..default_config
|
||||
};
|
||||
let mut config_file_name = PathBuf::from(self.data_dir.clone());
|
||||
config_file_name.push(file_name);
|
||||
|
||||
let mut data_dir_name = PathBuf::from(self.data_dir.clone());
|
||||
data_dir_name.push(GRIN_WALLET_DIR);
|
||||
|
||||
if config_file_name.exists() && data_dir_name.exists() {
|
||||
let msg = format!(
|
||||
"{} already exists in the target directory ({}). Please remove it first",
|
||||
@@ -140,10 +104,35 @@ where
|
||||
return Err(Error::Lifecycle(msg));
|
||||
}
|
||||
|
||||
// If config exists but the datadir return ok
|
||||
if config_file_name.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut default_config = GlobalWalletConfig::for_chain(&chain_type, &config_file_name);
|
||||
let config_file_version = default_config.members.config_file_version;
|
||||
let logging = match logging_config.clone() {
|
||||
Some(l) => Some(l),
|
||||
None => default_config.members.logging,
|
||||
};
|
||||
// Check if config was provided, if not load default and set update to "true"
|
||||
let (wallet, update) = match wallet_config.clone() {
|
||||
Some(w) => (w, false),
|
||||
None => (default_config.members.wallet, true),
|
||||
};
|
||||
let tor = match tor_config.clone() {
|
||||
Some(t) => Some(t),
|
||||
None => default_config.members.tor,
|
||||
};
|
||||
default_config = GlobalWalletConfig {
|
||||
members: GlobalWalletConfigMembers {
|
||||
config_file_version,
|
||||
wallet,
|
||||
tor,
|
||||
logging,
|
||||
},
|
||||
..default_config
|
||||
};
|
||||
|
||||
// default settings are updated if no config was provided, no support for top_dir/here
|
||||
let mut abs_path_node = std::env::current_dir()?;
|
||||
abs_path_node.push(self.data_dir.clone());
|
||||
@@ -171,6 +160,8 @@ where
|
||||
return Err(Error::Lifecycle(msg));
|
||||
}
|
||||
|
||||
reload_global_config(&config_file_name)?;
|
||||
|
||||
info!(
|
||||
"File {} configured and created",
|
||||
config_file_name.to_str().unwrap(),
|
||||
|
||||
+137
-92
@@ -13,7 +13,6 @@
|
||||
// limitations under the License.
|
||||
|
||||
use crate::tor::config::exp_sec_key_bytes;
|
||||
use crate::tor::{ArtiRuntimeWrapper, Tor};
|
||||
use arti_client::config::pt::TransportConfigBuilder;
|
||||
use arti_client::config::{BridgeConfigBuilder, TorClientConfigBuilder};
|
||||
use arti_client::{TorClient, TorClientConfig};
|
||||
@@ -21,6 +20,7 @@ use bytes::Bytes;
|
||||
use ed25519_dalek::hazmat::ExpandedSecretKey;
|
||||
use ed25519_dalek::Digest;
|
||||
use fs_mistrust::Mistrust;
|
||||
use fslock_guard::LockFileGuard;
|
||||
use grin_util::secp::SecretKey;
|
||||
use grin_wallet_config::TorConfig;
|
||||
use grin_wallet_libwallet::Error;
|
||||
@@ -31,10 +31,11 @@ use hyper_util::rt::TokioIo;
|
||||
use lazy_static::lazy_static;
|
||||
use serde::Serialize;
|
||||
use sha2::Sha512;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, LazyLock, Mutex};
|
||||
use std::thread;
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::time::Duration;
|
||||
use tor_hscrypto::pk::{HsIdKey, HsIdKeypair};
|
||||
use tor_hsrproxy::config::{
|
||||
@@ -42,19 +43,46 @@ use tor_hsrproxy::config::{
|
||||
};
|
||||
use tor_hsrproxy::OnionServiceReverseProxy;
|
||||
use tor_hsservice::config::OnionServiceConfigBuilder;
|
||||
use tor_hsservice::{HsIdKeypairSpecifier, HsIdPublicKeySpecifier, HsNickname};
|
||||
use tor_hsservice::{
|
||||
HsIdKeypairSpecifier, HsIdPublicKeySpecifier, HsNickname, RunningOnionService,
|
||||
};
|
||||
use tor_keymgr::config::CfgPath;
|
||||
use tor_keymgr::{ArtiNativeKeystore, KeyMgrBuilder, KeystoreSelector};
|
||||
use tor_llcrypto::pk::ed25519::ExpandedKeypair;
|
||||
use tor_rtcompat::tokio::TokioNativeTlsRuntime;
|
||||
use tor_rtcompat::{SleepProviderExt, ToplevelBlockOn};
|
||||
|
||||
// Arti Tokio runtime.
|
||||
struct TorService {
|
||||
service: Arc<RunningOnionService>,
|
||||
proxy: Arc<OnionServiceReverseProxy>,
|
||||
worker: JoinHandle<()>,
|
||||
state_lock: PathBuf,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref ARTI_RUNTIME: LazyLock<Mutex<Option<ArtiRuntimeWrapper>>> =
|
||||
/// Arti Tokio runtime.
|
||||
static ref ARTI_RUNTIME: LazyLock<Mutex<Option<ArtiRuntimeWrapper>>> =
|
||||
LazyLock::new(|| Mutex::new(ArtiRuntimeWrapper::create().ok()));
|
||||
pub static ref ARTI_CLIENT_CONFIG: LazyLock<Mutex<Option<(Arc<TorClient<TokioNativeTlsRuntime>>, TorClientConfig)>>> =
|
||||
/// Arti client and config.
|
||||
static ref ARTI_CLIENT_CONFIG: LazyLock<Mutex<Option<(Arc<TorClient<TokioNativeTlsRuntime>>, TorClientConfig)>>> =
|
||||
LazyLock::new(|| Mutex::new(None));
|
||||
/// Running services, where key is onion address.
|
||||
static ref ARTI_PROXY_SERVICES: LazyLock<Mutex<HashMap<String, TorService>>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
}
|
||||
|
||||
/// Arti client runtime wrapper.
|
||||
#[derive(Clone)]
|
||||
struct ArtiRuntimeWrapper {
|
||||
runtime: TokioNativeTlsRuntime,
|
||||
}
|
||||
|
||||
impl ArtiRuntimeWrapper {
|
||||
fn create() -> Result<ArtiRuntimeWrapper, std::io::Error> {
|
||||
Ok(Self {
|
||||
runtime: TokioNativeTlsRuntime::create()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get Tor client runtime.
|
||||
@@ -67,22 +95,51 @@ fn runtime() -> Result<TokioNativeTlsRuntime, Error> {
|
||||
Ok(r.runtime.clone())
|
||||
}
|
||||
|
||||
/// Stop running Tor service by onion address as key.
|
||||
pub fn stop_tor_service(onion_addr: String) -> Result<(), Error> {
|
||||
let service = ARTI_PROXY_SERVICES.lock().unwrap().remove(&onion_addr);
|
||||
let Some(service) = service else {
|
||||
error!("Service {} to stop was not found", onion_addr);
|
||||
return Ok(());
|
||||
};
|
||||
let TorService {
|
||||
service,
|
||||
proxy,
|
||||
worker,
|
||||
state_lock,
|
||||
} = service;
|
||||
|
||||
proxy.shutdown();
|
||||
worker
|
||||
.join()
|
||||
.map_err(|_| Error::TorProcess("Tor proxy thread panicked".into()))?;
|
||||
drop(service);
|
||||
drop(
|
||||
LockFileGuard::lock(state_lock)
|
||||
.map_err(|e| Error::TorProcess(format!("Can not stop Tor service: {}", e)))?,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get state and cache data paths.
|
||||
fn state_cache_paths(config: &TorConfig) -> (PathBuf, PathBuf) {
|
||||
let mut tor_dir = PathBuf::from(&config.send_config_dir);
|
||||
tor_dir.push("arti");
|
||||
let state_path = tor_dir.join("state");
|
||||
let cache_path = tor_dir.join("cache");
|
||||
(state_path, cache_path)
|
||||
}
|
||||
|
||||
/// Start Tor service from provided key.
|
||||
pub fn start_tor_service(
|
||||
key: SecretKey,
|
||||
tor_dir: &str,
|
||||
addr: &str,
|
||||
config: TorConfig,
|
||||
) -> Result<Tor, Error> {
|
||||
pub fn start_tor_service(key: SecretKey, addr: &str, config: &TorConfig) -> Result<(), Error> {
|
||||
info!("Starting integrated Tor listener.");
|
||||
let use_proxy = config.proxy.transport.is_some() && config.proxy.address.is_some();
|
||||
if use_proxy {
|
||||
info!("Proxy configuration will be ignored.");
|
||||
}
|
||||
|
||||
let state_path = Path::new(&tor_dir).join("state");
|
||||
let cache_path = Path::new(&tor_dir).join("cache");
|
||||
let (client, config) = init_client(&state_path, &cache_path, config, true)?;
|
||||
let (state_path, cache_path) = state_cache_paths(&config);
|
||||
let (client, config) = init_client(&state_path, &cache_path, config)?;
|
||||
|
||||
// Add service key to keystore.
|
||||
let onion_address =
|
||||
@@ -90,6 +147,10 @@ pub fn start_tor_service(
|
||||
let hs = HsNickname::new(onion_address.to_string())
|
||||
.map_err(|e| Error::TorConfig(format!("{:?}", e)))?;
|
||||
let keystore_path = Path::new(&state_path).join("keystore");
|
||||
// Arti 0.44 uses this lock to finish stopping a service before it can start again.
|
||||
let state_lock = state_path
|
||||
.join("hss")
|
||||
.join(format!("{}.lock", onion_address));
|
||||
let _ = add_service_key(config.fs_mistrust(), &key, &hs, keystore_path)?;
|
||||
|
||||
// Launch Onion service.
|
||||
@@ -97,77 +158,66 @@ pub fn start_tor_service(
|
||||
.nickname(hs.clone())
|
||||
.build()
|
||||
.map_err(|e| Error::TorConfig(format!("{:?}", e)))?;
|
||||
let running_onion_service = match client.launch_onion_service(service_config) {
|
||||
let (service, proxy, worker) = match client.launch_onion_service(service_config) {
|
||||
Ok(res) => {
|
||||
if let Some((service, mut request)) = res {
|
||||
let addr: SocketAddr = addr
|
||||
.parse()
|
||||
.map_err(|e| Error::TorProcess(format!("{:?}", e)))?;
|
||||
let proxy = create_service_proxy(addr)?;
|
||||
let c = client.clone();
|
||||
thread::spawn(move || {
|
||||
let p = proxy.clone();
|
||||
// Launch service proxy.
|
||||
let worker = thread::spawn(move || {
|
||||
c.clone().runtime().block_on(async move {
|
||||
// Launch service proxy.
|
||||
async fn run_proxy<S>(
|
||||
c: Arc<TorClient<TokioNativeTlsRuntime>>,
|
||||
addr: SocketAddr,
|
||||
request: &mut S,
|
||||
hs: HsNickname,
|
||||
) where
|
||||
S: futures::Stream<Item = tor_hsservice::RendRequest>
|
||||
+ Unpin
|
||||
+ Send
|
||||
+ 'static,
|
||||
{
|
||||
match run_service_proxy(c.clone(), addr, request, hs.clone()).await {
|
||||
loop {
|
||||
match run_service_proxy(p.clone(), &mut request, hs.clone()).await {
|
||||
Ok(_) => {
|
||||
info!("Tor proxy stopped");
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Tor proxy error: {:?}, restarting", e);
|
||||
tokio::time::sleep(Duration::from_millis(1000)).await;
|
||||
Box::pin(run_proxy(c, addr, request, hs)).await;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
run_proxy(c.clone(), addr, &mut request, hs.clone()).await;
|
||||
})
|
||||
});
|
||||
service
|
||||
(service, proxy, worker)
|
||||
} else {
|
||||
return Err(Error::TorProcess("Can not launch onion service".to_owned()));
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(Error::TorProcess(format!("{:?}", e))),
|
||||
};
|
||||
Ok(Tor {
|
||||
process: None,
|
||||
service: Some(running_onion_service),
|
||||
client: Some(client),
|
||||
})
|
||||
|
||||
let mut running_services = ARTI_PROXY_SERVICES.lock().unwrap();
|
||||
running_services.insert(
|
||||
onion_address.to_string(),
|
||||
TorService {
|
||||
service,
|
||||
proxy,
|
||||
worker,
|
||||
state_lock,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Start Tor client to send requests.
|
||||
pub fn start_tor_client(tor_dir: &str, config: TorConfig) -> Result<Tor, Error> {
|
||||
pub fn start_tor_client(config: TorConfig) -> Result<(), Error> {
|
||||
info!("Starting integrated Tor client");
|
||||
|
||||
let state_path = Path::new(tor_dir).join("state");
|
||||
let cache_path = Path::new(tor_dir).join("cache");
|
||||
|
||||
let (client, _) = init_client(&state_path, &cache_path, config, false)?;
|
||||
Ok(Tor {
|
||||
process: None,
|
||||
service: None,
|
||||
client: Some(client),
|
||||
})
|
||||
let (state_path, cache_path) = state_cache_paths(&config);
|
||||
let (_, _) = init_client(&state_path, &cache_path, &config)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Make POST request with provided client.
|
||||
pub fn tor_post<IN>(
|
||||
client: Arc<TorClient<TokioNativeTlsRuntime>>,
|
||||
tor_config: &TorConfig,
|
||||
input: &IN,
|
||||
url: &str,
|
||||
) -> Result<String, Error>
|
||||
/// Make POST request.
|
||||
pub fn tor_post<IN>(tor_config: &TorConfig, input: &IN, url: &str) -> Result<String, Error>
|
||||
where
|
||||
IN: Serialize,
|
||||
{
|
||||
@@ -183,6 +233,8 @@ where
|
||||
}
|
||||
.to_string();
|
||||
let timeout = tor_config.request_timeout();
|
||||
let (state_path, cache_path) = state_cache_paths(&tor_config);
|
||||
let (client, _) = init_client(&state_path, &cache_path, tor_config)?;
|
||||
let res: Result<String, Error> = thread::spawn(move || {
|
||||
let c = client.clone();
|
||||
client.runtime().block_on(async move {
|
||||
@@ -242,8 +294,7 @@ where
|
||||
fn init_client(
|
||||
state_path: &PathBuf,
|
||||
cache_path: &PathBuf,
|
||||
tor_config: TorConfig,
|
||||
reuse_client: bool,
|
||||
tor_config: &TorConfig,
|
||||
) -> Result<(Arc<TorClient<TokioNativeTlsRuntime>>, TorClientConfig), Error> {
|
||||
let mut builder = TorClientConfigBuilder::from_directories(&state_path, cache_path);
|
||||
builder.address_filter().allow_onion_addrs(true);
|
||||
@@ -283,31 +334,23 @@ fn init_client(
|
||||
.build()
|
||||
.map_err(|e| Error::TorConfig(format!("{:?}", e)))?;
|
||||
|
||||
if reuse_client {
|
||||
// Return existing client if config was not changed.
|
||||
let mut cached_client_config = ARTI_CLIENT_CONFIG.lock().unwrap();
|
||||
if let Some((client, c)) = cached_client_config.as_ref() {
|
||||
if c == &config {
|
||||
debug!("Reusing Arti Tor client from global state.");
|
||||
return Ok((client.clone(), c.clone()));
|
||||
} else {
|
||||
debug!("Tor config changed, rebuild client.");
|
||||
*cached_client_config = None;
|
||||
}
|
||||
// Return existing client if config was not changed.
|
||||
let mut cached_client_config = ARTI_CLIENT_CONFIG.lock().unwrap();
|
||||
if let Some((client, c)) = cached_client_config.as_ref() {
|
||||
if c == &config {
|
||||
debug!("Reusing Arti Tor client from global state.");
|
||||
return Ok((client.clone(), c.clone()));
|
||||
} else {
|
||||
debug!("Tor config changed, rebuild client.");
|
||||
*cached_client_config = None;
|
||||
}
|
||||
let res = launch_client(config.clone(), &tor_config);
|
||||
return match res {
|
||||
Ok(client) => {
|
||||
cached_client_config.replace((client.clone(), config.clone()));
|
||||
Ok((client, config))
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
}
|
||||
|
||||
let res = launch_client(config.clone(), &tor_config);
|
||||
match res {
|
||||
Ok(client) => Ok((client, config)),
|
||||
Ok(client) => {
|
||||
cached_client_config.replace((client.clone(), config.clone()));
|
||||
Ok((client, config))
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
@@ -355,18 +398,8 @@ fn launch_client(
|
||||
res
|
||||
}
|
||||
|
||||
/// Launch Onion service proxy.
|
||||
async fn run_service_proxy<S>(
|
||||
client: Arc<TorClient<TokioNativeTlsRuntime>>,
|
||||
addr: SocketAddr,
|
||||
request: &mut S,
|
||||
nickname: HsNickname,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
S: futures::Stream<Item = tor_hsservice::RendRequest> + Unpin + Send + 'static,
|
||||
{
|
||||
let runtime = client.runtime().clone();
|
||||
|
||||
/// Create Onion service proxy.
|
||||
fn create_service_proxy(addr: SocketAddr) -> Result<Arc<OnionServiceReverseProxy>, Error> {
|
||||
// Setup proxy to forward request from Tor address to local address.
|
||||
let proxy_rule = ProxyRule::new(
|
||||
ProxyPattern::one_port(80).map_err(|e| Error::TorConfig(format!("{}", e)))?,
|
||||
@@ -378,12 +411,24 @@ where
|
||||
.build()
|
||||
.map_err(|e| Error::TorConfig(format!("{}", e)))?;
|
||||
let proxy = OnionServiceReverseProxy::new(proxy_cfg);
|
||||
Ok(proxy)
|
||||
}
|
||||
|
||||
// Start proxy for launched service.
|
||||
/// Launch service proxy.
|
||||
async fn run_service_proxy<S>(
|
||||
proxy: Arc<OnionServiceReverseProxy>,
|
||||
request: &mut S,
|
||||
nickname: HsNickname,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
S: futures::Stream<Item = tor_hsservice::RendRequest> + Unpin + Send + 'static,
|
||||
{
|
||||
let runtime = runtime()?;
|
||||
proxy
|
||||
.handle_requests(runtime, nickname, request)
|
||||
.await
|
||||
.map_err(|e| Error::TorProcess(format!("{:?}", e)))
|
||||
.map_err(|e| Error::TorProcess(format!("{:?}", e)))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Save Onion service key to keystore.
|
||||
|
||||
@@ -17,28 +17,3 @@ pub mod bridge;
|
||||
pub mod config;
|
||||
pub mod process;
|
||||
pub mod proxy;
|
||||
|
||||
/// Running Tor instance control.
|
||||
pub struct Tor {
|
||||
/// External to process control.
|
||||
pub process: Option<process::TorProcess>,
|
||||
/// Integrated service.
|
||||
pub service: Option<std::sync::Arc<tor_hsservice::RunningOnionService>>,
|
||||
/// Integrated client.
|
||||
pub client:
|
||||
Option<std::sync::Arc<arti_client::TorClient<tor_rtcompat::tokio::TokioNativeTlsRuntime>>>,
|
||||
}
|
||||
|
||||
/// Arti client runtime wrapper.
|
||||
#[derive(Clone)]
|
||||
pub struct ArtiRuntimeWrapper {
|
||||
pub runtime: tor_rtcompat::tokio::TokioNativeTlsRuntime,
|
||||
}
|
||||
|
||||
impl ArtiRuntimeWrapper {
|
||||
fn create() -> Result<ArtiRuntimeWrapper, std::io::Error> {
|
||||
Ok(Self {
|
||||
runtime: tor_rtcompat::tokio::TokioNativeTlsRuntime::create()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user