Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix splice addr bug when password contains any special character #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rand = { version = "0.8", default-features = false, features = ["std", "std_rng"
redis = { version = "0.21", features = ["tokio-comp"] }
tokio = { version = "1", features = ["time"] }
log = "0.4"
url = "2.2.2"

[dev-dependencies]
anyhow = "1"
Expand Down
59 changes: 42 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,26 +475,41 @@ where
let connections = stream::iter(initial_nodes.iter().cloned())
.map(|info| async move {
let addr = match info.addr {
ConnectionAddr::Tcp(ref host, port) => match &info.redis.password {
Some(pw) => format!("redis://:{}@{}:{}", pw, host, port),
None => format!("redis://{}:{}", host, port),
},
ConnectionAddr::TcpTls { ref host, port, insecure } => match &info.redis.password {
Some(pw) if insecure => format!("rediss://:{}@{}:{}/#insecure", pw, host, port),
Some(pw) => format!("rediss://:{}@{}:{}", pw, host, port),
None if insecure => format!("rediss://{}:{}/#insecure", host, port),
None => format!("rediss://{}:{}", host, port),
},
ConnectionAddr::Tcp(ref host, port) => {
let addr = format!("redis://{}:{}", host, port);
match &info.redis.password {
Some(password) => set_password(addr, password),
None => Some(addr),
}
}
ConnectionAddr::TcpTls {
ref host,
port,
insecure,
} => {
let addr = if insecure {
format!("rediss://{}:{}", host, port)
} else {
format!("rediss://{}:{}/#insecure", host, port)
};
match &info.redis.password {
Some(password) => set_password(addr, password),
None => Some(addr),
}
}
_ => panic!("No reach."),
};

let result = connect_and_check(info).await;
match result {
Ok(conn) => Some((addr, async { conn }.boxed().shared())),
Err(e) => {
match (connect_and_check(info).await, addr) {
(Ok(conn), Some(addr)) => Some((addr, async { conn }.boxed().shared())),
(Err(e), _) => {
trace!("Failed to connect to initial node: {:?}", e);
None
},
}
(Ok(_), None) => {
trace!("Failed to parse addr, info addr");
None
}
}
})
.buffer_unordered(initial_nodes.len())
Expand Down Expand Up @@ -1138,9 +1153,10 @@ where
return None;
};
let scheme = if use_tls { "rediss" } else { "redis" };
let addr = format!("{}://{}:{}", scheme, ip, port);
match &password {
Some(pw) => Some(format!("{}://:{}@{}:{}", scheme, pw, ip, port)),
None => Some(format!("{}://{}:{}", scheme, ip, port)),
Some(pw) => set_password(addr, pw),
None => Some(addr),
}
} else {
None
Expand Down Expand Up @@ -1169,6 +1185,15 @@ fn get_password(addr: &str) -> Option<String> {
redis::parse_redis_url(addr).and_then(|url| url.password().map(|s| s.into()))
}

fn set_password(addr: String, password: &str) -> Option<String> {
if let Ok(mut url) = url::Url::parse(&addr) {
if url.set_password(Some(password)).is_ok() {
return Some(url.to_string());
}
}
None
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down