Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
whisper/cli: add p2p port and ip parameters (#10057)
Browse files Browse the repository at this point in the history
* whisper/cli: add p2p port and ip parameters

This is so that those params don't change randomly and are in sync with the URL that
is displayed.

* feedback: Result instead of panic

Co-Authored-By: gballet <gballet@gmail.com>

* feedback: Map error in port conversion

Co-Authored-By: gballet <gballet@gmail.com>

* whisper/cli: User can specify enode private key

So that the enode doesn't change at every run.

* whipser/cli: finish integrating review feedback.

* Accomodate error API change

* Update rustc-hex version in whisper/cli/Cargo.toml

Co-Authored-By: gballet <gballet@gmail.com>

* Update README with new whisper cli options

* Fix typo in error message

Co-Authored-By: gballet <gballet@gmail.com>

* Fix Cargo.lock and build issue after lib version upgrade

* Fix another typo

Co-Authored-By: gballet <gballet@gmail.com>
  • Loading branch information
gballet authored and ordian committed Mar 21, 2019
1 parent 9519493 commit b700ff3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions whisper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ Usage:
Options:
--whisper-pool-size SIZE Specify Whisper pool size [default: 10].
-p, --port PORT Specify which RPC port to use [default: 8545].
-a, --address ADDRESS Specify which address to use [default: 127.0.0.1].
-p, --port PORT Specify which P2P port to use [default: random].
-a, --address ADDRESS Specify which P2P address to use [default: 127.0.0.1].
-s, --secret KEYFILE Specify which file contains the key to generate the enode.
-P, --rpc-port PORT Specify which RPC port to use [default: 8545].
-A, --rpc-address ADDRESS Specify which RPC address to use [default: 127.0.0.1].
-l, --log LEVEL Specify the logging level. Must conform to the same format as RUST_LOG [default: Error].
-h, --help Display this message and exit.
```
Expand Down
2 changes: 2 additions & 0 deletions whisper/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ panic_hook = { path = "../../util/panic-hook" }
parity-whisper = { path = "../" }
serde = "1.0"
serde_derive = "1.0"
ethkey = { path = "../../accounts/ethkey" }
rustc-hex = "2.0"

[[bin]]
name = "whisper"
Expand Down
59 changes: 54 additions & 5 deletions whisper/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extern crate serde;
extern crate jsonrpc_core;
extern crate jsonrpc_pubsub;
extern crate jsonrpc_http_server;
extern crate ethkey;
extern crate rustc_hex;

#[macro_use]
extern crate log as rlog;
Expand All @@ -45,6 +47,10 @@ use std::{fmt, io, process, env, sync::Arc};
use jsonrpc_core::{Metadata, MetaIoHandler};
use jsonrpc_pubsub::{PubSubMetadata, Session};
use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation};
use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr};
use std::str::FromStr;
use ethkey::Secret;
use rustc_hex::FromHex;

const POOL_UNIT: usize = 1024 * 1024;
const USAGE: &'static str = r#"
Expand All @@ -57,8 +63,11 @@ Usage:
Options:
--whisper-pool-size SIZE Specify Whisper pool size [default: 10].
-p, --port PORT Specify which RPC port to use [default: 8545].
-a, --address ADDRESS Specify which address to use [default: 127.0.0.1].
-p, --port PORT Specify which P2P port to use [default: random].
-a, --address ADDRESS Specify which P2P address to use [default: 127.0.0.1].
-s, --secret KEYFILE Specify which file contains the key to generate the enode.
-P, --rpc-port PORT Specify which RPC port to use [default: 8545].
-A, --rpc-address ADDRESS Specify which RPC address to use [default: 127.0.0.1].
-l, --log LEVEL Specify the logging level. Must conform to the same format as RUST_LOG [default: Error].
-h, --help Display this message and exit.
"#;
Expand All @@ -79,7 +88,10 @@ struct Args {
flag_whisper_pool_size: usize,
flag_port: String,
flag_address: String,
flag_rpc_port: String,
flag_rpc_address: String,
flag_log: String,
flag_secret: String,
}

struct WhisperPoolHandle {
Expand Down Expand Up @@ -131,6 +143,8 @@ enum Error {
JsonRpc(jsonrpc_core::Error),
Network(net::Error),
SockAddr(std::net::AddrParseError),
FromHex(rustc_hex::FromHexError),
ParseInt(std::num::ParseIntError),
}

impl From<std::net::AddrParseError> for Error {
Expand Down Expand Up @@ -163,6 +177,18 @@ impl From<jsonrpc_core::Error> for Error {
}
}

impl From<rustc_hex::FromHexError> for Error {
fn from(err: rustc_hex::FromHexError) -> Self {
Error::FromHex(err)
}
}

impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Error::ParseInt(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
Expand All @@ -171,6 +197,8 @@ impl fmt::Display for Error {
Error::Io(ref e) => write!(f, "{}", e),
Error::JsonRpc(ref e) => write!(f, "{:?}", e),
Error::Network(ref e) => write!(f, "{}", e),
Error::ParseInt(ref e) => write!(f, "Invalid port: {}", e),
Error::FromHex(ref e) => write!(f, "Error deciphering key: {}", e),
}
}
}
Expand All @@ -196,7 +224,7 @@ fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>,
// Parse arguments
let args: Args = Docopt::new(USAGE).and_then(|d| d.argv(command).deserialize())?;
let pool_size = args.flag_whisper_pool_size * POOL_UNIT;
let url = format!("{}:{}", args.flag_address, args.flag_port);
let rpc_url = format!("{}:{}", args.flag_rpc_address, args.flag_rpc_port);

initialize_logger(args.flag_log);
info!(target: "whisper-cli", "start");
Expand All @@ -207,8 +235,29 @@ fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>,
// Whisper protocol network handler
let whisper_network_handler = Arc::new(whisper::net::Network::new(pool_size, manager.clone()));

let network_config = {
let mut cfg = net::NetworkConfiguration::new();
let port = match args.flag_port.as_str() {
"random" => 0 as u16,
port => port.parse::<u16>()?,

};
let addr = Ipv4Addr::from_str(&args.flag_address[..])?;
cfg.listen_address = Some(SocketAddr::V4(SocketAddrV4::new(addr, port)));
cfg.use_secret = match args.flag_secret.as_str() {
"" => None,
fname => {
let key_text = std::fs::read_to_string(fname)?;
let key : Vec<u8> = FromHex::from_hex(key_text.as_str())?;
Secret::from_slice(key.as_slice())
}
};
cfg.nat_enabled = false;
cfg
};

// Create network service
let network = devp2p::NetworkService::new(net::NetworkConfiguration::new_local(), None)?;
let network = devp2p::NetworkService::new(network_config, None)?;

// Start network service
network.start().map_err(|(err, _)| err)?;
Expand All @@ -233,7 +282,7 @@ fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>,

let server = jsonrpc_http_server::ServerBuilder::new(io)
.cors(DomainsValidation::AllowOnly(vec![AccessControlAllowOrigin::Null]))
.start_http(&url.parse()?)?;
.start_http(&rpc_url.parse()?)?;

server.wait();

Expand Down

0 comments on commit b700ff3

Please sign in to comment.