|
| 1 | +<?php |
| 2 | +// Copyright 2022 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 APIRoutingGatewayDefaultUpdate extends APIModel { |
| 21 | + # Create our method constructor |
| 22 | + public function __construct() { |
| 23 | + parent::__construct(); |
| 24 | + $this->privileges = ["page-all", "page-system-gateways"]; |
| 25 | + $this->change_note = "Set default gateway via API"; |
| 26 | + } |
| 27 | + |
| 28 | + public function action() { |
| 29 | + # Set the default gateways based on the validated input |
| 30 | + $this->config["gateways"]["defaultgw4"] = $this->validated_data["defaultgw4"]; |
| 31 | + $this->config["gateways"]["defaultgw6"] = $this->validated_data["defaultgw6"]; |
| 32 | + |
| 33 | + # Write these changes to config and apply if the client requested |
| 34 | + $this->write_config(); |
| 35 | + $this->apply(); |
| 36 | + return APIResponse\get(0, $this->validated_data); |
| 37 | + } |
| 38 | + |
| 39 | + private function __validate_defaultgw4() { |
| 40 | + # Optionally allow clients to update the 'defaultgw4' value |
| 41 | + if (isset($this->initial_data["defaultgw4"])) { |
| 42 | + # Ensure this is a valid IPv4 gateway |
| 43 | + if (APITools\is_gateway($this->initial_data["defaultgw4"], true) === "inet") { |
| 44 | + $this->validated_data["defaultgw4"] = $this->initial_data["defaultgw4"]; |
| 45 | + } |
| 46 | + # Allow client to set automatic gateway selection |
| 47 | + elseif (in_array($this->initial_data["defaultgw4"], ["", "automatic"])) { |
| 48 | + $this->validated_data["defaultgw4"] = ""; |
| 49 | + } |
| 50 | + # Allow client to set no gateway |
| 51 | + elseif (in_array($this->initial_data["defaultgw4"], ["-", "none"])) { |
| 52 | + $this->validated_data["defaultgw4"] = "-"; |
| 53 | + } |
| 54 | + else { |
| 55 | + $this->errors[] = APIResponse\get(6028); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private function __validate_defaultgw6() { |
| 61 | + # Optionally allow clients to update the 'defaultgw6' value |
| 62 | + if (isset($this->initial_data["defaultgw6"])) { |
| 63 | + # Ensure this is a valid IPv6 gateway |
| 64 | + if (APITools\is_gateway($this->initial_data["defaultgw6"], true) === "inet6") { |
| 65 | + $this->validated_data["defaultgw6"] = $this->initial_data["defaultgw6"]; |
| 66 | + } |
| 67 | + # Allow client to set automatic gateway selection |
| 68 | + elseif (in_array($this->initial_data["defaultgw6"], ["", "automatic"])) { |
| 69 | + $this->validated_data["defaultgw6"] = ""; |
| 70 | + } |
| 71 | + # Allow client to set no gateway |
| 72 | + elseif (in_array($this->initial_data["defaultgw6"], ["-", "none"])) { |
| 73 | + $this->validated_data["defaultgw6"] = "-"; |
| 74 | + } |
| 75 | + else { |
| 76 | + $this->errors[] = APIResponse\get(6028); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public function validate_payload() { |
| 82 | + # Fetch existing default gateway values |
| 83 | + $this->validated_data = [ |
| 84 | + "defaultgw4"=>$this->config["gateways"]["defaultgw4"], |
| 85 | + "defaultgw6"=>$this->config["gateways"]["defaultgw6"], |
| 86 | + ]; |
| 87 | + |
| 88 | + # Validate client input |
| 89 | + $this->__validate_defaultgw4(); |
| 90 | + $this->__validate_defaultgw6(); |
| 91 | + } |
| 92 | + |
| 93 | + public function apply() { |
| 94 | + # Mark the routing subsystem as changed, clear if applied |
| 95 | + mark_subsystem_dirty("staticroutes"); |
| 96 | + |
| 97 | + # Optionally allow clients to apply this route immediately if they passed in a true apply value |
| 98 | + # Note: this is a one-off case where this was better to default to true instead of false. |
| 99 | + if ($this->initial_data["apply"] !== false) { |
| 100 | + system_routing_configure(); |
| 101 | + system_resolvconf_generate(); |
| 102 | + filter_configure(); |
| 103 | + setup_gateways_monitor(); |
| 104 | + send_event("service reload dyndnsall"); |
| 105 | + clear_subsystem_dirty("staticroutes"); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments