fix proxy ip getter

This commit is contained in:
blek 2023-10-26 18:30:21 +10:00
parent c1220cac79
commit 6f9ef9be69
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 16 additions and 1 deletions

View File

@ -45,6 +45,7 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
let env_var = get_var::<&str, String>("PROXY_IP")?; let env_var = get_var::<&str, String>("PROXY_IP")?;
let ip = env_var.parse::<IpAddr>(); let ip = env_var.parse::<IpAddr>();
let ret =
if let Ok(ip) = ip { if let Ok(ip) = ip {
if ip == IpAddr::from([127, 0, 0, 1]) { if ip == IpAddr::from([127, 0, 0, 1]) {
log::warn!("Proxy address is 127.0.0.1. No proxy will be trusted") log::warn!("Proxy address is 127.0.0.1. No proxy will be trusted")
@ -56,6 +57,13 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
} }
ip ip
} else { } else {
let mut env_var = env_var;
// add port if not added
if env_var.split(":").collect::<Vec<&str>>().len() == 1 {
env_var.push_str(":80");
}
let sock = env_var.to_socket_addrs(); let sock = env_var.to_socket_addrs();
if let Err(err) = sock { if let Err(err) = sock {
return Err(format!("Can't resolve {env_var}: {:?}", err).into()); return Err(format!("Can't resolve {env_var}: {:?}", err).into());
@ -64,8 +72,15 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
if addrs.len() == 0 { if addrs.len() == 0 {
return Err(format!("{env_var} resolved to nothing").into()); return Err(format!("{env_var} resolved to nothing").into());
} }
addrs.next().unwrap().ip() let addr = addrs.next().unwrap().ip();
addr
};
#[cfg(debug_assertions)] {
if ret != IpAddr::from([ 127, 0, 0, 1 ]) {
log::debug!("Proxy ip is {}", ret)
} }
}
ret
}, },
redis: Redis { redis: Redis {
pass: get_var("REDIS_PASS")?, pass: get_var("REDIS_PASS")?,