Last active
April 23, 2017 16:09
-
-
Save b3rs3rk/ae9fe0a0f3d3e1c075cf95ed206c8ff4 to your computer and use it in GitHub Desktop.
Simple PHP script to retrieve all Netflix ranges for EU\US\SA banks and export to file as OpenVPN config routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Simple script to parse Amazon EC2 IP Range JSON file and Netflix ASN blocks into OpenVPN routes | |
| * | |
| * Running the script with the tunnel argument changes the routes to not have net_gateway added to them | |
| * in the event you are specifically routing certain traffic over the tunnel | |
| * | |
| * Created by PhpStorm. | |
| * User: b3rs3rk | |
| * Date: 11/26/2016 | |
| * Time: 6:33 PM | |
| */ | |
| if (isset($argv[1]) && $argv[1] === 'tunnel') { | |
| $gateway = ''; | |
| } else { | |
| $gateway = ' net_gateway'; | |
| } | |
| $outPutString = ''; | |
| parseAmazonJSON($outPutString); | |
| parseNetFlixASN($outPutString); | |
| if (!empty($outPutString)) { | |
| file_put_contents('netflix-routes.txt', $outPutString); | |
| } | |
| function parseAmazonJSON(&$outPutString) | |
| { | |
| $arrUs = $arrEu = $arrSa = $arrAp = $arrCn = array(); | |
| try { | |
| $awsRanges = json_decode(file_get_contents('https://ip-ranges.amazonaws.com/ip-ranges.json'), true); | |
| } catch (Exception $e) {} | |
| if (isset($awsRanges) && is_array($awsRanges)) { | |
| foreach ($awsRanges['prefixes'] AS $range) { | |
| if ($range['service'] === 'EC2' && isset($range['ip_prefix'])) { | |
| if (preg_match('/((\d+\.){3}\d+)/i', $range['ip_prefix'], $matches)) { | |
| if (stripos($range['region'], 'us') === 0) { | |
| $arrUs[] = $matches[1] . ' ' . cidr2NetmaskAddr($range['ip_prefix']); | |
| } | |
| else if (stripos($range['region'], 'eu') === 0) { | |
| $arrEu[] = $matches[1] . ' ' . cidr2NetmaskAddr($range['ip_prefix']); | |
| } | |
| else if (stripos($range['region'], 'sa') === 0) { | |
| $arrSa[] = $matches[1] . ' ' . cidr2NetmaskAddr($range['ip_prefix']); | |
| } | |
| else if (stripos($range['region'], 'ap') === 0) { | |
| $arrAp[] = $matches[1] . ' ' . cidr2NetmaskAddr($range['ip_prefix']); | |
| } | |
| else if (stripos($range['region'], 'cn') === 0) { | |
| $arrCn[] = $matches[1] . ' ' . cidr2NetmaskAddr($range['ip_prefix']); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| if (!empty($arrUs)) { | |
| writeArrToOutputString($outPutString, $arrUs, 'Amazon EC2 (United States)'); | |
| } | |
| if (!empty($arrEu)) { | |
| writeArrToOutputString($outPutString, $arrEu, 'Amazon EC2 (Europe)'); | |
| } | |
| if (!empty($arrSa)) { | |
| writeArrToOutputString($outPutString, $arrSa, 'Amazon EC2 (South America)'); | |
| } | |
| if (!empty($arrAp)) { | |
| writeArrToOutputString($outPutString, $arrAp, 'Amazon EC2 (Asia Pacific)'); | |
| } | |
| if (!empty($arrCn)) { | |
| writeArrToOutputString($outPutString, $arrCn, 'Amazon EC2 (China)'); | |
| } | |
| } | |
| function parseNetFlixASN(&$outPutString) | |
| { | |
| try { | |
| $netflixASNBlocks = file_get_contents('http://ipinfo.io/AS2906'); | |
| } catch (Exception $e2) {} | |
| if (isset($netflixASNBlocks)) { | |
| if(preg_match_all('#>((\d+\.){3}\d+\/\d+)<#i', $netflixASNBlocks, $matches)) { | |
| foreach ($matches[1] AS $RangeMatches) { | |
| $arrNetflix[] = preg_replace('#\/\d+$#i', '', $RangeMatches) . ' ' . cidr2NetmaskAddr($RangeMatches); | |
| } | |
| } | |
| } | |
| if (!empty($arrNetflix)) { | |
| writeArrToOutputString($outPutString, $arrNetflix, 'Netflix ASN'); | |
| } | |
| } | |
| function writeArrToOutputString(&$outPutString, $arrRegion, $header) | |
| { | |
| global $gateway; | |
| $outPutString .= "# {$header}" . PHP_EOL; | |
| foreach($arrRegion AS $key => $value) { | |
| $outPutString .= 'route ' . $value . $gateway . PHP_EOL; | |
| } | |
| $outPutString .= PHP_EOL; | |
| } | |
| // http://stackoverflow.com/questions/5710860/php-cidr-prefix-to-netmask | |
| function cidr2NetmaskAddr($cidr) | |
| { | |
| $ta = substr($cidr, strpos($cidr, '/') + 1) * 1; | |
| $netmask = str_split(str_pad(str_pad('', $ta, '1'), 32, '0'), 8); | |
| foreach ($netmask as &$element) $element = bindec($element); | |
| return join('.', $netmask); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I use the output of this script? I've added the output to the "Additional Config" within my "DD-WRT" VPN Client config, and the VPN will not connect. Thank you!