use std::net::SocketAddr;
pub fn is_private_ip(ip: &str) -> bool {
if let Ok(addr) = ip.parse::<std::net::IpAddr>() {
match addr {
std::net::IpAddr::V4(ipv4) => {
ipv4.is_private()
|| ipv4.is_loopback()
|| ipv4.is_link_local()
|| ipv4.is_unspecified()
|| ipv4.is_multicast()
}
std::net::IpAddr::V6(ipv6) => {
let seg0 = ipv6.segments()[0];
ipv6.is_loopback()
|| ipv6.is_unspecified()
|| (seg0 & 0xffc0) == 0xfe80
|| (seg0 & 0xfe00) == 0xfc00
|| ipv6.is_multicast()
}
}
} else {
false
}
}
pub fn extract_client_ip(_headers: &http::HeaderMap, connect_info: &SocketAddr) -> String {
connect_info.ip().to_string()
}
pub struct TrustedProxies {
nets: Vec<(std::net::IpAddr, u32)>,
}
impl TrustedProxies {
pub fn new(specs: &[String]) -> Self {
let mut nets = Vec::with_capacity(specs.len());
for s in specs {
let trimmed = s.trim();
if trimmed.is_empty() {
continue;
}
let (ip_str, bits) = match trimmed.split_once('/') {
Some((ip_s, pre_s)) => {
match (
ip_s.trim().parse::<std::net::IpAddr>(),
pre_s.trim().parse::<u32>(),
) {
(Ok(ip), Ok(b)) => (ip, b),
_ => continue,
}
}
None => match trimmed.parse::<std::net::IpAddr>() {
Ok(ip) => (ip, if ip.is_ipv4() { 32 } else { 128 }),
_ => continue,
},
};
nets.push((ip_str, bits));
}
TrustedProxies { nets }
}
pub fn is_empty(&self) -> bool {
self.nets.is_empty()
}
pub fn contains(&self, ip: &std::net::IpAddr) -> bool {
self.nets
.iter()
.any(|(net, bits)| ip_in_net(ip, net, *bits))
}
}
fn ip_in_net(ip: &std::net::IpAddr, net: &std::net::IpAddr, bits: u32) -> bool {
match (ip, net) {
(std::net::IpAddr::V4(a), std::net::IpAddr::V4(b)) => {
let bits = bits.min(32);
if bits == 0 {
return true;
}
let mask = if bits >= 32 {
u32::MAX
} else {
!(u32::MAX >> bits)
};
(u32::from(*a) & mask) == (u32::from(*b) & mask)
}
(std::net::IpAddr::V6(a), std::net::IpAddr::V6(b)) => {
let bits = bits.min(128);
if bits == 0 {
return true;
}
let ma = a.segments();
let mb = b.segments();
let mut done = 0u32;
for i in 0..8 {
if done >= bits {
return true;
}
let remain = bits - done;
let m: u16 = if remain >= 16 {
0xFFFF
} else {
!(0xFFFFu16 >> remain)
};
if (ma[i] & m) != (mb[i] & m) {
return false;
}
done += 16;
}
true
}
_ => false,
}
}
pub fn extract_client_ip_trusted(
headers: &http::HeaderMap,
connect_info: &SocketAddr,
trusted: &TrustedProxies,
) -> String {
let peer = connect_info.ip();
if !trusted.contains(&peer) {
return peer.to_string();
}
if let Some(v) = headers
.get("CF-Connecting-IP")
.and_then(|h| h.to_str().ok())
{
if let Ok(ip) = v.trim().parse::<std::net::IpAddr>() {
if !trusted.contains(&ip) {
return ip.to_string();
}
}
}
if let Some(v) = headers.get("X-Forwarded-For").and_then(|h| h.to_str().ok()) {
let ips: Vec<std::net::IpAddr> = v
.split(',')
.filter_map(|p| p.trim().parse::<std::net::IpAddr>().ok())
.collect();
for ip in ips.iter().rev() {
if trusted.contains(ip) || is_internal_ip(ip) {
continue;
}
return ip.to_string();
}
}
peer.to_string()
}
fn is_internal_ip(ip: &std::net::IpAddr) -> bool {
match ip {
std::net::IpAddr::V4(a) => a.is_private() || a.is_loopback() || a.is_unspecified(),
std::net::IpAddr::V6(a) => {
a.is_loopback() || a.is_unspecified() || (a.segments()[0] & 0xfe00) == 0xfc00
}
}
}
pub struct WebExt;
impl WebExt {
fn split_authority(url_str: &str) -> Option<(&str, Option<&str>)> {
let rest = url_str.split_once("://")?.1;
let (authority, path_part) = match rest.find(['/', '?', '#']) {
Some(i) => (&rest[..i], Some(&rest[i..])),
None => (rest, None),
};
let host_port = authority
.rsplit_once('@')
.map(|(_, h)| h)
.unwrap_or(authority);
Some((host_port, path_part))
}
pub fn domain(url_str: &str) -> Option<String> {
let (host_port, _) = Self::split_authority(url_str)?;
if host_port.is_empty() {
return None;
}
if let Some(ipv6) = host_port.strip_prefix('[') {
return ipv6.find(']').map(|end| ipv6[..end].to_string());
}
host_port.split(':').next().map(|h| h.to_string())
}
pub fn path(url_str: &str) -> Option<String> {
let (_, path_part) = Self::split_authority(url_str)?;
match path_part {
None => Some("/".to_string()),
Some(p) => {
if p.starts_with('?') || p.starts_with('#') {
return Some("/".to_string());
}
let end = p.find(['?', '#']).unwrap_or(p.len());
Some(p[..end].to_string())
}
}
}
pub fn real_ip(headers: &[(String, String)], remote_addr: &str) -> String {
for (key, val) in headers {
if key.eq_ignore_ascii_case("x-forwarded-for") {
if let Some(ip) = val
.split(',')
.filter_map(|p| p.trim().parse::<std::net::IpAddr>().ok())
.next()
{
return ip.to_string();
}
}
if key.eq_ignore_ascii_case("x-real-ip") {
if let Ok(ip) = val.trim().parse::<std::net::IpAddr>() {
return ip.to_string();
}
}
}
let trimmed = remote_addr.trim();
if let Ok(sa) = trimmed.parse::<SocketAddr>() {
return sa.ip().to_string();
}
if let Ok(ip) = trimmed.parse::<std::net::IpAddr>() {
return ip.to_string();
}
trimmed.split(':').next().unwrap_or(trimmed).to_string()
}
pub fn is_private_ip(ip: &str) -> bool {
is_private_ip(ip)
}
pub fn build_query(params: &[(&str, &str)]) -> String {
if params.is_empty() {
return String::new();
}
let parts: Vec<String> = params
.iter()
.map(|(k, v)| format!("{}={}", Self::percent_encode(k), Self::percent_encode(v)))
.collect();
format!("?{}", parts.join("&"))
}
pub fn percent_encode(seg: &str) -> String {
const HEX: &[u8; 16] = b"0123456789ABCDEF";
let mut out = String::with_capacity(seg.len() * 3);
for &byte in seg.as_bytes() {
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
out.push(byte as char);
} else {
out.push('%');
out.push(HEX[(byte >> 4) as usize] as char);
out.push(HEX[(byte & 0x0f) as usize] as char);
}
}
out
}
pub fn mime_guess(filename: &str) -> &'static str {
let ext = std::path::Path::new(filename)
.extension()
.and_then(|e| e.to_str())
.unwrap_or("bin");
match ext.to_ascii_lowercase().as_str() {
"jpg" | "jpeg" => "image/jpeg",
"png" => "image/png",
"gif" => "image/gif",
"webp" => "image/webp",
"svg" => "image/svg+xml",
"pdf" => "application/pdf",
"json" => "application/json",
"html" | "htm" => "text/html",
"css" => "text/css",
"js" => "application/javascript",
"txt" => "text/plain",
"csv" => "text/csv",
"zip" => "application/zip",
"mp4" => "video/mp4",
"mp3" => "audio/mpeg",
"doc" => "application/msword",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"xls" => "application/vnd.ms-excel",
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
_ => "application/octet-stream",
}
}
pub fn url_decode(s: &str) -> String {
urldecode(s)
}
pub fn content_type_mime(content_type: &str) -> Option<String> {
content_type
.split(';')
.next()
.map(|s| s.trim().to_lowercase())
.filter(|s| !s.is_empty())
}
}
fn urldecode(s: &str) -> String {
fn hex_val(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(c - b'a' + 10),
b'A'..=b'F' => Some(c - b'A' + 10),
_ => None,
}
}
let bytes = s.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'%' => match (bytes.get(i + 1), bytes.get(i + 2)) {
(Some(&h), Some(&l)) => match (hex_val(h), hex_val(l)) {
(Some(h), Some(l)) => {
out.push((h << 4) | l);
i += 3;
}
_ => {
out.push(b'%');
i += 1;
}
},
_ => {
out.push(b'%');
i += 1;
}
},
b'+' => {
out.push(b' ');
i += 1;
}
b => {
out.push(b);
i += 1;
}
}
}
String::from_utf8(out).unwrap_or_else(|_| s.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_url_decode_basic() {
assert_eq!(WebExt::url_decode("hello%20world"), "hello world");
assert_eq!(WebExt::url_decode("hello+world"), "hello world");
}
#[test]
fn test_url_decode_utf8() {
assert_eq!(WebExt::url_decode("%E4%BD%A0%E5%A5%BD"), "你好");
}
#[test]
fn test_url_decode_invalid() {
assert_eq!(WebExt::url_decode("%ZZ"), "%ZZ");
assert_eq!(WebExt::url_decode("%E"), "%E");
}
#[test]
fn test_url_decode_no_encoding() {
assert_eq!(WebExt::url_decode("hello"), "hello");
}
#[test]
fn test_percent_encode_keeps_unreserved() {
assert_eq!(WebExt::percent_encode("a-b_c.d~"), "a-b_c.d~");
assert_eq!(WebExt::percent_encode("a b"), "a%20b");
assert_eq!(WebExt::percent_encode("/"), "%2F");
assert_eq!(WebExt::percent_encode("中文"), "%E4%B8%AD%E6%96%87");
}
#[test]
fn test_mime_guess_extensions() {
assert_eq!(WebExt::mime_guess("a.PNG"), "image/png");
assert_eq!(WebExt::mime_guess("a.jpeg"), "image/jpeg");
assert_eq!(WebExt::mime_guess("noext"), "application/octet-stream");
}
#[test]
fn test_content_type_mime_json() {
assert_eq!(
WebExt::content_type_mime("application/json; charset=utf-8"),
Some("application/json".to_string())
);
}
#[test]
fn test_content_type_mime_html() {
assert_eq!(
WebExt::content_type_mime("text/html"),
Some("text/html".to_string())
);
}
#[test]
fn test_content_type_mime_with_params() {
assert_eq!(
WebExt::content_type_mime("multipart/form-data; boundary=something"),
Some("multipart/form-data".to_string())
);
}
#[test]
fn test_content_type_mime_empty() {
assert_eq!(WebExt::content_type_mime(""), None);
}
#[test]
fn test_content_type_mime_case_insensitive() {
assert_eq!(
WebExt::content_type_mime("Application/JSON"),
Some("application/json".to_string())
);
}
}
#[cfg(test)]
mod extra_web_tests {
use super::*;
use std::net::{IpAddr, SocketAddr};
fn ip(s: &str) -> IpAddr {
s.parse().unwrap()
}
#[test]
fn is_private_ip_all_families() {
assert!(is_private_ip("10.1.2.3")); assert!(is_private_ip("172.16.0.1")); assert!(is_private_ip("192.168.0.1"));
assert!(is_private_ip("127.0.0.1")); assert!(is_private_ip("169.254.1.1")); assert!(is_private_ip("0.0.0.0")); assert!(is_private_ip("224.0.0.1")); assert!(!is_private_ip("8.8.8.8"));
assert!(is_private_ip("::1")); assert!(is_private_ip("::")); assert!(is_private_ip("fe80::1")); assert!(is_private_ip("fc00::1")); assert!(is_private_ip("fd12::34")); assert!(is_private_ip("ff02::1")); assert!(!is_private_ip("2606:4700::1")); assert!(!is_private_ip("not-an-ip")); }
#[test]
fn extract_client_ip_never_reads_headers() {
let mut headers = http::HeaderMap::new();
headers.insert("x-forwarded-for", "8.8.8.8".parse().unwrap());
let peer: SocketAddr = "127.0.0.1:1234".parse().unwrap();
assert_eq!(extract_client_ip(&headers, &peer), "127.0.0.1");
}
#[test]
fn trusted_proxies_parse_contains() {
let t = TrustedProxies::new(&[
"10.0.0.0/8".into(),
"192.168.1.10".into(), "fc00::/7".into(),
"garbage".into(), "".into(), "10.0.0.0/abc".into(), ]);
assert!(!t.is_empty());
assert!(t.contains(&ip("10.1.2.3")));
assert!(t.contains(&ip("192.168.1.10")));
assert!(!t.contains(&ip("192.168.1.11"))); assert!(t.contains(&ip("fd00::1"))); let v4only = TrustedProxies::new(&["172.16.0.0/12".into()]);
assert!(!v4only.contains(&ip("::1")));
let any = TrustedProxies::new(&["0.0.0.0/0".into()]);
assert!(any.contains(&ip("8.8.8.8")));
assert!(TrustedProxies::new(&[]).is_empty());
}
#[test]
fn ip_in_net_edge_prefixes() {
let t = TrustedProxies::new(&["10.0.0.0/99".into()]); assert!(t.contains(&ip("10.0.0.0")));
assert!(!t.contains(&ip("10.0.0.1"))); let t6 = TrustedProxies::new(&["fe80::/10".into()]);
assert!(t6.contains(&ip("fe80::1")));
assert!(t6.contains(&ip("febf::1"))); assert!(!t6.contains(&ip("fec0::1"))); }
fn hm(pairs: &[(&str, &str)]) -> http::HeaderMap {
let mut h = http::HeaderMap::new();
for (k, v) in pairs {
h.insert(k.parse::<http::header::HeaderName>().unwrap(), v.parse().unwrap());
}
h
}
#[test]
fn extract_client_ip_trusted_paths() {
let trusted = TrustedProxies::new(&["127.0.0.1".into(), "10.0.0.0/8".into()]);
let peer_trusted: SocketAddr = "127.0.0.1:443".parse().unwrap();
let peer_untrusted: SocketAddr = "8.8.8.8:443".parse().unwrap();
let headers = hm(&[("x-forwarded-for", "1.2.3.4")]);
assert_eq!(
extract_client_ip_trusted(&headers, &peer_untrusted, &trusted),
"8.8.8.8"
);
let headers = hm(&[("cf-connecting-ip", "203.0.113.9"), ("x-forwarded-for", "1.1.1.1")]);
assert_eq!(
extract_client_ip_trusted(&headers, &peer_trusted, &trusted),
"203.0.113.9"
);
let headers = hm(&[("cf-connecting-ip", "127.0.0.1"), ("x-forwarded-for", "198.51.100.7, 10.0.0.9")]);
assert_eq!(
extract_client_ip_trusted(&headers, &peer_trusted, &trusted),
"198.51.100.7"
);
let headers = hm(&[("x-forwarded-for", "127.0.0.1, 10.1.1.1, 192.168.5.5")]);
assert_eq!(
extract_client_ip_trusted(&headers, &peer_trusted, &trusted),
"127.0.0.1"
);
let headers = hm(&[("x-forwarded-for", "not, an, ip")]);
assert_eq!(
extract_client_ip_trusted(&headers, &peer_trusted, &trusted),
"127.0.0.1"
);
assert_eq!(
extract_client_ip_trusted(&http::HeaderMap::new(), &peer_trusted, &trusted),
"127.0.0.1"
);
}
#[test]
fn webext_domain_and_path() {
assert_eq!(
WebExt::domain("http://user:pass@ex.com:8080/a/b?c=1#frag"),
Some("ex.com".into())
);
assert_eq!(WebExt::domain("https://ex.com"), Some("ex.com".into()));
assert_eq!(WebExt::domain("http://[::1]:80/x"), Some("::1".into()));
assert_eq!(WebExt::domain("[unclosed"), None); assert_eq!(WebExt::domain("http://"), None); assert_eq!(WebExt::domain("http://[::1"), None);
assert_eq!(WebExt::path("http://ex.com/a/b?c=1"), Some("/a/b".into()));
assert_eq!(WebExt::path("http://ex.com"), Some("/".into()));
assert_eq!(WebExt::path("http://ex.com?q=1"), Some("/".into()));
assert_eq!(WebExt::path("http://ex.com#f"), Some("/".into()));
assert_eq!(WebExt::path("http://ex.com/a#f"), Some("/a".into()));
assert_eq!(WebExt::path("relative/path"), None); }
#[test]
fn webext_real_ip_resolution() {
let h = |pairs: Vec<(String, String)>| pairs;
assert_eq!(
WebExt::real_ip(
&h(vec![("X-Forwarded-For".into(), "1.2.3.4, 5.6.7.8".into())]),
"9.9.9.9:443"
),
"1.2.3.4"
);
assert_eq!(
WebExt::real_ip(&h(vec![("x-real-ip".into(), "42.42.42.42".into())]), "peer"),
"42.42.42.42"
);
assert_eq!(
WebExt::real_ip(&h(vec![("x-forwarded-for".into(), "garbage".into())]), "9.9.9.9:443"),
"9.9.9.9"
);
assert_eq!(WebExt::real_ip(&h(vec![]), "1.1.1.1"), "1.1.1.1"); assert_eq!(WebExt::real_ip(&h(vec![]), "[::1]:8080"), "::1"); assert_eq!(WebExt::real_ip(&h(vec![]), "abc:123"), "abc"); assert_eq!(WebExt::real_ip(&h(vec![]), ""), "");
assert!(WebExt::is_private_ip("10.0.0.1"));
}
#[test]
fn build_query_percent_encodes() {
assert_eq!(WebExt::build_query(&[]), "");
assert_eq!(
WebExt::build_query(&[("a b", "中/文"), ("k", "v1")]),
"?a%20b=%E4%B8%AD%2F%E6%96%87&k=v1"
);
}
#[test]
fn urldecode_invalid_utf8_falls_back() {
assert_eq!(WebExt::url_decode("%FF%FE"), "%FF%FE");
assert_eq!(WebExt::url_decode("a%"), "a%");
}
}