|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dazzle\Http\Socket\Component\Firewall; |
|
4
|
|
|
|
|
5
|
|
|
use Dazzle\Http\Null\NullServer; |
|
6
|
|
|
use Dazzle\Http\NetworkComponentAwareInterface; |
|
7
|
|
|
use Dazzle\Http\NetworkConnectionInterface; |
|
8
|
|
|
use Dazzle\Http\NetworkMessageInterface; |
|
9
|
|
|
use Dazzle\Http\NetworkComponentInterface; |
|
10
|
|
|
|
|
11
|
|
|
class SocketFirewall implements SocketFirewallInterface, NetworkComponentAwareInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var NetworkComponentInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $component; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $blacklist; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param NetworkComponentAwareInterface|null $aware |
|
25
|
|
|
* @param NetworkComponentInterface|null $component |
|
26
|
|
|
*/ |
|
27
|
18 |
|
public function __construct(NetworkComponentAwareInterface $aware = null, NetworkComponentInterface $component = null) |
|
28
|
|
|
{ |
|
29
|
18 |
|
$this->component = $component; |
|
30
|
18 |
|
$this->blacklist = []; |
|
31
|
|
|
|
|
32
|
18 |
|
if ($aware !== null) |
|
33
|
|
|
{ |
|
34
|
18 |
|
$aware->setComponent($this); |
|
35
|
|
|
} |
|
36
|
18 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
*/ |
|
41
|
17 |
|
public function __destruct() |
|
42
|
|
|
{ |
|
43
|
17 |
|
unset($this->component); |
|
44
|
17 |
|
unset($this->blacklist); |
|
45
|
17 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @override |
|
49
|
|
|
* @inheritDoc |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function setComponent(NetworkComponentInterface $component = null) |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->component = $component === null ? new NullServer() : $component; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @override |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function getComponent() |
|
61
|
|
|
{ |
|
62
|
3 |
|
return $this->component; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @override |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
5 |
|
public function blockAddress($address) |
|
70
|
|
|
{ |
|
71
|
5 |
|
$this->blacklist[$address] = true; |
|
72
|
|
|
|
|
73
|
5 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @override |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function unblockAddress($address) |
|
81
|
|
|
{ |
|
82
|
2 |
|
if (isset($this->blacklist[$address])) |
|
83
|
|
|
{ |
|
84
|
2 |
|
unset($this->blacklist[$address]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @override |
|
92
|
|
|
* @inheritDoc |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function isAddressBlocked($address) |
|
95
|
|
|
{ |
|
96
|
2 |
|
return isset($this->blacklist[$address]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @override |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function getBlockedAddresses() |
|
104
|
|
|
{ |
|
105
|
1 |
|
return array_keys($this->blacklist); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @override |
|
110
|
|
|
* @inheritDoc |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function handleConnect(NetworkConnectionInterface $conn) |
|
113
|
|
|
{ |
|
114
|
2 |
|
if ($this->isAddressBlocked($conn->getHost())) |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $conn->close(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
return $this->component->handleConnect($conn); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @override |
|
124
|
|
|
* @inheritDoc |
|
125
|
|
|
*/ |
|
126
|
2 |
|
public function handleDisconnect(NetworkConnectionInterface $conn) |
|
127
|
|
|
{ |
|
128
|
2 |
|
if (!$this->isAddressBlocked($conn->getHost())) |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->component->handleDisconnect($conn); |
|
131
|
|
|
} |
|
132
|
2 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @override |
|
136
|
|
|
* @inheritDoc |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function handleMessage(NetworkConnectionInterface $conn, NetworkMessageInterface $message) |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $this->component->handleMessage($conn, $message); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @override |
|
145
|
|
|
* @inheritDoc |
|
146
|
|
|
*/ |
|
147
|
2 |
|
public function handleError(NetworkConnectionInterface $conn, $ex) |
|
148
|
|
|
{ |
|
149
|
2 |
|
if (!$this->isAddressBlocked($conn->getHost())) |
|
150
|
|
|
{ |
|
151
|
1 |
|
$this->component->handleError($conn, $ex); |
|
152
|
|
|
} |
|
153
|
2 |
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|