1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderBot\Service; |
4
|
|
|
|
5
|
|
|
use Fabiang\Xmpp\Options; |
6
|
|
|
|
7
|
|
|
class SocksProxy extends SocketClient |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
private $realAddress; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
private $username; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $password; |
17
|
|
|
|
18
|
|
|
public function __construct(Options $options) |
19
|
|
|
{ |
20
|
|
|
$contextOptions = $options->getContextOptions(); |
21
|
|
|
$contextOptions['ssl']['verify_peer_name'] = false; |
22
|
|
|
$addr = explode('@', ltrim('tcp://', $options->getSocksProxyAddress())); |
23
|
|
|
if (count($addr) > 1) { |
24
|
|
|
$socksAddr = $addr[1]; |
25
|
|
|
list($this->username, $this->password) = explode(':', $addr[0]); |
26
|
|
|
} else { |
27
|
|
|
$socksAddr = $addr[0]; |
28
|
|
|
} |
29
|
|
|
parent::__construct($socksAddr, $contextOptions); |
30
|
|
|
$this->realAddress = ltrim('tcp://', $options->getAddress()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function connect($timeout = 30, $persistent = false) |
34
|
|
|
{ |
35
|
|
|
parent::connect($timeout, $persistent); |
36
|
|
|
$methods = chr(0); |
37
|
|
|
if ($this->username) { |
38
|
|
|
$methods .= chr(2); |
39
|
|
|
} |
40
|
|
|
$this->write(chr(5).chr(strlen($methods)).$methods); |
41
|
|
|
$version = ord($this->read(1)); |
42
|
|
|
$method = ord($this->read(1)); |
43
|
|
|
if ($version !== 5) { |
44
|
|
|
throw new \Exception("Wrong SOCKS5 version: $version"); |
45
|
|
|
} |
46
|
|
|
if ($method === 2) { |
47
|
|
|
$this->write(chr(1).chr(strlen($this->username)).$this->username.chr(strlen($this->password)).$this->password); |
48
|
|
|
|
49
|
|
|
$version = ord($this->read(1)); |
50
|
|
|
if ($version !== 1) { |
51
|
|
|
throw new \Exception("Wrong authorized SOCKS version: $version"); |
52
|
|
|
} |
53
|
|
|
$result = ord($this->read(1)); |
54
|
|
|
if ($result !== 0) { |
55
|
|
|
throw new \Exception("Wrong authorization status: $result"); |
56
|
|
|
} |
57
|
|
|
} elseif ($method !== 0) { |
58
|
|
|
throw new \Exception("Wrong method: $method"); |
59
|
|
|
} |
60
|
|
|
list($address, $port) = explode(':', $this->realAddress); |
61
|
|
|
$payload = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($address)).$address; |
62
|
|
|
$payload .= pack('n', $port); |
63
|
|
|
$this->write($payload); |
64
|
|
|
|
65
|
|
|
$version = ord($this->read(1)); |
66
|
|
|
if ($version !== 5) { |
67
|
|
|
throw new \Exception("Wrong SOCKS5 version after CONNECT: $version"); |
68
|
|
|
} |
69
|
|
|
$rep = ord($this->read(1)); |
70
|
|
|
if ($rep !== 0) { |
71
|
|
|
throw new \Exception("Wrong SOCKS5 rep after CONNECT: $rep"); |
72
|
|
|
} |
73
|
|
|
$rsv = ord($this->read(1)); |
74
|
|
|
if ($rsv !== 0) { |
75
|
|
|
throw new \Exception("Wrong socks5 final RSV after CONNECT: $rsv"); |
76
|
|
|
} |
77
|
|
|
switch (ord($this->read(1))) { |
78
|
|
|
case 1: |
79
|
|
|
$ip = inet_ntop($this->read(4)); |
|
|
|
|
80
|
|
|
break; |
81
|
|
|
case 4: |
82
|
|
|
$ip = inet_ntop($this->read(16)); |
|
|
|
|
83
|
|
|
break; |
84
|
|
|
case 3: |
85
|
|
|
$ip = $this->read(ord($this->read(1))); |
|
|
|
|
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
$port = unpack('n', $this->read(2))[1]; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.