Skip to content

Commit 28e8e18

Browse files
Converted APIFirewallStatesRead model to uses pfSense's parsing function and added extra state values
1 parent d8b4341 commit 28e8e18

2 files changed

Lines changed: 22 additions & 23 deletions

File tree

pfSense-pkg-API/files/etc/inc/api/framework/APITools.inc

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -623,28 +623,6 @@ function sanitize_str($string) {
623623
return $string;
624624
}
625625

626-
// Sort our state table
627-
function sort_state_table() {
628-
// Local variables
629-
$states = shell_exec("/sbin/pfctl -s state");
630-
$state_table = array();
631-
// Parse our output
632-
$state_rows = explode("\n", $states);
633-
foreach ($state_rows as $id => $data) {
634-
$data = preg_replace('!\s+!', ' ', $data); // Remove extra whitespace
635-
$data_fields = explode(" ", $data); // Split on whitespace
636-
if (count($data_fields) > 1) {
637-
$state_table[$id] = array();
638-
$state_table[$id]["interface"] = $data_fields[0];
639-
$state_table[$id]["protocol"] = $data_fields[1];
640-
$state_table[$id]["source"] = ($data_fields[3] === "->") ? $data_fields[2] : $data_fields[4];
641-
$state_table[$id]["destination"] = ($data_fields[3] === "->") ? $data_fields[4] : $data_fields[2];
642-
$state_table[$id]["status"] = $data_fields[5];
643-
}
644-
}
645-
return $state_table;
646-
}
647-
648626
// Restarts the pfSense webConfigurator
649627
function restart_webconfigurator() {
650628
ob_flush();

pfSense-pkg-API/files/etc/inc/api/models/APIFirewallStatesRead.inc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ class APIFirewallStatesRead extends APIModel {
2525
}
2626

2727
public function action() {
28-
return APIResponse\get(0, APITools\sort_state_table());
28+
return APIResponse\get(0, $this->get_state_table());
29+
}
30+
31+
public static function get_state_table() {
32+
$raw_table = pfSense_get_pf_states();
33+
return array_map(function($table) {
34+
return array(
35+
'interface' => $table['if'],
36+
'protocol' => $table['proto'],
37+
'source' => $table['src'],
38+
'destination' => $table['dst'],
39+
'status' => $table["state"],
40+
'age' => $table["age"],
41+
"expires_in" => $table["expires in"],
42+
"packets_total" => $table["packets total"],
43+
"packets_in" => $table["packets in"],
44+
"packets_out" => $table["packets out"],
45+
"bytes_total" => $table["bytes total"],
46+
"bytes_in" => $table["bytes in"],
47+
"bytes_out" => $table["bytes out"],
48+
);
49+
}, $raw_table);
2950
}
3051
}

0 commit comments

Comments
 (0)