-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eval EXEC <[email protected]>
- Loading branch information
Showing
11 changed files
with
516 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod tor; | ||
|
||
pub use tor::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::utils::find_available_port; | ||
use crate::{Node, Spec}; | ||
use ckb_channel::{Receiver, Sender}; | ||
use ckb_logger::{error, info, warn}; | ||
use std::process::Child; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug)] | ||
struct TorServer { | ||
tor_command_path: String, | ||
socks_port: u16, | ||
control_port: u16, | ||
} | ||
|
||
impl TorServer { | ||
pub fn new() -> Self { | ||
TorServer { | ||
tor_command_path: std::option_env!("TOR_COMMAND_PATH") | ||
.unwrap_or("tor") | ||
.to_string(), | ||
socks_port: find_available_port(), | ||
control_port: find_available_port(), | ||
} | ||
} | ||
|
||
fn build_tor_args(&self) -> Vec<String> { | ||
vec![ | ||
"--SocksPort".to_string(), | ||
self.socks_port.to_string(), | ||
"--ControlPort".to_string(), | ||
self.control_port.to_string(), | ||
] | ||
} | ||
|
||
fn tor_start(&self) -> Child { | ||
let mut cmd = std::process::Command::new(&self.tor_command_path); | ||
let cmd = cmd.args(self.build_tor_args().clone()); | ||
let child = cmd.spawn().unwrap(); | ||
info!("tor started:({:?}) ; pid: {}", &self, child.id()); | ||
child | ||
} | ||
} | ||
|
||
// create a sender and receiver for tor_server signal | ||
static TOR_SERVER_PROCESS: std::sync::LazyLock<std::sync::Mutex<Option<Child>>> = | ||
std::sync::LazyLock::new(|| std::sync::Mutex::new(None)); | ||
|
||
struct TorServer_Guard {} | ||
|
||
impl Drop for TorServer_Guard { | ||
fn drop(&mut self) { | ||
let mut child = TOR_SERVER_PROCESS.lock().unwrap(); | ||
let child = child.as_mut().unwrap(); | ||
info!("killing tor server... {}", child.id()); | ||
match child.kill() { | ||
Ok(_) => { | ||
info!("tor server exit success"); | ||
} | ||
Err(e) => { | ||
error!("tor server exit failed: {:?}", e); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
pub struct TorService; | ||
|
||
impl Spec for TorService { | ||
crate::setup!(num_nodes: 1); | ||
|
||
fn before_run(&self) -> Vec<Node> { | ||
let tor_server = TorServer::new(); | ||
let tor_server_process = tor_server.tor_start(); | ||
*TOR_SERVER_PROCESS.lock().unwrap() = Some(tor_server_process); | ||
|
||
std::thread::sleep(std::time::Duration::from_secs(5)); | ||
|
||
let mut node0 = Node::new(self.name(), "node0"); | ||
node0.modify_app_config(|config: &mut ckb_app_config::CKBAppConfig| { | ||
config.network.onion.listen_on_onion = true; | ||
config.network.onion.onion_server = | ||
Some(format!("127.0.0.1:{}", tor_server.socks_port)); | ||
config.network.onion.tor_controller = format!("127.0.0.1:{}", tor_server.control_port); | ||
}); | ||
|
||
node0.start(); | ||
|
||
vec![node0] | ||
} | ||
|
||
fn run(&self, nodes: &mut Vec<Node>) { | ||
// when _tor_server_guard dropped, the tor server will be killed by Drop | ||
let _tor_server_guard = TorServer_Guard {}; | ||
|
||
let node = &nodes[0]; | ||
|
||
let rpc_client = node.rpc_client(); | ||
let node_info = rpc_client.local_node_info(); | ||
|
||
let contains_onion_addr = node_info.addresses.iter().any(|addr| { | ||
// check contains the onion address | ||
info!("addr: {:?}", addr.address); | ||
addr.address.contains("/onion3") | ||
}); | ||
assert!(contains_onion_addr, "node should contains onion address"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,9 +111,32 @@ pub struct ProxyConfig { | |
|
||
/// Onion related config options | ||
#[derive(Clone, Debug, Serialize, Deserialize, Default)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct OnionConfig { | ||
// like: socks5://username:[email protected]:1080 | ||
pub onion_url: Option<String>, | ||
// Automatically create Tor onion service, default: true | ||
#[serde(default = "default_listen_on_onion")] | ||
pub listen_on_onion: bool, | ||
// onion service target, if CKB's p2p listen address not on default 127.0.0.1:8115, you should set this | ||
pub onion_service_target: Option<String>, | ||
// Tor server url: like: 127.0.0.1:9050 | ||
pub onion_server: Option<String>, | ||
// path to store onion private key, default is ./data/network/onion/onion_private_key | ||
pub onion_private_key_path: Option<String>, | ||
// tor controllr url, example: 127.0.0.1:9050 | ||
#[serde(default = "default_tor_controller")] | ||
pub tor_controller: String, | ||
// tor controller hashed password | ||
pub tor_password: Option<String>, | ||
} | ||
|
||
/// By default, allow ckb to listen on onion address | ||
const fn default_listen_on_onion() -> bool { | ||
true | ||
} | ||
|
||
/// By default, use tor controller on "127.0.0.1:9051" | ||
fn default_tor_controller() -> String { | ||
"127.0.0.1:9051".to_string() | ||
} | ||
|
||
/// Chain synchronization config options. | ||
|
@@ -271,6 +294,13 @@ impl Config { | |
path | ||
} | ||
|
||
/// Gets the onion network private key path. | ||
pub fn onion_private_key_path(&self) -> PathBuf { | ||
let mut path = self.path.clone(); | ||
path.push("onion_private_key"); | ||
path | ||
} | ||
|
||
/// Gets the peer store path. | ||
pub fn peer_store_path(&self) -> PathBuf { | ||
let mut path = self.path.clone(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.