Use proc_open to get the exit cod and stderr

This commit is contained in:
FusionPBX
2026-03-19 03:18:22 +00:00
committed by GitHub
parent 9e5844977c
commit 6a535fe7ad
2 changed files with 33 additions and 18 deletions
@@ -107,22 +107,41 @@ class event_guard_iptables implements event_guard_interface {
$chains[] = $filter;
}
// Unblock the address
$command = $this->firewall_path.' -S | '. $this->grep_path . ' ' . $ip_address;
$result = trim(shell_exec($command));
if (!empty($result)) {
//remove the IP address from each chain
foreach($chains as $chain) {
for ($i = 1; $i <= 999; $i++) {
$i = 0;
while (true) {
// Remove the blocked IP address
$command = $this->firewall_path . ' -D ' . escapeshellarg($chain) . ' -s ' . $ip_address . ' -j DROP';
$result = shell_exec($command);
if (!empty($result)) {
$descriptors = [
0 => ['pipe', 'r'], // stdin
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'], // stderr
];
$process = proc_open($command, $descriptors, $pipes);
if (is_resource($process)) {
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
$exit_code = proc_close($process);
if ($exit_code !== 0 && strpos($stderr, "Bad rule") !== false) {
echo "exiting the loop\n";
break;
}
}
//added as a failsafe
if ($i > 1000) {
break;
}
//increment the iterator
$i++;
}
}
// Send information to the user
echo "Unblock address " . $ip_address . " line " . $line_number . " command " . $command . " result " . $result . "\n";
}
// Return success
return true;
@@ -293,9 +293,6 @@ class event_guard_service extends service {
$p->add('event_guard_log_add', 'temp');
$this->database->save($array, false);
$p->delete('event_guard_log_add', 'temp');
//send debug information to the console
$this->info("blocked address " . $ip_address . ", line " . __line__);
}
//return the result
@@ -310,10 +307,9 @@ class event_guard_service extends service {
//unblock the IP address
$result = $this->firewall->block_delete($ip_address, $filter);
if ($result) {
//send debug information to the console
$this->info("Unblock address " . $ip_address . ", line " . __line__);
}
$this->warning("unblocked: [ip_address: ".$ip_address.", filter: ".$filter.", line: ".__line__."]");
//return the result
return $result;