|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderBot\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Fabiang\Xmpp\Options; |
|
6
|
|
|
|
|
7
|
|
|
class SocksProxy extends SocketClient |
|
8
|
|
|
{ |
|
9
|
|
|
private $realAddress; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(Options $options) |
|
12
|
|
|
{ |
|
13
|
|
|
$contextOptions = $options->getContextOptions(); |
|
14
|
|
|
$contextOptions['ssl']['verify_peer_name'] = false; |
|
15
|
|
|
parent::__construct($options->getSocksProxyAddress(), $contextOptions); |
|
16
|
|
|
$this->realAddress = ltrim('tcp://', $options->getAddress()); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function connect($timeout = 30, $persistent = false) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::connect($timeout, $persistent); |
|
22
|
|
|
$this->write(chr(5).chr(1).chr(0)); |
|
23
|
|
|
$version = ord($this->read(1)); |
|
24
|
|
|
$method = ord($this->read(1)); |
|
25
|
|
|
if ($version !== 5) { |
|
26
|
|
|
throw new \Exception("Wrong SOCKS5 version: $version"); |
|
27
|
|
|
} |
|
28
|
|
|
if ($method !== 0) { |
|
29
|
|
|
throw new \Exception("Wrong method: $method"); |
|
30
|
|
|
} |
|
31
|
|
|
list($address, $port) = explode(':', $this->realAddress); |
|
32
|
|
|
$payload = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($address)).$address; |
|
33
|
|
|
$payload .= pack('n', $port); |
|
34
|
|
|
$this->write($payload); |
|
35
|
|
|
|
|
36
|
|
|
$version = ord($this->read(1)); |
|
37
|
|
|
if ($version !== 5) { |
|
38
|
|
|
throw new \Exception("Wrong SOCKS5 version after CONNECT: $version"); |
|
39
|
|
|
} |
|
40
|
|
|
$rep = ord($this->read(1)); |
|
41
|
|
|
if ($rep !== 0) { |
|
42
|
|
|
throw new \Exception("Wrong SOCKS5 rep after CONNECT: $rep"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$rsv = ord($this->read(1)); |
|
46
|
|
|
if ($rsv !== 0) { |
|
47
|
|
|
throw new \Exception("Wrong socks5 final RSV after CONNECT: $rsv"); |
|
48
|
|
|
} |
|
49
|
|
|
switch (ord($this->read(1))) { |
|
50
|
|
|
case 1: |
|
51
|
|
|
$ip = inet_ntop($this->read(4)); |
|
|
|
|
|
|
52
|
|
|
break; |
|
53
|
|
|
case 4: |
|
54
|
|
|
$ip = inet_ntop($this->read(16)); |
|
|
|
|
|
|
55
|
|
|
break; |
|
56
|
|
|
case 3: |
|
57
|
|
|
$ip = $this->read(ord($this->read(1))); |
|
|
|
|
|
|
58
|
|
|
break; |
|
59
|
|
|
} |
|
60
|
|
|
$port = unpack('n', $this->read(2))[1]; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.