Skip to content

Commit 52f3d9c

Browse files
committed
feat!: [#591] inject full socket address in configuration
not only the port. Old: ```toml [net] port = 3001 ``` New: ```toml [net] bind_address = "0.0.0.0:3001" ```
1 parent 1bc00ce commit 52f3d9c

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

src/app.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
5757
let importer_torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval;
5858
let importer_port = settings.tracker_statistics_importer.port;
5959
// From [net] config
60-
let net_ip = "0.0.0.0".to_string();
61-
let net_port = settings.net.port;
60+
let net_ip = settings.net.bind_address.ip().to_string();
61+
let net_port = settings.net.bind_address.port();
6262
let opt_net_tsl = settings.net.tsl.clone();
6363

6464
// IMPORTANT: drop settings before starting server to avoid read locks that

src/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ mod tests {
354354
token_valid_seconds = 7257600
355355
356356
[net]
357-
port = 3001
357+
bind_address = "0.0.0.0:3001"
358358
359359
[auth]
360360
email_on_signup = "optional"

src/config/v1/net.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2+
13
use serde::{Deserialize, Serialize};
24
use url::Url;
35

@@ -11,13 +13,16 @@ use crate::config::Tsl;
1113
/// via port 443, which is a very common setup.
1214
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1315
pub struct Network {
14-
/// The port to listen on. Default to `3001`.
15-
#[serde(default = "Network::default_port")]
16-
pub port: u16,
1716
/// The base URL for the API. For example: `http://localhost`.
1817
/// If not set, the base URL will be inferred from the request.
1918
#[serde(default = "Network::default_base_url")]
2019
pub base_url: Option<Url>,
20+
/// The address the tracker will bind to.
21+
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
22+
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
23+
/// system to choose a random port, use port `0`.
24+
#[serde(default = "Network::default_bind_address")]
25+
pub bind_address: SocketAddr,
2126
/// TSL configuration.
2227
#[serde(default = "Network::default_tsl")]
2328
pub tsl: Option<Tsl>,
@@ -26,14 +31,22 @@ pub struct Network {
2631
impl Default for Network {
2732
fn default() -> Self {
2833
Self {
29-
port: Self::default_port(),
34+
bind_address: Self::default_bind_address(),
3035
base_url: Self::default_base_url(),
3136
tsl: Self::default_tsl(),
3237
}
3338
}
3439
}
3540

3641
impl Network {
42+
fn default_bind_address() -> SocketAddr {
43+
SocketAddr::new(Self::default_ip(), Self::default_port())
44+
}
45+
46+
fn default_ip() -> IpAddr {
47+
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))
48+
}
49+
3750
fn default_port() -> u16 {
3851
3001
3952
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
//! token_valid_seconds = 7257600
178178
//!
179179
//! [net]
180-
//! port = 3001
180+
//! bind_address = "0.0.0.0:3001"
181181
//!
182182
//! [auth]
183183
//! email_on_signup = "optional"

src/web/api/client/v1/contexts/settings/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod responses;
22

3+
use std::net::SocketAddr;
4+
35
use serde::{Deserialize, Serialize};
46
use url::Url;
57

@@ -40,8 +42,8 @@ pub struct Tracker {
4042

4143
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
4244
pub struct Network {
43-
pub port: u16,
4445
pub base_url: Option<String>,
46+
pub bind_address: SocketAddr,
4547
}
4648

4749
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
@@ -141,8 +143,8 @@ impl From<DomainTracker> for Tracker {
141143
impl From<DomainNetwork> for Network {
142144
fn from(net: DomainNetwork) -> Self {
143145
Self {
144-
port: net.port,
145146
base_url: net.base_url.map(|url_without_port| url_without_port.to_string()),
147+
bind_address: net.bind_address,
146148
}
147149
}
148150
}

tests/common/contexts/settings/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod responses;
22

3+
use std::net::SocketAddr;
4+
35
use serde::{Deserialize, Serialize};
46
use torrust_index::config::{
57
Api as DomainApi, ApiToken, Auth as DomainAuth, Credentials as DomainCredentials, Database as DomainDatabase,
@@ -44,8 +46,8 @@ pub struct Tracker {
4446

4547
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
4648
pub struct Network {
47-
pub port: u16,
4849
pub base_url: Option<String>,
50+
pub bind_address: SocketAddr,
4951
}
5052

5153
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
@@ -154,7 +156,7 @@ impl From<DomainTracker> for Tracker {
154156
impl From<DomainNetwork> for Network {
155157
fn from(net: DomainNetwork) -> Self {
156158
Self {
157-
port: net.port,
159+
bind_address: net.bind_address,
158160
base_url: net.base_url.as_ref().map(std::string::ToString::to_string),
159161
}
160162
}

tests/environments/isolated.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2+
13
use tempfile::TempDir;
24
use torrust_index::config;
35
use torrust_index::config::{LogLevel, FREE_PORT};
@@ -76,7 +78,7 @@ fn ephemeral(temp_dir: &TempDir) -> config::Settings {
7678
configuration.logging.log_level = LogLevel::Off; // Change to `debug` for tests debugging
7779

7880
// Ephemeral API port
79-
configuration.net.port = FREE_PORT;
81+
configuration.net.bind_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), FREE_PORT);
8082

8183
// Ephemeral Importer API port
8284
configuration.tracker_statistics_importer.port = FREE_PORT;

0 commit comments

Comments
 (0)