2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-08-30 16:05:18 +02:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use std::env;
|
2016-09-01 10:16:04 +02:00
|
|
|
use std::str;
|
2017-04-03 10:27:37 +02:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::{Path, PathBuf};
|
2016-08-30 16:05:18 +02:00
|
|
|
use std::sync::Arc;
|
2016-10-22 15:21:41 +02:00
|
|
|
use env_logger::LogBuilder;
|
2017-04-03 10:27:37 +02:00
|
|
|
use jsonrpc_core::IoHandler;
|
|
|
|
use jsonrpc_http_server::{self as http, Host, DomainsValidation};
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-09-01 10:16:04 +02:00
|
|
|
use devtools::http_client;
|
2017-04-03 10:27:37 +02:00
|
|
|
use hash_fetch::urlhint::ContractClient;
|
|
|
|
use fetch::{Fetch, Client as FetchClient};
|
2017-03-22 07:02:14 +01:00
|
|
|
use parity_reactor::{EventLoop, Remote};
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
use {Middleware, SyncStatus, WebProxyTokens};
|
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
mod registrar;
|
|
|
|
mod fetch;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
use self::registrar::FakeRegistrar;
|
|
|
|
use self::fetch::FakeFetch;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-12-27 16:38:55 +01:00
|
|
|
const SIGNER_PORT: u16 = 18180;
|
2016-08-30 16:05:18 +02:00
|
|
|
|
2016-10-22 15:21:41 +02:00
|
|
|
fn init_logger() {
|
|
|
|
// Initialize logger
|
|
|
|
if let Ok(log) = env::var("RUST_LOG") {
|
|
|
|
let mut builder = LogBuilder::new();
|
|
|
|
builder.parse(&log);
|
2016-12-11 15:41:49 +01:00
|
|
|
let _ = builder.init(); // ignore errors since ./test.sh will call this multiple times.
|
2016-10-22 15:21:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub struct ServerLoop {
|
|
|
|
pub server: Server,
|
2017-03-22 07:02:14 +01:00
|
|
|
pub event_loop: EventLoop,
|
2017-01-11 20:02:27 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
impl ::std::ops::Deref for ServerLoop {
|
2017-01-11 20:02:27 +01:00
|
|
|
type Target = Server;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
pub fn init_server<F, B>(process: F, io: IoHandler, remote: Remote) -> (ServerLoop, Arc<FakeRegistrar>) where
|
2016-12-27 16:38:55 +01:00
|
|
|
F: FnOnce(ServerBuilder) -> ServerBuilder<B>,
|
|
|
|
B: Fetch,
|
|
|
|
{
|
2016-10-22 15:21:41 +02:00
|
|
|
init_logger();
|
2016-08-30 16:05:18 +02:00
|
|
|
let registrar = Arc::new(FakeRegistrar::new());
|
|
|
|
let mut dapps_path = env::temp_dir();
|
|
|
|
dapps_path.push("non-existent-dir-to-prevent-fs-files-from-loading");
|
2017-01-11 20:02:27 +01:00
|
|
|
|
2017-03-29 15:17:27 +02:00
|
|
|
// TODO [ToDr] When https://github.com/paritytech/jsonrpc/issues/26 is resolved
|
2017-01-11 20:02:27 +01:00
|
|
|
// this additional EventLoop wouldn't be needed, we should be able to re-use remote.
|
2017-03-22 07:02:14 +01:00
|
|
|
let event_loop = EventLoop::spawn();
|
2016-12-27 16:38:55 +01:00
|
|
|
let server = process(ServerBuilder::new(
|
2017-01-06 16:05:58 +01:00
|
|
|
&dapps_path, registrar.clone(), remote,
|
2016-12-27 16:38:55 +01:00
|
|
|
))
|
|
|
|
.signer_address(Some(("127.0.0.1".into(), SIGNER_PORT)))
|
2017-04-03 10:27:37 +02:00
|
|
|
.start_unsecured_http(&"127.0.0.1:0".parse().unwrap(), io).unwrap();
|
2016-09-01 11:16:19 +02:00
|
|
|
(
|
2017-01-11 20:02:27 +01:00
|
|
|
ServerLoop { server: server, event_loop: event_loop },
|
2016-09-01 11:16:19 +02:00
|
|
|
registrar,
|
|
|
|
)
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
pub fn serve_with_rpc(io: IoHandler) -> ServerLoop {
|
|
|
|
init_server(|builder| builder, io, Remote::new_sync()).0
|
2017-01-30 10:59:46 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve_hosts(hosts: Option<Vec<String>>) -> ServerLoop {
|
2017-03-22 07:02:14 +01:00
|
|
|
let hosts = hosts.map(|hosts| hosts.into_iter().map(Into::into).collect());
|
|
|
|
init_server(|builder| builder.allowed_hosts(hosts.into()), Default::default(), Remote::new_sync()).0
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve_with_registrar() -> (ServerLoop, Arc<FakeRegistrar>) {
|
2017-04-03 10:27:37 +02:00
|
|
|
init_server(|builder| builder, Default::default(), Remote::new_sync())
|
2016-10-22 20:06:30 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve_with_registrar_and_sync() -> (ServerLoop, Arc<FakeRegistrar>) {
|
|
|
|
init_server(|builder| {
|
2017-04-03 10:27:37 +02:00
|
|
|
builder.sync_status(Arc::new(|| true))
|
2017-01-30 10:59:46 +01:00
|
|
|
}, Default::default(), Remote::new_sync())
|
2016-12-27 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve_with_registrar_and_fetch() -> (ServerLoop, FakeFetch, Arc<FakeRegistrar>) {
|
2016-12-28 13:45:25 +01:00
|
|
|
serve_with_registrar_and_fetch_and_threads(false)
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve_with_registrar_and_fetch_and_threads(multi_threaded: bool) -> (ServerLoop, FakeFetch, Arc<FakeRegistrar>) {
|
2016-12-27 16:38:55 +01:00
|
|
|
let fetch = FakeFetch::default();
|
|
|
|
let f = fetch.clone();
|
2017-01-11 20:02:27 +01:00
|
|
|
let (server, reg) = init_server(move |builder| {
|
2017-04-03 10:27:37 +02:00
|
|
|
builder.fetch(f.clone())
|
2017-01-30 10:59:46 +01:00
|
|
|
}, Default::default(), if multi_threaded { Remote::new_thread_per_future() } else { Remote::new_sync() });
|
2016-12-27 16:38:55 +01:00
|
|
|
|
|
|
|
(server, fetch, reg)
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve_with_fetch(web_token: &'static str) -> (ServerLoop, FakeFetch) {
|
2016-12-27 16:38:55 +01:00
|
|
|
let fetch = FakeFetch::default();
|
|
|
|
let f = fetch.clone();
|
2017-01-11 20:02:27 +01:00
|
|
|
let (server, _) = init_server(move |builder| {
|
2016-12-27 16:38:55 +01:00
|
|
|
builder
|
|
|
|
.fetch(f.clone())
|
|
|
|
.web_proxy_tokens(Arc::new(move |token| &token == web_token))
|
2017-01-30 10:59:46 +01:00
|
|
|
}, Default::default(), Remote::new_sync());
|
2016-12-27 16:38:55 +01:00
|
|
|
|
|
|
|
(server, fetch)
|
2016-09-01 11:16:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn serve() -> ServerLoop {
|
2017-04-03 10:27:37 +02:00
|
|
|
init_server(|builder| builder, Default::default(), Remote::new_sync()).0
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
pub fn request(server: ServerLoop, request: &str) -> http_client::Response {
|
2016-09-01 10:16:04 +02:00
|
|
|
http_client::request(server.addr(), request)
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
2016-10-19 11:02:21 +02:00
|
|
|
|
|
|
|
pub fn assert_security_headers(headers: &[String]) {
|
2016-10-22 15:21:41 +02:00
|
|
|
http_client::assert_security_headers_present(headers, None)
|
|
|
|
}
|
|
|
|
pub fn assert_security_headers_for_embed(headers: &[String]) {
|
|
|
|
http_client::assert_security_headers_present(headers, Some(SIGNER_PORT))
|
2016-10-19 11:02:21 +02:00
|
|
|
}
|
2017-04-03 10:27:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/// Webapps HTTP+RPC server build.
|
|
|
|
pub struct ServerBuilder<T: Fetch = FetchClient> {
|
|
|
|
dapps_path: PathBuf,
|
|
|
|
registrar: Arc<ContractClient>,
|
|
|
|
sync_status: Arc<SyncStatus>,
|
|
|
|
web_proxy_tokens: Arc<WebProxyTokens>,
|
|
|
|
signer_address: Option<(String, u16)>,
|
|
|
|
allowed_hosts: DomainsValidation<Host>,
|
|
|
|
remote: Remote,
|
|
|
|
fetch: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerBuilder {
|
|
|
|
/// Construct new dapps server
|
|
|
|
pub fn new<P: AsRef<Path>>(dapps_path: P, registrar: Arc<ContractClient>, remote: Remote) -> Self {
|
|
|
|
ServerBuilder {
|
|
|
|
dapps_path: dapps_path.as_ref().to_owned(),
|
|
|
|
registrar: registrar,
|
|
|
|
sync_status: Arc::new(|| false),
|
|
|
|
web_proxy_tokens: Arc::new(|_| false),
|
|
|
|
signer_address: None,
|
|
|
|
allowed_hosts: DomainsValidation::Disabled,
|
|
|
|
remote: remote,
|
|
|
|
fetch: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Fetch> ServerBuilder<T> {
|
|
|
|
/// Set a fetch client to use.
|
|
|
|
pub fn fetch<X: Fetch>(self, fetch: X) -> ServerBuilder<X> {
|
|
|
|
ServerBuilder {
|
|
|
|
dapps_path: self.dapps_path,
|
|
|
|
registrar: self.registrar,
|
|
|
|
sync_status: self.sync_status,
|
|
|
|
web_proxy_tokens: self.web_proxy_tokens,
|
|
|
|
signer_address: self.signer_address,
|
|
|
|
allowed_hosts: self.allowed_hosts,
|
|
|
|
remote: self.remote,
|
|
|
|
fetch: Some(fetch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change default sync status.
|
|
|
|
pub fn sync_status(mut self, status: Arc<SyncStatus>) -> Self {
|
|
|
|
self.sync_status = status;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change default web proxy tokens validator.
|
|
|
|
pub fn web_proxy_tokens(mut self, tokens: Arc<WebProxyTokens>) -> Self {
|
|
|
|
self.web_proxy_tokens = tokens;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change default signer port.
|
|
|
|
pub fn signer_address(mut self, signer_address: Option<(String, u16)>) -> Self {
|
|
|
|
self.signer_address = signer_address;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change allowed hosts.
|
|
|
|
/// `None` - All hosts are allowed
|
|
|
|
/// `Some(whitelist)` - Allow only whitelisted hosts (+ listen address)
|
|
|
|
pub fn allowed_hosts(mut self, allowed_hosts: DomainsValidation<Host>) -> Self {
|
|
|
|
self.allowed_hosts = allowed_hosts;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asynchronously start server with no authentication,
|
|
|
|
/// returns result with `Server` handle on success or an error.
|
|
|
|
pub fn start_unsecured_http(self, addr: &SocketAddr, io: IoHandler) -> Result<Server, http::Error> {
|
|
|
|
let fetch = self.fetch_client();
|
|
|
|
Server::start_http(
|
|
|
|
addr,
|
|
|
|
io,
|
|
|
|
self.allowed_hosts,
|
|
|
|
self.signer_address,
|
|
|
|
self.dapps_path,
|
|
|
|
vec![],
|
|
|
|
self.registrar,
|
|
|
|
self.sync_status,
|
|
|
|
self.web_proxy_tokens,
|
|
|
|
self.remote,
|
|
|
|
fetch,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_client(&self) -> T {
|
|
|
|
match self.fetch.clone() {
|
|
|
|
Some(fetch) => fetch,
|
|
|
|
None => T::new().unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Webapps HTTP server.
|
|
|
|
pub struct Server {
|
|
|
|
server: Option<http::Server>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
|
|
|
fn start_http<F: Fetch>(
|
|
|
|
addr: &SocketAddr,
|
|
|
|
io: IoHandler,
|
|
|
|
allowed_hosts: DomainsValidation<Host>,
|
|
|
|
signer_address: Option<(String, u16)>,
|
|
|
|
dapps_path: PathBuf,
|
|
|
|
extra_dapps: Vec<PathBuf>,
|
|
|
|
registrar: Arc<ContractClient>,
|
|
|
|
sync_status: Arc<SyncStatus>,
|
|
|
|
web_proxy_tokens: Arc<WebProxyTokens>,
|
|
|
|
remote: Remote,
|
|
|
|
fetch: F,
|
|
|
|
) -> Result<Server, http::Error> {
|
|
|
|
let middleware = Middleware::new(
|
|
|
|
remote,
|
|
|
|
signer_address,
|
|
|
|
dapps_path,
|
|
|
|
extra_dapps,
|
|
|
|
registrar,
|
|
|
|
sync_status,
|
|
|
|
web_proxy_tokens,
|
|
|
|
fetch,
|
|
|
|
);
|
|
|
|
http::ServerBuilder::new(io)
|
|
|
|
.request_middleware(middleware)
|
|
|
|
.allowed_hosts(allowed_hosts)
|
|
|
|
.cors(http::DomainsValidation::Disabled)
|
|
|
|
.start_http(addr)
|
|
|
|
.map(|server| Server {
|
|
|
|
server: Some(server),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns address that this server is bound to.
|
|
|
|
pub fn addr(&self) -> &SocketAddr {
|
|
|
|
self.server.as_ref()
|
|
|
|
.expect("server is always Some at the start; it's consumed only when object is dropped; qed")
|
|
|
|
.addrs()
|
|
|
|
.first()
|
|
|
|
.expect("You cannot start the server without binding to at least one address; qed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Server {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.server.take().unwrap().close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|