IPv6 to IPv4 translation

Create an identifier that looks like an IPv4 address if the user has made their request using IPv6. Useful for origin servers that require an IPv4 address for some reason.

VCL

Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):

Compute@Edge

Use this solution in your Compute@Edge service:

  1. Rust
let client_ip_opt = Request::get_client_ip_addr(&req);
if let Some(ip) = client_ip_opt {
let ip_v4 = get_ipv4(&ip);
req.set_header("x-ipv4", ip_v4.to_string());
}
fn get_ipv4(ip: &IpAddr) -> IpAddr {
match ip {
IpAddr::V4(ip) => IpAddr::V4(*ip),
IpAddr::V6(ip) => {
let digest = md5::compute(ip.octets());
// Map a v6 IP hash to a consistent Class E v4 IP.
IpAddr::V4(Ipv4Addr::new(
0xf0 | digest[0],
digest[1],
digest[2],
digest[3],
))
}
}
}