Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a5ab6f2
Showing
16 changed files
with
3,442 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
<?php | ||
|
||
/** | ||
* Project: TorUtils: PHP classes for interacting with Tor | ||
* File: DirectoryClient.php | ||
* | ||
* Copyright (c) 2015, Drew Phillips | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, | ||
* are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* Any modifications to the library should be indicated clearly in the source code | ||
* to inform users that the changes are not a part of the original software. | ||
* | ||
* @copyright 2015 Drew Phillips | ||
* @author Drew Phillips <drew@drew-phillips.com> | ||
* @version 1.0 | ||
* | ||
*/ | ||
|
||
namespace Dapphp\TorUtils; | ||
|
||
require_once 'Parser.php'; | ||
require_once 'ProtocolReply.php'; | ||
|
||
use Dapphp\TorUtils\Parser; | ||
use Dapphp\TorUtils\ProtocolReply; | ||
|
||
/** | ||
* Class for getting router info from Tor directory authorities | ||
* | ||
*/ | ||
class DirectoryClient | ||
{ | ||
/** | ||
* https://gitweb.torproject.org/tor.git/tree/src/or/config.c#n854 | ||
* | ||
* @var $DirectoryAuthorities List of directory authorities | ||
*/ | ||
private $DirectoryAuthorities = array( | ||
'7BE683E65D48141321C5ED92F075C55364AC7123' => '193.23.244.244:80', // dannenberg | ||
'7EA6EAD6FD83083C538F44038BBFA077587DD755' => '194.109.206.212:80', // dizum | ||
'CF6D0AAFB385BE71B8E111FC5CFF4B47923733BC' => '154.35.175.225:80', // Faravahar | ||
'F2044413DAC2E02E3D6BCF4735A19BCA1DE97281' => '131.188.40.189:80', // gabelmoo | ||
'74A910646BCEEFBCD2E874FC1DC997430F968145' => '199.254.238.52:80', // longclaw | ||
'BD6A829255CB08E66FBE7D3748363586E46B3810' => '171.25.193.9:443', // maatuska | ||
'9695DFC35FFEB861329B9F1AB04C46397020CE31' => '128.31.0.34:9131', // moria1 | ||
'4A0CCD2DDC7995083D73F5D667100C8A5831F16D' => '82.94.251.203:80', // Tonga | ||
'847B1F850344D7876491A54892F904934E4EB85D' => '86.59.21.38:80', // tor26 | ||
'0AD3FA884D18F89EEA2D89C019379E0E7FD94417' => '208.83.223.34:443', // urras | ||
); | ||
|
||
private $_connectTimeout = 5; | ||
private $_userAgent = 'dapphp/TorUtils 0.1'; | ||
|
||
private $_parser; | ||
private $_serverList; | ||
|
||
/** | ||
* DirectoryClient constructor | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->_serverList = $this->DirectoryAuthorities; | ||
shuffle($this->_serverList); | ||
|
||
$this->_parser = new Parser(); | ||
} | ||
|
||
/** | ||
* Fetch a list of all known router descriptors on the Tor network | ||
* | ||
* @return array Array of RouterDescriptor objects | ||
*/ | ||
public function getAllServerDescriptors() | ||
{ | ||
$reply = $this->_request('/tor/server/all.z'); | ||
|
||
$descriptors = $this->_parser->parseDirectoryStatus($reply); | ||
|
||
return $descriptors; | ||
} | ||
|
||
/** | ||
* Fetch directory information about a router | ||
* @param string|array $fingerprint router fingerprint or array of fingerprints to get information about | ||
* @return mixed Array of RouterDescriptor objects, or a single RouterDescriptor object | ||
*/ | ||
public function getServerDescriptor($fingerprint) | ||
{ | ||
if (is_array($fingerprint)) { | ||
$fp = implode('+', $fingerprint); | ||
} else { | ||
$fp = $fingerprint; | ||
} | ||
|
||
$uri = sprintf('/tor/server/fp/%s.z', $fp); | ||
|
||
$reply = $this->_request($uri); | ||
|
||
$descriptors = $this->_parser->parseDirectoryStatus($reply); | ||
|
||
if (sizeof($descriptors) == 1) { | ||
return array_shift($descriptors); | ||
} else { | ||
return $descriptors; | ||
} | ||
} | ||
|
||
/** | ||
* Pick a random dir authority to query and perform the HTTP request for directory info | ||
* | ||
* @param string $uri Uri to request | ||
* @param string $directoryServer IP and port of the directory to query | ||
* @throws \Exception No authorities responded | ||
* @return \Dapphp\TorUtils\ProtocolReply The reply from the directory authority | ||
*/ | ||
private function _request($uri, $directoryServer = null) | ||
{ | ||
reset($this->_serverList); | ||
|
||
do { | ||
// pick a server from the list, it is randomized in __construct | ||
$server = $this->getNextServer(); | ||
if ($server === false) { | ||
throw new \Exception('No more directory servers available to query'); | ||
} | ||
|
||
list($host, $port) = @explode(':', $server); | ||
if (!$port) $port = 80; | ||
|
||
$fp = fsockopen($host, $port, $errno, $errstr, $this->_connectTimeout); | ||
if (!$fp) continue; | ||
|
||
$request = $this->_getHttpRequest('GET', $host, $uri); | ||
|
||
fwrite($fp, $request); | ||
|
||
$response = ''; | ||
|
||
while (!feof($fp)) { | ||
$response .= fgets($fp); | ||
} | ||
|
||
fclose($fp); | ||
|
||
list($headers, $body) = explode("\r\n\r\n", $response, 2); | ||
$headers = $this->_parseHttpResponseHeaders($headers); | ||
|
||
if ($headers['status_code'] !== '200') { | ||
throw new \Exception( | ||
sprintf('Directory returned a negative response code to request. %s %s', $headers['status_code'], $headers['message']) | ||
); | ||
} | ||
|
||
$encoding = (isset($headers['headers']['content-encoding'])) ? $headers['headers']['content-encoding'] : null; | ||
|
||
if ($encoding == 'deflate') { | ||
if (!function_exists('gzuncompress')) { | ||
throw new \Exception('Directory response was gzip compressed but PHP does not have zlib support enabled'); | ||
} | ||
|
||
$body = gzuncompress($body); | ||
if ($body === false) { | ||
throw new \Exception('Failed to inflate response data'); | ||
} | ||
} else if ($encoding == 'identity') { | ||
// nothing to do | ||
} else { | ||
throw new \Exception('Directory sent response in an unknown encoding: ' . $encoding); | ||
} | ||
|
||
break; | ||
} while (true); | ||
|
||
$reply = new ProtocolReply(); | ||
$reply->appendReplyLine( | ||
sprintf('%s %s', $headers['status_code'], $headers['message']) | ||
); | ||
$reply->appendReplyLines(explode("\n", $body)); | ||
|
||
return $reply; | ||
} | ||
|
||
/** | ||
* Construct an http request for talking to a directory server | ||
* | ||
* @param string $method GET|POST | ||
* @param string $host IP/hostname to query | ||
* @param string $uri The request URI | ||
* @return string Completed HTTP request | ||
*/ | ||
private function _getHttpRequest($method, $host, $uri) | ||
{ | ||
$request = sprintf( | ||
"%s %s HTTP/1.0\r\n" . | ||
"Host: $host\r\n" . | ||
"Connection: close\r\n" . | ||
"User-Agent: %s\r\n" . | ||
"\r\n", | ||
$method, $uri, $host, $this->_userAgent | ||
); | ||
|
||
return $request; | ||
} | ||
|
||
/** | ||
* Parse HTTP response headers from the directory reply | ||
* | ||
* @param string $headers String of http response headers | ||
* @throws \Exception Response was not a valid http response | ||
* @return array Array with http status_code, message, and lines of headers | ||
*/ | ||
private function _parseHttpResponseHeaders($headers) | ||
{ | ||
$lines = explode("\r\n", $headers); | ||
$response = array_shift($lines); | ||
$header = array(); | ||
|
||
if (!preg_match('/^HTTP\/\d\.\d (\d{3}) (.*)$/i', $response, $match)) { | ||
throw new \Exception('Directory server sent a malformed HTTP response'); | ||
} | ||
|
||
$code = $match[1]; | ||
$message = $match[2]; | ||
|
||
foreach($lines as $line) { | ||
if (strpos($line, ':') === false) { | ||
throw new \Exception('Directory server sent an HTTP response line missing the ":" separator'); | ||
} | ||
list($name, $value) = explode(':', $line, 2); | ||
$header[strtolower($name)] = trim($value); | ||
} | ||
|
||
return array( | ||
'status_code' => $code, | ||
'message' => $message, | ||
'headers' => $header, | ||
); | ||
} | ||
|
||
/** | ||
* Get the next directory authority from the list to query | ||
* | ||
* @return string IP:Port of directory | ||
*/ | ||
private function getNextServer() | ||
{ | ||
$server = current($this->_serverList); | ||
next($this->_serverList); | ||
return $server; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Copyright (c) 2015, Drew Phillips | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of TorUtils nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
Oops, something went wrong.