Completed
Pull Request — master (#76)
by
unknown
25:04
created

SocksProxy::connect()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 43
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 4.909
c 0
b 0
f 0
cc 9
eloc 33
nc 9
nop 2
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));
0 ignored issues
show
Unused Code introduced by
$ip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
55
                break;
56
            case 4:
57
                $ip = inet_ntop($this->read(16));
0 ignored issues
show
Unused Code introduced by
$ip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
58
                break;
59
            case 3:
60
                $ip = $this->read(ord($this->read(1)));
0 ignored issues
show
Unused Code introduced by
$ip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
61
                break;
62
        }
63
        $port = unpack('n', $this->read(2))[1];
0 ignored issues
show
Unused Code introduced by
$port is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
64
    }
65
}
66