x2Fusion

PHP Cloudflare Resolver

May 21st, 2015
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.82 KB | None | 0 0
  1. <?php
  2. /** Cloudflare Resolver **
  3.  */
  4. interface ICFlareResolver {
  5.     public function resolve_cf($url);
  6.     public function isValidIp($ip);
  7.     public function isValidUrl($url);
  8.     public function isCloudFlare($host);
  9. }
  10.  
  11. class CFlareResolver implements ICFlareResolver {
  12.     private $_addr_alloc = array(
  13.                 '199.27.128.0/21',
  14.                 '173.245.48.0/20',
  15.                 '103.21.244.0/22',
  16.                 '103.22.200.0/22',
  17.                 '103.31.4.0/22',
  18.                 '141.101.64.0/18',
  19.                 '108.162.192.0/18',
  20.                 '190.93.240.0/20',
  21.                 '188.114.96.0/20',
  22.                 '197.234.240.0/22',
  23.                 '198.41.128.0/17',
  24.                 '162.158.0.0/15',
  25.                 '104.16.0.0/12',
  26.                 '172.64.0.0/13'
  27.             );
  28.    
  29.     private $_subd_alloc = array(
  30.                 'mail',
  31.                 'direct',
  32.                 'direct-connect',
  33.                 'cpanel',
  34.                 'ftp',
  35.                 'forum',
  36.                 'blog',
  37.                 'test',
  38.                 'dev',
  39.                 'webmail',
  40.                 'record',
  41.                 'homepage',
  42.                 'testing',
  43.                 'secure',
  44.                 'server',
  45.                 'news',
  46.                 'images',
  47.                 'irc',
  48.                 'groups',
  49.                 'cp',
  50.                 'info',
  51.                 'games',
  52.                 'music',
  53.                 'portal',
  54.                 'ssl',
  55.                 'dns',
  56.                 'help',
  57.                 'host',
  58.                 'www',
  59.                 'help',
  60.                 'images',
  61.                 'news',
  62.                 '1'
  63.             );
  64.            
  65.     public function resolve_cf($url) {
  66.         if(!$this->isValidUrl($url)) {
  67.             $url = "http://{$url}";
  68.         }
  69.        
  70.         if($this->isCloudFlare($url)) {
  71.             $host = parse_url($url)['host'];
  72.             if(strstr($host, 'www.')) {
  73.                 $host = explode('www.', $host)[1];
  74.             }
  75.            
  76.             foreach($this->_subd_alloc as $subdomain) {
  77.             $host_pre_ = "{$subdomain}.{$host}";
  78.                 $host_ip = $this->getIp($host_pre_);
  79.                 if($this->isValidIp($host_ip)) {
  80.                     if($this->isCloudFlare($host_ip))
  81.                         echo "{$host_pre_}:::{$host_ip}:::Cloudflare" . (isset($ARGV[0])?"\r\n":"<br>\r\n";
  82.                     else {
  83.                         echo "{$host_pre_}:::{$host_ip}:::DirectIP" . (isset($ARGV[0])?"\r\n":"<br>\r\n";
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.    
  90.     public function isValidIp($ip) {
  91.         return filter_var($ip, FILTER_VALIDATE_IP) ? true : false;
  92.     }
  93.    
  94.     public function isValidUrl($url) {
  95.         return filter_var($url, FILTER_VALIDATE_URL) ? true : false;
  96.     }
  97.    
  98.     public function isCloudFlare($host) {
  99.         if($this->isValidIp($host)) {
  100.             foreach($this->_addr_alloc as $addr_alloc) {
  101.                 if($this->cidr_match($host, $addr_alloc)) {
  102.                     return true;
  103.                 }
  104.             }
  105.             return false;
  106.         }
  107.         elseif($this->isValidUrl($host)) {
  108.             $headers = implode("\n", get_headers($host));
  109.             $headers = explode("cloudflare", $headers);
  110.             return count($headers)>1 ? true : false;
  111.         }
  112.     }
  113.    
  114.     private function getIp($host)
  115.     {
  116.         return gethostbyname($host);
  117.     }
  118.    
  119.     private function cidr_match($ip, $range) {
  120.         list ($subnet, $bits) = explode('/', $range);
  121.         $ip = ip2long($ip);
  122.         $subnet = ip2long($subnet);
  123.         $mask = -1 << (32 - $bits);
  124.         $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
  125.        return ($ip & $mask) == $subnet;
  126.     }
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment