1#![warn(missing_docs)]
22
23mod middleware;
24
25use std::io;
26use tetsy_jsonrpc_core::{IoHandlerExtension, MetaIoHandler};
27use log::error;
28use pubsub::PubSubMetadata;
29
30const MAX_PAYLOAD: usize = 15 * 1024 * 1024;
32
33const WS_MAX_CONNECTIONS: usize = 100;
35
36pub type RpcHandler<T> = pubsub::PubSubHandler<T, RpcMiddleware>;
38
39pub use self::inner::*;
40pub use middleware::{RpcMiddleware, RpcMetrics};
41
42pub 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 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 pub type IpcServer = ipc::Server;
72 pub type HttpServer = http::Server;
74 pub type WsServer = ws::Server;
76
77 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 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 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 http::DomainsValidation::AllowOnly(vec![])
153 } else {
154 http::DomainsValidation::Disabled
155 }
156 }
157}
158
159#[cfg(target_os = "unknown")]
160mod inner {
161}