From 44dd1ef6f6a3325d3a55550da95dd22bd96684dc Mon Sep 17 00:00:00 2001 From: discomrade Date: Wed, 13 Oct 2021 13:32:23 +0000 Subject: [PATCH] Add IPv6 support for DNSBL Should have used the library but didn't want to experiement and posiibly break the includes --- inc/functions.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 44712b16..c2a7dc36 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1753,9 +1753,6 @@ function buildJavascript() { function checkDNSBL() { global $config; - if (isIPv6()) - return; // No IPv6 support yet. - if (!isset($_SERVER['REMOTE_ADDR'])) return; // Fix your web server configuration @@ -1765,7 +1762,11 @@ function checkDNSBL() { if (in_array($_SERVER['REMOTE_ADDR'], $config['dnsbl_exceptions'])) return; - $ipaddr = ReverseIPOctets($_SERVER['REMOTE_ADDR']); + if (isIPv6()) { + $ipaddr = ReverseIPv6Octets($_SERVER['REMOTE_ADDR']); + } else { + $ipaddr = ReverseIPv4Octets($_SERVER['REMOTE_ADDR']); + } foreach ($config['dnsbl'] as $blacklist) { if (!is_array($blacklist)) @@ -1801,10 +1802,30 @@ function isIPv6() { return strstr($_SERVER['REMOTE_ADDR'], ':') !== false; } -function ReverseIPOctets($ip) { +function ReverseIPv4Octets($ip) { return implode('.', array_reverse(explode('.', $ip))); } +function ReverseIPv6Octets($ip) { + return strrev(implode(".", str_split(str_replace(':', '', inet_expand($ip))))); +} + +// copypastad from lib/IP/Lifo/IP.php, TODO replace this with a proper include +function inet_expand($ip) +{ + // strip possible cidr notation off + if (($pos = strpos($ip, '/')) !== false) { + $ip = substr($ip, 0, $pos); + } + $bytes = unpack('n*', inet_pton($ip)); + if (count($bytes) > 2) { + return implode(':', array_map(function ($b) { + return sprintf("%04x", $b); + }, $bytes)); + } + return $ip; +} + function wordfilters(&$body) { global $config;