From dbe330298729cdaa9c07e72e8778daad0a87e1c3 Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Sat, 3 Dec 2011 11:52:31 +1100 Subject: [PATCH] Avoid DNS timeouts by using `host` and cache if available --- inc/config.php | 4 ++++ inc/functions.php | 24 ++++++++++++++++++++++++ mod.php | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/inc/config.php b/inc/config.php index a2e5b8ae..e18f329f 100644 --- a/inc/config.php +++ b/inc/config.php @@ -75,6 +75,10 @@ // Use syslog() for logging all error messages and unauthorized login attempts. $config['syslog'] = false; + // Use `host` via shell_exec() to lookup hostnames, avoiding query timeouts. May not work on your system. + // Requires safe_mode to be disabled. + $config['dns_system'] = false; + /* * ==================== * Database settings diff --git a/inc/functions.php b/inc/functions.php index c79e3a68..db1b930a 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -487,6 +487,7 @@ $query->bindValue(':board', $board); $query->execute() or error(db_error($query)); } + if($query->rowCount() < 1 && $config['ban_cidr'] && !isIPv6()) { // my most insane SQL query yet $query = prepare("SELECT `set`, `expires`, `reason`, `board`, `uri`, `bans`.`id` FROM `bans` LEFT JOIN `boards` ON `boards`.`id` = `board` WHERE (`board` IS NULL OR `uri` = :board) @@ -1472,4 +1473,27 @@ } } + function rDNS($ip_addr) { + global $config; + + if($config['cache']['enabled'] && ($host = cache::get('rdns_' . $ip_addr))) { + return $host; + } + + if(!$config['dns_system']) { + $host = gethostbyaddr($ip_addr); + } else { + $resp = shell_exec('host -W 1 ' . $ip_addr); + if(preg_match('/domain name pointer ([^\s]+)$/', $resp, $m)) + $host = $m[1]; + else + $host = $ip_addr; + } + + if($config['cache']['enabled']) + cache::set('rdns_' . $ip_addr, $host, 3600); + + return $host; + } + ?> diff --git a/mod.php b/mod.php index 34a540f7..453e6737 100644 --- a/mod.php +++ b/mod.php @@ -2408,7 +2408,7 @@ // View information on an IP address $ip = $matches[1]; - $host = $config['mod']['dns_lookup'] ? gethostbyaddr($ip) : false; + $host = $config['mod']['dns_lookup'] ? rDNS($ip) : false; if(hasPermission($config['mod']['unban']) && isset($_POST['unban']) && isset($_POST['ban_id'])) { removeBan($_POST['ban_id']);