diff --git a/Cargo.lock b/Cargo.lock index d19039b51a..c6f23c7051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1363,6 +1363,7 @@ dependencies = [ "tokio", "tokio-util", "trust-dns-resolver", + "url", ] [[package]] @@ -3684,16 +3685,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "idna" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "idna" version = "1.0.3" @@ -5392,9 +5383,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" -version = "0.12.8" +version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f713147fbe92361e52392c73b8c9e48c04c6625bce969ef54dc901e58e042a7b" +checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ "base64 0.22.1", "bytes", @@ -6568,7 +6559,7 @@ dependencies = [ [[package]] name = "tentacle" -version = "0.6.5" +version = "0.6.6" dependencies = [ "async-trait", "bytes", @@ -6593,6 +6584,7 @@ dependencies = [ "tokio-tungstenite", "tokio-util", "tokio-yamux", + "url", "wasm-bindgen", "wasm-bindgen-futures", "winapi", @@ -7340,12 +7332,12 @@ dependencies = [ [[package]] name = "url" -version = "2.5.2" +version = "2.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" dependencies = [ "form_urlencoded", - "idna 0.5.0", + "idna 1.0.3", "percent-encoding", "serde", ] diff --git a/network/Cargo.toml b/network/Cargo.toml index c653ddf17e..861865177d 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -35,6 +35,7 @@ bloom-filters = "0.1" ckb-spawn = { path = "../util/spawn", version = "= 0.120.0-pre" } bitflags = "1.0" p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle", 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 = [ diff --git a/network/src/errors.rs b/network/src/errors.rs index 0e3bb8d496..4814159db0 100644 --- a/network/src/errors.rs +++ b/network/src/errors.rs @@ -27,6 +27,8 @@ pub enum Error { Dial(String), /// Peer store error PeerStore(PeerStoreError), + /// Config error + Config(String), } /// Error from tentacle diff --git a/network/src/lib.rs b/network/src/lib.rs index c672dab65a..57423034a9 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -17,6 +17,7 @@ pub mod peer_registry; pub mod peer_store; mod protocols; mod services; +mod proxy; #[cfg(test)] mod tests; diff --git a/network/src/network.rs b/network/src/network.rs index 3215965daf..78c0ff3505 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -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; @@ -122,6 +122,9 @@ impl NetworkState { let peer_store = Mutex::new(PeerStore::load_from_dir_or_default( config.peer_store_path(), )); + info!("Loaded the peer store."); + proxy::check_proxy_url(&config.proxy_config.proxy_url).map_err(|reason| Error::Config(reason))?; + let bootnodes = config.bootnodes(); let peer_registry = PeerRegistry::new( @@ -1018,11 +1021,11 @@ impl NetworkService { service_builder = service_builder.tcp_config(bind_fn); if config.proxy_config.enable { - let proxy_config = Some(ProxyConfig { + let proxy_config = ProxyConfig { proxy_url: config.proxy_config.proxy_url.clone(), - }); + }; service_builder = - service_builder.tcp_proxy_config(proxy_config); + service_builder.tcp_proxy_config(Some(proxy_config)); } } } diff --git a/network/src/proxy.rs b/network/src/proxy.rs new file mode 100644 index 0000000000..ca1e4fd014 --- /dev/null +++ b/network/src/proxy.rs @@ -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)); +} diff --git a/util/app-config/src/configs/network.rs b/util/app-config/src/configs/network.rs index bcfcd7048a..060e357309 100644 --- a/util/app-config/src/configs/network.rs +++ b/util/app-config/src/configs/network.rs @@ -101,6 +101,7 @@ pub struct Config { #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct ProxyConfig { pub enable: bool, + // like: socks5://username:password@127.0.0.1:1080 pub proxy_url: String, }