Skip to content

Commit

Permalink
add local IP string replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron committed Apr 1, 2024
1 parent 1508b94 commit 9c60902
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cdn-broker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ derivative.workspace = true
dashmap = "5"
parking_lot.workspace = true
rand.workspace = true
local-ip-address = "0.6.1"
22 changes: 17 additions & 5 deletions cdn-broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -46,6 +47,11 @@ pub struct Config<BrokerScheme: SignatureScheme> {
/// 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,
Expand All @@ -58,11 +64,6 @@ pub struct Config<BrokerScheme: SignatureScheme> {
#[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,

Expand Down Expand Up @@ -136,6 +137,17 @@ impl<Def: RunDef> Broker<Def> {
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(),
Expand Down
4 changes: 2 additions & 2 deletions cdn-broker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions process-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9c60902

Please sign in to comment.