pid = $pid; $this->free = true; $this->queue = $queue; return; } // In the child, store our PID and the queue, then starts // waiting for instructions. $this->pid = posix_getpid(); $this->queue = $queue; $this->waitLoop(); } /** This method is called by the manager to send messages to the * threads. */ public function send($message) { return $this->ended ? false : msg_send($this->queue, $this->pid, $message, true); } /** This method implements the instruction loop. */ private function waitLoop() { $quit = false; $error = false; do { $success = msg_receive($this->queue, $this->pid, $type, 32768, $message, true); if (! $success) { l::error("Child failed to receive a message"); $quit = $error = true; } elseif ($message['type'] == 'QUIT') { $quit = true; } elseif ($message['type'] == 'SCAN') { list($host, $port) = $message['scan']; $found = $this->executeCheck($host, $port, "GET") || $this->executeCheck($host, $port, "POST"); if (!msg_send($this->queue, 1, array($this->pid, $host, $found), true)) { l::error("Child failed to send message"); $quit = $error = true; } } else { l::notice("Unknown message {$message['type']} received"); } } while (!$quit); exit($error ? 1 : 0); } /** This method will try to check one port for open proxy software, using * a specific method (POST or GET). */ private function executeCheck($ipAddress, $port, $method) { $key = md5(uniqid(rand())); // Open the socket $socket = @fsockopen("tcp://$ipAddress", $port, $errno, $errstr, pcheck_manager::$timeout); if ($socket === FALSE) { return false; } // Make sure I/O doesn't timeout stream_set_timeout($socket, pcheck_manager::$timeout); // Send the request $result = @fwrite($socket, preg_replace('/__key__/', $key, pcheck_manager::$requests[$method])); if ($result !== FALSE) { $info = stream_get_meta_data($socket); if ($info['timed_out']) { $result = false; } } // Get the page if ($result !== FALSE) { $result = @fread($socket, 4096); } if ($result !== FALSE) { $info = stream_get_meta_data($socket); if ($info['timed_out']) { $result = false; } } // Close the socket @fclose($socket); if ($result === FALSE) { return false; } return preg_match("/Key is \\<$key\\>/", $result); } } ?>