-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoGuard.php
More file actions
248 lines (194 loc) · 7.2 KB
/
Copy pathGeoGuard.php
File metadata and controls
248 lines (194 loc) · 7.2 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
require_once 'htaccess.php';
class GeoGuard {
private $ip2location_api_key;
private $htaccess;
private $options;
private $settings_file;
private $attempts_file;
public function __construct($settings_file = 'geoguard_settings.json', $attempts_file = 'geoguard_attempts.json') {
$this->settings_file = dirname(__FILE__) . '/' . $settings_file;
$this->attempts_file = dirname(__FILE__) . '/' . $attempts_file;
$this->loadSettings();
$this->initializeHtaccess();
}
public function performIPCheck() {
$visitor_ip = $_SERVER['REMOTE_ADDR'];
$admin_ip = $this->options['admin_ip'];
if ($visitor_ip === $admin_ip) {
return;
}
//Where is this attempt from?
$geo_data = $this->getIPLocation($visitor_ip);
sleep($this->options['login_failed_delay']);
//Should block by country?
if ($this->shouldBlockIP($geo_data)) {
$this->blockIP($visitor_ip, 'Blocked country: ' . $geo_data['country_name']);
die('Access denied based on your location.');
}
//Multiple Attempts?
if ($this->isSuspiciousActivity($visitor_ip)) {
$this->blockIP($visitor_ip, 'Suspicious activity');
die($this->options['403_message']);
}
}
public function loadSettings() {
if (file_exists($this->settings_file)) {
$this->options = json_decode(file_get_contents($this->settings_file), true);
} else {
$this->setDefaultOptions();
$this->saveSettings();
}
$this->ip2location_api_key = $this->options['ip2location_api_key'];
}
public function saveSettings() {
file_put_contents($this->settings_file, json_encode($this->options, JSON_PRETTY_PRINT));
}
public function setDefaultOptions() {
$this->options = [
'allowed_attempts' => 20,
'ip2location_api_key' => '',
'reset_time' => 60,
'login_failed_delay' => 1,
'inform_user' => true,
'send_email' => false,
'403_message' => 'Access denied due to suspicious activity.',
'admin_ip' => '',
'htaccess_dir' => '/home/username/public_html/',
'blocked_countries' => [],
'whitelist' => []
];
}
public function initializeHtaccess() {
$this->htaccess = new Htaccess();
$this->htaccess->setPath($this->options['htaccess_dir']);
}
public function getIPLocation($ip) {
$url = "https://api.ip2location.io/?ip={$ip}&key={$this->ip2location_api_key}";
$response = file_get_contents($url);
return json_decode($response, true);
}
public function shouldBlockIP($geo_data) {
return in_array($geo_data['country_code'], $this->options['blocked_countries']);
}
public function isSuspiciousActivity($ip) {
$attempts = $this->getAttempts($ip);
if ($attempts >= $this->options['allowed_attempts']) {
return true;
}
$this->incrementAttempts($ip);
return false;
}
public function blockIP($ip, $reason) {
$this->htaccess->denyIP($ip);
$this->clearAttempts($ip);
// Optionally log the reason for blocking
}
public function unblockIP($ip) {
$this->clearAttempts($ip);
return $this->htaccess->undenyIP($ip);
}
public function addToWhitelist($ip) {
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
return false;
}
$this->htaccess->undenyIP($ip);
if (!isset($this->options['whitelist'])) {
$this->options['whitelist'] = [];
}
$this->options['whitelist'][] = $ip;
$this->options['whitelist'] = array_unique($this->options['whitelist']);
$this->saveSettings();
return true;
}
public function removeFromWhitelist($ip) {
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
return false;
}
if (!isset($this->options['whitelist'])) {
return false;
}
$key = array_search($ip, $this->options['whitelist']);
if ($key === false) {
return false;
}
unset($this->options['whitelist'][$key]);
$this->saveSettings();
return true;
}
public function updateSettings($new_settings) {
$this->options = array_merge($this->options, $new_settings);
$this->saveSettings();
}
public function getDeniedIPs() {
return $this->htaccess->getDeniedIPs();
}
public function set403Message($message) {
return $this->htaccess->edit403Message($message);
}
public function commentGeoGuardLines() {
return $this->htaccess->commentLines();
}
public function uncommentGeoGuardLines() {
return $this->htaccess->uncommentLines();
}
public function checkHtaccessRequirements() {
return $this->htaccess->checkRequirements();
}
public function getSettings() {
return $this->options;
}
public function setBlockedCountries($countries) {
$this->options['blocked_countries'] = $countries;
$this->saveSettings();
}
public function getBlockedCountries() {
return $this->options['blocked_countries'];
}
public function setAdminIP($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$this->options['admin_ip'] = $ip;
$this->saveSettings();
return true;
}
return false;
}
public function setIP2LocationAPIKey($key) {
$this->options['ip2location_api_key'] = $key;
$this->ip2location_api_key = $key;
$this->saveSettings();
}
public function getWhitelist() {
if (!file_exists($this->attempts_file)) {
return 0;
}
$attempts = json_decode(file_get_contents($this->attempts_file), true);
return $attempts['whitelist'];
}
public function getAttempts($ip = null) {
if (!file_exists($this->attempts_file)) {
return 0;
}
$attempts = json_decode(file_get_contents($this->attempts_file), true);
if ($ip) return $attempts[$ip] ?? 0; // checks for attempts, otherwise returns all attempts
unset ($attempts['whitelist']); // removes whitelist from
return $attempts;
}
public function incrementAttempts($ip = NULL) {
if ($ip == NULL) $ip = $_SERVER['REMOTE_ADDR'];
$attempts = file_exists($this->attempts_file) ? json_decode(file_get_contents($this->attempts_file), true) : [];
$attempts[$ip] = ($attempts[$ip] ?? 0) + 1;
file_put_contents($this->attempts_file, json_encode($attempts, JSON_PRETTY_PRINT));
}
public function clearAttempts($ip = NULL) {
if ($ip == NULL) $ip = $_SERVER['REMOTE_ADDR'];
if (!file_exists($this->attempts_file)) {
return;
}
$attempts = json_decode(file_get_contents($this->attempts_file), true);
if (isset($attempts[$ip])) {
unset($attempts[$ip]);
}
file_put_contents($this->attempts_file, json_encode($attempts, JSON_PRETTY_PRINT));
}
}