]> git.mxchange.org Git - core.git/commitdiff
Added proxy support
authorRoland Häder <roland@mxchange.org>
Mon, 2 Jul 2012 19:13:54 +0000 (19:13 +0000)
committerRoland Häder <roland@mxchange.org>
Mon, 2 Jul 2012 19:13:54 +0000 (19:13 +0000)
inc/classes/main/console/class_ConsoleTools.php
inc/config.php

index e4a615a0e98c794bcfec28bd2def80e55affb1b3..b82eb19e5e42fd5f3db63dc0a7377a07daaee9ee 100644 (file)
@@ -46,7 +46,7 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        protected function resolveIpAddress ($hostname) {
                // Debug message
-               $this->debugOutput(sprintf('[%s:] Our host name is: %s',
+               $this->debugOutput(sprintf('[%s:] Host name to resolve is: %s',
                        $this->__toString(),
                        $hostname
                ));
@@ -80,6 +80,72 @@ class ConsoleTools extends BaseFrameworkSystem {
                return $ip;
        }
 
+       /**
+        * 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
+        * @param       $socketResource         Resource of a socket
+        * @return      $response                       Response array
+        */
+       protected function setupProxyTunnel ($host, $port, $socketResource) {
+               // Initialize array
+               $response = array('', '', '');
+               $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 ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
+                       // Add it as well
+                       $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password'));
+                       $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL;
+               } // END - if
+
+               // Add last new-line
+               $proxyTunnel .= self::HTTP_EOL;
+               //* DEBUG: */ $this->debugOutput('CONSOLE-TOOLS: proxyTunnel=' . $proxyTunnel);
+
+               // Write request
+               fwrite($socketResource, $proxyTunnel);
+
+               // Got response?
+               if (feof($socketResource)) {
+                       // No response received
+                       return $response;
+               } // END - if
+
+               // 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;
+               } // END - if
+
+               // All fine!
+               return $respArray;
+       }
+
        /**
         * Aquires the IP address of this host by reading the /etc/hostname file
         * and solving it. It is now stored in configuration
@@ -159,7 +225,18 @@ class ConsoleTools extends BaseFrameworkSystem {
 
                // First get a socket
                // @TODO Add some DNS caching here
-               $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 5);
+
+               // Open connection
+               if ($helperInstance->isProxyUsed() === true) {
+                       // Resolve hostname into IP address
+                       $ip = $helperInstance->resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
+
+                       // 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) {
@@ -168,10 +245,30 @@ class ConsoleTools extends BaseFrameworkSystem {
                } // END - if
 
                // Prepare the GET request
-               $request  = 'GET /ip.php HTTP/1.0' . self::HTTP_EOL;
+               $request  = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://ship-simu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
                $request .= 'Host: ship-simu.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 ($helperInstance->isProxyUsed() === true) {
+                       // CONNECT method?
+                       if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') {
+                               // Setup proxy tunnel
+                               $response = $helperInstance->setupProxyTunnel('ship-simu.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
@@ -182,12 +279,23 @@ class ConsoleTools extends BaseFrameworkSystem {
 
                // And read the reply
                while (!feof($socketResource)) {
-                       $externalAddress = fgets($socketResource, 128);
+                       // 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: */ $helperInstance->debugOutput('CONSOLE-TOOLS: Resolved external address: ' . $externalAddress);
+
                // Return determined external IP
                return $externalAddress;
        }
index 64bd8a384633c8b839fdd3df60ed9c67817bb72a..a81ea62eb8957ef33fd2adf18e525c727c5874ba 100644 (file)
@@ -332,5 +332,20 @@ $cfg->setConfigEntry('compressor_channel_class', 'CompressorChannel');
 // CFG: DEBUG-OUTPUT-TIMINGS
 $cfg->setConfigEntry('debug_output_timings', 'N');
 
+// CFG: PROXY-HOST
+$cfg->setConfigEntry('proxy_host', '');
+
+// CFG: PROXY-PORT
+$cfg->setConfigEntry('proxy_port', '');
+
+// CFG: PROXY-USERNAME
+$cfg->setConfigEntry('proxy_username', '');
+
+// CFG: PROXY-PASSWORD
+$cfg->setConfigEntry('proxy_password', '');
+
+// CFG: PROXY-CONNECT-METHOD
+$cfg->setConfigEntry('proxy_connect_method', 'Y');
+
 // [EOF]
 ?>