@@ -80,7 +80,7 @@ impl Config {
80
80
}
81
81
}
82
82
83
- /// Start WS server listening on given address.
83
+ /// Start JSON-RPC server listening on given address.
84
84
pub async fn start_server < M : Send + Sync + ' static > (
85
85
addrs : [ SocketAddr ; 2 ] ,
86
86
cors : Option < & Vec < String > > ,
@@ -89,20 +89,23 @@ pub async fn start_server<M: Send + Sync + 'static>(
89
89
rpc_api : RpcModule < M > ,
90
90
rt : tokio:: runtime:: Handle ,
91
91
id_provider : Option < Box < dyn IdProvider > > ,
92
+ transport_label : & str ,
92
93
) -> Result < ServerHandle , Box < dyn StdError + Send + Sync > > {
93
94
let ( max_payload_in, max_payload_out, max_connections, max_subs_per_conn) =
94
95
config. deconstruct ( ) ;
95
96
96
- let ( allow_hosts, cors) = {
97
+ let host_filter = hosts_filter ( cors. is_some ( ) , & addrs) ;
98
+
99
+ let cors = {
97
100
if let Some ( cors) = cors {
98
101
let mut list = Vec :: new ( ) ;
99
102
for origin in cors {
100
103
list. push ( HeaderValue :: from_str ( origin. as_str ( ) ) ?) ;
101
104
}
102
- ( allowed_hosts ( & addrs ) , CorsLayer :: new ( ) . allow_origin ( AllowOrigin :: list ( list) ) )
105
+ CorsLayer :: new ( ) . allow_origin ( AllowOrigin :: list ( list) )
103
106
} else {
104
107
// allow all cors
105
- ( AllowHosts :: Any , CorsLayer :: permissive ( ) )
108
+ CorsLayer :: permissive ( )
106
109
}
107
110
} ;
108
111
@@ -117,7 +120,7 @@ pub async fn start_server<M: Send + Sync + 'static>(
117
120
. max_connections ( max_connections)
118
121
. max_subscriptions_per_connection ( max_subs_per_conn)
119
122
. ping_interval ( std:: time:: Duration :: from_secs ( 30 ) )
120
- . set_host_filtering ( allow_hosts )
123
+ . set_host_filtering ( host_filter )
121
124
. set_middleware ( middleware)
122
125
. custom_tokio_runtime ( rt) ;
123
126
@@ -140,23 +143,15 @@ pub async fn start_server<M: Send + Sync + 'static>(
140
143
} ;
141
144
142
145
log:: info!(
143
- "Running JSON-RPC server: addr={}, cors={:?}" ,
146
+ "Running JSON-RPC {} server: addr={}, cors={:?}" ,
147
+ transport_label,
144
148
addr. map_or_else( |_| "unknown" . to_string( ) , |a| a. to_string( ) ) ,
145
149
cors
146
150
) ;
147
151
148
152
Ok ( handle)
149
153
}
150
154
151
- fn allowed_hosts ( addrs : & [ SocketAddr ] ) -> AllowHosts {
152
- let mut hosts = Vec :: with_capacity ( addrs. len ( ) * 2 ) ;
153
- for addr in addrs {
154
- hosts. push ( format ! ( "localhost:{}" , addr. port( ) ) . into ( ) ) ;
155
- hosts. push ( format ! ( "127.0.0.1:{}" , addr. port( ) ) . into ( ) ) ;
156
- }
157
- AllowHosts :: Only ( hosts)
158
- }
159
-
160
155
fn build_rpc_api < M : Send + Sync + ' static > ( mut rpc_api : RpcModule < M > ) -> RpcModule < M > {
161
156
let mut available_methods = rpc_api. method_names ( ) . collect :: < Vec < _ > > ( ) ;
162
157
available_methods. sort ( ) ;
@@ -175,3 +170,17 @@ fn build_rpc_api<M: Send + Sync + 'static>(mut rpc_api: RpcModule<M>) -> RpcModu
175
170
fn payload_size_or_default ( size_mb : Option < usize > ) -> usize {
176
171
size_mb. map_or ( RPC_MAX_PAYLOAD_DEFAULT , |mb| mb. saturating_mul ( MEGABYTE ) )
177
172
}
173
+
174
+ fn hosts_filter ( enabled : bool , addrs : & [ SocketAddr ] ) -> AllowHosts {
175
+ if enabled {
176
+ // NOTE The listening addresses are whitelisted by default.
177
+ let mut hosts = Vec :: with_capacity ( addrs. len ( ) * 2 ) ;
178
+ for addr in addrs {
179
+ hosts. push ( format ! ( "localhost:{}" , addr. port( ) ) . into ( ) ) ;
180
+ hosts. push ( format ! ( "127.0.0.1:{}" , addr. port( ) ) . into ( ) ) ;
181
+ }
182
+ AllowHosts :: Only ( hosts)
183
+ } else {
184
+ AllowHosts :: Any
185
+ }
186
+ }
0 commit comments