Skip to content

Instantly share code, notes, and snippets.

@little-apps
Created May 6, 2015 01:38
Show Gist options
  • Save little-apps/08a400d4e6bc6360c409 to your computer and use it in GitHub Desktop.
Save little-apps/08a400d4e6bc6360c409 to your computer and use it in GitHub Desktop.
Transfers IP addresses from CSF to CloudFlare
<?php
// Must be run as root
// This will re-add IP addresses as Cloudflare API doesn't support getting existing blacklists IP addresses
define('CSF_DENY', '/etc/csf/csf.deny');
define('CF_TOKEN', '');
define('CF_EMAIL', '');
if (!file_exists(CSF_DENY))
throw new Exception('CSF deny file ('.CSF_DENY.') not found.');
if (!is_readable(CSF_DENY))
throw new Exception('CSF deny file ('.CSF_DENY.') is not readable. Are you running as root?');
if (($lines = file(CSF_DENY)) === false)
throw new Exception('Unable to read CSF deny file ('.CSF_DENY.')');
$ips = array();
foreach ($lines as $line) {
$matches = array();
if (preg_match('/(\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b)/', $line, $matches) > 0) {
if (!empty($matches[0]) && filter_var($matches[0], FILTER_VALIDATE_IP))
$ips[] = $matches[0];
}
}
if (!empty($ips)) {
if (($ch = curl_init('https://www.cloudflare.com/api_json.html')) === false)
throw new Exception('Unable to initialize cURL');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'a' => 'ban',
'u' => CF_EMAIL,
'tkn' => CF_TOKEN,
'key' => ''
);
foreach ($ips as $ip) {
$data['key'] = $ip;
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($ch);
$ret = json_decode($resp);
// Do something with JSON response
}
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment