Skip to content

Commit 85ae3ec

Browse files
committed
Add redact_auth_from_url
1 parent 7d849a9 commit 85ae3ec

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

tentacle/src/runtime/tokio_runtime/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ pub use tokio::{
66
task::{JoinHandle, block_in_place, spawn_blocking, yield_now},
77
};
88

9-
use crate::service::config::{
10-
TcpSocket, TcpSocketConfig, TcpSocketTransformer, TransformerContext,
9+
use crate::{
10+
service::config::{TcpSocket, TcpSocketConfig, TcpSocketTransformer, TransformerContext},
11+
utils::redact_auth_from_url,
1112
};
1213
use socket2::{Domain, Protocol as SocketProtocol, Socket, Type};
1314
#[cfg(unix)]
@@ -170,7 +171,7 @@ async fn connect_by_proxy(
170171
io::ErrorKind::Other,
171172
format!(
172173
"socks5_connect to target_addr: {}, target_port: {} by proxy_server: {} failed, err: {}",
173-
target_addr, target_port, proxy_server_url, err
174+
target_addr, target_port, redact_auth_from_url(&proxy_server_url), err
174175
),
175176
)
176177
})

tentacle/src/utils.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use url::Url;
2+
13
use crate::{
24
multiaddr::{Multiaddr, Protocol},
35
secio::PeerId,
46
};
57
use std::{
8+
borrow::Cow,
69
iter::{self},
710
net::{IpAddr, SocketAddr},
811
};
@@ -153,6 +156,37 @@ pub fn find_type(addr: &Multiaddr) -> TransportType {
153156
.unwrap_or(TransportType::Tcp)
154157
}
155158

159+
/// Function to redact the username and password from a URL:
160+
/// This function takes a URL of the form "https://user:password@example.com/path?key=value#hash"
161+
/// and returns a modified version where the password is replaced with "****", resulting in:
162+
/// "https://user:****@example.com/path?key=value#hash".
163+
/// If the URL does not contain a username or password, it is returned unchanged.
164+
/// Returns a `Cow<'_, Url>` to manage ownership efficiently, using `Borrowed` when possible.
165+
///
166+
/// # Parameters
167+
/// - `url`: A reference to a `Url` object representing the original input URL.
168+
///
169+
/// # Returns
170+
/// - A `Cow<'_, Url>` object representing the URL with redacted credentials.
171+
pub fn redact_auth_from_url(url: &Url) -> Cow<'_, Url> {
172+
let mut owned_url = url.clone();
173+
let mut modified = false;
174+
175+
if url.username() != "" && owned_url.set_username("****").is_ok() {
176+
modified = true;
177+
}
178+
179+
if url.password().is_some() && owned_url.set_password(Some("****")).is_ok() {
180+
modified = true;
181+
}
182+
183+
if modified {
184+
Cow::Owned(owned_url)
185+
} else {
186+
Cow::Borrowed(url)
187+
}
188+
}
189+
156190
#[cfg(test)]
157191
mod test {
158192
use crate::{

0 commit comments

Comments
 (0)