3 * Determines own remote IP address (e.g. can be used to probe if we are
4 * reachable from outside by determining external address and then connect to it.
5 * This is accomblished by connecting to the IP of www.shipsimu.org which
6 * should default to 188.138.90.169 and requesting /ip.php which does only
7 * return the content of $_SERVER['REMOTE_ADDR']. Of course, this method
8 * requires a working Internet connection.
10 * This method is taken from a user comment on php.net and heavily rewritten.
11 * Compare to following link:
12 * http://de.php.net/manual/en/function.socket-create.php#49368
14 * @return $externalAddress The determined external address address
15 * @todo Make IP, host name, port and script name configurable
17 public static function determineExternalAddress () {
18 // Get helper instance
19 $helperInstance = new ConsoleTools();
22 // @TODO Add some DNS caching here
25 if ($helperInstance->isProxyUsed() === true) {
26 // Resolve hostname into IP address
27 $ip = ConsoleTools::resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
29 // @TODO Handle $ip = false
31 // Connect to host through proxy connection
32 $socketResource = fsockopen($ip, $helperInstance->getConfigInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30);
34 // Connect to host directly
35 $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 30);
38 // Check if there was an error else
41 throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET);
44 // Prepare the GET request
45 $request = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
46 $request .= 'Host: shipsimu.org' . self::HTTP_EOL;
47 $request .= 'User-Agent: ' . $this->getUserAgent() . self::HTTP_EOL;
48 $request .= 'Connection: close' . self::HTTP_EOL;
51 if ($helperInstance->isProxyUsed() === true) {
53 if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') {
55 $response = $helperInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource);
57 // If the response is invalid, abort
58 if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
60 $helperInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true));
63 // Add header for proxy
64 $request .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL;
69 $request .= self::HTTP_EOL;
71 // Send it to the socket
72 fwrite($socketResource, $request);
74 // Init IP (this will always be the last line)
75 $externalAddress = 'invalid';
78 while (!feof($socketResource)) {
80 $externalAddress = trim(fgets($socketResource, 128));
82 // Detect HTTP response
83 if ((substr($externalAddress, 0, 7) == 'HTTP/1.') && (substr($externalAddress, -6, 6) != '200 OK')) {
90 fclose($socketResource);
94 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Resolved external address: ' . $externalAddress);
96 // Return determined external address
97 return $externalAddress;