-
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
15 changed files
with
694 additions
and
11 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
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,46 @@ | ||
mod tor_basic; | ||
mod tor_connect; | ||
|
||
use std::process::Child; | ||
|
||
use ckb_logger::info; | ||
pub use tor_basic::*; | ||
pub use tor_connect::*; | ||
|
||
use crate::utils::find_available_port; | ||
|
||
#[derive(Clone, 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 | ||
} | ||
} |
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,86 @@ | ||
use crate::utils::find_available_port; | ||
use crate::{Node, Spec}; | ||
use ckb_logger::{error, info}; | ||
use std::process::Child; | ||
|
||
use super::TorServer; | ||
|
||
// 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)); | ||
|
||
static TOR_SERVER: std::sync::OnceLock<TorServer> = std::sync::OnceLock::new(); | ||
|
||
struct TorServerGuard {} | ||
|
||
impl Drop for TorServerGuard { | ||
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 TorServiceContainsPublicAddr; | ||
|
||
impl Spec for TorServiceContainsPublicAddr { | ||
crate::setup!(num_nodes: 1); | ||
|
||
fn before_run(&self) -> Vec<Node> { | ||
let tor_server = TorServer::new(); | ||
|
||
TOR_SERVER.set(tor_server.clone()); | ||
|
||
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 = TorServerGuard {}; | ||
|
||
let node = &nodes[0]; | ||
|
||
let rpc_client = node.rpc_client(); | ||
let node_info = rpc_client.local_node_info(); | ||
|
||
let node_onion_addrs: Vec<_> = node_info | ||
.addresses | ||
.iter() | ||
.filter(|addr| { | ||
// check contains the onion address | ||
info!("addr: {:?}", addr.address); | ||
addr.address.contains("/onion3") | ||
}) | ||
.collect(); | ||
assert!( | ||
!node_onion_addrs.is_empty(), | ||
"node should contains onion address" | ||
); | ||
|
||
let node_onion_p2p_addr: String = node_onion_addrs.first().unwrap().address.clone(); | ||
info!("node_onion_p2p_addr: {}", node_onion_p2p_addr); | ||
} | ||
} |
Oops, something went wrong.