XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
SOCKS5.php
Go to the documentation of this file.
1 <?php
22 require_once 'HTTP/Request2/SocketWrapper.php';
23 
37 {
51  public function __construct(
52  $address, $timeout = 10, array $contextOptions = array(),
53  $username = null, $password = null
54  ) {
55  parent::__construct($address, $timeout, $contextOptions);
56 
57  if (strlen($username)) {
58  $request = pack('C4', 5, 2, 0, 2);
59  } else {
60  $request = pack('C3', 5, 1, 0);
61  }
62  $this->write($request);
63  $response = unpack('Cversion/Cmethod', $this->read(3));
64  if (5 != $response['version']) {
66  'Invalid version received from SOCKS5 proxy: ' . $response['version'],
68  );
69  }
70  switch ($response['method']) {
71  case 2:
72  $this->performAuthentication($username, $password);
73  case 0:
74  break;
75  default:
77  "Connection rejected by proxy due to unsupported auth method"
78  );
79  }
80  }
81 
92  protected function performAuthentication($username, $password)
93  {
94  $request = pack('C2', 1, strlen($username)) . $username
95  . pack('C', strlen($password)) . $password;
96 
97  $this->write($request);
98  $response = unpack('Cvn/Cstatus', $this->read(3));
99  if (1 != $response['vn'] || 0 != $response['status']) {
101  'Connection rejected by proxy due to invalid username and/or password'
102  );
103  }
104  }
105 
115  public function connect($remoteHost, $remotePort)
116  {
117  $request = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($remoteHost))
118  . $remoteHost . pack('n', $remotePort);
119 
120  $this->write($request);
121  $response = unpack('Cversion/Creply/Creserved', $this->read(1024));
122  if (5 != $response['version'] || 0 != $response['reserved']) {
124  'Invalid response received from SOCKS5 proxy',
126  );
127  } elseif (0 != $response['reply']) {
129  "Unable to connect to {$remoteHost}:{$remotePort} through SOCKS5 proxy",
130  0, $response['reply']
131  );
132  }
133  }
134 }
135 ?>
performAuthentication($username, $password)
Definition: SOCKS5.php:92
__construct($address, $timeout=10, array $contextOptions=array(), $username=null, $password=null)
Definition: SOCKS5.php:51
connect($remoteHost, $remotePort)
Definition: SOCKS5.php:115