1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SamIT\Proxy; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use React\EventLoop\LoopInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Connection class that implements a PortConnectionInterface. |
11
|
|
|
* @package SamIT\Proxy |
12
|
|
|
*/ |
13
|
|
|
class PortConnection extends \React\Socket\Connection implements PortConnectionInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var boolean Whether this is an incoming connection. |
17
|
|
|
*/ |
18
|
|
|
protected $incoming; |
19
|
|
|
|
20
|
3 |
|
public function __construct($stream, LoopInterface $loop, $incoming = false) |
21
|
|
|
{ |
22
|
3 |
|
parent::__construct($stream, $loop); |
23
|
3 |
|
$this->incoming = $incoming; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return int The remote port for this connection. |
29
|
|
|
*/ |
30
|
|
|
public function getRemotePort() |
31
|
|
|
{ |
32
|
|
|
return $this->parsePort(stream_socket_get_name($this->stream, true)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return int The target port for this connection. |
37
|
|
|
*/ |
38
|
1 |
|
public function getTargetPort() |
39
|
1 |
|
{ |
40
|
|
|
return $this->parsePort(stream_socket_get_name($this->stream, !$this->incoming)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string The target address for this connection. |
45
|
|
|
*/ |
46
|
|
|
public function getTargetAddress() |
47
|
|
|
{ |
48
|
|
|
return $this->parseAddress(stream_socket_get_name($this->stream, !$this->incoming)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $address |
53
|
|
|
* @return string |
54
|
|
|
* @todo Make parent function protected. |
55
|
|
|
*/ |
56
|
|
|
protected function parseAddress($address) |
57
|
|
|
{ |
58
|
|
|
return trim(substr($address, 0, strrpos($address, ':')), '[]'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function parsePort($address) |
62
|
|
|
{ |
63
|
|
|
$parts = explode(':', $address); |
64
|
|
|
return end($parts); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int The source port for this connection. |
69
|
|
|
*/ |
70
|
1 |
|
public function getSourcePort() |
71
|
|
|
{ |
72
|
1 |
|
return $this->incoming ? $this->getRemotePort() : $this->parsePort(stream_socket_get_name($this->stream, false)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string The source address for this connection. This is the same as the remote address if this connection is incoming. |
77
|
|
|
*/ |
78
|
1 |
|
public function getSourceAddress() |
79
|
|
|
{ |
80
|
1 |
|
return $this->incoming ? $this->getRemoteAddress() : $this->parseAddress(stream_socket_get_name($this->stream, false)); |
81
|
|
|
} |
82
|
|
|
} |