From 42158500df147c290a16536ff37dcb9c9fb5679e Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 3 Apr 2020 10:56:01 +0200 Subject: [PATCH] os-firewall: add serialize rule logic for https://github.com/opnsense/plugins/issues/1749 --- .../FieldTypes/SourceNatRuleField.php | 114 ++++++++++++++++++ .../app/models/OPNsense/Firewall/Filter.xml | 2 +- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/SourceNatRuleField.php diff --git a/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/SourceNatRuleField.php b/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/SourceNatRuleField.php new file mode 100644 index 0000000000..06329f0776 --- /dev/null +++ b/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/FieldTypes/SourceNatRuleField.php @@ -0,0 +1,114 @@ + false, + 'source_net' => false, + 'source_not' => false, + 'source_port' => 'sourceport', + 'destination_net' => false, + 'destination_not' => false, + 'destination_port' => 'dstport', + 'target_port' => 'natport', + 'description' => 'descr' + ]; + // 1-on-1 map (with type conversion if needed) + foreach ($this->iterateItems() as $key => $node) { + $target_fieldname = isset($source_mapper[$key]) ? $source_mapper[$key] : $key; + if ($target_fieldname) { + if (is_a($node, "OPNsense\\Base\\FieldTypes\\BooleanField")) { + $result[$target_fieldname] = !empty((string)$node); + } elseif (is_a($node, "OPNsense\\Base\\FieldTypes\\ProtocolField")) { + if ((string)$node != 'any') { + $result[$target_fieldname] = (string)$node; + } + } else { + $result[$target_fieldname] = (string)$node; + } + } + } + + $result['disabled'] = empty((string)$this->enabled); + // source / destination mapping, doesn't use port construct like it would for rules. + $result['source'] = array(); + if (!empty((string)$this->source_net)) { + $result['source']['network'] = (string)$this->source_net; + if (!empty((string)$this->source_not)) { + $result['source']['not'] = true; + } + } + $result['destination'] = array(); + if (!empty((string)$this->destination_net)) { + $result['destination']['network'] = (string)$this->destination_net; + if (!empty((string)$this->destination_not)) { + $result['destination']['not'] = true; + } + } + + return $result; + } +} + +/** + * Class SourceNatRuleField + * @package OPNsense\Firewall\FieldTypes + */ +class SourceNatRuleField extends ArrayField +{ + /** + * @inheritDoc + */ + public function newContainerField($ref, $tagname) + { + $container_node = new SourceNatRuleContainerField($ref, $tagname); + $parentmodel = $this->getParentModel(); + $container_node->setParentModel($parentmodel); + return $container_node; + } +} \ No newline at end of file diff --git a/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/Filter.xml b/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/Filter.xml index a9961b45a0..c870569f73 100644 --- a/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/Filter.xml +++ b/net/firewall/src/opnsense/mvc/app/models/OPNsense/Firewall/Filter.xml @@ -107,7 +107,7 @@ - + 1 Y