You are here

public function ClientIpRestore::getCloudFlareIpRanges in CloudFlare 8

Get a list of cloudflare IP Ranges.

Return value

array Listing of the CloudFlareIP edge server IP ranges

1 call to ClientIpRestore::getCloudFlareIpRanges()
ClientIpRestore::onRequest in src/EventSubscriber/ClientIpRestore.php
Restores the origination client IP delivered to Drupal from CloudFlare.

File

src/EventSubscriber/ClientIpRestore.php, line 156

Class

ClientIpRestore
Restores the true client Ip address.

Namespace

Drupal\cloudflare\EventSubscriber

Code

public function getCloudFlareIpRanges() {
  if ($cache = $this->cache
    ->get(self::CLOUDFLARE_RANGE_KEY)) {
    return $cache->data;
  }
  try {
    $ipv4_raw_listings = trim((string) $this->httpClient
      ->get(SELF::IPV4_ENDPOINTS_URL)
      ->getBody());
    $ipv6_raw_listings = trim((string) $this->httpClient
      ->get(SELF::IPV6_ENDPOINTS_URL)
      ->getBody());
    $iv4_endpoints = explode("\n", $ipv4_raw_listings);
    $iv6_endpoints = explode("\n", $ipv6_raw_listings);
    $cloudflare_ips = array_merge($iv4_endpoints, $iv6_endpoints);
    $cloudflare_ips = array_map('trim', $cloudflare_ips);
    if (empty($cloudflare_ips)) {
      $this->logger
        ->error("Unable to get a listing of CloudFlare IPs.");
      return [];
    }
    $this->cache
      ->set(SELF::CLOUDFLARE_RANGE_KEY, $cloudflare_ips, Cache::PERMANENT);
    return $cloudflare_ips;
  } catch (RequestException $exception) {
    $this->logger
      ->error("Unable to get a listing of CloudFlare IPs. " . $exception
      ->getMessage());
  }
}