Created
October 11, 2016 15:21
-
-
Save andrieslouw/3c833332cbf66f95ca6751f82013acf5 to your computer and use it in GitHub Desktop.
Cloudflare V4 PHP API - block / unblock IP in firewall
This file contains 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 | |
/** | |
* @author https://github.com/andrieslouw | |
* @copyright 2016 | |
**/ | |
function cfban($ipaddr){ | |
$cfheaders = array( | |
'Content-Type: application/json', | |
'X-Auth-Email: your@email.com', | |
'X-Auth-Key: yourauthkeyhere' | |
); | |
$data = array( | |
'mode' => 'block', | |
'configuration' => array('target' => 'ip', 'value' => $ipaddr), | |
'notes' => 'Banned on '.date('Y-m-d H:i:s').' by PHP-script' | |
); | |
$json = json_encode($data); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $cfheaders); | |
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules'); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
if ($return === false){ | |
return false; | |
}else{ | |
$return = json_decode($return,true); | |
if(isset($return['success']) && $return['success'] == true){ | |
return $return['result']['id']; | |
}else{ | |
return false; | |
} | |
} | |
} | |
function cfunban($block_rule_id){ | |
$cfheaders = array( | |
'Content-Type: application/json', | |
'X-Auth-Email: your@email.com', | |
'X-Auth-Key: yourauthkeyhere' | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $cfheaders); | |
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules/'.$block_rule_id); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
if ($return === false){ | |
return false; | |
}else{ | |
$return = json_decode($return,true); | |
if(isset($return['success']) && $return['success'] == true){ | |
return $return['result']['id']; | |
}else{ | |
return false; | |
} | |
} | |
} | |
?> |
where I get the block_rule_id?
where I get the block_rule_id?
cfban function returns id on succesful ban
Is it possible to batch requests without foreach loop in order to block or unblock a list of IP addresses.
The objective is to issue one HTTP request for increased performance.
Does this even exist in the CloudFlare api? How to do it with your script?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to use your script but I am a nobe at scripting. After playing around with your script didn't reach anywhere. I want to build a script to ban Ip based on its rpm on my server and eventually removing the ban after a while say 600 seconds or 20 min. using Cloudflare and any other scan script. The idea behind is to block any DDoS attack.