| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * SOCKS5 proxy connection class
|
|---|
| 4 | *
|
|---|
| 5 | * PHP version 5
|
|---|
| 6 | *
|
|---|
| 7 | * LICENSE:
|
|---|
| 8 | *
|
|---|
| 9 | * Copyright (c) 2008-2012, Alexey Borzov <[email protected]>
|
|---|
| 10 | * All rights reserved.
|
|---|
| 11 | *
|
|---|
| 12 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | * modification, are permitted provided that the following conditions
|
|---|
| 14 | * are met:
|
|---|
| 15 | *
|
|---|
| 16 | * * Redistributions of source code must retain the above copyright
|
|---|
| 17 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 18 | * * Redistributions in binary form must reproduce the above copyright
|
|---|
| 19 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 20 | * documentation and/or other materials provided with the distribution.
|
|---|
| 21 | * * The names of the authors may not be used to endorse or promote products
|
|---|
| 22 | * derived from this software without specific prior written permission.
|
|---|
| 23 | *
|
|---|
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|---|
| 25 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 26 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|---|
| 27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|---|
| 28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|---|
| 29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|---|
| 31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|---|
| 32 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|---|
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|---|
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 35 | *
|
|---|
| 36 | * @category HTTP
|
|---|
| 37 | * @package HTTP_Request2
|
|---|
| 38 | * @author Alexey Borzov <[email protected]>
|
|---|
| 39 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|---|
| 40 | * @version SVN: $Id: SOCKS5.php 324953 2012-04-08 07:24:12Z avb $
|
|---|
| 41 | * @link http://pear.php.net/package/HTTP_Request2
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | /** Socket wrapper class used by Socket Adapter */
|
|---|
| 45 | require_once 'HTTP/Request2/SocketWrapper.php';
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * SOCKS5 proxy connection class (used by Socket Adapter)
|
|---|
| 49 | *
|
|---|
| 50 | * @category HTTP
|
|---|
| 51 | * @package HTTP_Request2
|
|---|
| 52 | * @author Alexey Borzov <[email protected]>
|
|---|
| 53 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|---|
| 54 | * @version Release: 2.1.1
|
|---|
| 55 | * @link http://pear.php.net/package/HTTP_Request2
|
|---|
| 56 | * @link http://pear.php.net/bugs/bug.php?id=19332
|
|---|
| 57 | * @link http://tools.ietf.org/html/rfc1928
|
|---|
| 58 | */
|
|---|
| 59 | class HTTP_Request2_SOCKS5 extends HTTP_Request2_SocketWrapper
|
|---|
| 60 | {
|
|---|
| 61 | /**
|
|---|
| 62 | * Constructor, tries to connect and authenticate to a SOCKS5 proxy
|
|---|
| 63 | *
|
|---|
| 64 | * @param string $address Proxy address, e.g. 'tcp://localhost:1080'
|
|---|
| 65 | * @param int $timeout Connection timeout (seconds)
|
|---|
| 66 | * @param array $sslOptions SSL context options
|
|---|
| 67 | * @param string $username Proxy user name
|
|---|
| 68 | * @param string $password Proxy password
|
|---|
| 69 | *
|
|---|
| 70 | * @throws HTTP_Request2_LogicException
|
|---|
| 71 | * @throws HTTP_Request2_ConnectionException
|
|---|
| 72 | * @throws HTTP_Request2_MessageException
|
|---|
| 73 | */
|
|---|
| 74 | public function __construct(
|
|---|
| 75 | $address, $timeout = 10, array $sslOptions = array(),
|
|---|
| 76 | $username = null, $password = null
|
|---|
| 77 | ) {
|
|---|
| 78 | parent::__construct($address, $timeout, $sslOptions);
|
|---|
| 79 |
|
|---|
| 80 | if (strlen($username)) {
|
|---|
| 81 | $request = pack('C4', 5, 2, 0, 2);
|
|---|
| 82 | } else {
|
|---|
| 83 | $request = pack('C3', 5, 1, 0);
|
|---|
| 84 | }
|
|---|
| 85 | $this->write($request);
|
|---|
| 86 | $response = unpack('Cversion/Cmethod', $this->read(3));
|
|---|
| 87 | if (5 != $response['version']) {
|
|---|
| 88 | throw new HTTP_Request2_MessageException(
|
|---|
| 89 | 'Invalid version received from SOCKS5 proxy: ' . $response['version'],
|
|---|
| 90 | HTTP_Request2_Exception::MALFORMED_RESPONSE
|
|---|
| 91 | );
|
|---|
| 92 | }
|
|---|
| 93 | switch ($response['method']) {
|
|---|
| 94 | case 2:
|
|---|
| 95 | $this->performAuthentication($username, $password);
|
|---|
| 96 | case 0:
|
|---|
| 97 | break;
|
|---|
| 98 | default:
|
|---|
| 99 | throw new HTTP_Request2_ConnectionException(
|
|---|
| 100 | "Connection rejected by proxy due to unsupported auth method"
|
|---|
| 101 | );
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Performs username/password authentication for SOCKS5
|
|---|
| 107 | *
|
|---|
| 108 | * @param string $username Proxy user name
|
|---|
| 109 | * @param string $password Proxy password
|
|---|
| 110 | *
|
|---|
| 111 | * @throws HTTP_Request2_ConnectionException
|
|---|
| 112 | * @throws HTTP_Request2_MessageException
|
|---|
| 113 | * @link http://tools.ietf.org/html/rfc1929
|
|---|
| 114 | */
|
|---|
| 115 | protected function performAuthentication($username, $password)
|
|---|
| 116 | {
|
|---|
| 117 | $request = pack('C2', 1, strlen($username)) . $username
|
|---|
| 118 | . pack('C', strlen($password)) . $password;
|
|---|
| 119 |
|
|---|
| 120 | $this->write($request);
|
|---|
| 121 | $response = unpack('Cvn/Cstatus', $this->read(3));
|
|---|
| 122 | if (1 != $response['vn'] || 0 != $response['status']) {
|
|---|
| 123 | throw new HTTP_Request2_ConnectionException(
|
|---|
| 124 | 'Connection rejected by proxy due to invalid username and/or password'
|
|---|
| 125 | );
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * Connects to a remote host via proxy
|
|---|
| 131 | *
|
|---|
| 132 | * @param string $remoteHost Remote host
|
|---|
| 133 | * @param int $remotePort Remote port
|
|---|
| 134 | *
|
|---|
| 135 | * @throws HTTP_Request2_ConnectionException
|
|---|
| 136 | * @throws HTTP_Request2_MessageException
|
|---|
| 137 | */
|
|---|
| 138 | public function connect($remoteHost, $remotePort)
|
|---|
| 139 | {
|
|---|
| 140 | $request = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($remoteHost))
|
|---|
| 141 | . $remoteHost . pack('n', $remotePort);
|
|---|
| 142 |
|
|---|
| 143 | $this->write($request);
|
|---|
| 144 | $response = unpack('Cversion/Creply/Creserved', $this->read(1024));
|
|---|
| 145 | if (5 != $response['version'] || 0 != $response['reserved']) {
|
|---|
| 146 | throw new HTTP_Request2_MessageException(
|
|---|
| 147 | 'Invalid response received from SOCKS5 proxy',
|
|---|
| 148 | HTTP_Request2_Exception::MALFORMED_RESPONSE
|
|---|
| 149 | );
|
|---|
| 150 | } elseif (0 != $response['reply']) {
|
|---|
| 151 | throw new HTTP_Request2_ConnectionException(
|
|---|
| 152 | "Unable to connect to {$remoteHost}:{$remotePort} through SOCKS5 proxy",
|
|---|
| 153 | 0, $response['reply']
|
|---|
| 154 | );
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | ?> |
|---|