2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-07 10:49:00 +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/>.
|
|
|
|
|
|
|
|
//! Ethcore Webapplications for Parity
|
|
|
|
#![warn(missing_docs)]
|
2017-02-13 16:38:47 +01:00
|
|
|
#![cfg_attr(feature="nightly", feature(plugin))]
|
2016-04-07 10:49:00 +02:00
|
|
|
#![cfg_attr(feature="nightly", plugin(clippy))]
|
|
|
|
|
2017-02-04 09:52:14 +01:00
|
|
|
extern crate base32;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate futures;
|
2017-08-17 16:05:26 +02:00
|
|
|
extern crate itertools;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate linked_hash_map;
|
|
|
|
extern crate mime_guess;
|
2017-09-02 20:09:13 +02:00
|
|
|
extern crate parking_lot;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate rand;
|
2017-07-06 11:26:14 +02:00
|
|
|
extern crate rustc_hex;
|
2016-05-17 16:55:08 +02:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_json;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate time;
|
|
|
|
extern crate unicase;
|
|
|
|
extern crate url as url_lib;
|
2016-08-18 12:19:09 +02:00
|
|
|
extern crate zip;
|
2017-03-22 07:02:14 +01:00
|
|
|
|
2016-04-07 10:49:00 +02:00
|
|
|
extern crate jsonrpc_core;
|
|
|
|
extern crate jsonrpc_http_server;
|
2017-03-22 07:02:14 +01:00
|
|
|
|
2016-08-18 12:19:09 +02:00
|
|
|
extern crate ethcore_util as util;
|
2017-09-04 16:36:49 +02:00
|
|
|
extern crate ethcore_bigint as bigint;
|
2017-09-06 20:47:45 +02:00
|
|
|
extern crate ethcore_bytes as bytes;
|
2016-09-27 16:27:06 +02:00
|
|
|
extern crate fetch;
|
2017-08-28 14:11:55 +02:00
|
|
|
extern crate node_health;
|
2016-10-22 15:21:41 +02:00
|
|
|
extern crate parity_dapps_glue as parity_dapps;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate parity_hash_fetch as hash_fetch;
|
2016-12-22 18:26:39 +01:00
|
|
|
extern crate parity_reactor;
|
2017-05-24 12:24:07 +02:00
|
|
|
extern crate parity_ui;
|
2017-08-31 11:35:41 +02:00
|
|
|
extern crate hash;
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2016-10-20 23:41:15 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate mime;
|
2017-02-13 16:38:47 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2016-10-20 23:41:15 +02:00
|
|
|
|
2016-09-01 10:16:04 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate ethcore_devtools as devtools;
|
2016-10-22 15:21:41 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate env_logger;
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2016-04-14 20:38:48 +02:00
|
|
|
mod endpoint;
|
2016-04-07 12:10:26 +02:00
|
|
|
mod apps;
|
|
|
|
mod page;
|
|
|
|
mod router;
|
2016-07-06 11:24:29 +02:00
|
|
|
mod handlers;
|
2016-04-14 20:38:48 +02:00
|
|
|
mod api;
|
2016-05-12 14:45:09 +02:00
|
|
|
mod proxypac;
|
2016-07-07 09:42:49 +02:00
|
|
|
mod url;
|
2016-12-22 18:26:39 +01:00
|
|
|
mod web;
|
2016-08-30 16:05:18 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2016-04-07 12:10:26 +02:00
|
|
|
|
2017-08-09 19:06:40 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::mem;
|
2017-04-03 10:27:37 +02:00
|
|
|
use std::path::PathBuf;
|
2017-03-22 07:02:14 +01:00
|
|
|
use std::sync::Arc;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::RwLock;
|
2016-07-13 19:59:59 +02:00
|
|
|
|
2017-06-22 20:05:40 +02:00
|
|
|
use jsonrpc_http_server::{self as http, hyper, Origin};
|
2017-03-22 07:02:14 +01:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
use fetch::Fetch;
|
2017-08-28 14:11:55 +02:00
|
|
|
use node_health::NodeHealth;
|
2016-12-22 18:26:39 +01:00
|
|
|
use parity_reactor::Remote;
|
2016-04-08 16:11:58 +02:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
pub use hash_fetch::urlhint::ContractClient;
|
2017-08-28 14:11:55 +02:00
|
|
|
pub use node_health::SyncStatus;
|
2016-05-16 16:08:52 +02:00
|
|
|
|
2016-09-01 11:16:19 +02:00
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
/// Validates Web Proxy tokens
|
|
|
|
pub trait WebProxyTokens: Send + Sync {
|
2017-06-22 20:05:40 +02:00
|
|
|
/// Should return a domain allowed to be accessed by this token or `None` if the token is not valid
|
|
|
|
fn domain(&self, token: &str) -> Option<Origin>;
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 20:05:40 +02:00
|
|
|
impl<F> WebProxyTokens for F where F: Fn(String) -> Option<Origin> + Send + Sync {
|
|
|
|
fn domain(&self, token: &str) -> Option<Origin> { self(token.to_owned()) }
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
/// Current supported endpoints.
|
2017-08-09 19:06:40 +02:00
|
|
|
#[derive(Default, Clone)]
|
2017-05-24 12:24:07 +02:00
|
|
|
pub struct Endpoints {
|
2017-08-09 19:06:40 +02:00
|
|
|
local_endpoints: Arc<RwLock<Vec<String>>>,
|
|
|
|
endpoints: Arc<RwLock<endpoint::Endpoints>>,
|
|
|
|
dapps_path: PathBuf,
|
|
|
|
embeddable: Option<ParentFrameSettings>,
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Endpoints {
|
|
|
|
/// Returns a current list of app endpoints.
|
|
|
|
pub fn list(&self) -> Vec<apps::App> {
|
2017-08-09 19:06:40 +02:00
|
|
|
self.endpoints.read().iter().filter_map(|(ref k, ref e)| {
|
2017-05-24 12:24:07 +02:00
|
|
|
e.info().map(|ref info| apps::App::from_info(k, info))
|
|
|
|
}).collect()
|
|
|
|
}
|
2017-08-09 19:06:40 +02:00
|
|
|
|
|
|
|
/// Check for any changes in the local dapps folder and update.
|
|
|
|
pub fn refresh_local_dapps(&self) {
|
|
|
|
let new_local = apps::fs::local_endpoints(&self.dapps_path, self.embeddable.clone());
|
|
|
|
let old_local = mem::replace(&mut *self.local_endpoints.write(), new_local.keys().cloned().collect());
|
|
|
|
let (_, to_remove): (_, Vec<_>) = old_local
|
|
|
|
.into_iter()
|
|
|
|
.partition(|k| new_local.contains_key(&k.clone()));
|
|
|
|
|
|
|
|
let mut endpoints = self.endpoints.write();
|
|
|
|
// remove the dead dapps
|
|
|
|
for k in to_remove {
|
|
|
|
endpoints.remove(&k);
|
|
|
|
}
|
|
|
|
// new dapps to be added
|
|
|
|
for (k, v) in new_local {
|
|
|
|
if !endpoints.contains_key(&k) {
|
|
|
|
endpoints.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
/// Dapps server as `jsonrpc-http-server` request middleware.
|
2017-04-05 11:37:45 +02:00
|
|
|
pub struct Middleware {
|
2017-08-09 19:06:40 +02:00
|
|
|
endpoints: Endpoints,
|
2017-04-05 11:37:45 +02:00
|
|
|
router: router::Router,
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 11:37:45 +02:00
|
|
|
impl Middleware {
|
2017-05-24 12:24:07 +02:00
|
|
|
/// Get local endpoints handle.
|
2017-08-09 19:06:40 +02:00
|
|
|
pub fn endpoints(&self) -> &Endpoints {
|
|
|
|
&self.endpoints
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates new middleware for UI server.
|
2017-07-11 12:23:46 +02:00
|
|
|
pub fn ui<F: Fetch>(
|
2017-08-28 14:11:55 +02:00
|
|
|
health: NodeHealth,
|
2017-05-24 12:24:07 +02:00
|
|
|
remote: Remote,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain: &str,
|
2017-05-24 12:24:07 +02:00
|
|
|
registrar: Arc<ContractClient>,
|
|
|
|
sync_status: Arc<SyncStatus>,
|
|
|
|
fetch: F,
|
|
|
|
) -> Self {
|
|
|
|
let content_fetcher = Arc::new(apps::fetcher::ContentFetcher::new(
|
|
|
|
hash_fetch::urlhint::URLHintContract::new(registrar),
|
2017-07-11 12:23:46 +02:00
|
|
|
sync_status.clone(),
|
2017-05-24 12:24:07 +02:00
|
|
|
remote.clone(),
|
|
|
|
fetch.clone(),
|
|
|
|
).embeddable_on(None).allow_dapps(false));
|
|
|
|
let special = {
|
2017-07-11 12:23:46 +02:00
|
|
|
let mut special = special_endpoints(
|
2017-08-28 14:11:55 +02:00
|
|
|
health,
|
2017-07-11 12:23:46 +02:00
|
|
|
content_fetcher.clone(),
|
|
|
|
remote.clone(),
|
|
|
|
);
|
2017-05-24 12:24:07 +02:00
|
|
|
special.insert(router::SpecialEndpoint::Home, Some(apps::ui()));
|
|
|
|
special
|
|
|
|
};
|
|
|
|
let router = router::Router::new(
|
|
|
|
content_fetcher,
|
|
|
|
None,
|
|
|
|
special,
|
|
|
|
None,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain.to_owned(),
|
2017-05-24 12:24:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Middleware {
|
|
|
|
endpoints: Default::default(),
|
2017-08-09 19:06:40 +02:00
|
|
|
router: router,
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
/// Creates new Dapps server middleware.
|
2017-07-11 12:23:46 +02:00
|
|
|
pub fn dapps<F: Fetch>(
|
2017-08-28 14:11:55 +02:00
|
|
|
health: NodeHealth,
|
2017-04-03 10:27:37 +02:00
|
|
|
remote: Remote,
|
2017-05-24 12:24:07 +02:00
|
|
|
ui_address: Option<(String, u16)>,
|
2017-07-11 13:45:23 +02:00
|
|
|
extra_embed_on: Vec<(String, u16)>,
|
2017-08-10 18:32:10 +02:00
|
|
|
extra_script_src: Vec<(String, u16)>,
|
2017-01-06 16:05:58 +01:00
|
|
|
dapps_path: PathBuf,
|
|
|
|
extra_dapps: Vec<PathBuf>,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain: &str,
|
2016-08-23 19:28:21 +02:00
|
|
|
registrar: Arc<ContractClient>,
|
2016-09-01 11:16:19 +02:00
|
|
|
sync_status: Arc<SyncStatus>,
|
2016-12-27 11:15:02 +01:00
|
|
|
web_proxy_tokens: Arc<WebProxyTokens>,
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: F,
|
2017-04-03 10:27:37 +02:00
|
|
|
) -> Self {
|
2017-08-10 18:32:10 +02:00
|
|
|
let embeddable = as_embeddable(ui_address, extra_embed_on, extra_script_src, dapps_domain);
|
2017-04-05 11:37:45 +02:00
|
|
|
let content_fetcher = Arc::new(apps::fetcher::ContentFetcher::new(
|
2016-12-22 18:26:39 +01:00
|
|
|
hash_fetch::urlhint::URLHintContract::new(registrar),
|
2017-07-11 12:23:46 +02:00
|
|
|
sync_status.clone(),
|
2016-12-22 18:26:39 +01:00
|
|
|
remote.clone(),
|
|
|
|
fetch.clone(),
|
2017-07-04 16:04:09 +02:00
|
|
|
).embeddable_on(embeddable.clone()).allow_dapps(true));
|
2017-08-09 19:06:40 +02:00
|
|
|
let (local_endpoints, endpoints) = apps::all_endpoints(
|
|
|
|
dapps_path.clone(),
|
2017-01-06 16:05:58 +01:00
|
|
|
extra_dapps,
|
2017-07-11 13:45:23 +02:00
|
|
|
dapps_domain,
|
|
|
|
embeddable.clone(),
|
2017-01-06 16:05:58 +01:00
|
|
|
web_proxy_tokens,
|
|
|
|
remote.clone(),
|
|
|
|
fetch.clone(),
|
2017-04-03 10:27:37 +02:00
|
|
|
);
|
2017-08-09 19:06:40 +02:00
|
|
|
let endpoints = Endpoints {
|
|
|
|
endpoints: Arc::new(RwLock::new(endpoints)),
|
|
|
|
dapps_path,
|
|
|
|
local_endpoints: Arc::new(RwLock::new(local_endpoints)),
|
|
|
|
embeddable: embeddable.clone(),
|
|
|
|
};
|
2016-10-22 15:21:41 +02:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
let special = {
|
2017-07-11 12:23:46 +02:00
|
|
|
let mut special = special_endpoints(
|
2017-08-28 14:11:55 +02:00
|
|
|
health,
|
2017-07-11 12:23:46 +02:00
|
|
|
content_fetcher.clone(),
|
|
|
|
remote.clone(),
|
|
|
|
);
|
2017-07-04 16:04:09 +02:00
|
|
|
special.insert(
|
|
|
|
router::SpecialEndpoint::Home,
|
2017-07-11 13:45:23 +02:00
|
|
|
Some(apps::ui_redirection(embeddable.clone())),
|
2017-07-04 16:04:09 +02:00
|
|
|
);
|
2016-05-13 18:40:20 +02:00
|
|
|
special
|
2017-04-03 10:27:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let router = router::Router::new(
|
|
|
|
content_fetcher,
|
2017-05-24 12:24:07 +02:00
|
|
|
Some(endpoints.clone()),
|
2017-04-03 10:27:37 +02:00
|
|
|
special,
|
2017-07-04 16:04:09 +02:00
|
|
|
embeddable,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain.to_owned(),
|
2017-04-03 10:27:37 +02:00
|
|
|
);
|
2016-04-23 12:29:12 +02:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
Middleware {
|
2017-08-09 19:06:40 +02:00
|
|
|
endpoints,
|
|
|
|
router,
|
2017-04-03 10:27:37 +02:00
|
|
|
}
|
2016-08-30 16:05:18 +02:00
|
|
|
}
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 11:37:45 +02:00
|
|
|
impl http::RequestMiddleware for Middleware {
|
2017-04-03 10:27:37 +02:00
|
|
|
fn on_request(&self, req: &hyper::server::Request<hyper::net::HttpStream>, control: &hyper::Control) -> http::RequestMiddlewareAction {
|
|
|
|
self.router.on_request(req, control)
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 10:49:00 +02:00
|
|
|
|
2017-08-28 14:11:55 +02:00
|
|
|
fn special_endpoints(
|
|
|
|
health: NodeHealth,
|
2017-07-11 12:23:46 +02:00
|
|
|
content_fetcher: Arc<apps::fetcher::Fetcher>,
|
|
|
|
remote: Remote,
|
|
|
|
) -> HashMap<router::SpecialEndpoint, Option<Box<endpoint::Endpoint>>> {
|
2017-05-24 12:24:07 +02:00
|
|
|
let mut special = HashMap::new();
|
|
|
|
special.insert(router::SpecialEndpoint::Rpc, None);
|
|
|
|
special.insert(router::SpecialEndpoint::Utils, Some(apps::utils()));
|
2017-07-11 12:23:46 +02:00
|
|
|
special.insert(router::SpecialEndpoint::Api, Some(api::RestApi::new(
|
|
|
|
content_fetcher,
|
2017-08-28 14:11:55 +02:00
|
|
|
health,
|
2017-07-11 12:23:46 +02:00
|
|
|
remote,
|
|
|
|
)));
|
2017-05-24 12:24:07 +02:00
|
|
|
special
|
2016-04-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 16:04:09 +02:00
|
|
|
fn address(host: &str, port: u16) -> String {
|
|
|
|
format!("{}:{}", host, port)
|
|
|
|
}
|
|
|
|
|
2017-07-11 13:45:23 +02:00
|
|
|
fn as_embeddable(
|
|
|
|
ui_address: Option<(String, u16)>,
|
|
|
|
extra_embed_on: Vec<(String, u16)>,
|
2017-08-10 18:32:10 +02:00
|
|
|
extra_script_src: Vec<(String, u16)>,
|
2017-07-11 13:45:23 +02:00
|
|
|
dapps_domain: &str,
|
|
|
|
) -> Option<ParentFrameSettings> {
|
|
|
|
ui_address.map(|(host, port)| ParentFrameSettings {
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
extra_embed_on,
|
2017-08-10 18:32:10 +02:00
|
|
|
extra_script_src,
|
2017-07-11 13:45:23 +02:00
|
|
|
dapps_domain: dapps_domain.to_owned(),
|
|
|
|
})
|
2016-04-07 10:49:00 +02:00
|
|
|
}
|
2016-08-18 12:19:09 +02:00
|
|
|
|
|
|
|
/// Random filename
|
2016-12-22 18:26:39 +01:00
|
|
|
fn random_filename() -> String {
|
2016-08-18 12:19:09 +02:00
|
|
|
use ::rand::Rng;
|
|
|
|
let mut rng = ::rand::OsRng::new().unwrap();
|
|
|
|
rng.gen_ascii_chars().take(12).collect()
|
|
|
|
}
|
2017-07-04 16:04:09 +02:00
|
|
|
|
|
|
|
type Embeddable = Option<ParentFrameSettings>;
|
|
|
|
|
|
|
|
/// Parent frame host and port allowed to embed the content.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ParentFrameSettings {
|
|
|
|
/// Hostname
|
|
|
|
pub host: String,
|
|
|
|
/// Port
|
|
|
|
pub port: u16,
|
2017-08-10 18:32:10 +02:00
|
|
|
/// Additional URLs the dapps can be embedded on.
|
2017-07-11 13:45:23 +02:00
|
|
|
pub extra_embed_on: Vec<(String, u16)>,
|
2017-08-10 18:32:10 +02:00
|
|
|
/// Additional URLs the dapp scripts can be loaded from.
|
|
|
|
pub extra_script_src: Vec<(String, u16)>,
|
2017-07-04 16:04:09 +02:00
|
|
|
/// Dapps Domain (web3.site)
|
|
|
|
pub dapps_domain: String,
|
|
|
|
}
|