[solved]
del url is not del_state it's delstate, docs are wrong.
query states first
get response
id field:
is actually the stateid and creator id (anemic docs, non existent/wrong.)
pass as url params (anemic docs, non existent/wrong, says post, wrong.)
attention; setting length, despite blank body (not mentioned in docs/non existent/wrong.)
and by parsing through and deleting this way you can purge the states by ip.
hope this helps someone...
https://docs.opnsense.org/development/api/core/diagnostics.html
this page needs serious work
issue since 2021
del url is not del_state it's delstate, docs are wrong.
Code (php) Select
$del_url = $opnsense_host . "/api/diagnostics/firewall/delState";
query states first
Code (php) Select
$query_url = $opnsense_host . "/api/diagnostics/firewall/query_states";
get response
Code (json) Select
{
"label": "21d61fb65e9a253dc46b79181dc7044c",
"descr": "Default allow LAN to any rule",
"nat_addr": "",
"nat_port": "",
"gateway": "",
"iface": "all",
"proto": "udp",
"ipproto": "ipv4",
"flags": [],
"direction": "in",
"dst_addr": "185.211.73.104",
"dst_port": "41677",
"src_addr": "192.168.2.189",
"src_port": "3232",
"state": "MULTIPLE:MULTIPLE",
"age": "32:21:44",
"expires": "00:00:58",
"pkts": {
"out": 18123,
"in": 24102
},
"bytes": {
"out": 2349563,
"in": 2946399
},
"rule": 59,
"id": "09048e6900000000/f1c4a296",
"interface": "all"
}
id field:
Code (php) Select
"id": "09048e6900000000/f1c4a296",
is actually the stateid and creator id (anemic docs, non existent/wrong.)
Code (php) Select
list($stateid, $creatorid) = explode('/', $full_id);
pass as url params (anemic docs, non existent/wrong, says post, wrong.)
Code (php) Select
$del_url_full = $del_url . '/' . $stateid . '/' . $creatorid;
$ch = curl_init($del_url_full);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $opnsense_key . ':' . $opnsense_secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
$del_resp = curl_exec($ch);
curl_close($ch);
attention; setting length, despite blank body (not mentioned in docs/non existent/wrong.)
Code (php) Select
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
and by parsing through and deleting this way you can purge the states by ip.
Code (php) Select
function purgeStatesByIP($opnsense_host, $opnsense_key, $opnsense_secret, $ip) {
// Query all states
$query_url = $opnsense_host . "/api/diagnostics/firewall/query_states";
$ch = curl_init($query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $opnsense_key . ':' . $opnsense_secret);
$resp = curl_exec($ch);
curl_close($ch);
if (!$resp) {
throw new Exception("Failed to query states.");
}
$states = json_decode($resp, true);
if (!is_array($states)) {
throw new Exception("Invalid response: " . $resp);
}
$del_url = $opnsense_host . "/api/diagnostics/firewall/delState";
$deleted = [];
foreach ($states as $state) {
if ($state['src_addr'] === $ip || $state['dst_addr'] === $ip) {
// Split the "id" field into stateid + creatorid
list($stateid, $creatorid) = explode('/', $state['id']);
$del_url_full = $del_url . '/' . $stateid . '/' . $creatorid;
$ch = curl_init($del_url_full);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $opnsense_key . ':' . $opnsense_secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); // Required even if empty
$del_resp = curl_exec($ch);
curl_close($ch);
$deleted[] = [
'state' => $state,
'response' => $del_resp
];
}
}
return $deleted;
}
// Example usage:
try {
$deletedStates = purgeStatesByIP(
"https://firewall.example.com",
"your_api_key",
"your_api_secret",
"185.211.73.104"
);
echo "Deleted " . count($deletedStates) . " states.\n";
foreach ($deletedStates as $d) {
echo " - " . $d['state']['src_addr'] . " → " . $d['state']['dst_addr'] . "\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
hope this helps someone...
https://docs.opnsense.org/development/api/core/diagnostics.html
this page needs serious work
issue since 2021