Use host as ws/dapps url if present.

This commit is contained in:
Tomasz Drwięga 2017-09-21 14:52:44 +02:00
parent 684b142711
commit 965e8ae74e
No known key found for this signature in database
GPG Key ID: D066F497E62CAF66
9 changed files with 89 additions and 62 deletions

View File

@ -1322,7 +1322,7 @@ mod tests {
origins: Some(vec!["chrome-extension://*".into(), "moz-extension://*".into()]), origins: Some(vec!["chrome-extension://*".into(), "moz-extension://*".into()]),
hosts: Some(vec![]), hosts: Some(vec![]),
signer_path: expected.into(), signer_path: expected.into(),
ui_address: Some(("127.0.0.1".to_owned(), 8180)), ui_address: Some("127.0.0.1:8180".into()),
support_token_api: true support_token_api: true
}, UiConfiguration { }, UiConfiguration {
enabled: true, enabled: true,

View File

@ -57,7 +57,7 @@ impl Default for Configuration {
} }
impl Configuration { impl Configuration {
pub fn address(&self, address: Option<(String, u16)>) -> Option<(String, u16)> { pub fn address(&self, address: Option<::parity_rpc::Host>) -> Option<::parity_rpc::Host> {
match self.enabled { match self.enabled {
true => address, true => address,
false => None, false => None,

View File

@ -47,11 +47,8 @@ pub struct HttpConfiguration {
} }
impl HttpConfiguration { impl HttpConfiguration {
pub fn address(&self) -> Option<(String, u16)> { pub fn address(&self) -> Option<rpc::Host> {
match self.enabled { address(self.enabled, &self.interface, self.port, &self.hosts)
true => Some((self.interface.clone(), self.port)),
false => None,
}
} }
} }
@ -79,11 +76,18 @@ pub struct UiConfiguration {
} }
impl UiConfiguration { impl UiConfiguration {
pub fn address(&self) -> Option<(String, u16)> { pub fn address(&self) -> Option<rpc::Host> {
match self.enabled { address(self.enabled, &self.interface, self.port, &self.hosts)
true => Some((self.interface.clone(), self.port)), }
false => None,
} pub fn redirection_address(&self) -> Option<(String, u16)> {
self.address().map(|host| {
let mut it = host.split(':');
let hostname: Option<String> = it.next().map(|s| s.to_owned());
let port: Option<u16> = it.next().and_then(|s| s.parse().ok());
(hostname.unwrap_or_else(|| "localhost".into()), port.unwrap_or(8180))
})
} }
} }
@ -145,7 +149,7 @@ pub struct WsConfiguration {
pub hosts: Option<Vec<String>>, pub hosts: Option<Vec<String>>,
pub signer_path: PathBuf, pub signer_path: PathBuf,
pub support_token_api: bool, pub support_token_api: bool,
pub ui_address: Option<(String, u16)>, pub ui_address: Option<rpc::Host>,
} }
impl Default for WsConfiguration { impl Default for WsConfiguration {
@ -160,17 +164,25 @@ impl Default for WsConfiguration {
hosts: Some(Vec::new()), hosts: Some(Vec::new()),
signer_path: replace_home(&data_dir, "$BASE/signer").into(), signer_path: replace_home(&data_dir, "$BASE/signer").into(),
support_token_api: true, support_token_api: true,
ui_address: Some(("127.0.0.1".to_owned(), 8180)), ui_address: Some("127.0.0.1:8180".into()),
} }
} }
} }
impl WsConfiguration { impl WsConfiguration {
pub fn address(&self) -> Option<(String, u16)> { pub fn address(&self) -> Option<rpc::Host> {
match self.enabled { address(self.enabled, &self.interface, self.port, &self.hosts)
true => Some((self.interface.clone(), self.port)), }
false => None, }
}
fn address(enabled: bool, bind_iface: &str, bind_port: u16, hosts: &Option<Vec<String>>) -> Option<rpc::Host> {
if !enabled {
return None;
}
match *hosts {
Some(ref hosts) if !hosts.is_empty() => Some(hosts[0].clone().into()),
_ => Some(format!("{}:{}", bind_iface, bind_port).into()),
} }
} }
@ -190,17 +202,15 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
} }
let domain = DAPPS_DOMAIN; let domain = DAPPS_DOMAIN;
let ws_address = (conf.interface, conf.port); let url = format!("{}:{}", conf.interface, conf.port);
let url = format!("{}:{}", ws_address.0, ws_address.1);
let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?; let addr = url.parse().map_err(|_| format!("Invalid WebSockets listen host/port given: {}", url))?;
let pool = deps.pool.clone(); let full_handler = setup_apis(rpc_apis::ApiSet::SafeContext, deps);
let full_handler = setup_apis(rpc_apis::ApiSet::SafeContext, deps, pool.clone());
let handler = { let handler = {
let mut handler = MetaIoHandler::with_middleware(( let mut handler = MetaIoHandler::with_middleware((
rpc::WsDispatcher::new(full_handler), rpc::WsDispatcher::new(full_handler),
Middleware::new(deps.stats.clone(), deps.apis.activity_notifier(), pool) Middleware::new(deps.stats.clone(), deps.apis.activity_notifier(), deps.pool.clone())
)); ));
let apis = conf.apis.list_apis(); let apis = conf.apis.list_apis();
deps.apis.extend_with_set(&mut handler, &apis); deps.apis.extend_with_set(&mut handler, &apis);
@ -210,8 +220,8 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
let remote = deps.remote.clone(); let remote = deps.remote.clone();
let ui_address = conf.ui_address.clone(); let ui_address = conf.ui_address.clone();
let allowed_origins = into_domains(with_domain(conf.origins, domain, &[ui_address])); let allowed_origins = into_domains(with_domain(conf.origins, domain, &ui_address));
let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &[Some(ws_address)])); let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &Some(url.clone().into())));
let signer_path; let signer_path;
let path = match conf.support_token_api && conf.ui_address.is_some() { let path = match conf.support_token_api && conf.ui_address.is_some() {
@ -253,15 +263,13 @@ pub fn new_http<D: rpc_apis::Dependencies>(
} }
let domain = DAPPS_DOMAIN; let domain = DAPPS_DOMAIN;
let http_address = (conf.interface, conf.port); let url = format!("{}:{}", conf.interface, conf.port);
let url = format!("{}:{}", http_address.0, http_address.1);
let addr = url.parse().map_err(|_| format!("Invalid {} listen host/port given: {}", id, url))?; let addr = url.parse().map_err(|_| format!("Invalid {} listen host/port given: {}", id, url))?;
let pool = deps.pool.clone(); let handler = setup_apis(conf.apis, deps);
let handler = setup_apis(conf.apis, deps, pool);
let remote = deps.remote.clone(); let remote = deps.remote.clone();
let cors_domains = into_domains(conf.cors); let cors_domains = into_domains(conf.cors);
let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &[Some(http_address)])); let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &Some(url.clone().into())));
let start_result = rpc::start_http( let start_result = rpc::start_http(
&addr, &addr,
@ -296,8 +304,7 @@ pub fn new_ipc<D: rpc_apis::Dependencies>(
return Ok(None); return Ok(None);
} }
let pool = dependencies.pool.clone(); let handler = setup_apis(conf.apis, dependencies);
let handler = setup_apis(conf.apis, dependencies, pool);
let remote = dependencies.remote.clone(); let remote = dependencies.remote.clone();
match rpc::start_ipc(&conf.socket_addr, handler, remote, rpc::RpcExtractor) { match rpc::start_ipc(&conf.socket_addr, handler, remote, rpc::RpcExtractor) {
Ok(server) => Ok(Some(server)), Ok(server) => Ok(Some(server)),
@ -309,29 +316,46 @@ fn into_domains<T: From<String>>(items: Option<Vec<String>>) -> DomainsValidatio
items.map(|vals| vals.into_iter().map(T::from).collect()).into() items.map(|vals| vals.into_iter().map(T::from).collect()).into()
} }
fn with_domain(items: Option<Vec<String>>, domain: &str, addresses: &[Option<(String, u16)>]) -> Option<Vec<String>> { fn with_domain(items: Option<Vec<String>>, domain: &str, address: &Option<rpc::Host>) -> Option<Vec<String>> {
fn extract_port(s: &str) -> Option<u16> {
s.split(':').nth(1).and_then(|s| s.parse().ok())
}
items.map(move |items| { items.map(move |items| {
let mut items = items.into_iter().collect::<HashSet<_>>(); let mut items = items.into_iter().collect::<HashSet<_>>();
for address in addresses { if let Some(host) = address.clone() {
if let Some((host, port)) = address.clone() { items.insert(host.to_string());
items.insert(format!("{}:{}", host, port)); items.insert(host.replace("127.0.0.1", "localhost"));
items.insert(format!("{}:{}", host.replace("127.0.0.1", "localhost"), port)); items.insert(format!("http://*.{}", domain)); //proxypac
if let Some(port) = extract_port(&*host) {
items.insert(format!("http://*.{}:{}", domain, port)); items.insert(format!("http://*.{}:{}", domain, port));
items.insert(format!("http://*.{}", domain)); //proxypac
} }
} }
items.into_iter().collect() items.into_iter().collect()
}) })
} }
fn setup_apis<D>(apis: ApiSet, deps: &Dependencies<D>, pool: Option<CpuPool>) -> MetaIoHandler<Metadata, Middleware<D::Notifier>> fn setup_apis<D>(apis: ApiSet, deps: &Dependencies<D>) -> MetaIoHandler<Metadata, Middleware<D::Notifier>>
where D: rpc_apis::Dependencies where D: rpc_apis::Dependencies
{ {
let mut handler = MetaIoHandler::with_middleware( let mut handler = MetaIoHandler::with_middleware(
Middleware::new(deps.stats.clone(), deps.apis.activity_notifier(), pool) Middleware::new(deps.stats.clone(), deps.apis.activity_notifier(), deps.pool.clone())
); );
let apis = apis.list_apis(); let apis = apis.list_apis();
deps.apis.extend_with_set(&mut handler, &apis); deps.apis.extend_with_set(&mut handler, &apis);
handler handler
} }
#[cfg(test)]
mod tests {
use super::address;
#[test]
fn should_return_proper_address() {
assert_eq!(address(false, "localhost", 8180, &None), None);
assert_eq!(address(true, "localhost", 8180, &None), Some("localhost:8180".into()));
assert_eq!(address(true, "localhost", 8180, &Some(vec!["host:443".into()])), Some("host:443".into()));
assert_eq!(address(true, "localhost", 8180, &Some(vec!["host".into()])), Some("host".into()));
}
}

View File

@ -37,7 +37,7 @@ use node_health::NodeHealth;
use parity_reactor; use parity_reactor;
use parity_rpc::dispatch::{FullDispatcher, LightDispatcher}; use parity_rpc::dispatch::{FullDispatcher, LightDispatcher};
use parity_rpc::informant::{ActivityNotifier, ClientNotifier}; use parity_rpc::informant::{ActivityNotifier, ClientNotifier};
use parity_rpc::{Metadata, NetworkSettings}; use parity_rpc::{Metadata, NetworkSettings, Host};
use updater::Updater; use updater::Updater;
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
@ -222,8 +222,8 @@ pub struct FullDependencies {
pub health: NodeHealth, pub health: NodeHealth,
pub geth_compatibility: bool, pub geth_compatibility: bool,
pub dapps_service: Option<Arc<DappsService>>, pub dapps_service: Option<Arc<DappsService>>,
pub dapps_address: Option<(String, u16)>, pub dapps_address: Option<Host>,
pub ws_address: Option<(String, u16)>, pub ws_address: Option<Host>,
pub fetch: FetchClient, pub fetch: FetchClient,
pub remote: parity_reactor::Remote, pub remote: parity_reactor::Remote,
pub whisper_rpc: Option<::whisper::RpcFactory>, pub whisper_rpc: Option<::whisper::RpcFactory>,
@ -412,8 +412,8 @@ pub struct LightDependencies<T> {
pub cache: Arc<Mutex<LightDataCache>>, pub cache: Arc<Mutex<LightDataCache>>,
pub transaction_queue: Arc<RwLock<LightTransactionQueue>>, pub transaction_queue: Arc<RwLock<LightTransactionQueue>>,
pub dapps_service: Option<Arc<DappsService>>, pub dapps_service: Option<Arc<DappsService>>,
pub dapps_address: Option<(String, u16)>, pub dapps_address: Option<Host>,
pub ws_address: Option<(String, u16)>, pub ws_address: Option<Host>,
pub fetch: FetchClient, pub fetch: FetchClient,
pub geth_compatibility: bool, pub geth_compatibility: bool,
pub remote: parity_reactor::Remote, pub remote: parity_reactor::Remote,

View File

@ -329,7 +329,7 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) ->
remote: event_loop.raw_remote(), remote: event_loop.raw_remote(),
fetch: fetch.clone(), fetch: fetch.clone(),
signer: signer_service.clone(), signer: signer_service.clone(),
ui_address: cmd.ui_conf.address(), ui_address: cmd.ui_conf.redirection_address(),
}) })
}; };
@ -723,7 +723,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R
remote: event_loop.raw_remote(), remote: event_loop.raw_remote(),
fetch: fetch.clone(), fetch: fetch.clone(),
signer: signer_service.clone(), signer: signer_service.clone(),
ui_address: cmd.ui_conf.address(), ui_address: cmd.ui_conf.redirection_address(),
}) })
}; };
let dapps_middleware = dapps::new(cmd.dapps_conf.clone(), dapps_deps.clone())?; let dapps_middleware = dapps::new(cmd.dapps_conf.clone(), dapps_deps.clone())?;

