Skip to content

Commit

Permalink
Use configure_me to configure the proxy
Browse files Browse the repository at this point in the history
The proxy used a fixed port for configuration which would be a problem
if the user ran a service on the same port already. This change allows
the user to configure the port using `configure_me` which supports
passing arguments, environment variables or configuration files.
  • Loading branch information
Kixunil committed Sep 10, 2024
1 parent 35f837d commit dc394fa
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 8 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ repository = "https://github.com/rust-bitcoin/bip324"
readme = "README.md"
rust-version = "1.63.0"

[package.metadata.configure_me]
spec = "config_spec.toml"

[dependencies]
bitcoin = { version = "0.32.0" }
tokio = { version = "1.37.0", features = ["full"] }
bytes = "1.6.0"
hex = { package = "hex-conservative", version = "0.2.0" }
bip324 = { path = "../protocol" }
configure_me = "0.4.0"

[build-dependencies]
configure_me_codegen = { version = "0.4.7", default-features = false }
3 changes: 3 additions & 0 deletions proxy/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
configure_me_codegen::build_script_auto().unwrap_or_else(|e| e.report_and_exit());
}
16 changes: 16 additions & 0 deletions proxy/config_spec.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[general]
env_prefix = "BIP324_PROXY"
conf_file_param = "conf"
conf_dir_param = "conf_dir"

[[param]]
name = "bind_port"
type = "u16"
default = "1324"
doc = "The port to listen on"

[[param]]
name = "bind_host"
type = "String"
default = "\"127.0.0.1\".into()"
doc = "The address to listen on"
12 changes: 9 additions & 3 deletions proxy/src/bin/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use bytes::BytesMut;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};

configure_me::include_config!();

/// Validate and bootstrap proxy connection.
async fn proxy_conn(client: TcpStream) -> Result<(), bip324_proxy::Error> {
let remote_ip = bip324_proxy::peek_addr(&client)
Expand Down Expand Up @@ -135,12 +137,16 @@ async fn proxy_conn(client: TcpStream) -> Result<(), bip324_proxy::Error> {

#[tokio::main]
async fn main() {
let proxy = TcpListener::bind(bip324_proxy::DEFAULT_PROXY)
let (config, _) = Config::including_optional_config_files::<&[&str]>(&[])
.unwrap_or_exit();

let proxy = TcpListener::bind((&*config.bind_host, config.bind_port))
.await
.expect("Failed to bind to proxy port.");
println!(
"Listening for connections on {}",
bip324_proxy::DEFAULT_PROXY
"Listening for connections on {}:{}",
config.bind_host,
config.bind_port,
);
loop {
let (stream, _) = proxy
Expand Down
12 changes: 9 additions & 3 deletions proxy/src/bin/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use bip324_proxy::{read_v1, write_v1};
use tokio::net::{TcpListener, TcpStream};

configure_me::include_config!();

/// Validate and bootstrap proxy connection.
async fn proxy_conn(client: TcpStream) -> Result<(), bip324_proxy::Error> {
let remote_ip = bip324_proxy::peek_addr(&client).await?;
Expand Down Expand Up @@ -52,12 +54,16 @@ async fn proxy_conn(client: TcpStream) -> Result<(), bip324_proxy::Error> {

#[tokio::main]
async fn main() {
let proxy = TcpListener::bind(bip324_proxy::DEFAULT_PROXY)
let (config, _) = Config::including_optional_config_files::<&[&str]>(&[])
.unwrap_or_exit();

let proxy = TcpListener::bind((&*config.bind_host, config.bind_port))
.await
.expect("Failed to bind to proxy port.");
println!(
"Listening for connections on {}",
bip324_proxy::DEFAULT_PROXY
"Listening for connections on {}:{}",
config.bind_host,
config.bind_port,
);
loop {
let (stream, _) = proxy
Expand Down
2 changes: 0 additions & 2 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use hex::prelude::*;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;

/// Default to local host on port 1324.
pub const DEFAULT_PROXY: &str = "127.0.0.1:1324";
const DEFAULT_MAGIC: Magic = Magic::BITCOIN;
/// All V1 messages have a 24 byte header.
const V1_HEADER_BYTES: usize = 24;
Expand Down

0 comments on commit dc394fa

Please sign in to comment.