Skip to content

Commit 1475ead

Browse files
committed
refactor: [#852] eenrich field types in UdpTracker config struct
1 parent 384e9f8 commit 1475ead

File tree

5 files changed

+14
-21
lines changed

5 files changed

+14
-21
lines changed

packages/configuration/src/v1/udp_tracker.rs

+4-2
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

35
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
@@ -8,13 +10,13 @@ pub struct UdpTracker {
810
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
911
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
1012
/// system to choose a random port, use port `0`.
11-
pub bind_address: String,
13+
pub bind_address: SocketAddr,
1214
}
1315
impl Default for UdpTracker {
1416
fn default() -> Self {
1517
Self {
1618
enabled: false,
17-
bind_address: String::from("0.0.0.0:6969"),
19+
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 6969),
1820
}
1921
}
2022
}

packages/test-helpers/src/configuration.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tracker configuration factories for testing.
22
use std::env;
3-
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
3+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
44

55
use torrust_tracker_configuration::Configuration;
66
use torrust_tracker_primitives::TrackerMode;
@@ -44,7 +44,7 @@ pub fn ephemeral() -> Configuration {
4444
// Ephemeral socket address for UDP tracker
4545
let udp_port = 0u16;
4646
config.udp_trackers[0].enabled = true;
47-
config.udp_trackers[0].bind_address = format!("127.0.0.1:{}", &udp_port);
47+
config.udp_trackers[0].bind_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), udp_port);
4848

4949
// Ephemeral socket address for HTTP tracker
5050
let http_port = 0u16;
@@ -136,10 +136,10 @@ pub fn ephemeral_with_external_ip(ip: IpAddr) -> Configuration {
136136
pub fn ephemeral_ipv6() -> Configuration {
137137
let mut cfg = ephemeral();
138138

139-
let ipv6 = format!("[::]:{}", 0);
139+
let ipv6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), 0);
140140

141-
cfg.http_api.bind_address.clone_from(&ipv6);
142-
cfg.http_trackers[0].bind_address.clone_from(&ipv6);
141+
cfg.http_api.bind_address.clone_from(&ipv6.to_string());
142+
cfg.http_trackers[0].bind_address.clone_from(&ipv6.to_string());
143143
cfg.udp_trackers[0].bind_address = ipv6;
144144

145145
cfg

src/bootstrap/jobs/udp_tracker.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ use crate::servers::udp::server::{Launcher, UdpServer};
2727
/// It will panic if the task did not finish successfully.
2828
#[must_use]
2929
pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>, form: ServiceRegistrationForm) -> JoinHandle<()> {
30-
let bind_to = config
31-
.bind_address
32-
.parse::<std::net::SocketAddr>()
33-
.expect("it should have a valid udp tracker bind address");
30+
let bind_to = config.bind_address;
3431

3532
let server = UdpServer::new(Launcher::new(bind_to))
3633
.start(tracker, form)

src/servers/udp/server.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -463,19 +463,16 @@ mod tests {
463463
let cfg = Arc::new(ephemeral_mode_public());
464464
let tracker = initialize_with_configuration(&cfg);
465465
let config = &cfg.udp_trackers[0];
466-
467-
let bind_to = config
468-
.bind_address
469-
.parse::<std::net::SocketAddr>()
470-
.expect("Tracker API bind_address invalid.");
471-
466+
let bind_to = config.bind_address;
472467
let register = &Registar::default();
473468

474469
let stopped = UdpServer::new(Launcher::new(bind_to));
470+
475471
let started = stopped
476472
.start(tracker, register.give_form())
477473
.await
478474
.expect("it should start the server");
475+
479476
let stopped = started.stop().await.expect("it should stop the server");
480477

481478
tokio::time::sleep(Duration::from_secs(1)).await;

tests/servers/udp/environment.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ impl Environment<Stopped> {
3131

3232
let config = Arc::new(configuration.udp_trackers[0].clone());
3333

34-
let bind_to = config
35-
.bind_address
36-
.parse::<std::net::SocketAddr>()
37-
.expect("Tracker API bind_address invalid.");
34+
let bind_to = config.bind_address;
3835

3936
let server = UdpServer::new(Launcher::new(bind_to));
4037

0 commit comments

Comments
 (0)