1
1
//! Utilities to parse Torrust Tracker logs.
2
+ use regex:: Regex ;
2
3
use serde:: { Deserialize , Serialize } ;
3
4
4
5
const INFO_LOG_LEVEL : & str = "INFO" ;
5
-
6
- const UDP_TRACKER_TARGET : & str = "UDP TRACKER" ;
7
- const UDP_TRACKER_SOCKET_ADDR_START_PATTERN : & str = "Started on: udp://" ;
8
-
9
- const HTTP_TRACKER_TARGET : & str = "HTTP TRACKER" ;
10
- const HTTP_TRACKER_URL_START_PATTERN : & str = "Started on: " ;
11
-
12
- const HEALTH_CHECK_TARGET : & str = "HEALTH CHECK API" ;
13
- const HEALTH_CHECK_URL_START_PATTERN : & str = "Started on: " ;
6
+ const UDP_TRACKER_LOG_TARGET : & str = "UDP TRACKER" ;
7
+ const HTTP_TRACKER_LOG_TARGET : & str = "HTTP TRACKER" ;
8
+ const HEALTH_CHECK_API_LOG_TARGET : & str = "HEALTH CHECK API" ;
14
9
15
10
#[ derive( Serialize , Deserialize , Debug , Default ) ]
16
11
pub struct RunningServices {
@@ -59,19 +54,43 @@ impl RunningServices {
59
54
///
60
55
/// NOTICE: Using colors in the console output could affect this method
61
56
/// due to the hidden control chars.
57
+ ///
58
+ /// # Panics
59
+ ///
60
+ /// Will panic is the regular expression to parse the services can't be compiled.
62
61
#[ must_use]
63
62
pub fn parse_from_logs ( logs : & str ) -> Self {
64
63
let mut udp_trackers: Vec < String > = Vec :: new ( ) ;
65
64
let mut http_trackers: Vec < String > = Vec :: new ( ) ;
66
65
let mut health_checks: Vec < String > = Vec :: new ( ) ;
67
66
67
+ let udp_re = Regex :: new ( r"Started on: udp://([0-9.]+:[0-9]+)" ) . unwrap ( ) ;
68
+ let http_re = Regex :: new ( r"Started on: (https?://[0-9.]+:[0-9]+)" ) . unwrap ( ) ; // DevSkim: ignore DS137138
69
+ let health_re = Regex :: new ( r"Started on: (https?://[0-9.]+:[0-9]+)" ) . unwrap ( ) ; // DevSkim: ignore DS137138
70
+ let ansi_escape_re = Regex :: new ( r"\x1b\[[0-9;]*m" ) . unwrap ( ) ;
71
+
68
72
for line in logs. lines ( ) {
69
- if let Some ( address) = Self :: extract_udp_tracker_url ( line) {
70
- udp_trackers. push ( address) ;
71
- } else if let Some ( address) = Self :: extract_http_tracker_url ( line) {
72
- http_trackers. push ( address) ;
73
- } else if let Some ( address) = Self :: extract_health_check_api_url ( line) {
74
- health_checks. push ( format ! ( "{address}/health_check" ) ) ;
73
+ let clean_line = ansi_escape_re. replace_all ( line, "" ) ;
74
+
75
+ if !line. contains ( INFO_LOG_LEVEL ) {
76
+ continue ;
77
+ } ;
78
+
79
+ if line. contains ( UDP_TRACKER_LOG_TARGET ) {
80
+ if let Some ( captures) = udp_re. captures ( & clean_line) {
81
+ let address = Self :: replace_wildcard_ip_with_localhost ( & captures[ 1 ] ) ;
82
+ udp_trackers. push ( address) ;
83
+ }
84
+ } else if line. contains ( HTTP_TRACKER_LOG_TARGET ) {
85
+ if let Some ( captures) = http_re. captures ( & clean_line) {
86
+ let address = Self :: replace_wildcard_ip_with_localhost ( & captures[ 1 ] ) ;
87
+ http_trackers. push ( address) ;
88
+ }
89
+ } else if line. contains ( HEALTH_CHECK_API_LOG_TARGET ) {
90
+ if let Some ( captures) = health_re. captures ( & clean_line) {
91
+ let address = format ! ( "{}/health_check" , Self :: replace_wildcard_ip_with_localhost( & captures[ 1 ] ) ) ;
92
+ health_checks. push ( address) ;
93
+ }
75
94
}
76
95
}
77
96
@@ -82,34 +101,6 @@ impl RunningServices {
82
101
}
83
102
}
84
103
85
- fn extract_udp_tracker_url ( line : & str ) -> Option < String > {
86
- if !line. contains ( INFO_LOG_LEVEL ) || !line. contains ( UDP_TRACKER_TARGET ) {
87
- return None ;
88
- } ;
89
-
90
- line. find ( UDP_TRACKER_SOCKET_ADDR_START_PATTERN ) . map ( |start| {
91
- Self :: replace_wildcard_ip_with_localhost ( line[ start + UDP_TRACKER_SOCKET_ADDR_START_PATTERN . len ( ) ..] . trim ( ) )
92
- } )
93
- }
94
-
95
- fn extract_http_tracker_url ( line : & str ) -> Option < String > {
96
- if !line. contains ( INFO_LOG_LEVEL ) || !line. contains ( HTTP_TRACKER_TARGET ) {
97
- return None ;
98
- } ;
99
-
100
- line. find ( HTTP_TRACKER_URL_START_PATTERN )
101
- . map ( |start| Self :: replace_wildcard_ip_with_localhost ( line[ start + HTTP_TRACKER_URL_START_PATTERN . len ( ) ..] . trim ( ) ) )
102
- }
103
-
104
- fn extract_health_check_api_url ( line : & str ) -> Option < String > {
105
- if !line. contains ( INFO_LOG_LEVEL ) || !line. contains ( HEALTH_CHECK_TARGET ) {
106
- return None ;
107
- } ;
108
-
109
- line. find ( HEALTH_CHECK_URL_START_PATTERN )
110
- . map ( |start| Self :: replace_wildcard_ip_with_localhost ( line[ start + HEALTH_CHECK_URL_START_PATTERN . len ( ) ..] . trim ( ) ) )
111
- }
112
-
113
104
fn replace_wildcard_ip_with_localhost ( address : & str ) -> String {
114
105
address. replace ( "0.0.0.0" , "127.0.0.1" )
115
106
}
0 commit comments