Firewall and NAT

Requires OPNsense Firmware >= 26.1.1

async OPNsenseClient.get_firewall()

Return firewall filter rules and all supported NAT rule groups.

Returns:

Mapping with top-level rules for firewall

filter rules, excluding lockout and automatically generated firewall rules, and nat groups for destination NAT, one-to-one NAT, source NAT, and NPT rules. Rule groups are keyed by rule UUID.

Return type:

dict[str, Any]

async OPNsenseClient.toggle_firewall_rule(uuid, toggle_on_off=None)

Toggle a Firewall Rule on or off and apply the filter.

Parameters:
  • uuid (str) – UUID of the firewall filter rule to toggle.

  • toggle_on_off (str | None, optional) – Target state. Use on to enable, off to disable, or None or toggle to flip the current state.

Returns:

True when the Router acknowledged the save and confirmed

the apply. False when the save was not acknowledged (no apply is sent), the apply was not confirmed, or an error was suppressed. With throw_errors enabled, an invalid toggle_on_off raises OPNsenseInvalidArgument and transport errors raise OPNsenseError.

Return type:

bool

async OPNsenseClient.toggle_nat_rule(nat_rule_type, uuid, toggle_on_off=None)

Toggle a NAT rule on or off and apply the NAT changes.

Parameters:
  • nat_rule_type (str) – NAT rule category path segment, such as d_nat, source_nat, one_to_one, or npt.

  • uuid (str) – UUID of the NAT rule to toggle.

  • toggle_on_off (str | None, optional) – Target state. Use on to enable, off to disable, or None or toggle to flip the current state.

Returns:

True when the Router acknowledged the save and confirmed

the apply. False when the save was not acknowledged (no apply is sent), the apply was not confirmed, or an error was suppressed. With throw_errors enabled, an invalid toggle_on_off raises OPNsenseInvalidArgument and transport errors raise OPNsenseError.

Return type:

bool

async OPNsenseClient.kill_states(ip_addr)

Kill the active states of the IP address.

Parameters:

ip_addr (str) – IP address whose states should be terminated.

Returns:

Mapping with success and

dropped_states from the firewall state-kill response.

Return type:

MutableMapping[str, Any]

async OPNsenseClient.toggle_alias(alias, toggle_on_off=None)

Toggle an alias on or off, commit it and reconfigure the aliases.

Parameters:
  • alias (str) – Alias name to toggle in firewall configuration.

  • toggle_on_off (str | None, optional) – Target state. Use on to enable, off to disable, or None or toggle to flip the current state.

Returns:

True when the Router acknowledged the toggle and the

commit and confirmed the reconfigure. False when the alias cannot be found, a save step was not acknowledged (nothing further is sent), the reconfigure was not confirmed, or an error was suppressed. With throw_errors enabled, an invalid toggle_on_off raises OPNsenseInvalidArgument and transport errors raise OPNsenseError.

Return type:

bool