1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This file is part of Substrate.

// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

//! Substrate RPC servers.

#![warn(missing_docs)]

mod middleware;

use jsonrpc_core::{IoHandlerExtension, MetaIoHandler};
use log::error;
use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
use pubsub::PubSubMetadata;
use std::io;

const MEGABYTE: usize = 1024 * 1024;

/// Maximal payload accepted by RPC servers.
pub const RPC_MAX_PAYLOAD_DEFAULT: usize = 15 * MEGABYTE;

/// Maximal buffer size in WS server.
pub const WS_MAX_BUFFER_CAPACITY_DEFAULT: usize = 16 * MEGABYTE;

/// Default maximum number of connections for WS RPC servers.
const WS_MAX_CONNECTIONS: usize = 100;

/// The RPC IoHandler containing all requested APIs.
pub type RpcHandler<T> = pubsub::PubSubHandler<T, RpcMiddleware>;

pub use middleware::{method_names, RpcMetrics, RpcMiddleware};

/// Construct rpc `IoHandler`
pub fn rpc_handler<M: PubSubMetadata>(
	extension: impl IoHandlerExtension<M>,
	rpc_middleware: RpcMiddleware,
) -> RpcHandler<M> {
	let io_handler = MetaIoHandler::with_middleware(rpc_middleware);
	let mut io = pubsub::PubSubHandler::new(io_handler);
	extension.augment(&mut io);

	// add an endpoint to list all available methods.
	let mut methods = io.iter().map(|x| x.0.clone()).collect::<Vec<String>>();
	io.add_method("rpc_methods", {
		methods.sort();
		let methods = serde_json::to_value(&methods)
			.expect("Serialization of Vec<String> is infallible; qed");

		move |_| {
			let methods = methods.clone();
			async move {
				Ok(serde_json::json!({
					"version": 1,
					"methods": methods,
				}))
			}
		}
	});
	io
}

/// RPC server-specific prometheus metrics.
#[derive(Debug, Clone, Default)]
pub struct ServerMetrics {
	/// Number of sessions opened.
	session_opened: Option<Counter<U64>>,
	/// Number of sessions closed.
	session_closed: Option<Counter<U64>>,
}

impl ServerMetrics {
	/// Create new WebSocket RPC server metrics.
	pub fn new(registry: Option<&Registry>) -> Result<Self, PrometheusError> {
		registry
			.map(|r| {
				Ok(Self {
					session_opened: register(
						Counter::new(
							"rpc_sessions_opened",
							"Number of persistent RPC sessions opened",
						)?,
						r,
					)?
					.into(),
					session_closed: register(
						Counter::new(
							"rpc_sessions_closed",
							"Number of persistent RPC sessions closed",
						)?,
						r,
					)?
					.into(),
				})
			})
			.unwrap_or_else(|| Ok(Default::default()))
	}
}

/// Type alias for ipc server
pub type IpcServer = ipc::Server;
/// Type alias for http server
pub type HttpServer = http::Server;
/// Type alias for ws server
pub type WsServer = ws::Server;

impl ws::SessionStats for ServerMetrics {
	fn open_session(&self, _id: ws::SessionId) {
		self.session_opened.as_ref().map(|m| m.inc());
	}

	fn close_session(&self, _id: ws::SessionId) {
		self.session_closed.as_ref().map(|m| m.inc());
	}
}

/// Start HTTP server listening on given address.
pub fn start_http<M: pubsub::PubSubMetadata + Default + Unpin>(
	addr: &std::net::SocketAddr,
	cors: Option<&Vec<String>>,
	io: RpcHandler<M>,
	maybe_max_payload_mb: Option<usize>,
	tokio_handle: tokio::runtime::Handle,
) -> io::Result<http::Server> {
	let max_request_body_size = maybe_max_payload_mb
		.map(|mb| mb.saturating_mul(MEGABYTE))
		.unwrap_or(RPC_MAX_PAYLOAD_DEFAULT);

	http::ServerBuilder::new(io)
		.threads(1)
		.event_loop_executor(tokio_handle)
		.health_api(("/health", "system_health"))
		.allowed_hosts(hosts_filtering(cors.is_some()))
		.rest_api(if cors.is_some() { http::RestApi::Secure } else { http::RestApi::Unsecure })
		.cors(map_cors::<http::AccessControlAllowOrigin>(cors))
		.max_request_body_size(max_request_body_size)
		.start_http(addr)
}

/// Start IPC server listening on given path.
pub fn start_ipc<M: pubsub::PubSubMetadata + Default>(
	addr: &str,
	io: RpcHandler<M>,
	server_metrics: ServerMetrics,
) -> io::Result<ipc::Server> {
	let builder = ipc::ServerBuilder::new(io);
	#[cfg(target_os = "unix")]
	builder.set_security_attributes({
		let security_attributes = ipc::SecurityAttributes::empty();
		security_attributes.set_mode(0o600)?;
		security_attributes
	});
	builder.session_stats(server_metrics).start(addr)
}

/// Start WS server listening on given address.
pub fn start_ws<
	M: pubsub::PubSubMetadata + From<futures::channel::mpsc::UnboundedSender<String>>,
>(
	addr: &std::net::SocketAddr,
	max_connections: Option<usize>,
	cors: Option<&Vec<String>>,
	io: RpcHandler<M>,
	maybe_max_payload_mb: Option<usize>,
	maybe_max_out_buffer_capacity_mb: Option<usize>,
	server_metrics: ServerMetrics,
	tokio_handle: tokio::runtime::Handle,
) -> io::Result<ws::Server> {
	let max_payload = maybe_max_payload_mb
		.map(|mb| mb.saturating_mul(MEGABYTE))
		.unwrap_or(RPC_MAX_PAYLOAD_DEFAULT);
	let max_out_buffer_capacity = maybe_max_out_buffer_capacity_mb
		.map(|mb| mb.saturating_mul(MEGABYTE))
		.unwrap_or(WS_MAX_BUFFER_CAPACITY_DEFAULT);

	if max_payload > max_out_buffer_capacity {
		log::warn!(
			"maximum payload ({}) is more than maximum output buffer ({}) size in ws server, the payload will actually be limited by the buffer size",
			max_payload,
			max_out_buffer_capacity,
		)
	}

	ws::ServerBuilder::with_meta_extractor(io, |context: &ws::RequestContext| {
		context.sender().into()
	})
	.event_loop_executor(tokio_handle)
	.max_payload(max_payload)
	.max_connections(max_connections.unwrap_or(WS_MAX_CONNECTIONS))
	.max_out_buffer_capacity(max_out_buffer_capacity)
	.allowed_origins(map_cors(cors))
	.allowed_hosts(hosts_filtering(cors.is_some()))
	.session_stats(server_metrics)
	.start(addr)
	.map_err(|err| match err {
		ws::Error::Io(io) => io,
		ws::Error::ConnectionClosed => io::ErrorKind::BrokenPipe.into(),
		e => {
			error!("{}", e);
			io::ErrorKind::Other.into()
		},
	})
}

fn map_cors<T: for<'a> From<&'a str>>(cors: Option<&Vec<String>>) -> http::DomainsValidation<T> {
	cors.map(|x| x.iter().map(AsRef::as_ref).map(Into::into).collect::<Vec<_>>())
		.into()
}

fn hosts_filtering(enable: bool) -> http::DomainsValidation<http::Host> {
	if enable {
		// NOTE The listening address is whitelisted by default.
		// Setting an empty vector here enables the validation
		// and allows only the listening address.
		http::DomainsValidation::AllowOnly(vec![])
	} else {
		http::DomainsValidation::Disabled
	}
}