-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracker.rs
More file actions
514 lines (466 loc) · 18.8 KB
/
Copy pathtracker.rs
File metadata and controls
514 lines (466 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Tracker service information for display purposes
//!
//! This module contains DTOs for the Torrust Tracker service endpoints.
use std::net::IpAddr;
use crate::domain::environment::runtime_outputs::ServiceEndpoints;
use crate::domain::grafana::GrafanaConfig;
use crate::domain::tracker::config::is_localhost;
use crate::domain::tracker::TrackerConfig;
/// Tracker service information for display purposes
///
/// This information is available for Released and Running states and shows
/// the tracker services configured for the environment.
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct ServiceInfo {
/// UDP tracker URLs (e.g., `udp://10.0.0.1:6969/announce`)
pub udp_trackers: Vec<String>,
/// HTTP tracker URLs with HTTPS via Caddy (e.g., `https://http1.tracker.local/announce`)
pub https_http_trackers: Vec<String>,
/// HTTP tracker URLs with direct access (e.g., `http://10.0.0.1:7072/announce`)
pub direct_http_trackers: Vec<String>,
/// HTTP tracker URLs that are localhost-only (internal access via SSH tunnel)
pub localhost_http_trackers: Vec<LocalhostServiceInfo>,
/// HTTP API endpoint URL (e.g., `http://10.0.0.1:1212/api` or `https://api.tracker.local/api`)
pub api_endpoint: String,
/// Whether the API endpoint uses HTTPS via Caddy
pub api_uses_https: bool,
/// Whether the API endpoint is localhost-only (not externally accessible)
pub api_is_localhost_only: bool,
/// Health check API URL (e.g., `http://10.0.0.1:1313/health_check` or `https://health.tracker.local/health_check`)
pub health_check_url: String,
/// Whether the health check endpoint uses HTTPS via Caddy
pub health_check_uses_https: bool,
/// Whether the health check endpoint is localhost-only (not externally accessible)
pub health_check_is_localhost_only: bool,
/// Domains configured for TLS services (for /etc/hosts hint)
pub tls_domains: Vec<TlsDomainInfo>,
}
/// Information about a localhost-only service (for SSH tunnel hint)
#[derive(Debug, Clone)]
pub struct LocalhostServiceInfo {
/// The service name (e.g., `http_tracker_1`)
pub service_name: String,
/// The port the service is bound to on localhost
pub port: u16,
}
/// Information about a TLS-enabled domain for /etc/hosts hint
#[derive(Debug, Clone)]
pub struct TlsDomainInfo {
/// The domain name
pub domain: String,
/// Internal port that is NOT exposed (for informational purposes)
pub internal_port: u16,
}
impl TlsDomainInfo {
/// Create a new `TlsDomainInfo`
#[must_use]
pub fn new(domain: String, internal_port: u16) -> Self {
Self {
domain,
internal_port,
}
}
}
impl ServiceInfo {
/// Create a new `ServiceInfo`
#[must_use]
#[allow(clippy::too_many_arguments)]
#[allow(clippy::fn_params_excessive_bools)]
pub fn new(
udp_trackers: Vec<String>,
https_http_trackers: Vec<String>,
direct_http_trackers: Vec<String>,
localhost_http_trackers: Vec<LocalhostServiceInfo>,
api_endpoint: String,
api_uses_https: bool,
api_is_localhost_only: bool,
health_check_url: String,
health_check_uses_https: bool,
health_check_is_localhost_only: bool,
tls_domains: Vec<TlsDomainInfo>,
) -> Self {
Self {
udp_trackers,
https_http_trackers,
direct_http_trackers,
localhost_http_trackers,
api_endpoint,
api_uses_https,
api_is_localhost_only,
health_check_url,
health_check_uses_https,
health_check_is_localhost_only,
tls_domains,
}
}
/// Build `ServiceInfo` from tracker configuration and instance IP
///
/// This method constructs service URLs by combining the configured bind
/// addresses with the actual instance IP address. It separates HTTP trackers
/// into HTTPS-enabled (via Caddy), direct HTTP access, and localhost-only groups.
///
/// # Arguments
///
/// * `tracker_config` - The tracker configuration containing service settings
/// * `instance_ip` - The IP address of the deployed instance
/// * `grafana_config` - Optional Grafana configuration (for TLS domain info)
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn from_tracker_config(
tracker_config: &TrackerConfig,
instance_ip: IpAddr,
grafana_config: Option<&GrafanaConfig>,
) -> Self {
let udp_trackers = tracker_config
.udp_trackers
.iter()
.map(|udp| {
let host = udp
.domain
.as_ref()
.map_or_else(|| instance_ip.to_string(), |d| d.as_str().to_string());
format!("udp://{}:{}/announce", host, udp.bind_address.port())
})
.collect();
// Separate HTTP trackers by TLS configuration and localhost status
let mut https_http_trackers = Vec::new();
let mut direct_http_trackers = Vec::new();
let mut localhost_http_trackers = Vec::new();
let mut tls_domains = Vec::new();
for (index, http) in tracker_config.http_trackers.iter().enumerate() {
if http.use_tls_proxy {
if let Some(domain) = &http.domain {
// TLS-enabled tracker - use HTTPS domain URL
// Note: localhost + TLS is rejected at config validation time,
// so we don't need to check for it here
https_http_trackers.push(format!("https://{}/announce", domain.as_str()));
tls_domains.push(TlsDomainInfo {
domain: domain.as_str().to_string(),
internal_port: http.bind_address.port(),
});
}
} else if is_localhost(&http.bind_address) {
// Localhost-only tracker - internal access only
localhost_http_trackers.push(LocalhostServiceInfo {
service_name: format!("http_tracker_{}", index + 1),
port: http.bind_address.port(),
});
} else {
// Non-TLS, non-localhost tracker - use direct IP URL
direct_http_trackers.push(format!(
"http://{}:{}/announce", // DevSkim: ignore DS137138
instance_ip,
http.bind_address.port()
));
}
}
// Build API endpoint based on TLS configuration and localhost status
let api_is_localhost_only = is_localhost(&tracker_config.http_api.bind_address);
let (api_endpoint, api_uses_https) = if tracker_config.http_api.use_tls_proxy {
if let Some(domain) = &tracker_config.http_api.domain {
tls_domains.push(TlsDomainInfo {
domain: domain.as_str().to_string(),
internal_port: tracker_config.http_api.bind_address.port(),
});
(format!("https://{}/api", domain.as_str()), true)
} else {
// TLS proxy without domain shouldn't happen after validation
(
format!(
"http://{}:{}/api", // DevSkim: ignore DS137138
instance_ip,
tracker_config.http_api.bind_address.port()
),
false,
)
}
} else {
(
format!(
"http://{}:{}/api", // DevSkim: ignore DS137138
instance_ip,
tracker_config.http_api.bind_address.port()
),
false,
)
};
// Add Grafana TLS domain if configured
if let Some(grafana) = grafana_config {
if let Some(domain) = grafana.tls_domain() {
tls_domains.push(TlsDomainInfo {
domain: domain.to_string(),
internal_port: 3000, // Grafana internal port
});
}
}
// Build health check URL based on TLS configuration and localhost status
let health_check_is_localhost_only =
is_localhost(&tracker_config.health_check_api.bind_address);
let (health_check_url, health_check_uses_https) =
if let Some(domain) = tracker_config.health_check_api.tls_domain() {
tls_domains.push(TlsDomainInfo {
domain: domain.to_string(),
internal_port: tracker_config.health_check_api.bind_address.port(),
});
(format!("https://{domain}/health_check"), true)
} else {
(
format!(
"http://{}:{}/health_check", // DevSkim: ignore DS137138
instance_ip,
tracker_config.health_check_api.bind_address.port()
),
false,
)
};
Self::new(
udp_trackers,
https_http_trackers,
direct_http_trackers,
localhost_http_trackers,
api_endpoint,
api_uses_https,
api_is_localhost_only,
health_check_url,
health_check_uses_https,
health_check_is_localhost_only,
tls_domains,
)
}
/// Build `ServiceInfo` from stored `ServiceEndpoints`
///
/// This method extracts service URLs from the runtime outputs
/// that were stored when services were started.
///
/// Note: This method is for backward compatibility with stored endpoints.
/// New deployments should use `from_tracker_config` which has full TLS awareness.
#[must_use]
pub fn from_service_endpoints(endpoints: &ServiceEndpoints) -> Self {
let udp_trackers = endpoints
.udp_trackers
.iter()
.map(ToString::to_string)
.collect();
// For backward compatibility, all HTTP trackers go to direct access
// (stored endpoints don't have TLS or localhost information)
let direct_http_trackers = endpoints
.http_trackers
.iter()
.map(ToString::to_string)
.collect();
let api_endpoint = endpoints
.api_endpoint
.as_ref()
.map_or_else(String::new, ToString::to_string);
let health_check_url = endpoints
.health_check_url
.as_ref()
.map_or_else(String::new, ToString::to_string);
Self::new(
udp_trackers,
Vec::new(), // No HTTPS trackers from legacy endpoints
direct_http_trackers,
Vec::new(), // No localhost tracker info from legacy endpoints
api_endpoint,
false, // Legacy endpoints don't have TLS info
false, // Legacy endpoints don't have localhost info
health_check_url,
false, // Legacy endpoints don't have health check TLS info
false, // Legacy endpoints don't have localhost info
Vec::new(), // No TLS domains from legacy endpoints
)
}
/// Returns true if any service has TLS enabled
#[must_use]
pub fn has_any_tls(&self) -> bool {
!self.tls_domains.is_empty()
}
/// Returns true if any service is localhost-only
#[must_use]
pub fn has_any_localhost_only(&self) -> bool {
self.api_is_localhost_only
|| self.health_check_is_localhost_only
|| !self.localhost_http_trackers.is_empty()
}
/// Returns all TLS domain names (for /etc/hosts hint)
#[must_use]
pub fn tls_domain_names(&self) -> Vec<&str> {
self.tls_domains.iter().map(|d| d.domain.as_str()).collect()
}
/// Returns all internal ports that are not exposed due to TLS
#[must_use]
pub fn unexposed_ports(&self) -> Vec<u16> {
self.tls_domains.iter().map(|d| d.internal_port).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_service_info() {
let services = ServiceInfo::new(
vec!["udp://10.0.0.1:6969/announce".to_string()],
vec!["https://http1.tracker.local/announce".to_string()],
vec!["http://10.0.0.1:7072/announce".to_string()], // DevSkim: ignore DS137138
vec![], // No localhost HTTP trackers
"http://10.0.0.1:1212/api".to_string(), // DevSkim: ignore DS137138
false,
false, // API not localhost-only
"http://10.0.0.1:1313/health_check".to_string(), // DevSkim: ignore DS137138
false, // Health check doesn't use HTTPS
false, // Health check not localhost-only
vec![TlsDomainInfo {
domain: "http1.tracker.local".to_string(),
internal_port: 7070,
}],
);
assert_eq!(services.udp_trackers.len(), 1);
assert_eq!(services.https_http_trackers.len(), 1);
assert_eq!(services.direct_http_trackers.len(), 1);
assert!(services.api_endpoint.contains("1212"));
assert!(!services.api_uses_https);
assert!(!services.api_is_localhost_only);
assert!(services.health_check_url.contains("1313"));
assert!(services.has_any_tls());
assert!(!services.has_any_localhost_only());
}
#[test]
fn it_should_return_tls_domain_names() {
let services = ServiceInfo::new(
vec![],
vec!["https://api.tracker.local/announce".to_string()],
vec![],
vec![],
"https://api.tracker.local/api".to_string(),
true,
false, // API not localhost-only
"http://10.0.0.1:1313/health_check".to_string(), // DevSkim: ignore DS137138
false, // Health check doesn't use HTTPS
false, // Health check not localhost-only
vec![
TlsDomainInfo {
domain: "api.tracker.local".to_string(),
internal_port: 1212,
},
TlsDomainInfo {
domain: "grafana.tracker.local".to_string(),
internal_port: 3000,
},
],
);
let domains = services.tls_domain_names();
assert_eq!(domains.len(), 2);
assert!(domains.contains(&"api.tracker.local"));
assert!(domains.contains(&"grafana.tracker.local"));
}
#[test]
fn it_should_return_unexposed_ports() {
let services = ServiceInfo::new(
vec![],
vec![],
vec![],
vec![],
"https://api.tracker.local/api".to_string(),
true,
false, // API not localhost-only
String::new(),
false, // Health check doesn't use HTTPS
false, // Health check not localhost-only
vec![
TlsDomainInfo {
domain: "api.tracker.local".to_string(),
internal_port: 1212,
},
TlsDomainInfo {
domain: "http1.tracker.local".to_string(),
internal_port: 7070,
},
],
);
let ports = services.unexposed_ports();
assert_eq!(ports.len(), 2);
assert!(ports.contains(&1212));
assert!(ports.contains(&7070));
}
#[test]
fn it_should_detect_no_tls_when_empty() {
let services = ServiceInfo::new(
vec!["udp://10.0.0.1:6969/announce".to_string()],
vec![],
vec!["http://10.0.0.1:7070/announce".to_string()], // DevSkim: ignore DS137138
vec![], // No localhost HTTP trackers
"http://10.0.0.1:1212/api".to_string(), // DevSkim: ignore DS137138
false,
false, // API not localhost-only
"http://10.0.0.1:1313/health_check".to_string(), // DevSkim: ignore DS137138
false, // Health check doesn't use HTTPS
false, // Health check not localhost-only
vec![],
);
assert!(!services.has_any_tls());
assert!(!services.has_any_localhost_only());
assert!(services.tls_domain_names().is_empty());
assert!(services.unexposed_ports().is_empty());
}
#[test]
fn it_should_detect_localhost_only_api() {
let services = ServiceInfo::new(
vec![],
vec![],
vec![],
vec![],
"http://127.0.0.1:1212/api".to_string(), // DevSkim: ignore DS137138
false,
true, // API is localhost-only
"http://10.0.0.1:1313/health_check".to_string(), // DevSkim: ignore DS137138
false,
false,
vec![],
);
assert!(services.has_any_localhost_only());
assert!(services.api_is_localhost_only);
assert!(!services.health_check_is_localhost_only);
}
#[test]
fn it_should_detect_localhost_only_health_check() {
let services = ServiceInfo::new(
vec![],
vec![],
vec![],
vec![],
"http://10.0.0.1:1212/api".to_string(), // DevSkim: ignore DS137138
false,
false,
"http://127.0.0.1:1313/health_check".to_string(), // DevSkim: ignore DS137138
false,
true, // Health check is localhost-only
vec![],
);
assert!(services.has_any_localhost_only());
assert!(!services.api_is_localhost_only);
assert!(services.health_check_is_localhost_only);
}
#[test]
fn it_should_detect_localhost_only_http_trackers() {
let services = ServiceInfo::new(
vec![],
vec![],
vec![],
vec![LocalhostServiceInfo {
service_name: "http_tracker_1".to_string(),
port: 7070,
}],
"http://10.0.0.1:1212/api".to_string(), // DevSkim: ignore DS137138
false,
false,
"http://10.0.0.1:1313/health_check".to_string(), // DevSkim: ignore DS137138
false,
false,
vec![],
);
assert!(services.has_any_localhost_only());
assert_eq!(services.localhost_http_trackers.len(), 1);
assert_eq!(services.localhost_http_trackers[0].port, 7070);
}
}