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