0.34.13_cleaup_code
This commit is contained in:
1 parent
ec46a96a29
commit
5c2e50ec06
87 files changed
+4890
-1338
No files matched your search
+68
-29
@@ -221,8 +221,15 @@ impl Compiled {
|
||||
include_original_url: bool,
|
||||
) -> Result<Context> {
|
||||
self.context_with_peer(
|
||||
incoming, peer, local, tls, authority, uri, include_original_url,
|
||||
None, false,
|
||||
incoming,
|
||||
peer,
|
||||
local,
|
||||
tls,
|
||||
authority,
|
||||
uri,
|
||||
include_original_url,
|
||||
None,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -245,7 +252,9 @@ impl Compiled {
|
||||
if !trusted_peer {
|
||||
let scheme = if tls { "https" } else { "http" };
|
||||
let authority_text = authority.to_str()?;
|
||||
if !validated_host { crate::config::request_host(authority_text)?; }
|
||||
if !validated_host {
|
||||
crate::config::request_host(authority_text)?;
|
||||
}
|
||||
let target = uri.path_and_query().map(|v| v.as_str()).unwrap_or("/");
|
||||
let original_url = if include_original_url {
|
||||
format!("{scheme}://{authority_text}{target}")
|
||||
@@ -267,12 +276,19 @@ impl Compiled {
|
||||
};
|
||||
let by_text = cached.is_none().then(|| node(canonical(local.ip())));
|
||||
let peer_node = cached.map_or_else(
|
||||
|| if peer_ip.is_ipv4() { peer_text.as_deref().unwrap_or("") }
|
||||
else { ipv6_node.as_deref().unwrap_or("") },
|
||||
|| {
|
||||
if peer_ip.is_ipv4() {
|
||||
peer_text.as_deref().unwrap_or("")
|
||||
} else {
|
||||
ipv6_node.as_deref().unwrap_or("")
|
||||
}
|
||||
},
|
||||
|cached| cached.peer_node.as_str(),
|
||||
);
|
||||
let by_node = cached.map_or_else(|| by_text.as_deref().unwrap_or(""),
|
||||
|cached| cached.by_node.as_str());
|
||||
let by_node = cached.map_or_else(
|
||||
|| by_text.as_deref().unwrap_or(""),
|
||||
|cached| cached.by_node.as_str(),
|
||||
);
|
||||
let forwarded = format!(
|
||||
"for={peer_node};by={by_node};proto={scheme};host=\"{authority_text}\""
|
||||
);
|
||||
@@ -282,11 +298,14 @@ impl Compiled {
|
||||
values.insert("x-real-ip", peer_header);
|
||||
values.insert("x-forwarded-host", authority.clone());
|
||||
values.insert("x-forwarded-proto", HeaderValue::from_static(scheme));
|
||||
values.insert("x-forwarded-port", if let Some(cached) = cached {
|
||||
cached.port_header.clone()
|
||||
} else {
|
||||
local.port().to_string().parse()?
|
||||
});
|
||||
values.insert(
|
||||
"x-forwarded-port",
|
||||
if let Some(cached) = cached {
|
||||
cached.port_header.clone()
|
||||
} else {
|
||||
local.port().to_string().parse()?
|
||||
},
|
||||
);
|
||||
if let Some(peer) = cached {
|
||||
peer.remember_untrusted(authority, tls, &values);
|
||||
}
|
||||
@@ -320,7 +339,9 @@ impl Compiled {
|
||||
};
|
||||
}
|
||||
let authority_text = authority.to_str()?;
|
||||
if !validated_host { crate::config::request_host(authority_text)?; }
|
||||
if !validated_host {
|
||||
crate::config::request_host(authority_text)?;
|
||||
}
|
||||
let original_target = uri.path_and_query().map(|v| v.as_str()).unwrap_or("/");
|
||||
let original_url = if include_original_url {
|
||||
format!("{scheme}://{authority_text}{original_target}")
|
||||
@@ -331,24 +352,36 @@ impl Compiled {
|
||||
let mut forwarded = String::with_capacity(ips.len() * 52 + 80 + authority_text.len());
|
||||
let mut xff = String::with_capacity(ips.len() * 44);
|
||||
for (index, ip) in ips.iter().enumerate() {
|
||||
if index > 0 { forwarded.push_str(", "); xff.push_str(", "); }
|
||||
if index > 0 {
|
||||
forwarded.push_str(", ");
|
||||
xff.push_str(", ");
|
||||
}
|
||||
forwarded.push_str("for=");
|
||||
if index + 1 == ips.len() {
|
||||
if let Some(cached) = cached { forwarded.push_str(&cached.peer_node); }
|
||||
else { forwarded.push_str(&node(*ip)); }
|
||||
if let Some(cached) = cached {
|
||||
forwarded.push_str(&cached.peer_node);
|
||||
} else {
|
||||
forwarded.push_str(&node(*ip));
|
||||
}
|
||||
} else {
|
||||
forwarded.push_str(&node(*ip));
|
||||
}
|
||||
if index + 1 == ips.len() {
|
||||
if let Some(cached) = cached { xff.push_str(cached.peer_header.to_str()?); }
|
||||
else { write!(&mut xff, "{ip}")?; }
|
||||
if let Some(cached) = cached {
|
||||
xff.push_str(cached.peer_header.to_str()?);
|
||||
} else {
|
||||
write!(&mut xff, "{ip}")?;
|
||||
}
|
||||
} else {
|
||||
write!(&mut xff, "{ip}")?;
|
||||
}
|
||||
}
|
||||
forwarded.push_str(";by=");
|
||||
if let Some(cached) = cached { forwarded.push_str(&cached.by_node); }
|
||||
else { forwarded.push_str(&node(canonical(local.ip()))); }
|
||||
if let Some(cached) = cached {
|
||||
forwarded.push_str(&cached.by_node);
|
||||
} else {
|
||||
forwarded.push_str(&node(canonical(local.ip())));
|
||||
}
|
||||
write!(&mut forwarded, ";proto={scheme};host=\"{authority_text}\"")?;
|
||||
let mut values = HeaderMap::with_capacity(7);
|
||||
values.insert("forwarded", forwarded.parse()?);
|
||||
@@ -364,11 +397,14 @@ impl Compiled {
|
||||
} else {
|
||||
local.port()
|
||||
};
|
||||
values.insert("x-forwarded-port", if cached.is_some() && !(trusted_peer && self.trust_proto) {
|
||||
cached.expect("tested above").port_header.clone()
|
||||
} else {
|
||||
port.to_string().parse()?
|
||||
});
|
||||
values.insert(
|
||||
"x-forwarded-port",
|
||||
if cached.is_some() && !(trusted_peer && self.trust_proto) {
|
||||
cached.expect("tested above").port_header.clone()
|
||||
} else {
|
||||
port.to_string().parse()?
|
||||
},
|
||||
);
|
||||
values.insert("x-forwarded-uri", original_target.parse()?);
|
||||
Ok(Context {
|
||||
client_ip,
|
||||
@@ -423,14 +459,15 @@ impl Context {
|
||||
for name in headers.keys() {
|
||||
let text = name.as_str();
|
||||
if matches!(text, "x-original-url" | "x-original-method")
|
||||
|| (text.starts_with("x-forwarded-")
|
||||
&& !self.values.contains_key(text))
|
||||
|| (text.starts_with("x-forwarded-") && !self.values.contains_key(text))
|
||||
{
|
||||
remove.get_or_insert_with(Vec::new).push(name.clone());
|
||||
}
|
||||
}
|
||||
if let Some(remove) = remove {
|
||||
for name in remove { headers.remove(name); }
|
||||
for name in remove {
|
||||
headers.remove(name);
|
||||
}
|
||||
}
|
||||
if !include_client_ip {
|
||||
for name in ["forwarded", "x-forwarded-for", "x-real-ip"] {
|
||||
@@ -438,7 +475,9 @@ impl Context {
|
||||
}
|
||||
}
|
||||
for (name, value) in &self.values {
|
||||
if !include_client_ip && matches!(name.as_str(), "forwarded" | "x-forwarded-for" | "x-real-ip") {
|
||||
if !include_client_ip
|
||||
&& matches!(name.as_str(), "forwarded" | "x-forwarded-for" | "x-real-ip")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
headers.insert(name.clone(), value.clone());
|
||||
|
||||
Reference in new issue
Block a user