Skip to content

Commit

Permalink
TcpSocketConfig bind_fn should take aware of SocketContext
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Dec 24, 2024
1 parent 9f7a3de commit f9f0733
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 250 deletions.
250 changes: 18 additions & 232 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ serde_json = "1.0"
bloom-filters = "0.1"
ckb-spawn = { path = "../util/spawn", version = "= 0.121.0-pre" }
bitflags = "1.0"
p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle", package = "tentacle", default-features = false }
p2p = { git = "https://github.com/eval-exec/tentacle", branch="exec/proxy-and-onion", package = "tentacle", default-features = false }
url = "2.5.4"

[target.'cfg(not(target_family = "wasm"))'.dependencies]
p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle", package = "tentacle", default-features = false, features = [
p2p = { git = "https://github.com/eval-exec/tentacle", branch="exec/proxy-and-onion", package = "tentacle", default-features = false, features = [
"upnp",
"parking_lot",
"openssl-vendored",
Expand All @@ -48,7 +49,7 @@ p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle",
socket2 = "0.5"

[target.'cfg(target_family = "wasm")'.dependencies]
p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle", package = "tentacle", default-features = false, features = [
p2p = { git = "https://github.com/eval-exec/tentacle", branch="exec/proxy-and-onion", package = "tentacle", default-features = false, features = [
"wasm-timer",
] }
idb = "0.6"
Expand Down
2 changes: 2 additions & 0 deletions network/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum Error {
Dial(String),
/// Peer store error
PeerStore(PeerStoreError),
/// Config error
Config(String),
}

/// Error from tentacle
Expand Down
1 change: 1 addition & 0 deletions network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod peer;
pub mod peer_registry;
pub mod peer_store;
mod protocols;
mod proxy;
mod services;

#[cfg(test)]
Expand Down
42 changes: 29 additions & 13 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::services::{
dump_peer_store::DumpPeerStoreService, outbound_peer::OutboundPeerService,
protocol_type_checker::ProtocolTypeCheckerService,
};
use crate::{Behaviour, CKBProtocol, Peer, PeerIndex, ProtocolId, ServiceControl};
use crate::{proxy, Behaviour, CKBProtocol, Peer, PeerIndex, ProtocolId, ServiceControl};
use ckb_app_config::{default_support_all_protocols, NetworkConfig, SupportProtocol};
use ckb_logger::{debug, error, info, trace, warn};
use ckb_spawn::Spawn;
Expand Down Expand Up @@ -122,6 +122,12 @@ impl NetworkState {
let peer_store = Mutex::new(PeerStore::load_from_dir_or_default(
config.peer_store_path(),
));
info!("Loaded the peer store.");
if config.proxy_config.enable {
proxy::check_proxy_url(&config.proxy_config.proxy_url)
.map_err(|reason| Error::Config(reason))?;
}

let bootnodes = config.bootnodes();

let peer_registry = PeerRegistry::new(
Expand Down Expand Up @@ -992,6 +998,15 @@ impl NetworkService {
if init.is_ready() {
break;
}
let proxy_config_enable = config.proxy_config.enable;

if proxy_config_enable {
let proxy_config = ProxyConfig {
proxy_url: config.proxy_config.proxy_url.clone(),
};
service_builder = service_builder.tcp_proxy_config(Some(proxy_config));
}

match find_type(multi_addr) {
TransportType::Tcp => {
// only bind once
Expand All @@ -1000,7 +1015,7 @@ impl NetworkService {
}
if let Some(addr) = multiaddr_to_socketaddr(multi_addr) {
let domain = socket2::Domain::for_address(addr);
let bind_fn = move |socket: p2p::service::TcpSocket| {
let bind_fn = move |socket: p2p::service::TcpSocket, context: p2p::service::TransformerContext| {
let socket_ref = socket2::SockRef::from(&socket);
#[cfg(all(
unix,
Expand All @@ -1010,20 +1025,14 @@ impl NetworkService {
socket_ref.set_reuse_port(true)?;
socket_ref.set_reuse_address(true)?;
if socket_ref.domain()? == domain {
socket_ref.bind(&addr.into())?;
if !(proxy_config_enable && matches!(context.state, p2p::service::SocketState::Dial)) {
socket_ref.bind(&addr.into())?;
}
}
Ok(socket)
};
init.transform(TransportType::Tcp);
service_builder = service_builder.tcp_config(bind_fn);

if config.proxy_config.enable {
let proxy_config = Some(ProxyConfig {
proxy_url: config.proxy_config.proxy_url.clone(),
});
service_builder =
service_builder.tcp_proxy_config(proxy_config);
}
}
}
TransportType::Ws => {
Expand All @@ -1033,7 +1042,7 @@ impl NetworkService {
}
if let Some(addr) = multiaddr_to_socketaddr(multi_addr) {
let domain = socket2::Domain::for_address(addr);
let bind_fn = move |socket: p2p::service::TcpSocket| {
let bind_fn = move |socket: p2p::service::TcpSocket, context: p2p::service::TransformerContext| {
let socket_ref = socket2::SockRef::from(&socket);
#[cfg(all(
unix,
Expand All @@ -1043,7 +1052,14 @@ impl NetworkService {
socket_ref.set_reuse_port(true)?;
socket_ref.set_reuse_address(true)?;
if socket_ref.domain()? == domain {
socket_ref.bind(&addr.into())?;
if !(proxy_config_enable
&& matches!(
context.state,
p2p::service::SocketState::Dial
))
{
socket_ref.bind(&addr.into())?;
}
}
Ok(socket)
};
Expand Down
28 changes: 28 additions & 0 deletions network/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub(crate) fn check_proxy_url(proxy_url: &str) -> Result<(), String> {
let parsed_url = url::Url::parse(proxy_url).map_err(|e| e.to_string())?;
if parsed_url.host_str().is_none() {
return Err(format!("missing host in proxy url: {}", proxy_url));
}
let scheme = parsed_url.scheme();
if scheme.ne("socks5") {
return Err(format!("CKB doesn't support proxy scheme: {}", scheme));
}
Ok(())
}

#[test]
fn parse_socks5_url() {
let result = url::Url::parse("socks5://username:password@localhost:1080");
assert!(result.is_ok());
let parsed_url = result.unwrap();
dbg!(&parsed_url);
assert_eq!(parsed_url.scheme(), "socks5");
// username
assert_eq!(parsed_url.username(), "username");
// password
assert_eq!(parsed_url.password(), Some("password"));
// host
assert_eq!(parsed_url.host_str(), Some("localhost"));
// port
assert_eq!(parsed_url.port(), Some(1080));
}
4 changes: 2 additions & 2 deletions util/app-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ ckb-pow = { path = "../../pow", version = "= 0.121.0-pre" }
ckb-resource = { path = "../../resource", version = "= 0.121.0-pre" }
ckb-build-info = { path = "../build-info", version = "= 0.121.0-pre" }
ckb-types = { path = "../types", version = "= 0.121.0-pre" }
secio = { path="/home/exec/Projects/github.com/nervosnetwork/tentacle/secio", package = "tentacle-secio" }
multiaddr = { path="/home/exec/Projects/github.com/nervosnetwork/tentacle/multiaddr", package = "tentacle-multiaddr" }
secio = { git = "https://github.com/eval-exec/tentacle.git", branch="exec/proxy-and-onion", package = "tentacle-secio" }
multiaddr = { git = "https://github.com/eval-exec/tentacle.git", branch="exec/proxy-and-onion", package = "tentacle-multiaddr" }
rand = "0.8"
sentry = { version = "0.34.0", optional = true }
ckb-systemtime = { path = "../systemtime", version = "= 0.121.0-pre" }
Expand Down
1 change: 1 addition & 0 deletions util/app-config/src/configs/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct Config {
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct ProxyConfig {
pub enable: bool,
// like: socks5://username:[email protected]:1080
pub proxy_url: String,
}

Expand Down

0 comments on commit f9f0733

Please sign in to comment.