Skip to content

Commit a020a8c

Browse files
author
Jared Hendrickson
committed
Added cached_ip and cached_ipv6 fields to DDNS endpoint
1 parent f50083a commit a020a8c

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class APIQuery {
167167

168168
# Checks if an integer or numeric string is less than a given value
169169
private function lt($value, $limit) {
170-
if (is_numeric($value) and is_numeric($value)) {
170+
if (is_numeric($value) and is_numeric($limit)) {
171171
if (floatval($value) < floatval($limit)) {
172172
return true;
173173
}
@@ -177,7 +177,7 @@ class APIQuery {
177177

178178
# Checks if an integer or numeric string is less than or equal to a given value
179179
private function lte($value, $limit) {
180-
if (is_numeric($value) and is_numeric($value)) {
180+
if (is_numeric($value) and is_numeric($limit)) {
181181
if (floatval($value) <= floatval($limit)) {
182182
return true;
183183
}
@@ -187,7 +187,7 @@ class APIQuery {
187187

188188
# Checks if an integer or numeric string is greater than a given value
189189
private function gt($value, $limit) {
190-
if (is_numeric($value) and is_numeric($value)) {
190+
if (is_numeric($value) and is_numeric($limit)) {
191191
if (floatval($value) > floatval($limit)) {
192192
return true;
193193
}
@@ -197,7 +197,7 @@ class APIQuery {
197197

198198
# Checks if an integer or numeric string is greater than or equal to a given value
199199
private function gte($value, $limit) {
200-
if (is_numeric($value) and is_numeric($value)) {
200+
if (is_numeric($value) and is_numeric($limit)) {
201201
if (floatval($value) >= floatval($limit)) {
202202
return true;
203203
}

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@ class APIServicesDDNSRead extends APIModel {
2929
if (!empty($this->config['dyndnses']['dyndns'])) {
3030
$this->validated_data = $this->config['dyndnses']['dyndns'];
3131
# Loop through each entry and check if it's address is current
32-
foreach ($this->validated_data as $id=>$dyndns_ent) {
33-
$this->validated_data[$id]["current"] = $this->__is_dyndns_current($dyndns_ent);
32+
foreach ($this->validated_data as $id=>$ddns_ent) {
33+
$this->validated_data[$id]["cached_ip"] = $this->__get_cached_ip($ddns_ent);
34+
$this->validated_data[$id]["cached_ipv6"] = $this->__get_cached_ip($ddns_ent, true);
35+
$this->validated_data[$id]["current"] = $this->__is_dyndns_current($ddns_ent);
3436
}
3537
} else {
3638
$this->validated_data = [];
3739
}
3840
return APIResponse\get(0, $this->validated_data);
3941
}
4042

41-
private function __is_dyndns_current($ddns_ent) {
43+
private function __get_cached_ip($ddns_ent, $ipv6=false) {
4244
global $dyndns_split_domain_types;
45+
4346
# Determine our DDNS hostname
4447
if (in_array($ddns_ent['type'], $dyndns_split_domain_types)) {
4548
$host = $ddns_ent['host'] . "." . $ddns_ent['domainname'];
@@ -50,26 +53,43 @@ class APIServicesDDNSRead extends APIModel {
5053
$file = "/conf/dyndns_".$ddns_ent['interface'].$ddns_ent['type'].escapeshellarg($host).$ddns_ent['id'].".cache";
5154
$file_v6 = "/conf/dyndns_".$ddns_ent['interface'].$ddns_ent['type'].escapeshellarg($host).$ddns_ent['id']."_v6.cache";
5255

53-
# Check if either the IPv4 or IPv6 files exist, otherwise return false.
54-
if (file_exists($file)) {
55-
$ipaddr = dyndnsCheckIP($ddns_ent['interface']);
56+
# Get our cached IPv4 address
57+
if (!$ipv6 and file_exists($file)) {
5658
$cached_ip_s = explode("|", file_get_contents($file));
57-
$cached_ip = $cached_ip_s[0];
58-
if ($ipaddr != $cached_ip) {
59-
return false;
60-
} else {
61-
return true;
62-
}
63-
} else if (file_exists($file_v6)) {
64-
$ipv6addr = get_interface_ipv6($ddns_ent['interface']);
59+
return $cached_ip_s[0];
60+
}
61+
# Check our cached IPv6 address +
62+
if ($ipv6 and file_exists($file_v6)) {
6563
$cached_ipv6_s = explode("|", file_get_contents($file_v6));
66-
$cached_ipv6 = $cached_ipv6_s[0];
67-
if ($ipv6addr != $cached_ipv6) {
68-
return false;
64+
return $cached_ipv6_s[0];
65+
}
66+
}
67+
68+
private function __is_dyndns_current($ddns_ent) {
69+
# Local variables
70+
$cached_ip = $this->__get_cached_ip($ddns_ent);
71+
$cached_ipv6 = $this->__get_cached_ip($ddns_ent, true);
72+
73+
# Check if we found a cached IPv4 address
74+
if ($cached_ip) {
75+
# Check if our interfaces IP matches our cached DDNS IP
76+
if (dyndnsCheckIP($ddns_ent['interface']) === $cached_ip) {
77+
return true;
6978
} else {
79+
return false;
80+
}
81+
}
82+
# Otherwise, check if we found a cached IPv6 address
83+
elseif ($cached_ipv6) {
84+
# Check if our interfaces IPv6 matches our cached DDNS IPv6
85+
if (get_interface_ipv6($ddns_ent['interface']) === $cached_ipv6) {
7086
return true;
87+
} else {
88+
return false;
7189
}
72-
} else {
90+
}
91+
# If no DDNS cache was found, simply return false
92+
else {
7393
return false;
7494
}
7595
}

0 commit comments

Comments
 (0)