Skip to content

Commit

Permalink
Allow 0 value for "rpcport" to indicate rpc server should bind to any…
Browse files Browse the repository at this point in the history
… available port
  • Loading branch information
Alrighttt committed Feb 6, 2025
1 parent 927f84b commit 014c874
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ impl MmCtx {
},
None => 7783, // Default port if `rpcport` does not exist in the config
};
if port < 1000 {
// A 0 value indicates that the rpc interface should bind on any available port.
if port != 0 && port < 1000 {
return ERR!("rpcport < 1000");
}
if port > u16::MAX as u64 {
Expand Down
20 changes: 14 additions & 6 deletions mm2src/mm2_main/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ pub extern "C" fn spawn_rpc(ctx_h: u32) {
use std::env;
use std::fs::File;
use std::io::{self, BufReader};
use std::net::TcpListener;

// Reads a certificate and a key from the specified files.
fn read_certificate_and_key(
Expand Down Expand Up @@ -425,6 +426,11 @@ pub extern "C" fn spawn_rpc(ctx_h: u32) {
// By entering the context, we tie `tokio::spawn` to this executor.
let _runtime_guard = CORE.0.enter();

// Create a TcpListener
// Binds on the specified IP and port or allocates a random port if the port is 0.
let listener =
TcpListener::bind(&rpc_ip_port).unwrap_or_else(|err| panic!("Can't bind on {}: {}", rpc_ip_port, err));

if ctx.is_https() {
let cert_path = env::var("MM_CERT_PATH").unwrap_or_else(|_| "cert.pem".to_string());
let (cert_chain, privkey) = match File::open(cert_path.clone()) {
Expand All @@ -450,6 +456,7 @@ pub extern "C" fn spawn_rpc(ctx_h: u32) {
// Create a TcpListener
let incoming =
AddrIncoming::bind(&rpc_ip_port).unwrap_or_else(|err| panic!("Can't bind on {}: {}", rpc_ip_port, err));
let bound_to_addr = incoming.local_addr();
let acceptor = TlsAcceptor::builder()
.with_single_cert(cert_chain, privkey)
.unwrap_or_else(|err| panic!("Can't set certificate for TlsAcceptor: {}", err))
Expand All @@ -461,15 +468,16 @@ pub extern "C" fn spawn_rpc(ctx_h: u32) {
.serve(make_svc!(TlsStream))
.with_graceful_shutdown(get_shutdown_future!(ctx));

spawn_server!(server, ctx, rpc_ip_port.ip(), rpc_ip_port.port());
spawn_server!(server, ctx, bound_to_addr.ip(), bound_to_addr.port());
} else {
let server = Server::try_bind(&rpc_ip_port)
.unwrap_or_else(|err| panic!("Can't bind on {}: {}", rpc_ip_port, err))
let server = Server::from_tcp(listener)
.unwrap_or_else(|err| panic!("Failed to bind rpc server on {}: {}", rpc_ip_port, err))
.http1_half_close(false)
.serve(make_svc!(AddrStream))
.with_graceful_shutdown(get_shutdown_future!(ctx));
.serve(make_svc!(AddrStream));
let bound_to_addr = server.local_addr();
let graceful_shutdown_server = server.with_graceful_shutdown(get_shutdown_future!(ctx));

spawn_server!(server, ctx, rpc_ip_port.ip(), rpc_ip_port.port());
spawn_server!(graceful_shutdown_server, ctx, bound_to_addr.ip(), bound_to_addr.port());
}
}

Expand Down

0 comments on commit 014c874

Please sign in to comment.