From 9c60902a008961fa86b13083c4c78864f8cccfde Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 1 Apr 2024 08:50:45 -0400 Subject: [PATCH] add local IP string replacement --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++++ cdn-broker/Cargo.toml | 1 + cdn-broker/src/lib.rs | 22 +++++++++++++++++----- cdn-broker/src/main.rs | 4 ++-- process-compose.yaml | 4 ++-- 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 832407c..5d3afdf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -795,6 +795,7 @@ dependencies = [ "derive_builder", "jf-primitives", "lazy_static", + "local-ip-address", "parking_lot", "pprof", "prometheus", @@ -2220,6 +2221,18 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +[[package]] +name = "local-ip-address" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136ef34e18462b17bf39a7826f8f3bbc223341f8e83822beb8b77db9a3d49696" +dependencies = [ + "libc", + "neli", + "thiserror", + "windows-sys 0.48.0", +] + [[package]] name = "lock_api" version = "0.4.11" @@ -2369,6 +2382,31 @@ dependencies = [ "version_check", ] +[[package]] +name = "neli" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1100229e06604150b3becd61a4965d5c70f3be1759544ea7274166f4be41ef43" +dependencies = [ + "byteorder", + "libc", + "log", + "neli-proc-macros", +] + +[[package]] +name = "neli-proc-macros" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c168194d373b1e134786274020dae7fc5513d565ea2ebb9bc9ff17ffb69106d4" +dependencies = [ + "either", + "proc-macro2", + "quote", + "serde", + "syn 1.0.109", +] + [[package]] name = "nix" version = "0.26.4" diff --git a/cdn-broker/Cargo.toml b/cdn-broker/Cargo.toml index 3dace1c..945bb58 100644 --- a/cdn-broker/Cargo.toml +++ b/cdn-broker/Cargo.toml @@ -45,3 +45,4 @@ derivative.workspace = true dashmap = "5" parking_lot.workspace = true rand.workspace = true +local-ip-address = "0.6.1" \ No newline at end of file diff --git a/cdn-broker/src/lib.rs b/cdn-broker/src/lib.rs index bea7396..e62d260 100644 --- a/cdn-broker/src/lib.rs +++ b/cdn-broker/src/lib.rs @@ -34,6 +34,7 @@ use cdn_proto::{ }; use connections::Connections; use derive_builder::Builder; +use local_ip_address::local_ip; use tokio::{select, spawn}; use tracing::info; @@ -46,6 +47,11 @@ pub struct Config { /// The user (public) bind address: the public-facing address we bind to. pub public_bind_address: String, + /// The broker (private) advertise address: what other brokers use to connect to us. + pub private_advertise_address: String, + /// The broker (private) bind address: the private-facing address we bind to. + pub private_bind_address: String, + /// Whether or not we want to serve metrics #[builder(default = "true")] pub metrics_enabled: bool, @@ -58,11 +64,6 @@ pub struct Config { #[builder(default = "String::from(\"127.0.0.1\")")] pub metrics_ip: String, - /// The broker (private) advertise address: what other brokers use to connect to us. - pub private_advertise_address: String, - /// The broker (private) bind address: the private-facing address we bind to. - pub private_bind_address: String, - /// The discovery endpoint. We use this to maintain consistency between brokers and marshals. pub discovery_endpoint: String, @@ -136,6 +137,17 @@ impl Broker { ca_key_path, } = config; + // Get the local IP address so we can replace in + let local_ip = local_ip() + .expect("failed to obtain local IP and none was supplied") + .to_string(); + + // Replace "local_ip" with the actual local IP address + let public_bind_address = public_bind_address.replace("local_ip", &local_ip); + let public_advertise_address = public_advertise_address.replace("local_ip", &local_ip); + let private_bind_address = private_bind_address.replace("local_ip", &local_ip); + let private_advertise_address = private_advertise_address.replace("local_ip", &local_ip); + // Create a unique broker identifier let identity = BrokerIdentifier { public_advertise_address: public_advertise_address.clone(), diff --git a/cdn-broker/src/main.rs b/cdn-broker/src/main.rs index 9df3986..a82b0d4 100644 --- a/cdn-broker/src/main.rs +++ b/cdn-broker/src/main.rs @@ -40,7 +40,7 @@ struct Args { public_bind_address: String, /// The user-facing address to advertise - #[arg(long, default_value = "127.0.0.1:1738")] + #[arg(long, default_value = "local_ip:1738")] public_advertise_address: String, /// The broker-facing address to bind to for connections from @@ -49,7 +49,7 @@ struct Args { private_bind_address: String, /// The broker-facing address to advertise - #[arg(long, default_value = "127.0.0.1:1739")] + #[arg(long, default_value = "local_ip:1739")] private_advertise_address: String, /// The seed for broker key generation diff --git a/process-compose.yaml b/process-compose.yaml index 35ac2d4..e921b95 100644 --- a/process-compose.yaml +++ b/process-compose.yaml @@ -13,9 +13,9 @@ processes: broker_1: command: cargo run --package cdn-broker --release -- --public-bind-address 0.0.0.0:1740 - --public-advertise-address 127.0.0.1:1740 + --public-advertise-address local_ip:1740 --private-bind-address 0.0.0.0:1741 - --private-advertise-address 127.0.0.1:1741 + --private-advertise-address local_ip:1741 -d "redis://:changeme!@localhost:6379" client_0: