|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace MikoPBX\PBXCoreREST\Lib\Firewall; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Core\System\Processes; |
|
23
|
|
|
use MikoPBX\Core\System\Util; |
|
24
|
|
|
use MikoPBX\PBXCoreREST\Lib\PBXApiResult; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class GetBannedIp |
|
28
|
|
|
* Retrieve a list of banned IP addresses or get data for a specific IP address. |
|
29
|
|
|
* |
|
30
|
|
|
* @package MikoPBX\PBXCoreREST\Lib\Firewall |
|
31
|
|
|
*/ |
|
32
|
|
|
class GetBannedIp extends \Phalcon\Di\Injectable |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Retrieve a list of banned IP addresses or get data for a specific IP address. |
|
36
|
|
|
* |
|
37
|
|
|
* @return PBXApiResult An object containing the result of the API call. |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function main(): PBXApiResult |
|
40
|
|
|
{ |
|
41
|
|
|
$res = new PBXApiResult(); |
|
42
|
|
|
$res->processor = __METHOD__; |
|
43
|
|
|
$res->success = true; |
|
44
|
|
|
$res->data = self::getBanIpWithTime(); |
|
45
|
|
|
return $res; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Retrieve a list of banned IP addresses with their corresponding ban and unban timestamps. |
|
50
|
|
|
* |
|
51
|
|
|
* @return array An array containing the banned IP addresses and their timestamps. |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getBanIpWithTime():array |
|
54
|
|
|
{ |
|
55
|
|
|
$groupedResults = []; |
|
56
|
|
|
$sep = '"|"'; |
|
57
|
|
|
$sepSpace = '" "'; |
|
58
|
|
|
$fail2banPath = Util::which('fail2ban-client'); |
|
59
|
|
|
$awkPath = Util::which('awk'); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
$shellData = str_replace("'", '"', shell_exec("$fail2banPath banned")); |
|
63
|
|
|
$data = json_decode($shellData, true, 512, JSON_THROW_ON_ERROR); |
|
64
|
|
|
$data = array_merge(... $data); |
|
65
|
|
|
}catch (\Throwable $e){ |
|
66
|
|
|
$data = []; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$jails = array_keys($data); |
|
71
|
|
|
foreach ($jails as $jail){ |
|
72
|
|
|
$data = []; |
|
73
|
|
|
Processes::mwExec("$fail2banPath get $jail banip --with-time | $awkPath '{print $1 $sep $2 $sepSpace $3 $sep $7 $sepSpace $8 }'", $data); |
|
74
|
|
|
foreach ($data as $ipData){ |
|
75
|
|
|
$ipData = explode('|', $ipData); |
|
76
|
|
|
$ip = $ipData[0]??''; |
|
77
|
|
|
if(empty($ip)){ |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Check if this IP is already in the result array. |
|
82
|
|
|
if (!isset($groupedResults[$ip])) { |
|
83
|
|
|
// If not, initialize it. |
|
84
|
|
|
$groupedResults[$ip] = []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Append the ban details to the existing array for this IP. |
|
88
|
|
|
$groupedResults[$ip][] = [ |
|
89
|
|
|
'jail' => "{$jail}_v2", |
|
90
|
|
|
'timeofban' => self::time2stamp($ipData[1]), |
|
91
|
|
|
'timeunban' => self::time2stamp($ipData[2]), |
|
92
|
|
|
'v' => '2', |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return $groupedResults; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Convert a string representation of a time to a UNIX timestamp. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $strTime The string representation of the time. |
|
103
|
|
|
* @return int The UNIX timestamp. |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function time2stamp(string $strTime):int |
|
106
|
|
|
{ |
|
107
|
|
|
$result = 0; |
|
108
|
|
|
$d = \DateTime::createFromFormat('Y-m-d H:i:s', $strTime); |
|
109
|
|
|
if ($d !== false) { |
|
110
|
|
|
$result = $d->getTimestamp(); |
|
111
|
|
|
} |
|
112
|
|
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |