Node/RPC: listens on all resolved RPC addresses

This commit is contained in:
Vincent Bernardoff 2018-07-16 15:17:27 +02:00 committed by Benjamin Canou
parent 18b45449ad
commit d7d7457fa3

View File

@ -214,16 +214,8 @@ let sanitize_cors_headers ~default headers =
String.Set.(union (of_list default)) |> String.Set.(union (of_list default)) |>
String.Set.elements String.Set.elements
let init_rpc (rpc_config: Node_config_file.rpc) node = let launch_rpc_server
match rpc_config.listen_addr with (rpc_config : Node_config_file.rpc) node (addr, port) =
| None ->
lwt_log_notice "Not listening to RPC calls." >>= fun () ->
return_none
| Some addr ->
Node_config_file.resolve_rpc_listening_addrs addr >>= function
| [] ->
failwith "Cannot resolve listening address: %S" addr
| (addr, port) :: _ ->
let host = Ipaddr.V6.to_string addr in let host = Ipaddr.V6.to_string addr in
let dir = Node.build_rpc_directory node in let dir = Node.build_rpc_directory node in
let mode = let mode =
@ -233,24 +225,34 @@ let init_rpc (rpc_config: Node_config_file.rpc) node =
`TLS (`Crt_file_path cert, `Key_file_path key, `TLS (`Crt_file_path cert, `Key_file_path key,
`No_password, `Port port) in `No_password, `Port port) in
lwt_log_notice lwt_log_notice
"Starting the RPC server listening on port %d%s." "Starting a RPC server listening on %s:%d%s."
port host port
(if rpc_config.tls = None then "" else " (TLS enabled)") >>= fun () -> (if rpc_config.tls = None then "" else " (TLS enabled)") >>= fun () ->
let cors_headers = let cors_headers =
sanitize_cors_headers sanitize_cors_headers
~default:["Content-Type"] rpc_config.cors_headers in ~default:["Content-Type"] rpc_config.cors_headers in
Lwt.catch Lwt.catch begin fun () ->
(fun () ->
RPC_server.launch ~host mode dir RPC_server.launch ~host mode dir
~media_types:Media_type.all_media_types ~media_types:Media_type.all_media_types
~cors:{ allowed_origins = rpc_config.cors_origins ; ~cors:{ allowed_origins = rpc_config.cors_origins ;
allowed_headers = cors_headers } >>= fun server -> allowed_headers = cors_headers } >>= return
return_some server) end begin function
(function | Unix.Unix_error(Unix.EADDRINUSE, "bind","") ->
|Unix.Unix_error(Unix.EADDRINUSE, "bind","") ->
fail (RPC_Port_already_in_use [(addr,port)]) fail (RPC_Port_already_in_use [(addr,port)])
| exn -> Lwt.return (error_exn exn) | exn -> Lwt.return (error_exn exn)
) end
let init_rpc (rpc_config: Node_config_file.rpc) node =
match rpc_config.listen_addr with
| None ->
lwt_log_notice "Not listening to RPC calls." >>= fun () ->
return_nil
| Some addr ->
Node_config_file.resolve_rpc_listening_addrs addr >>= function
| [] ->
failwith "Cannot resolve listening address: %S" addr
| addrs ->
map_s (launch_rpc_server rpc_config node) addrs
let init_signal () = let init_signal () =
let handler name id = try let handler name id = try
@ -275,7 +277,7 @@ let run ?verbosity ?sandbox ?checkpoint (config : Node_config_file.t) =
lwt_log_notice "Shutting down the Tezos node..." >>= fun () -> lwt_log_notice "Shutting down the Tezos node..." >>= fun () ->
Node.shutdown node >>= fun () -> Node.shutdown node >>= fun () ->
lwt_log_notice "Shutting down the RPC server..." >>= fun () -> lwt_log_notice "Shutting down the RPC server..." >>= fun () ->
Lwt_utils.may ~f:RPC_server.shutdown rpc >>= fun () -> Lwt_list.iter_s RPC_server.shutdown rpc >>= fun () ->
lwt_log_notice "BYE (%d)" x >>= fun () -> lwt_log_notice "BYE (%d)" x >>= fun () ->
Logging_unix.close () >>= fun () -> Logging_unix.close () >>= fun () ->
return_unit return_unit