|
| 1 | +use clap::Parser; |
| 2 | +use sc_cli::{ |
| 3 | + Cors, RpcMethods, RPC_DEFAULT_MAX_CONNECTIONS, RPC_DEFAULT_MAX_REQUEST_SIZE_MB, |
| 4 | + RPC_DEFAULT_MAX_RESPONSE_SIZE_MB, RPC_DEFAULT_MAX_SUBS_PER_CONN, |
| 5 | + RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN, |
| 6 | +}; |
| 7 | +use sc_service::config::IpNetwork; |
| 8 | +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; |
| 9 | +use std::num::NonZeroU32; |
| 10 | + |
| 11 | +/// RPC parameters extracted from the upstream RunCmd. |
| 12 | +#[derive(Debug, Clone, Parser)] |
| 13 | +pub struct RpcParams { |
| 14 | + /// Listen to all RPC interfaces (default: local). |
| 15 | + /// |
| 16 | + /// Not all RPC methods are safe to be exposed publicly. |
| 17 | + /// |
| 18 | + /// Use an RPC proxy server to filter out dangerous methods. More details: |
| 19 | + /// <https://docs.substrate.io/build/remote-procedure-calls/#public-rpc-interfaces>. |
| 20 | + /// |
| 21 | + /// Use `--unsafe-rpc-external` to suppress the warning if you understand the risks. |
| 22 | + #[arg(long)] |
| 23 | + pub rpc_external: bool, |
| 24 | + |
| 25 | + /// Listen to all RPC interfaces. |
| 26 | + /// |
| 27 | + /// Same as `--rpc-external`. |
| 28 | + #[arg(long)] |
| 29 | + pub unsafe_rpc_external: bool, |
| 30 | + |
| 31 | + /// RPC methods to expose. |
| 32 | + #[arg( |
| 33 | + long, |
| 34 | + value_name = "METHOD SET", |
| 35 | + value_enum, |
| 36 | + ignore_case = true, |
| 37 | + default_value_t = RpcMethods::Auto, |
| 38 | + verbatim_doc_comment |
| 39 | + )] |
| 40 | + pub rpc_methods: RpcMethods, |
| 41 | + |
| 42 | + /// RPC rate limiting (calls/minute) for each connection. |
| 43 | + /// |
| 44 | + /// This is disabled by default. |
| 45 | + /// |
| 46 | + /// For example `--rpc-rate-limit 10` will maximum allow |
| 47 | + /// 10 calls per minute per connection. |
| 48 | + #[arg(long)] |
| 49 | + pub rpc_rate_limit: Option<NonZeroU32>, |
| 50 | + |
| 51 | + /// Disable RPC rate limiting for certain ip addresses. |
| 52 | + /// |
| 53 | + /// Each IP address must be in CIDR notation such as `1.2.3.4/24`. |
| 54 | + #[arg(long, num_args = 1..)] |
| 55 | + pub rpc_rate_limit_whitelisted_ips: Vec<IpNetwork>, |
| 56 | + |
| 57 | + /// Trust proxy headers for disable rate limiting. |
| 58 | + /// |
| 59 | + /// By default the rpc server will not trust headers such `X-Real-IP`, `X-Forwarded-For` and |
| 60 | + /// `Forwarded` and this option will make the rpc server to trust these headers. |
| 61 | + /// |
| 62 | + /// For instance this may be secure if the rpc server is behind a reverse proxy and that the |
| 63 | + /// proxy always sets these headers. |
| 64 | + #[arg(long)] |
| 65 | + pub rpc_rate_limit_trust_proxy_headers: bool, |
| 66 | + |
| 67 | + /// Set the maximum RPC request payload size for both HTTP and WS in megabytes. |
| 68 | + #[arg(long, default_value_t = RPC_DEFAULT_MAX_REQUEST_SIZE_MB)] |
| 69 | + pub rpc_max_request_size: u32, |
| 70 | + |
| 71 | + /// Set the maximum RPC response payload size for both HTTP and WS in megabytes. |
| 72 | + #[arg(long, default_value_t = RPC_DEFAULT_MAX_RESPONSE_SIZE_MB)] |
| 73 | + pub rpc_max_response_size: u32, |
| 74 | + |
| 75 | + /// Set the maximum concurrent subscriptions per connection. |
| 76 | + #[arg(long, default_value_t = RPC_DEFAULT_MAX_SUBS_PER_CONN)] |
| 77 | + pub rpc_max_subscriptions_per_connection: u32, |
| 78 | + |
| 79 | + /// Specify JSON-RPC server TCP port. |
| 80 | + #[arg(long, value_name = "PORT")] |
| 81 | + pub rpc_port: Option<u16>, |
| 82 | + |
| 83 | + /// Maximum number of RPC server connections. |
| 84 | + #[arg(long, value_name = "COUNT", default_value_t = RPC_DEFAULT_MAX_CONNECTIONS)] |
| 85 | + pub rpc_max_connections: u32, |
| 86 | + |
| 87 | + /// The number of messages the RPC server is allowed to keep in memory. |
| 88 | + /// |
| 89 | + /// If the buffer becomes full then the server will not process |
| 90 | + /// new messages until the connected client start reading the |
| 91 | + /// underlying messages. |
| 92 | + /// |
| 93 | + /// This applies per connection which includes both |
| 94 | + /// JSON-RPC methods calls and subscriptions. |
| 95 | + #[arg(long, default_value_t = RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)] |
| 96 | + pub rpc_message_buffer_capacity_per_connection: u32, |
| 97 | + |
| 98 | + /// Disable RPC batch requests |
| 99 | + #[arg(long, alias = "rpc_no_batch_requests", conflicts_with_all = &["rpc_max_batch_request_len"])] |
| 100 | + pub rpc_disable_batch_requests: bool, |
| 101 | + |
| 102 | + /// Limit the max length per RPC batch request |
| 103 | + #[arg(long, conflicts_with_all = &["rpc_disable_batch_requests"], value_name = "LEN")] |
| 104 | + pub rpc_max_batch_request_len: Option<u32>, |
| 105 | + |
| 106 | + /// Specify browser *origins* allowed to access the HTTP & WS RPC servers. |
| 107 | + /// |
| 108 | + /// A comma-separated list of origins (protocol://domain or special `null` |
| 109 | + /// value). Value of `all` will disable origin validation. Default is to |
| 110 | + /// allow localhost and <https://polkadot.js.org> origins. When running in |
| 111 | + /// `--dev` mode the default is to allow all origins. |
| 112 | + #[arg(long, value_name = "ORIGINS")] |
| 113 | + pub rpc_cors: Option<Cors>, |
| 114 | +} |
| 115 | + |
| 116 | +impl RpcParams { |
| 117 | + pub(crate) fn rpc_cors(&self, is_dev: bool) -> sc_cli::Result<Option<Vec<String>>> { |
| 118 | + Ok(self |
| 119 | + .rpc_cors |
| 120 | + .clone() |
| 121 | + .unwrap_or_else(|| { |
| 122 | + if is_dev { |
| 123 | + tracing::warn!("Running in --dev mode, RPC CORS has been disabled."); |
| 124 | + Cors::All |
| 125 | + } else { |
| 126 | + Cors::List(vec![ |
| 127 | + "http://localhost:*".into(), |
| 128 | + "http://127.0.0.1:*".into(), |
| 129 | + "https://localhost:*".into(), |
| 130 | + "https://127.0.0.1:*".into(), |
| 131 | + "https://polkadot.js.org".into(), |
| 132 | + ]) |
| 133 | + } |
| 134 | + }) |
| 135 | + .into()) |
| 136 | + } |
| 137 | + |
| 138 | + pub(crate) fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<SocketAddr>> { |
| 139 | + let interface = rpc_interface( |
| 140 | + self.rpc_external, |
| 141 | + self.unsafe_rpc_external, |
| 142 | + self.rpc_methods, |
| 143 | + false, // TODO: miner |
| 144 | + )?; |
| 145 | + |
| 146 | + Ok(Some(SocketAddr::new( |
| 147 | + interface, |
| 148 | + self.rpc_port.unwrap_or(default_listen_port), |
| 149 | + ))) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +fn rpc_interface( |
| 154 | + is_external: bool, |
| 155 | + is_unsafe_external: bool, |
| 156 | + rpc_methods: RpcMethods, |
| 157 | + is_validator: bool, |
| 158 | +) -> sc_cli::Result<IpAddr> { |
| 159 | + if is_external && is_validator && rpc_methods != RpcMethods::Unsafe { |
| 160 | + return Err(sc_cli::Error::Input( |
| 161 | + "--rpc-external option shouldn't be used if the node is running as \ |
| 162 | + a validator. Use `--unsafe-rpc-external` or `--rpc-methods=unsafe` if you understand \ |
| 163 | + the risks. See the options description for more information." |
| 164 | + .to_owned(), |
| 165 | + )); |
| 166 | + } |
| 167 | + |
| 168 | + if is_external || is_unsafe_external { |
| 169 | + if rpc_methods == RpcMethods::Unsafe { |
| 170 | + tracing::warn!( |
| 171 | + "It isn't safe to expose RPC publicly without a proxy server that filters \ |
| 172 | + available set of RPC methods." |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + Ok(Ipv4Addr::UNSPECIFIED.into()) |
| 177 | + } else { |
| 178 | + Ok(Ipv4Addr::LOCALHOST.into()) |
| 179 | + } |
| 180 | +} |
0 commit comments