Skip to content

Commit e55f631

Browse files
committed
wip
Signed-off-by: Eval EXEC <[email protected]>
1 parent 10b60c5 commit e55f631

File tree

7 files changed

+51
-20
lines changed

7 files changed

+51
-20
lines changed

Cargo.lock

+8-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

network/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bloom-filters = "0.1"
3535
ckb-spawn = { path = "../util/spawn", version = "= 0.120.0-pre" }
3636
bitflags = "1.0"
3737
p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle", package = "tentacle", default-features = false }
38+
url = "2.5.4"
3839

3940
[target.'cfg(not(target_family = "wasm"))'.dependencies]
4041
p2p = { path = "/home/exec/Projects/github.com/nervosnetwork/tentacle/tentacle", package = "tentacle", default-features = false, features = [

network/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub enum Error {
2727
Dial(String),
2828
/// Peer store error
2929
PeerStore(PeerStoreError),
30+
/// Config error
31+
Config(String),
3032
}
3133

3234
/// Error from tentacle

network/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod peer_registry;
1717
pub mod peer_store;
1818
mod protocols;
1919
mod services;
20+
mod proxy;
2021

2122
#[cfg(test)]
2223
mod tests;

network/src/network.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::services::{
1919
dump_peer_store::DumpPeerStoreService, outbound_peer::OutboundPeerService,
2020
protocol_type_checker::ProtocolTypeCheckerService,
2121
};
22-
use crate::{Behaviour, CKBProtocol, Peer, PeerIndex, ProtocolId, ServiceControl};
22+
use crate::{proxy, Behaviour, CKBProtocol, Peer, PeerIndex, ProtocolId, ServiceControl};
2323
use ckb_app_config::{default_support_all_protocols, NetworkConfig, SupportProtocol};
2424
use ckb_logger::{debug, error, info, trace, warn};
2525
use ckb_spawn::Spawn;
@@ -122,6 +122,12 @@ impl NetworkState {
122122
let peer_store = Mutex::new(PeerStore::load_from_dir_or_default(
123123
config.peer_store_path(),
124124
));
125+
info!("Loaded the peer store.");
126+
if config.proxy_config.enable {
127+
proxy::check_proxy_url(&config.proxy_config.proxy_url)
128+
.map_err(|reason| Error::Config(reason))?;
129+
}
130+
125131
let bootnodes = config.bootnodes();
126132

127133
let peer_registry = PeerRegistry::new(
@@ -1018,11 +1024,11 @@ impl NetworkService {
10181024
service_builder = service_builder.tcp_config(bind_fn);
10191025

10201026
if config.proxy_config.enable {
1021-
let proxy_config = Some(ProxyConfig {
1027+
let proxy_config = ProxyConfig {
10221028
proxy_url: config.proxy_config.proxy_url.clone(),
1023-
});
1029+
};
10241030
service_builder =
1025-
service_builder.tcp_proxy_config(proxy_config);
1031+
service_builder.tcp_proxy_config(Some(proxy_config));
10261032
}
10271033
}
10281034
}

network/src/proxy.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub(crate) fn check_proxy_url(proxy_url: &str) -> Result<(), String> {
2+
let parsed_url = url::Url::parse(proxy_url).map_err(|e| e.to_string())?;
3+
if parsed_url.host_str().is_none() {
4+
return Err( format!("missing host in proxy url: {}", proxy_url) );
5+
}
6+
let scheme = parsed_url.scheme();
7+
if scheme.ne("socks5") {
8+
return Err(format!("CKB doesn't support proxy scheme: {}", scheme));
9+
}
10+
Ok(())
11+
}
12+
13+
#[test]
14+
fn parse_socks5_url() {
15+
let result = url::Url::parse("socks5://username:password@localhost:1080");
16+
assert!(result.is_ok());
17+
let parsed_url = result.unwrap();
18+
dbg!(&parsed_url);
19+
assert_eq!(parsed_url.scheme(), "socks5");
20+
// username
21+
assert_eq!(parsed_url.username(), "username");
22+
// password
23+
assert_eq!(parsed_url.password(), Some("password"));
24+
// host
25+
assert_eq!(parsed_url.host_str(), Some("localhost"));
26+
// port
27+
assert_eq!(parsed_url.port(), Some(1080));
28+
}

util/app-config/src/configs/network.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub struct Config {
101101
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
102102
pub struct ProxyConfig {
103103
pub enable: bool,
104+
// like: socks5://username:[email protected]:1080
104105
pub proxy_url: String,
105106
}
106107

0 commit comments

Comments
 (0)