tc_rpc_server/
lib.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Tetcore RPC servers.
20
21#![warn(missing_docs)]
22
23mod middleware;
24
25use std::io;
26use tetsy_jsonrpc_core::{IoHandlerExtension, MetaIoHandler};
27use log::error;
28use pubsub::PubSubMetadata;
29
30/// Maximal payload accepted by RPC servers.
31const MAX_PAYLOAD: usize = 15 * 1024 * 1024;
32
33/// Default maximum number of connections for WS RPC servers.
34const WS_MAX_CONNECTIONS: usize = 100;
35
36/// The RPC IoHandler containing all requested APIs.
37pub type RpcHandler<T> = pubsub::PubSubHandler<T, RpcMiddleware>;
38
39pub use self::inner::*;
40pub use middleware::{RpcMiddleware, RpcMetrics};
41
42/// Construct rpc `IoHandler`
43pub fn rpc_handler<M: PubSubMetadata>(
44	extension: impl IoHandlerExtension<M>,
45	rpc_middleware: RpcMiddleware,
46) -> RpcHandler<M> {
47	let io_handler = MetaIoHandler::with_middleware(rpc_middleware);
48	let mut io = pubsub::PubSubHandler::new(io_handler);
49	extension.augment(&mut io);
50
51	// add an endpoint to list all available methods.
52	let mut methods = io.iter().map(|x| x.0.clone()).collect::<Vec<String>>();
53	io.add_method("rpc_methods", {
54		methods.sort();
55		let methods = serde_json::to_value(&methods)
56			.expect("Serialization of Vec<String> is infallible; qed");
57
58		move |_| Ok(serde_json::json!({
59			"version": 1,
60			"methods": methods.clone(),
61		}))
62	});
63	io
64}
65
66#[cfg(not(target_os = "unknown"))]
67mod inner {
68	use super::*;
69
70	/// Type alias for ipc server
71	pub type IpcServer = ipc::Server;
72	/// Type alias for http server
73	pub type HttpServer = http::Server;
74	/// Type alias for ws server
75	pub type WsServer = ws::Server;
76
77	/// Start HTTP server listening on given address.
78	///
79	/// **Note**: Only available if `not(target_os = "unknown")`.
80	pub fn start_http<M: pubsub::PubSubMetadata + Default>(
81		addr: &std::net::SocketAddr,
82		cors: Option<&Vec<String>>,
83		io: RpcHandler<M>,
84	) -> io::Result<http::Server> {
85		http::ServerBuilder::new(io)
86			.threads(4)
87			.health_api(("/health", "system_health"))
88			.allowed_hosts(hosts_filtering(cors.is_some()))
89			.rest_api(if cors.is_some() {
90				http::RestApi::Secure
91			} else {
92				http::RestApi::Unsecure
93			})
94			.cors(map_cors::<http::AccessControlAllowOrigin>(cors))
95			.max_request_body_size(MAX_PAYLOAD)
96			.start_http(addr)
97	}
98
99	/// Start IPC server listening on given path.
100	///
101	/// **Note**: Only available if `not(target_os = "unknown")`.
102	pub fn start_ipc<M: pubsub::PubSubMetadata + Default>(
103		addr: &str,
104		io: RpcHandler<M>,
105	) -> io::Result<ipc::Server> {
106		let builder = ipc::ServerBuilder::new(io);
107		#[cfg(target_os = "unix")]
108		builder.set_security_attributes({
109			let security_attributes = ipc::SecurityAttributes::empty();
110			security_attributes.set_mode(0o600)?;
111			security_attributes
112		});
113		builder.start(addr)
114	}
115
116	/// Start WS server listening on given address.
117	///
118	/// **Note**: Only available if `not(target_os = "unknown")`.
119	pub fn start_ws<M: pubsub::PubSubMetadata + From<tetsy_jsonrpc_core::futures::sync::mpsc::Sender<String>>> (
120		addr: &std::net::SocketAddr,
121		max_connections: Option<usize>,
122		cors: Option<&Vec<String>>,
123		io: RpcHandler<M>,
124	) -> io::Result<ws::Server> {
125		ws::ServerBuilder::with_meta_extractor(io, |context: &ws::RequestContext| context.sender().into())
126			.max_payload(MAX_PAYLOAD)
127			.max_connections(max_connections.unwrap_or(WS_MAX_CONNECTIONS))
128			.allowed_origins(map_cors(cors))
129			.allowed_hosts(hosts_filtering(cors.is_some()))
130			.start(addr)
131			.map_err(|err| match err {
132				ws::Error::Io(io) => io,
133				ws::Error::ConnectionClosed => io::ErrorKind::BrokenPipe.into(),
134				e => {
135					error!("{}", e);
136					io::ErrorKind::Other.into()
137				}
138			})
139	}
140
141	fn map_cors<T: for<'a> From<&'a str>>(
142		cors: Option<&Vec<String>>
143	) -> http::DomainsValidation<T> {
144		cors.map(|x| x.iter().map(AsRef::as_ref).map(Into::into).collect::<Vec<_>>()).into()
145	}
146
147	fn hosts_filtering(enable: bool) -> http::DomainsValidation<http::Host> {
148		if enable {
149			// NOTE The listening address is whitelisted by default.
150			// Setting an empty vector here enables the validation
151			// and allows only the listening address.
152			http::DomainsValidation::AllowOnly(vec![])
153		} else {
154			http::DomainsValidation::Disabled
155		}
156	}
157}
158
159#[cfg(target_os = "unknown")]
160mod inner {
161}