Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Dec 20, 2024
1 parent 9f7a3de commit cb72a28
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 241 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 @@ -17,6 +17,7 @@ pub mod peer_registry;
pub mod peer_store;
mod protocols;
mod services;
mod proxy;

#[cfg(test)]
mod tests;
Expand Down
14 changes: 10 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,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 @@ -1018,11 +1024,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));
}
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:password@127.0.0.1:1080
pub proxy_url: String,
}

Expand Down

0 comments on commit cb72a28

Please sign in to comment.