// Copyright 2015-2017 Parity Technologies (UK) Ltd. // This file is part of Parity. // Parity is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity. If not, see . use std::io; use std::sync::Arc; use std::path::PathBuf; use std::collections::HashSet; use dapps; use dir::default_data_path; use helpers::{parity_ipc_path, replace_home}; use jsonrpc_core::MetaIoHandler; use parity_reactor::TokioRemote; use parity_rpc::informant::{RpcStats, Middleware}; use parity_rpc::{self as rpc, Metadata, DomainsValidation}; use rpc_apis::{self, ApiSet}; pub use parity_rpc::{IpcServer, HttpServer, RequestMiddleware}; pub use parity_rpc::ws::Server as WsServer; pub const DAPPS_DOMAIN: &'static str = "web3.site"; #[derive(Debug, Clone, PartialEq)] pub struct HttpConfiguration { pub enabled: bool, pub interface: String, pub port: u16, pub apis: ApiSet, pub cors: Option>, pub hosts: Option>, pub threads: Option, } impl HttpConfiguration { pub fn address(&self) -> Option<(String, u16)> { match self.enabled { true => Some((self.interface.clone(), self.port)), false => None, } } } impl Default for HttpConfiguration { fn default() -> Self { HttpConfiguration { enabled: true, interface: "127.0.0.1".into(), port: 8545, apis: ApiSet::UnsafeContext, cors: None, hosts: Some(Vec::new()), threads: None, } } } #[derive(Debug, PartialEq, Clone)] pub struct UiConfiguration { pub enabled: bool, pub interface: String, pub port: u16, pub hosts: Option>, } impl UiConfiguration { pub fn address(&self) -> Option<(String, u16)> { match self.enabled { true => Some((self.interface.clone(), self.port)), false => None, } } } impl From for HttpConfiguration { fn from(conf: UiConfiguration) -> Self { HttpConfiguration { enabled: conf.enabled, interface: conf.interface, port: conf.port, apis: rpc_apis::ApiSet::SafeContext, cors: None, hosts: conf.hosts, threads: None, } } } impl Default for UiConfiguration { fn default() -> Self { UiConfiguration { enabled: true, port: 8180, interface: "127.0.0.1".into(), hosts: Some(vec![]), } } } #[derive(Debug, PartialEq)] pub struct IpcConfiguration { pub enabled: bool, pub socket_addr: String, pub apis: ApiSet, } impl Default for IpcConfiguration { fn default() -> Self { IpcConfiguration { enabled: true, socket_addr: if cfg!(windows) { r"\\.\pipe\jsonrpc.ipc".into() } else { let data_dir = ::dir::default_data_path(); parity_ipc_path(&data_dir, "$BASE/jsonrpc.ipc", 0) }, apis: ApiSet::IpcContext, } } } #[derive(Debug, Clone, PartialEq)] pub struct WsConfiguration { pub enabled: bool, pub interface: String, pub port: u16, pub apis: ApiSet, pub origins: Option>, pub hosts: Option>, pub signer_path: PathBuf, pub ui_address: Option<(String, u16)>, } impl Default for WsConfiguration { fn default() -> Self { let data_dir = default_data_path(); WsConfiguration { enabled: true, interface: "127.0.0.1".into(), port: 8546, apis: ApiSet::UnsafeContext, origins: Some(vec!["chrome-extension://*".into()]), hosts: Some(Vec::new()), signer_path: replace_home(&data_dir, "$BASE/signer").into(), ui_address: Some(("127.0.0.1".to_owned(), 8180)), } } } impl WsConfiguration { pub fn address(&self) -> Option<(String, u16)> { match self.enabled { true => Some((self.interface.clone(), self.port)), false => None, } } } pub struct Dependencies { pub apis: Arc, pub remote: TokioRemote, pub stats: Arc, } pub fn new_ws( conf: WsConfiguration, deps: &Dependencies, ) -> Result, String> { if !conf.enabled { return Ok(None); } let domain = DAPPS_DOMAIN; let ws_address = (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 full_handler = setup_apis(rpc_apis::ApiSet::SafeContext, deps); let handler = { let mut handler = MetaIoHandler::with_middleware(( rpc::WsDispatcher::new(full_handler), Middleware::new(deps.stats.clone(), deps.apis.activity_notifier()) )); let apis = conf.apis.list_apis().into_iter().collect::>(); deps.apis.extend_with_set(&mut handler, &apis); handler }; let remote = deps.remote.clone(); let ui_address = conf.ui_address.clone(); 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 signer_path = conf.signer_path; let signer_path = conf.ui_address.map(move |_| ::signer::codes_path(&signer_path)); let path = signer_path.as_ref().map(|p| p.as_path()); let start_result = rpc::start_ws( &addr, handler, remote.clone(), allowed_origins, allowed_hosts, rpc::WsExtractor::new(path.clone()), rpc::WsExtractor::new(path.clone()), rpc::WsStats::new(deps.stats.clone()), ); match start_result { Ok(server) => Ok(Some(server)), Err(rpc::ws::Error::Io(ref err)) if err.kind() == io::ErrorKind::AddrInUse => Err( format!("WebSockets address {} is already in use, make sure that another instance of an Ethereum client is not running or change the address using the --ws-port and --ws-interface options.", url) ), Err(e) => Err(format!("WebSockets error: {:?}", e)), } } pub fn new_http( id: &str, options: &str, conf: HttpConfiguration, deps: &Dependencies, middleware: Option, ) -> Result, String> { if !conf.enabled { return Ok(None); } let domain = DAPPS_DOMAIN; let http_address = (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 handler = setup_apis(conf.apis, deps); let remote = deps.remote.clone(); let cors_domains = into_domains(conf.cors); let allowed_hosts = into_domains(with_domain(conf.hosts, domain, &[Some(http_address)])); let start_result = rpc::start_http( &addr, cors_domains, allowed_hosts, handler, remote, rpc::RpcExtractor, match (conf.threads, middleware) { (Some(threads), None) => rpc::HttpSettings::Threads(threads), (None, middleware) => rpc::HttpSettings::Dapps(middleware), (Some(_), Some(_)) => { return Err("Dapps and fast multi-threaded RPC server cannot be enabled at the same time.".into()) }, } ); match start_result { Ok(server) => Ok(Some(server)), Err(rpc::HttpServerError::Io(ref err)) if err.kind() == io::ErrorKind::AddrInUse => Err( format!("{} address {} is already in use, make sure that another instance of an Ethereum client is not running or change the address using the --{}-port and --{}-interface options.", id, url, options, options) ), Err(e) => Err(format!("{} error: {:?}", id, e)), } } pub fn new_ipc( conf: IpcConfiguration, dependencies: &Dependencies ) -> Result, String> { if !conf.enabled { return Ok(None); } let handler = setup_apis(conf.apis, dependencies); let remote = dependencies.remote.clone(); match rpc::start_ipc(&conf.socket_addr, handler, remote, rpc::RpcExtractor) { Ok(server) => Ok(Some(server)), Err(io_error) => Err(format!("IPC error: {}", io_error)), } } fn into_domains>(items: Option>) -> DomainsValidation { items.map(|vals| vals.into_iter().map(T::from).collect()).into() } fn with_domain(items: Option>, domain: &str, addresses: &[Option<(String, u16)>]) -> Option> { items.map(move |items| { let mut items = items.into_iter().collect::>(); for address in addresses { if let Some((host, port)) = address.clone() { items.insert(format!("{}:{}", host, port)); items.insert(format!("{}:{}", host.replace("127.0.0.1", "localhost"), port)); items.insert(format!("http://*.{}:{}", domain, port)); items.insert(format!("http://*.{}", domain)); //proxypac } } items.into_iter().collect() }) } fn setup_apis(apis: ApiSet, deps: &Dependencies) -> MetaIoHandler> where D: rpc_apis::Dependencies { let mut handler = MetaIoHandler::with_middleware( Middleware::new(deps.stats.clone(), deps.apis.activity_notifier()) ); let apis = apis.list_apis().into_iter().collect::>(); deps.apis.extend_with_set(&mut handler, &apis); handler }