-
Notifications
You must be signed in to change notification settings - Fork 23
/
SecurityHelper.php
104 lines (90 loc) · 3.11 KB
/
SecurityHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
class SecurityHelper
{
public function cfban($ipaddr){
$cfheaders = array(
'Content-Type: application/json',
'X-Auth-Email: '.env('CF_EMAIL'),
'X-Auth-Key: '.env('CF_KEY')
);
$data = array(
'mode' => 'block',
'configuration' => array('target' => 'ip', 'value' => $ipaddr),
'notes' => 'Banned on '.date('Y-m-d H:i:s').' by Nafezly.com Firewall'
);
$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 True;
}else{
return false;
}
}
}
public function cfunban($block_rule_id){
$cfheaders = array(
'Content-Type: application/json',
'X-Auth-Email: '.env('CF_EMAIL'),
'X-Auth-Key: '.env('CF_KEY')
);
$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);
if ($return === false){
return false;
}else{
$return = json_decode($return,true);
if(isset($return['success']) && $return['success'] == true){
return True;
}else{
return false;
}
}
}
public function blockIp($ip)
{
return $this->cfban($ip);
}
public function unBlockIp($state_id)
{
return $this->cfunban($state_id);
}
public function enableUnderAttackMode(){
$res = \Http::withHeaders([
'Content-Type'=>'application/json',
'X-Auth-Email'=>env('CF_EMAIL'),
'X-Auth-Key'=>env('CF_KEY')
])->patch("https://api.cloudflare.com/client/v4/zones/".env('CF_Z')."/settings/security_level",
['value'=>"under_attack"])->json();
if($res['success']==true){
return true;
}
return false;
}
public function disableUnderAttackMode()
{
$res = \Http::withHeaders([
'Content-Type'=>'application/json',
'X-Auth-Email'=>env('CF_EMAIL'),
'X-Auth-Key'=>env('CF_KEY')
])->patch("https://api.cloudflare.com/client/v4/zones/".env('CF_Z')."/settings/security_level",
['value'=>"medium"])->json();
if($res['success']==true){
return true;
}
return false;
}
}