View File

@ -51,6 +51,6 @@ pub use self::signer::SignerService;
pub use self::subscribers::Subscribers; pub use self::subscribers::Subscribers;
pub use self::subscription_manager::GenericPollManager; pub use self::subscription_manager::GenericPollManager;
pub fn to_url(address: &Option<(String, u16)>) -> Option<String> { pub fn to_url(address: &Option<::Host>) -> Option<String> {
address.as_ref().map(|&(ref iface, ref port)| format!("{}:{}", iface, port)) address.as_ref().map(|host| (**host).to_owned())
} }

View File

@ -46,6 +46,7 @@ use v1::types::{
OperationsInfo, DappId, ChainStatus, OperationsInfo, DappId, ChainStatus,
AccountInfo, HwAccountInfo, Header, RichHeader, AccountInfo, HwAccountInfo, Header, RichHeader,
}; };
use Host;
/// Parity implementation for light client. /// Parity implementation for light client.
pub struct ParityClient { pub struct ParityClient {
@ -55,8 +56,8 @@ pub struct ParityClient {
settings: Arc<NetworkSettings>, settings: Arc<NetworkSettings>,
health: NodeHealth, health: NodeHealth,
signer: Option<Arc<SignerService>>, signer: Option<Arc<SignerService>>,
dapps_address: Option<(String, u16)>, dapps_address: Option<Host>,
ws_address: Option<(String, u16)>, ws_address: Option<Host>,
eip86_transition: u64, eip86_transition: u64,
} }
@ -70,8 +71,8 @@ impl ParityClient {
settings: Arc<NetworkSettings>, settings: Arc<NetworkSettings>,
health: NodeHealth, health: NodeHealth,
signer: Option<Arc<SignerService>>, signer: Option<Arc<SignerService>>,
dapps_address: Option<(String, u16)>, dapps_address: Option<Host>,
ws_address: Option<(String, u16)>, ws_address: Option<Host>,
) -> Self { ) -> Self {
ParityClient { ParityClient {
light_dispatch, light_dispatch,

View File

@ -51,6 +51,7 @@ use v1::types::{
OperationsInfo, DappId, ChainStatus, OperationsInfo, DappId, ChainStatus,
AccountInfo, HwAccountInfo, RichHeader AccountInfo, HwAccountInfo, RichHeader
}; };
use Host;
/// Parity implementation. /// Parity implementation.
pub struct ParityClient<C, M, U> { pub struct ParityClient<C, M, U> {
@ -64,8 +65,8 @@ pub struct ParityClient<C, M, U> {
logger: Arc<RotatingLogger>, logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>, settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>, signer: Option<Arc<SignerService>>,
dapps_address: Option<(String, u16)>, dapps_address: Option<Host>,
ws_address: Option<(String, u16)>, ws_address: Option<Host>,
eip86_transition: u64, eip86_transition: u64,
} }
@ -84,8 +85,8 @@ impl<C, M, U> ParityClient<C, M, U> where
logger: Arc<RotatingLogger>, logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>, settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>, signer: Option<Arc<SignerService>>,
dapps_address: Option<(String, u16)>, dapps_address: Option<Host>,
ws_address: Option<(String, u16)>, ws_address: Option<Host>,
) -> Self { ) -> Self {
let eip86_transition = client.eip86_transition(); let eip86_transition = client.eip86_transition();
ParityClient { ParityClient {

View File

@ -31,6 +31,7 @@ use v1::metadata::Metadata;
use v1::helpers::{SignerService, NetworkSettings}; use v1::helpers::{SignerService, NetworkSettings};
use v1::tests::helpers::{TestSyncProvider, Config, TestMinerService, TestUpdater}; use v1::tests::helpers::{TestSyncProvider, Config, TestMinerService, TestUpdater};
use super::manage_network::TestManageNetwork; use super::manage_network::TestManageNetwork;
use Host;
pub type TestParityClient = ParityClient<TestBlockChainClient, TestMinerService, TestUpdater>; pub type TestParityClient = ParityClient<TestBlockChainClient, TestMinerService, TestUpdater>;
@ -44,8 +45,8 @@ pub struct Dependencies {
pub settings: Arc<NetworkSettings>, pub settings: Arc<NetworkSettings>,
pub network: Arc<ManageNetwork>, pub network: Arc<ManageNetwork>,
pub accounts: Arc<AccountProvider>, pub accounts: Arc<AccountProvider>,
pub dapps_address: Option<(String, u16)>, pub dapps_address: Option<Host>,
pub ws_address: Option<(String, u16)>, pub ws_address: Option<Host>,
} }
impl Dependencies { impl Dependencies {
@ -74,8 +75,8 @@ impl Dependencies {
}), }),
network: Arc::new(TestManageNetwork), network: Arc::new(TestManageNetwork),
accounts: Arc::new(AccountProvider::transient_provider()), accounts: Arc::new(AccountProvider::transient_provider()),
dapps_address: Some(("127.0.0.1".into(), 18080)), dapps_address: Some("127.0.0.1:18080".into()),
ws_address: Some(("127.0.0.1".into(), 18546)), ws_address: Some("127.0.0.1:18546".into()),
} }
} }