dns/rfc2136 - move get_dyndns_ip() to plugin to avoid further usage from core, related to https://github.com/opnsense/core/issues/5434

This commit is contained in:
Ad Schellevis
2022-01-17 14:28:36 +01:00
parent 6ed716ea25
commit 9012be99d8
3 changed files with 44 additions and 6 deletions
@@ -173,7 +173,7 @@ EOD;
list($cachedipv4, $cacheTimev4) = array('', '');
}
if (isset($dnsupdate['usepublicip'])) {
$wanip = get_dyndns_ip($dnsupdate['interface'], 4);
$wanip = get_rfc2136_ip_address($dnsupdate['interface'], 4);
} else {
$wanip = get_interface_ip($dnsupdate['interface']);
}
@@ -200,7 +200,7 @@ EOD;
list($cachedipv6, $cacheTimev6) = array('', '');
}
if (isset($dnsupdate['usepublicip'])) {
$wanipv6 = get_dyndns_ip($dnsupdate['interface'], 6);
$wanipv6 = get_rfc2136_ip_address($dnsupdate['interface'], 6);
} else {
$wanipv6 = get_interface_ipv6($dnsupdate['interface']);
}
@@ -239,3 +239,41 @@ EOD;
echo "done.\n";
}
}
function get_rfc2136_ip_address($int, $ipver = 4)
{
$ip_address = $ipver == 6 ? get_interface_ipv6($int) : get_interface_ip($int);
if (empty($ip_address)) {
log_error("Aborted IPv{$ipver} detection: no address for {$int}");
return 'down';
}
if ($ipver != 6 && is_private_ip($ip_address)) {
/* Chinese alternative is http://ip.3322.net/ */
$hosttocheck = 'http://checkip.dyndns.org';
$ip_ch = curl_init($hosttocheck);
curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ip_ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$ip_result = curl_exec($ip_ch);
if ($ip_result !== false) {
preg_match('=<body>Current IP Address: (.*)</body>=siU', $ip_result, $matches);
$ip_address = trim($matches[1]);
} else {
log_error('Aborted IPv4 detection: ' . curl_error($ip_ch));
$ip_address = '';
}
curl_close($ip_ch);
} elseif ($ipver == 6 && is_linklocal($ip_address)) {
log_error('Aborted IPv6 detection: cannot bind to link-local address');
$ip_address = '';
}
if (($ipver == 6 && !is_ipaddrv6($ip_address)) || ($ipver != 6 && !is_ipaddrv4($ip_address))) {
return 'down';
}
return $ip_address;
}
+2 -2
View File
@@ -146,7 +146,7 @@ legacy_html_escape_form_data($a_rfc2136);
if (file_exists($filename) && !empty($rfc2136['enable']) && (empty($rfc2136['recordtype']) || $rfc2136['recordtype'] == 'A')) {
echo "IPv4: ";
if (isset($rfc2136['usepublicip'])) {
$ipaddr = get_dyndns_ip($rfc2136['interface'], 4);
$ipaddr = get_rfc2136_ip_address($rfc2136['interface'], 4);
} else {
$ipaddr = get_interface_ip($rfc2136['interface']);
}
@@ -167,7 +167,7 @@ legacy_html_escape_form_data($a_rfc2136);
if (file_exists($filename6) && !empty($rfc2136['enable']) && (empty($rfc2136['recordtype']) || $rfc2136['recordtype'] == 'AAAA')) {
echo "IPv6: ";
if (isset($rfc2136['usepublicip'])) {
$ipaddr = get_dyndns_ip($rfc2136['interface'], 6);
$ipaddr = get_rfc2136_ip_address($rfc2136['interface'], 6);
} else {
$ipaddr = get_interface_ipv6($rfc2136['interface']);
}
@@ -49,14 +49,14 @@ if (!empty($_REQUEST['getrfc2136status'])) {
$filename = rfc2136_cache_file($rfc2136, 4);
$fdata = '';
if (!empty($rfc2136['enable']) && (empty($rfc2136['recordtype']) || $rfc2136['recordtype'] == 'A') && file_exists($filename)) {
$ipaddr = get_dyndns_ip($rfc2136['interface'], 4);
$ipaddr = get_rfc2136_ip_address($rfc2136['interface'], 4);
$fdata = @file_get_contents($filename);
}
$filename_v6 = rfc2136_cache_file($rfc2136, 6);
$fdata6 = '';
if (!empty($rfc2136['enable']) && (empty($rfc2136['recordtype']) || $rfc2136['recordtype'] == 'AAAA') && file_exists($filename_v6)) {
$ipv6addr = get_dyndns_ip($rfc2136['interface'], 6);
$ipv6addr = get_rfc2136_ip_address($rfc2136['interface'], 6);
$fdata6 = @file_get_contents($filename_v6);
}