Skip to content

Commit

Permalink
Removes manual DNS resolution
Browse files Browse the repository at this point in the history
Starting with PHP 5.6.0, ssl certificates fail when using mismatching
hostnames in the request and certificate. Because of that, the hostname
used in fsockopen has to be stream.twitter.com instead of a manually
resolved ip.
  • Loading branch information
mkraemer committed Sep 15, 2014
1 parent 408d782 commit 7d37d7a
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions lib/Phirehose.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,21 +632,9 @@ protected function connect()
$scheme = ($urlParts['scheme'] == 'https') ? 'ssl://' : 'tcp://';
$port = ($urlParts['scheme'] == 'https') ? $this->secureHostPort : $this->hostPort;

/**
* We must perform manual host resolution here as Twitter's IP regularly rotates (ie: DNS TTL of 60 seconds) and
* PHP appears to cache it the result if in a long running process (as per Phirehose).
*/
$streamIPs = gethostbynamel($urlParts['host']);
if(empty($streamIPs)) {
throw new PhirehoseNetworkException("Unable to resolve hostname: '" . $urlParts['host'] . '"');
}

// Choose one randomly (if more than one)
$this->log('Resolved host ' . $urlParts['host'] . ' to ' . implode(', ', $streamIPs));
$streamIP = $streamIPs[rand(0, (count($streamIPs) - 1))];
$this->log("Connecting to {$scheme}{$streamIP}, port={$port}, connectTimeout={$this->connectTimeout}");
$this->log("Connecting to {$scheme}{$urlParts['host']}, port={$port}, connectTimeout={$this->connectTimeout}");

@$this->conn = fsockopen($scheme . $streamIP, $port, $errNo, $errStr, $this->connectTimeout);
@$this->conn = fsockopen($scheme . $urlParts['host'], $port, $errNo, $errStr, $this->connectTimeout);

// No go - handle errors/backoff
if (!$this->conn || !is_resource($this->conn)) {
Expand All @@ -667,7 +655,7 @@ protected function connect()
}

// TCP connect OK, clear last error (if present)
$this->log('Connection established to ' . $streamIP);
$this->log('Connection established to ' . $urlParts['host']);
$this->lastErrorMsg = NULL;
$this->lastErrorNo = NULL;

Expand Down

0 comments on commit 7d37d7a

Please sign in to comment.