2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-02-14 19:30:37 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-02-14 19:30:37 +01:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-14 19:30:37 +01:00
|
|
|
|
|
|
|
extern crate multihash;
|
|
|
|
extern crate cid;
|
2017-10-05 12:35:01 +02:00
|
|
|
extern crate unicase;
|
2017-02-14 19:30:37 +01:00
|
|
|
|
|
|
|
extern crate rlp;
|
2019-08-13 12:33:34 +02:00
|
|
|
extern crate common_types;
|
2017-02-14 19:30:37 +01:00
|
|
|
extern crate ethcore;
|
Delete crates from parity-ethereum and fetch them from parity-common instead (#9083)
Use crates from parity-common: hashdb, keccak-hash, kvdb, kvdb-memorydb, kvdb-rocksdb, memorydb, parity-bytes, parity-crypto, path, patricia_trie, plain_hasher, rlp, target, test-support, trie-standardmap, triehash
2018-07-10 14:59:19 +02:00
|
|
|
extern crate parity_bytes as bytes;
|
2018-01-10 13:35:18 +01:00
|
|
|
extern crate ethereum_types;
|
2017-10-05 12:35:01 +02:00
|
|
|
extern crate jsonrpc_core as core;
|
2017-03-22 07:02:14 +01:00
|
|
|
extern crate jsonrpc_http_server as http;
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2017-02-24 10:32:42 +01:00
|
|
|
pub mod error;
|
|
|
|
mod route;
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
use std::thread;
|
|
|
|
use std::sync::{mpsc, Arc};
|
2017-02-24 10:32:42 +01:00
|
|
|
use std::net::{SocketAddr, IpAddr};
|
2017-10-05 12:35:01 +02:00
|
|
|
|
|
|
|
use core::futures::future::{self, FutureResult};
|
|
|
|
use core::futures::{self, Future};
|
|
|
|
use ethcore::client::BlockChainClient;
|
2018-10-22 09:40:50 +02:00
|
|
|
use http::hyper::{self, server, Method, StatusCode, Body,
|
|
|
|
header::{self, HeaderValue},
|
|
|
|
};
|
2017-10-05 12:35:01 +02:00
|
|
|
|
2017-02-14 19:30:37 +01:00
|
|
|
use error::ServerError;
|
2017-02-24 10:32:42 +01:00
|
|
|
use route::Out;
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2017-03-22 07:02:14 +01:00
|
|
|
pub use http::{AccessControlAllowOrigin, Host, DomainsValidation};
|
2017-02-24 10:32:42 +01:00
|
|
|
|
|
|
|
/// Request/response handler
|
|
|
|
pub struct IpfsHandler {
|
|
|
|
/// Allowed CORS domains
|
|
|
|
cors_domains: Option<Vec<AccessControlAllowOrigin>>,
|
|
|
|
/// Hostnames allowed in the `Host` request header
|
2017-03-22 07:02:14 +01:00
|
|
|
allowed_hosts: Option<Vec<Host>>,
|
2017-02-24 10:32:42 +01:00
|
|
|
/// Reference to the Blockchain Client
|
|
|
|
client: Arc<BlockChainClient>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IpfsHandler {
|
|
|
|
pub fn client(&self) -> &BlockChainClient {
|
|
|
|
&*self.client
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:14 +01:00
|
|
|
pub fn new(cors: DomainsValidation<AccessControlAllowOrigin>, hosts: DomainsValidation<Host>, client: Arc<BlockChainClient>) -> Self {
|
2017-02-24 10:32:42 +01:00
|
|
|
IpfsHandler {
|
2017-03-22 07:02:14 +01:00
|
|
|
cors_domains: cors.into(),
|
|
|
|
allowed_hosts: hosts.into(),
|
2017-02-24 10:32:42 +01:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn on_request(&self, req: hyper::Request<Body>) -> (Option<HeaderValue>, Out) {
|
2017-06-16 16:29:15 +02:00
|
|
|
match *req.method() {
|
2018-10-22 09:40:50 +02:00
|
|
|
Method::GET | Method::POST => {},
|
2017-10-05 12:35:01 +02:00
|
|
|
_ => return (None, Out::Bad("Invalid Request")),
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:14 +01:00
|
|
|
if !http::is_host_allowed(&req, &self.allowed_hosts) {
|
2017-10-05 12:35:01 +02:00
|
|
|
return (None, Out::Bad("Disallowed Host header"));
|
2017-02-24 10:32:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
let cors_header = http::cors_allow_origin(&req, &self.cors_domains);
|
|
|
|
if cors_header == http::AllowCors::Invalid {
|
2017-10-05 12:35:01 +02:00
|
|
|
return (None, Out::Bad("Disallowed Origin header"));
|
2017-02-16 16:08:54 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
let path = req.uri().path();
|
|
|
|
let query = req.uri().query();
|
|
|
|
return (cors_header.into(), self.route(path, query));
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
2017-10-05 12:35:01 +02:00
|
|
|
}
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
impl hyper::service::Service for IpfsHandler {
|
|
|
|
type ReqBody = Body;
|
|
|
|
type ResBody = Body;
|
2017-10-05 12:35:01 +02:00
|
|
|
type Error = hyper::Error;
|
2018-10-22 09:40:50 +02:00
|
|
|
type Future = FutureResult<hyper::Response<Body>, Self::Error>;
|
2017-10-05 12:35:01 +02:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
fn call(&mut self, request: hyper::Request<Self::ReqBody>) -> Self::Future {
|
2017-10-05 12:35:01 +02:00
|
|
|
let (cors_header, out) = self.on_request(request);
|
|
|
|
|
|
|
|
let mut res = match out {
|
|
|
|
Out::OctetStream(bytes) => {
|
2018-10-22 09:40:50 +02:00
|
|
|
hyper::Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header("content-type", HeaderValue::from_static("application/octet-stream"))
|
|
|
|
.body(bytes.into())
|
2017-02-14 19:30:37 +01:00
|
|
|
},
|
2017-10-05 12:35:01 +02:00
|
|
|
Out::NotFound(reason) => {
|
2018-10-22 09:40:50 +02:00
|
|
|
hyper::Response::builder()
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
.header("content-type", HeaderValue::from_static("text/plain; charset=utf-8"))
|
|
|
|
.body(reason.into())
|
2017-02-14 19:30:37 +01:00
|
|
|
},
|
2017-10-05 12:35:01 +02:00
|
|
|
Out::Bad(reason) => {
|
2018-10-22 09:40:50 +02:00
|
|
|
hyper::Response::builder()
|
|
|
|
.status(StatusCode::BAD_REQUEST)
|
|
|
|
.header("content-type", HeaderValue::from_static("text/plain; charset=utf-8"))
|
|
|
|
.body(reason.into())
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
2018-10-22 09:40:50 +02:00
|
|
|
}.expect("Response builder: Parsing 'content-type' header name will not fail; qed");
|
2017-02-24 10:32:42 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
if let Some(cors_header) = cors_header {
|
2018-10-22 09:40:50 +02:00
|
|
|
res.headers_mut().append(header::ACCESS_CONTROL_ALLOW_ORIGIN, cors_header);
|
|
|
|
res.headers_mut().append(header::VARY, HeaderValue::from_static("origin"));
|
2017-02-24 10:32:42 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
future::ok(res)
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 10:32:42 +01:00
|
|
|
/// Add current interface (default: "127.0.0.1:5001") to list of allowed hosts
|
2017-03-22 07:02:14 +01:00
|
|
|
fn include_current_interface(mut hosts: Vec<Host>, interface: String, port: u16) -> Vec<Host> {
|
2017-02-24 10:32:42 +01:00
|
|
|
hosts.push(match port {
|
|
|
|
80 => interface,
|
|
|
|
_ => format!("{}:{}", interface, port),
|
2017-03-22 07:02:14 +01:00
|
|
|
}.into());
|
2017-02-24 10:32:42 +01:00
|
|
|
|
|
|
|
hosts
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Listening {
|
|
|
|
close: Option<futures::sync::oneshot::Sender<()>>,
|
|
|
|
thread: Option<thread::JoinHandle<()>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Listening {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.close.take().unwrap().send(()).unwrap();
|
|
|
|
let _ = self.thread.take().unwrap().join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 10:32:42 +01:00
|
|
|
pub fn start_server(
|
|
|
|
port: u16,
|
|
|
|
interface: String,
|
2017-03-22 07:02:14 +01:00
|
|
|
cors: DomainsValidation<AccessControlAllowOrigin>,
|
|
|
|
hosts: DomainsValidation<Host>,
|
2017-02-24 10:32:42 +01:00
|
|
|
client: Arc<BlockChainClient>
|
|
|
|
) -> Result<Listening, ServerError> {
|
|
|
|
|
|
|
|
let ip: IpAddr = interface.parse().map_err(|_| ServerError::InvalidInterface)?;
|
|
|
|
let addr = SocketAddr::new(ip, port);
|
2017-03-22 07:02:14 +01:00
|
|
|
let hosts: Option<Vec<_>> = hosts.into();
|
|
|
|
let hosts: DomainsValidation<_> = hosts.map(move |hosts| include_current_interface(hosts, interface, port)).into();
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
let (close, shutdown_signal) = futures::sync::oneshot::channel::<()>();
|
2018-10-22 09:40:50 +02:00
|
|
|
let (tx, rx) = mpsc::sync_channel::<Result<(), ServerError>>(1);
|
2017-10-05 12:35:01 +02:00
|
|
|
let thread = thread::spawn(move || {
|
|
|
|
let send = |res| tx.send(res).expect("rx end is never dropped; qed");
|
2018-10-22 09:40:50 +02:00
|
|
|
|
|
|
|
let server_bldr = match server::Server::try_bind(&addr) {
|
|
|
|
Ok(s) => s,
|
2017-10-05 12:35:01 +02:00
|
|
|
Err(err) => {
|
2018-10-22 09:40:50 +02:00
|
|
|
send(Err(ServerError::from(err)));
|
2017-10-05 12:35:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2017-02-15 19:25:57 +01:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
let new_service = move || {
|
|
|
|
Ok::<_, ServerError>(
|
|
|
|
IpfsHandler::new(cors.clone(), hosts.clone(), client.clone())
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let server = server_bldr
|
|
|
|
.serve(new_service)
|
|
|
|
.map_err(|_| ())
|
|
|
|
.select(shutdown_signal.map_err(|_| ()))
|
|
|
|
.then(|_| Ok(()));
|
|
|
|
|
|
|
|
hyper::rt::run(server);
|
|
|
|
send(Ok(()));
|
2017-10-05 12:35:01 +02:00
|
|
|
});
|
2017-02-15 19:25:57 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
// Wait for server to start successfuly.
|
|
|
|
rx.recv().expect("tx end is never dropped; qed")?;
|
2017-02-15 19:25:57 +01:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
Ok(Listening {
|
|
|
|
close: close.into(),
|
|
|
|
thread: thread.into(),
|
|
|
|
})
|
2017-02-15 19:25:57 +01:00
|
|
|
}
|