<?php
/*
 * @package 	RSFirewall!
 * @copyright 	(c) 2009 - 2024 RSJoomla!
 * @link 		https://www.rsjoomla.com/joomla-extensions/joomla-security.html
 * @license 	GNU General Public License https://www.gnu.org/licenses/gpl-3.0.en.html
 */

\defined('_JEXEC') or die;

abstract class RSFirewallResolver
{
	public static function getInstance()
	{
		require_once JPATH_ADMINISTRATOR.'/components/com_rsfirewall/helpers/Net/DNS2.php';

		$nameservers = array(
			'208.67.222.222', '208.67.220.220', // Open DNS
			'8.26.56.26', '8.20.247.20', // Comodo
			'1.1.1.1', '1.0.0.1', // Cloudflare
			'8.8.8.8', '8.8.4.4', // Google
		);

		// Let's see if we have our own DNS
		if ($addresses = RSFirewallConfig::getInstance()->get('dns_address', '', true))
		{
			$addresses = array_filter($addresses, array('RSFirewallResolver', 'filterIp'));
			if (!empty($addresses))
			{
				$nameservers = $addresses;
			}
		}

		return new Net_DNS2_Resolver(array(
			'nameservers' => $nameservers,
			'timeout' => 1
		));
	}

	public static function filterIp($ip)
	{
		return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
	}
}