+++ /dev/null
-
- /**
- * 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
- * @todo Make IP, host name, port and script name configurable
- */
- public static function determineExternalAddress () {
- // Get helper instance
- $helperInstance = new ConsoleTools();
-
- // First get a socket
- // @TODO Add some DNS caching here
-
- // Open connection
- if ($helperInstance->isProxyUsed() === true) {
- // Resolve hostname into IP address
- $ip = ConsoleTools::resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
-
- // @TODO Handle $ip = false
-
- // Connect to host through proxy connection
- $socketResource = fsockopen($ip, $helperInstance->getConfigInstance()->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
- if ($errorNo > 0) {
- // Then throw again
- throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- // Prepare the GET request
- $request = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
- $request .= 'Host: shipsimu.org' . self::HTTP_EOL;
- $request .= 'User-Agent: ' . $this->getUserAgent() . self::HTTP_EOL;
- $request .= 'Connection: close' . self::HTTP_EOL;
-
- // Do we use proxy?
- if ($helperInstance->isProxyUsed() === true) {
- // CONNECT method?
- if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') {
- // Setup proxy tunnel
- $response = $helperInstance->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!
- $helperInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true));
- } // END - if
- } else {
- // Add header for proxy
- $request .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL;
- }
- } // END - if
-
- // Add last HTTP_EOL
- $request .= self::HTTP_EOL;
-
- // Send it to the socket
- fwrite($socketResource, $request);
-
- // Init IP (this will always be the last line)
- $externalAddress = 'invalid';
-
- // And read the reply
- while (!feof($socketResource)) {
- // Get line
- $externalAddress = trim(fgets($socketResource, 128));
-
- // Detect HTTP response
- if ((substr($externalAddress, 0, 7) == 'HTTP/1.') && (substr($externalAddress, -6, 6) != '200 OK')) {
- // Stop processing
- break;
- } // END - if
- } // END - while
-
- // Close socket
- fclose($socketResource);
-
-
- // Debug message
- /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Resolved external address: ' . $externalAddress);
-
- // Return determined external address
- return $externalAddress;
- }
+++ /dev/null
-<?php
-// Own namespace
-namespace CoreFramework\Client\Http;
-
-/**
- * A HTTP client class
- *
- * @author Roland Haeder <webmaster@ship-simu.org>
- * @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
- * @license GNU GPL 3.0 or any newer version
- * @link http://www.ship-simu.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 <http://www.gnu.org/licenses/>.
- */
-class HttpClient extends BaseClient implements Client {
- // Constants
- const HTTP_EOL = "\r\n";
- const HTTP_USER_AGENT = 'HttpClient-Core/1.0';
-
- /**
- * Protected constructor
- *
- * @return void
- */
- protected function __construct () {
- // Set default user agent string (to allow other classes to override this)
- $this->setUserAgent(self::HTTP_USER_AGENT);
-
- // Call parent constructor
- parent::__construct(__CLASS__);
- }
-
- /**
- * Creates an instance of this Client class and prepares it for usage
- *
- * @param $socketResource Resource of a socket (optional)
- * @return $clientInstance An instance of a Client class
- */
- public final static function createHttpClient ($socketResouce = false) {
- // Get a new instance
- $clientInstance = new HttpClient();
-
- // Set socket resource
- $clientInstance->setSocketResource($socketResource);
-
- // Return the prepared instance
- return $clientInstance;
- }
-
- /**
- * Checks wether proxy configuration is used
- *
- * @return $isUsed Wether proxy is used
- */
- protected function isProxyUsed () {
- // Do we have cache?
- if (!isset($GLOBALS[__METHOD__])) {
- // Determine it
- $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0));
- } // END - if
-
- // Return cache
- 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
- * @return $response Response array
- */
- protected function setupProxyTunnel ($host, $port) {
- // Initialize array
- $response = array('', '', '');
-
- // Do the connect
- $respArray = $this->doConnectRequest($host, $port);
-
- // Analyze first header line
- if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
- // Response code is not 200
- return $response;
- } // END - if
-
- // All fine!
- return $respArray;
- }
-
- /**
- * Sends a raw HTTP request out to given IP/host and port number
- *
- * @param $method Request method (GET, POST, HEAD, CONNECT, ...)
- * @param $host Host to connect to
- * @param $port Port number to connect to
- * @return $responseArray Array with raw response
- */
- private function sendRawHttpRequest ($method, $host, $port, array $header = array()) {
- // Minimum raw HTTP/1.1 request
- $rawRequest = $method . ' ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
- $rawRequest .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
-
- // Use login data to proxy? (username at least)
- if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
- // Add it as well
- $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password'));
- $rawRequest .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL;
- } // END - if
-
- // Add last new-line
- $rawRequest .= self::HTTP_EOL;
- //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT[' . __METHOD__ . ':' . __LINE__ . ']: rawRequest=' . $rawRequest);
-
- // Write request
- fwrite($this->getSocketResource(), $rawRequest);
-
- // Got response?
- if (feof($this->getSocketResource())) {
- // No response received
- return $response;
- } // END - if
-
- // Read the first line
- $resp = trim(fgets($this->getSocketResource(), 10240));
-
- // "Explode" the string to an array
- $responseArray = explode(' ', $resp);
-
- // And return it
- return $responseArray;
- }
-
- /**
- * A HTTP/1.1 CONNECT request
- *
- * @param $host Host to connect to
- * @param $port Port number to connect to
- * @return $responseArray An array with the read response
- */
- public function doConnectRequest ($host, $port) {
- // Prepare extra header(s)
- $headers = array(
- 'Proxy-Connection' => 'Keep-Alive'
- );
-
- // Prepare raw request
- $responseArray = $this->sendRawHttpRequest('CONNECT', $host, $port, $headers);
-
- // Return response array
- return $responseArray;
- }
-
-}