Completed
Pull Request — master (#77)
by
unknown
48:36 queued 23:28
created

SocksProxy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
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));
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...
52
                break;
53
            case 4:
54
                $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...
55
                break;
56
            case 3:
57
                $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...
58
                break;
59
        }
60
        $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...
61
    }
62
}
63