Skip to content

Commit b80e463

Browse files
Created APIFirewallTrafficShaperDelete and APIFirewallTrafficShaperDelete models, created unit tests for each, minor validation corrections
1 parent 5739752 commit b80e463

8 files changed

Lines changed: 1263 additions & 30 deletions

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6817,4 +6817,3 @@ URL: https://{{$hostname}}/api/v1/user/privilege
68176817

68186818
---
68196819
[Back to top](#pfsense-rest-api-documentation)
6820-
> Made with ♥ by [thedevsaddam](https://github.com/thedevsaddam) | Generated at: 2021-03-14 22:47:05 by [docgen](https://github.com/thedevsaddam/docgen)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
// Copyright 2021 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIEndpoint.inc");
17+
18+
class APIFirewallTrafficShaperLimiter extends APIEndpoint {
19+
public function __construct() {
20+
$this->url = "/api/v1/firewall/traffic_shaper/limiter";
21+
}
22+
23+
protected function get() {
24+
return (new APIFirewallTrafficShaperLimiterRead())->call();
25+
}
26+
27+
protected function post() {
28+
return (new APIFirewallTrafficShaperLimiterCreate())->call();
29+
}
30+
31+
protected function delete() {
32+
return (new APIFirewallTrafficShaperLimiterDelete())->call();
33+
}
34+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,12 @@ function get($id, $data=[], $all=false) {
22642264
"return" => $id,
22652265
"message" => "Unknown firewall traffic shaper limiter bandwidth scale"
22662266
],
2267+
4214 => [
2268+
"status" => "bad request",
2269+
"code" => 400,
2270+
"return" => $id,
2271+
"message" => "Firewall traffic shaper limiter and child queues cannot be deleted while in use"
2272+
],
22672273

22682274
//5000-5999 reserved for /users API calls
22692275
5000 => [
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
// Copyright 2021 Jared Hendrickson
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
require_once("api/framework/APIModel.inc");
17+
require_once("api/framework/APIResponse.inc");
18+
19+
20+
class APIFirewallTrafficShaperLimiterDelete extends APIModel {
21+
# Create our method constructor
22+
public function __construct() {
23+
parent::__construct();
24+
$this->privileges = ["page-all", "page-firewall-trafficshaper-limiter"];
25+
$this->change_note = "Deleted firewall traffic shaper limiter via API";
26+
}
27+
28+
public function action() {
29+
unset($this->config["dnshaper"]["queue"][$this->id]);
30+
$this->write_config();
31+
filter_configure();
32+
33+
return APIResponse\get(0, $this->validated_data);
34+
}
35+
36+
public function validate_payload() {
37+
$this->__validate_name();
38+
}
39+
40+
private function __validate_name() {
41+
# Check for our required `name` payload value
42+
if (isset($this->initial_data["name"])) {
43+
if ($this->get_limiter_id_by_name($this->initial_data["name"], true)) {
44+
# Ensure limiter is not in use
45+
if (!$this->is_limiter_in_use($this->initial_data["name"])) {
46+
$this->id = $this->get_limiter_id_by_name($this->initial_data["name"]);
47+
$this->validated_data = $this->config["dnshaper"]["queue"][$this->id];
48+
} else {
49+
$this->errors[] = APIResponse\get(4214);
50+
}
51+
} else {
52+
$this->errors[] = APIResponse\get(4209);
53+
}
54+
} else {
55+
$this->errors[] = APIResponse\get(4167);
56+
}
57+
}
58+
59+
public function get_limiter_id_by_name($name, $as_bool=false) {
60+
# Loop through each configured limiter and see if it uses this name
61+
foreach ($this->config["dnshaper"]["queue"] as $id=>$limiter) {
62+
# Check for matching names
63+
if ($name === $limiter["name"]) {
64+
return ($as_bool) ? true : $id;
65+
}
66+
}
67+
return ($as_bool) ? false : null;
68+
}
69+
70+
public function is_limiter_in_use($name) {
71+
# Loop through each configured firewall rule and check if it's using this limiter
72+
foreach ($this->config["filter"]["rule"] as $rule) {
73+
# Loop through each configured limiter
74+
foreach ($this->config["dnshaper"]["queue"] as $limiter) {
75+
# Check for matches
76+
if ($rule["dnpipe"] === $limiter["name"] or $rule["pdnpipe"] === $limiter["name"]) {
77+
return true;
78+
}
79+
80+
# Also loop through child queues to ensure they do not use the same name
81+
foreach ($limiter["queue"] as $queue) {
82+
# Check for matches
83+
if ($rule["dnpipe"] === $queue["name"] or $rule["pdnpipe"] === $queue["name"]) {
84+
return true;
85+
};
86+
}
87+
}
88+
}
89+
return false;
90+
}
91+
92+
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ class APIFirewallTrafficShaperLimiterBandwidthCreate extends APIModel {
2222
public function __construct() {
2323
parent::__construct();
2424
$this->privileges = ["page-all", "page-firewall-trafficshaper-limiter"];
25-
$this->change_note = "Added firewall limiter bandwidth via API";
25+
$this->change_note = "Added firewall traffic shaper limiter bandwidth via API";
2626
}
2727

2828
public function action() {
29-
# Write this schedule time range to the config and reload the firewall filter
29+
$this->__init_config();
3030
$this->config["dnshaper"]["queue"][$this->id]["bandwidth"]["item"][] = $this->validated_data;
3131
$this->write_config();
3232
filter_configure();
@@ -42,6 +42,9 @@ class APIFirewallTrafficShaperLimiterBandwidthCreate extends APIModel {
4242
$this->__validate_bw();
4343
$this->__validate_bwscale();
4444
$this->__validate_bwsched();
45+
46+
# Set static fields
47+
$this->validated_data["burst"] = "";
4548
}
4649

4750
private function __validate_name() {
@@ -61,7 +64,7 @@ class APIFirewallTrafficShaperLimiterBandwidthCreate extends APIModel {
6164
# Check for our required `bw` payload value
6265
if (isset($this->initial_data["bw"])) {
6366
if (is_numeric($this->initial_data["bw"]) and intval($this->initial_data["bw"] >= 1)) {
64-
$this->validated_data["bw"] = $this->initial_data["bw"];
67+
$this->validated_data["bw"] = intval($this->initial_data["bw"]);
6568
} else {
6669
$this->errors[] = APIResponse\get(4211);
6770
}
@@ -118,4 +121,14 @@ class APIFirewallTrafficShaperLimiterBandwidthCreate extends APIModel {
118121
return false;
119122
}
120123

124+
private function __init_config() {
125+
# Check if there is no dnshaper array in the config
126+
if (empty($this->config["dnshaper"]["queue"][$this->id]["queue"])) {
127+
$this->config["dnshaper"]["queue"][$this->id]["queue"] = [];
128+
}
129+
# Check if there is no dnshaper queue array in the config
130+
if (empty($this->config["dnshaper"]["queue"][$this->id]["queue"]["item"])) {
131+
$this->config["dnshaper"]["queue"][$this->id]["queue"]["item"] = [];
132+
}
133+
}
121134
}

0 commit comments

Comments
 (0)