Skip to content

Instantly share code, notes, and snippets.

@maxbeatty
Created June 7, 2012 21:58
Show Gist options
  • Save maxbeatty/2891868 to your computer and use it in GitHub Desktop.
Save maxbeatty/2891868 to your computer and use it in GitHub Desktop.
Is this URL from a US based AWS?
function in_ip_range($lower, $upper, $target) {
// is the target IP in the given lower and upper bounds of a range?
// if lower = 4.4.4.4 and upper is 8.8.8.8 and target is 6.6.6.6 => yes
return (ip2long($lower) <= ip2long($target) && ip2long($upper) >= ip2long($target));
}
function is_us_based_aws($url) {
$h = parse_url($url, PHP_URL_HOST);
$ip = gethostbyname($h);
$ranges = array(
// S3 ranges
array("72.21.192.0", "72.21.223.255"), // s3-1.amazonaws.com
array("207.171.160.0", "207.171.191.255"), // s3-2.amazonaws.com
array("204.246.160.0", "204.246.191.255"), // s3-us-west-1.amazonaws.com
array("205.251.192.0", "205.251.255.255"), // s3-us-west-2.amazonaws.com
// EC2 public address ranges kept up to date w/ https://forums.aws.amazon.com/ann.jspa?annID=1453
// US East (Northern Virginia)
array("72.44.32.0", "72.44.63.255"),
array("67.202.0.0", "67.202.63.255"),
array("75.101.128.0", "75.101.255.255"),
array("174.129.0.0", "174.129.255.255"),
array("204.236.192.0", "204.236.255.255"),
array("184.73.0.0", "184.73.255.255"),
array("184.72.128.0", "184.72.255.255"),
array("184.72.64.0", "184.72.127.255"),
array("50.16.0.0", "50.17.255.255"),
array("50.19.0.0", "50.19.255.255"),
array("107.20.0.0", "107.23.255.255"),
array("23.20.0.0", "23.23.255.255"),
array("54.242.0.0", "54.243.255.255"),
// US West (Oregon)
array("50.112.0.0", "50.112.255.255"),
array("54.245.0.0", "54.245.255.255"),
// US West (Northern California)
array("204.236.128.0", "204.236.191.255"),
array("184.72.0.0", "184.72.63.255"),
array("50.18.0.0", "50.18.255.255"),
array("184.169.128.0", "184.169.255.255"),
array("54.241.0.0", "54.241.255.255"),
// CloudFront ranges based on https://forums.aws.amazon.com/ann.jspa?annID=910
array("54.240.128.0", "54.240.191.254"),
array("204.246.164.0", "204.246.167.254"),
array("204.246.168.0", "204.246.171.254"),
array("204.246.174.0", "204.246.175.254"),
array("204.246.176.0", "204.246.191.254"),
array("205.251.192.0", "205.251.223.254"),
array("205.251.240.0", "205.251.243.254"),
array("205.251.249.0", "205.251.249.254"),
array("205.251.250.0", "205.251.251.254"),
array("205.251.252.0", "205.251.253.254"),
array("205.251.254.0", "205.251.254.254"),
array("216.137.32.0", "216.137.63.254")
);
foreach($ranges as $range) {
if (self::in_ip_range($range[0], $range[1], $ip) === TRUE) {
return TRUE;
}
}
// could get geoip to be super sure ...
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment