Skip to content

Commit 6db03db

Browse files
Fixed #86, /api/v1/system/dns/server will only add DNS servers that don't currently exist
1 parent 2ffd0e4 commit 6db03db

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,27 @@ class APISystemDNSServerCreate extends APIModel {
2727
}
2828

2929
public function action() {
30+
# Write changes to config and apply backend changes
3031
$this->config["system"]["dnsserver"] = $this->validated_data["dnsserver"];
31-
$this->write_config(); // Apply our configuration change
32+
$this->write_config();
33+
$this->apply_backend_changes();
3234

33-
// Update a slew of backend services
34-
system_resolvconf_generate();
35-
if (isset($this->config['dnsmasq']['enable'])) {
36-
services_dnsmasq_configure();
37-
} elseif (isset($this->config['unbound']['enable'])) {
38-
services_unbound_configure();
39-
}
40-
41-
// Reload DNS services and firewall filter
42-
send_event("service reload dns");
43-
filter_configure();
4435
return APIResponse\get(0, $this->validated_data);
4536
}
4637

4738
public function validate_payload() {
48-
if (isset($this->initial_data['dnsserver'])) {
49-
$this->validated_data["dnsserver"] = $this->config["system"]["dnsserver"];
39+
$this->validated_data["dnsserver"] = $this->config["system"]["dnsserver"];
40+
$this->__validate_dnsserver();
41+
}
5042

43+
private function __validate_dnsserver() {
44+
if (isset($this->initial_data['dnsserver'])) {
5145
# If values are not an array, convert it
5246
if (!is_array($this->initial_data["dnsserver"])) {
5347
$this->initial_data["dnsserver"] = array($this->initial_data["dnsserver"]);
5448
}
5549
if (!is_array($this->validated_data["dnsserver"])) {
5650
$this->validated_data["dnsserver"] = [$this->validated_data["dnsserver"]];
57-
5851
}
5952

6053
# Loop through each requested DNS server and ensure it is valid. Add this to our existing servers.
@@ -67,7 +60,25 @@ class APISystemDNSServerCreate extends APIModel {
6760
$this->validated_data["dnsserver"][] = $ds;
6861
}
6962
}
70-
$this->validated_data = array_filter($this->validated_data);
63+
64+
# Ensure array values are unique, reindexed and purge duplicate items
65+
$this->validated_data["dnsserver"] = array_filter($this->validated_data["dnsserver"]);
66+
$this->validated_data["dnsserver"] = array_unique($this->validated_data["dnsserver"]);
67+
$this->validated_data["dnsserver"] = array_values($this->validated_data["dnsserver"]);
68+
}
69+
}
70+
71+
public function apply_backend_changes() {
72+
# Update a slew of backend services
73+
system_resolvconf_generate();
74+
if (isset($this->config['dnsmasq']['enable'])) {
75+
services_dnsmasq_configure();
76+
} elseif (isset($this->config['unbound']['enable'])) {
77+
services_unbound_configure();
7178
}
79+
80+
# Reload DNS services and firewall filter
81+
send_event("service reload dns");
82+
filter_configure();
7283
}
7384
}

0 commit comments

Comments
 (0)