* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class ConsoleTools extends BaseFrameworkSystem { // Constants const HTTP_EOL = "\r\n"; const HTTP_USER_AGENT = 'ConsoleTools/1.0.1'; /** * Default is that this class is noisy */ private static $isQuietResolver = FALSE; /** * Cached values */ private static $cache = []; /** * Protected constructor * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); // Cache configuration entry self::$isQuietResolver = FrameworkBootstrap::getConfigurationInstance()->isEnabled('quiet_dns_resolver'); } /** * Checks wether proxy configuration is used * * @return $isUsed Wether proxy is used */ protected function isProxyUsed () { // Do we have cache? //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!'); if (!isset($GLOBALS[__METHOD__])) { // Determine it $GLOBALS[__METHOD__] = ( ( FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host') != '' ) && ( FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_port') > 0 ) ); } // Return cache //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: isUsed=%d', $GLOBALS[__METHOD__])); return $GLOBALS[__METHOD__]; } /** * Sets up a proxy tunnel for given hostname and through resource * * @param $host Host to connect to * @param $port Port number to connect to * @param $socketResource An instance of a Socket class * @return $response Response array * @throws InvalidArgumentException If a parameter is not valid */ protected function setupProxyTunnel (string $host, int $port, Socket $socketResource) { // Validate parameter //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: host=%s,port=%d,socketResource=%s - CALLED!', $host, $port, $socketResource)); if (empty($host)) { // Throw IAE throw new InvalidArgumentException('Parameter "host" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif ($port < 1) { // Throw IAE throw new InvalidArgumentException(sprintf('port=%d is not valid', $port)); } // Initialize array $response = ['', '', '']; $proxyTunnel = ''; // Generate CONNECT request header $proxyTunnel .= 'CONNECT ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL; $proxyTunnel .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL; $proxyTunnel .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL; // Use login data to proxy? (username at least!) if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_username') != '') { // Add it as well $encodedAuth = base64_encode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_username') . ':' . FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_password')); $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL; } // Add last new-line $proxyTunnel .= self::HTTP_EOL; // Write request //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Sending %d bytes to socketResource=%s (proxy tunnel) ...', strlen($proxyTunnel), $socketResource)); fwrite($socketResource, $proxyTunnel); // Got response? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: feof(%s)=%d', $socketResource, intval(feof($socketResource)))); if (feof($socketResource)) { // No response received //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: response()=%d, feof! - EXIT!', count($response))); return $response; } // Read the first line $resp = trim(fgets($socketResource, 10240)); $respArray = explode(' ', $resp); if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) { // Invalid response! return $response; } // All fine! //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: respArray()=%d - EXIT!', count($respArray))); return $respArray; } /** * Tried to extract hostname from given raw data. On a Genntoo system, this could be multiple lines with # as comments. So try to get rid of it * * @param $rawData Raw data from /etc/hostname file * @return $hostname Extracted host name * @throws InvalidArgumentException If a parameter is not valid */ protected function extractHostnameFromRawData (string $rawData) { // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: rawData=%s - CALLED!', $rawData)); if (empty($rawData)) { // Throw IAE throw new InvalidArgumentException('Parameter "rawData" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Default is invalid $hostname = 'invalid'; // Try to "explode" it $data = explode(PHP_EOL, $rawData); // "Walk" through it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: data()=%d', count($data))); foreach ($data as $line) { // Trim it $line = trim($line); // Begins with a hash (#) = comment? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: line=%s', $line)); if (substr($line, 0, 1) == '#') { // Then skip it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: line=%s - SKIPPED!', $line)); continue; } // Has an equals sign? if (strpos($line, '=') !== false) { // Then "explode" it again, right part is hostname in quotes $hostData = explode('=', $line); // Make sure only a key=value pair goes through //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostData()=%d', count($hostData))); if (count($hostData) < 2) { // Ops, wrong count throw new UnexpectedValueException(sprintf('hostData()=%d is unexpected, line=%s', count($hostData), $line), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE); } // Try to get it and abort $hostname = str_replace( ['"', chr(39)], ['', ''], $hostData[1] ); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from hostData - BREAK!', $hostname)); break; } else { // Use it directly $hostname = $line; //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from line - BREAK!', $line)); break; } } // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - EXIT!', $hostname)); return $hostname; } /** * Tries to resolve an IP address from given hostname. Currently only IPv * addresses are resolved. * * @param $hostname Host name we shall resolve * @return $ipAddress IPv4 address resolved from host name * @throws InvalidArgumentException If a parameter is not valid * @todo This should be connected to a caching class to cache DNS requests */ public static function resolveIpAddress (string $hostname) { // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - CALLED!', $hostname)); if (empty($hostname)) { // Throw IAE throw new InvalidArgumentException('Parameter "hostname" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (self::$isQuietResolver !== TRUE) { // Debug message self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('[%s:] Host name to resolve is: %s', __CLASS__, $hostname )); } // Default is false $ipAddress = false; // Is a dot at the end? if (substr($hostname, -1, 1) != '.') { /* * Then append it to prevent lookup of invalid host names through * all search-domains. This will greatly improve lookup performance * and has no disadvantages for anybody. A dot at the end of a * domain btw means full-qualified domain, do not prepend to any * other domain, basically. */ $hostname .= '.'; } // Resolve it // @TODO Here should the cacher be implemented //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s', $hostname)); $ipResolved = gethostbyname($hostname); // Was it fine? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: ipResolved[%s]=%s', gettype($ipResolved), $ipResolved)); if (($ipResolved !== false) && ($ipResolved != $hostname)) { // Okay, this works! $ipAddress = $ipResolved; // Quiet? if (self::$isQuietResolver !== TRUE) { // Debug message self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('[%s:] Resolved IP address is: %s', __CLASS__, $ipAddress )); } } else { // Problem while resolving IP address self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('[%s:] Problem resolving IP address for host %s. Please check your /etc/hosts file.', __CLASS__, $hostname )); } // Return resolved IP //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s - EXIT!', $ipAddress)); return $ipAddress; } /** * acquires this host's LAN name. It tries a varity of different source. * * @return $hostName The acquired name of this host or something invalid */ public static function acquireHostname () { // Is cache there? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!'); if (!isset(self::$cache[__METHOD__])) { // Get a new instance $toolsInstance = new ConsoleTools(); // Get SplFileInfo instance $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hostname_file')); // Init host name //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: infoInstance=%s', $infoInstance->__toString())); $hostname = 'host.invalid'; // Try to check /etc/hostname first try { // Get a file pointer $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', [$infoInstance]); // Read the file //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: fileInstance=%s', $fileInstance->__toString())); $rawData = trim($fileInstance->readFromFile()); // Close the file //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: rawData=%s', $rawData)); $fileInstance->closeFile(); // Extract hostname from it $hostname = $toolsInstance->extractHostnameFromRawData($rawData); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from /etc/hostname', $hostname)); } catch (FileNotFoundException $e) { // Fall-back to 'SESSION_SVR' which found on my Sun Station if (isset($_SERVER['SESSION_SVR'])) { // Resolve it $hostname = $_SERVER['SESSION_SVR']; } elseif (isset($_SERVER['COMPUTERNAME'])) { // May happen on some Windows XP systems, so also try this $hostname = $_SERVER['COMPUTERNAME']; } else { // Could not find our hostname self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('[%s:] WARNING: Cannot acquire my own host name.', $toolsInstance->__toString() )); } // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from _SERVER array', $hostname)); } catch (FrameworkException $e) { // Output debug message self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s', $toolsInstance->__toString(), $e->__toString(), $e->getHexCode(), $e->getMessage() )); } // Set cache //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting hostname=%s ...', $hostname)); self::$cache[__METHOD__] = $hostname; } else { // Get from cache $hostname = self::$cache[__METHOD__]; } // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - EXIT!', $hostname)); return $hostname; } /** * acquires the IP address of this host by reading the /etc/hostname file * and solving it. It is now stored in configuration * * @return $ipAddress acquired IPv4 address */ public static function acquireSelfIpAddress () { // Is cache there? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!'); if (!isset(self::$cache[__METHOD__])) { // Get host name $hostname = self::acquireHostname(); // Resolve the IP number //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s', $hostname)); $ipAddress = self::resolveIpAddress($hostname); // Set cache //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting ipAddress=%s ...', $ipAddress)); self::$cache[__METHOD__] = $ipAddress; } else { // Get it from cache $ipAddress = self::$cache[__METHOD__]; } // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s - EXIT!', $ipAddress)); return $ipAddress; } /** * Determines own remote IP address (e.g. can be used to probe if we are * reachable from outside by determining external address and then connect to it. * This is accomblished by connecting to the IP of www.shipsimu.org which * should default to 188.138.90.169 and requesting /ip.php which does only * return the content of $_SERVER['REMOTE_ADDR']. Of course, this method * requires a working Internet connection. * * This method is taken from a user comment on php.net and heavily rewritten. * Compare to following link: * http://de.php.net/manual/en/function.socket-create.php#49368 * * @return $externalAddress The determined external address address * @throws InvalidSocketException If socket initialization wents wrong or if an errors occurs * @todo This should be moved out to an external class, e.g. HttpClient * @todo Make IP, host name, port and script name configurable */ public static function determineExternalAddress () { // Get helper instance //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!'); $toolsInstance = new ConsoleTools(); // First get a socket // @TODO Add some DNS caching here // Open connection if ($toolsInstance->isProxyUsed() === true) { // Resolve hostname into IP address $ipAddress = self::resolveIpAddress(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host')); // Connect to host through proxy connection //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s', $ipAddress)); $socketResource = fsockopen($ipAddress, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30); } else { // Connect to host directly $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 30); } // Check if there was an error else //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: socketResource[%s]=%s,errorNo=%d,errorStr=%s', gettype($socketResource), $socketResource, $errorNo, $errorStr)); if ($errorNo > 0) { // Then throw again throw new InvalidSocketException([$toolsInstance, $socketResource, $errorNo, $errorStr], BaseFrameworkSystem::EXCEPTION_INVALID_SOCKET); } // Prepare the GET request $request = 'GET ' . ($toolsInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL; $request .= 'Host: shipsimu.org' . self::HTTP_EOL; $request .= 'User-Agent: ' . self::HTTP_USER_AGENT . self::HTTP_EOL; $request .= 'Connection: close' . self::HTTP_EOL; // Do we use proxy? if ($toolsInstance->isProxyUsed() === true) { // CONNECT method? if (FrameworkBootstrap::getConfigurationInstance()->isEnabled('proxy_connect_method')) { // Setup proxy tunnel $response = $toolsInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource); // If the response is invalid, abort if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) { // Invalid response! $toolsInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true)); } } else { // Add header for proxy $request .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL; } } // Add last HTTP_EOL $request .= self::HTTP_EOL; // Send it to the socket //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Writing %d bytes to socketResource=%s ...', strlen($request), $socketResource)); fwrite($socketResource, $request); // Init IP (this will always be the last line) $externalAddress = 'invalid'; // And read the reply while (!feof($socketResource)) { // Get line //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: feof()=%d', intval(feof($socketResource)))); $externalAddress = trim(fgets($socketResource, 128)); // Detect HTTP response //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: externalAddress=%s', $externalAddress)); if ((substr($externalAddress, 0, 7) == 'HTTP/1.') && (substr($externalAddress, -6, 6) != '200 OK')) { // Stop processing //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CONSOLE-TOOLS: BREAK!'); break; } } // Close socket fclose($socketResource); // Return determined external address //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: externalAddress=%s - EXIT!', $externalAddress)); return $externalAddress; } /** * Analyzes the 'environment', mostly $_SERVER, for presence of elements * which indicates clearly that e.g. this script has been executed from * console or web. * * @return $type The analyzed type, can be 'http' or 'console' */ public static function analyzeEnvironmentForType () { // Default is the console //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!'); $type = 'console'; // Now, do we have a request method, or query string set? if ((isset($_SERVER['REQUEST_METHOD'])) || (isset($_SERVER['QUERY_STRING']))) { // Possibly HTTP request $type = 'http'; } // Return it //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: type=%s - EXIT!', $type)); return $type; } /** * Analyzes the 'environment', mostly $_SERVER, for presence of elements * which indicates clearly that e.g. this script has been executed from * console or web. This method should be used for class names, they * currently are named differently. Here is a list to clarify this: * * Request type | Class type * ----------------------------- * http | web * console | console * * @return $type The analyzed type, can be 'http' or 'console' */ public static function analyzeEnvironmentForClassType () { // Default is the console //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!'); $type = 'console'; // Now, do we have a request method, or query string set? if (self::analyzeEnvironmentForType() == 'http') { // Possibly HTTP request $type = 'web'; } // Return it //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: type=%s - EXIT!', $type)); return $type; } }