Skip to content

Commit

Permalink
wip
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 5, 2024
1 parent 10b60c5 commit fe5d648
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 20 deletions.
24 changes: 8 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
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 @@ -17,6 +17,7 @@ pub mod peer_registry;
pub mod peer_store;
mod protocols;
mod services;
mod proxy;

#[cfg(test)]
mod tests;
Expand Down
11 changes: 7 additions & 4 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,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(
Expand Down Expand Up @@ -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));
}
}
}
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));
}
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 @@ -101,6 +101,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 fe5d648

Please sign in to comment.