|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace MikoPBX\Common\Models; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Modules\Config\ConfigClass; |
|
23
|
|
|
use Phalcon\Mvc\Model\Relation; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class FirewallRules |
|
27
|
|
|
* |
|
28
|
|
|
* @method static mixed findByCategory(string $category) |
|
29
|
|
|
* |
|
30
|
|
|
* @package MikoPBX\Common\Models |
|
31
|
|
|
*/ |
|
32
|
|
|
class FirewallRules extends ModelsBase |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @Primary |
|
36
|
|
|
* @Identity |
|
37
|
|
|
* @Column(type="integer", nullable=false) |
|
38
|
|
|
*/ |
|
39
|
|
|
public $id; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @Column(type="string", nullable=true){'udp','tcp','icmp'} |
|
43
|
|
|
*/ |
|
44
|
|
|
public ?string $protocol = ''; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Column(type="integer", nullable=true) |
|
48
|
|
|
*/ |
|
49
|
|
|
public ?string $portfrom = ''; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @Column(type="integer", nullable=true) |
|
53
|
|
|
*/ |
|
54
|
|
|
public ?string $portto = ''; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @Column(type="integer", nullable=true) |
|
58
|
|
|
*/ |
|
59
|
|
|
public ?string $networkfilterid = ''; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @Column(type="string", nullable=true){'allow','block'} |
|
63
|
|
|
*/ |
|
64
|
|
|
public ?string $action = 'allow'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @Column(type="string", nullable=true){'SIP','WEB','SSH','AMI','CTI','ICMP'} |
|
68
|
|
|
*/ |
|
69
|
|
|
public ?string $category = ''; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @Column(type="string", nullable=true) |
|
73
|
|
|
*/ |
|
74
|
|
|
public ?string $portFromKey = ''; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @Column(type="string", nullable=true) |
|
78
|
|
|
*/ |
|
79
|
|
|
public ?string $portToKey = ''; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @Column(type="string", nullable=true) |
|
83
|
|
|
*/ |
|
84
|
|
|
public ?string $description = ''; |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
public static function getDefaultRules(): array |
|
88
|
|
|
{ |
|
89
|
|
|
$defaultRTPFrom = PbxSettings::getValueByKey('RTPPortFrom'); |
|
90
|
|
|
$defaultRTPTo = PbxSettings::getValueByKey('RTPPortTo'); |
|
91
|
|
|
$defaultSIP = PbxSettings::getValueByKey('SIPPort'); |
|
92
|
|
|
$defaultAMI = PbxSettings::getValueByKey('AMIPort'); |
|
93
|
|
|
$defaultAJAM = PbxSettings::getValueByKey('AJAMPort'); |
|
94
|
|
|
$defaultAJAMTLS = PbxSettings::getValueByKey('AJAMPortTLS'); |
|
95
|
|
|
$defaultWeb = PbxSettings::getValueByKey('WEBPort'); |
|
96
|
|
|
$defaultWebHttps = PbxSettings::getValueByKey('WEBHTTPSPort'); |
|
97
|
|
|
$defaultSSH = PbxSettings::getValueByKey('SSHPort'); |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$template = [ |
|
101
|
|
|
'SIP' => [ |
|
102
|
|
|
'rules' => [ |
|
103
|
|
|
[ |
|
104
|
|
|
'portfrom' => $defaultSIP, |
|
105
|
|
|
'portto' => $defaultSIP, |
|
106
|
|
|
'protocol' => 'udp', |
|
107
|
|
|
'portFromKey' => 'SIPPort', |
|
108
|
|
|
'portToKey' => 'SIPPort', |
|
109
|
|
|
], |
|
110
|
|
|
[ |
|
111
|
|
|
'portfrom' => $defaultSIP, |
|
112
|
|
|
'portto' => $defaultSIP, |
|
113
|
|
|
'protocol' => 'tcp', |
|
114
|
|
|
'portFromKey' => 'SIPPort', |
|
115
|
|
|
'portToKey' => 'SIPPort', |
|
116
|
|
|
], |
|
117
|
|
|
[ |
|
118
|
|
|
'portfrom' => $defaultRTPFrom, |
|
119
|
|
|
'portto' => $defaultRTPTo, |
|
120
|
|
|
'protocol' => 'udp', |
|
121
|
|
|
'portFromKey' => 'RTPPortFrom', |
|
122
|
|
|
'portToKey' => 'RTPPortTo', |
|
123
|
|
|
], |
|
124
|
|
|
], |
|
125
|
|
|
'action' => 'allow', |
|
126
|
|
|
'shortName' => 'SIP & RTP', |
|
127
|
|
|
], |
|
128
|
|
|
'WEB' => [ |
|
129
|
|
|
'rules' => [ |
|
130
|
|
|
[ |
|
131
|
|
|
'portfrom' => $defaultWeb, |
|
132
|
|
|
'portto' => $defaultWeb, |
|
133
|
|
|
'protocol' => 'tcp', |
|
134
|
|
|
'portFromKey' => 'WEBPort', |
|
135
|
|
|
'portToKey' => 'WEBPort', |
|
136
|
|
|
], |
|
137
|
|
|
[ |
|
138
|
|
|
'portfrom' => $defaultWebHttps, |
|
139
|
|
|
'portto' => $defaultWebHttps, |
|
140
|
|
|
'protocol' => 'tcp', |
|
141
|
|
|
'portFromKey' => 'WEBHTTPSPort', |
|
142
|
|
|
'portToKey' => 'WEBHTTPSPort', |
|
143
|
|
|
], |
|
144
|
|
|
], |
|
145
|
|
|
'action' => 'allow', |
|
146
|
|
|
'shortName' => 'WEB', |
|
147
|
|
|
|
|
148
|
|
|
], |
|
149
|
|
|
'SSH' => [ |
|
150
|
|
|
'rules' => [ |
|
151
|
|
|
[ |
|
152
|
|
|
'portfrom' => $defaultSSH, |
|
153
|
|
|
'portto' => $defaultSSH, |
|
154
|
|
|
'protocol' => 'tcp', |
|
155
|
|
|
'portFromKey' => 'SSHPort', |
|
156
|
|
|
'portToKey' => 'SSHPort', |
|
157
|
|
|
], |
|
158
|
|
|
], |
|
159
|
|
|
'action' => 'allow', |
|
160
|
|
|
'shortName' => 'SSH', |
|
161
|
|
|
], |
|
162
|
|
|
'AMI' => [ |
|
163
|
|
|
'rules' => [ |
|
164
|
|
|
[ |
|
165
|
|
|
'portfrom' => $defaultAMI, |
|
166
|
|
|
'portto' => $defaultAMI, |
|
167
|
|
|
'protocol' => 'tcp', |
|
168
|
|
|
'portFromKey' => 'AMIPort', |
|
169
|
|
|
'portToKey' => 'AMIPort', |
|
170
|
|
|
], |
|
171
|
|
|
], |
|
172
|
|
|
'action' => 'allow', |
|
173
|
|
|
'shortName' => 'AMI', |
|
174
|
|
|
], |
|
175
|
|
|
'AJAM' => [ |
|
176
|
|
|
'rules' => [ |
|
177
|
|
|
[ |
|
178
|
|
|
'portfrom' => $defaultAJAM, |
|
179
|
|
|
'portto' => $defaultAJAM, |
|
180
|
|
|
'protocol' => 'tcp', |
|
181
|
|
|
'portFromKey' => 'AJAMPort', |
|
182
|
|
|
'portToKey' => 'AJAMPort', |
|
183
|
|
|
], |
|
184
|
|
|
[ |
|
185
|
|
|
'portfrom' => $defaultAJAMTLS, |
|
186
|
|
|
'portto' => $defaultAJAMTLS, |
|
187
|
|
|
'protocol' => 'tcp', |
|
188
|
|
|
'portFromKey' => 'AJAMPortTLS', |
|
189
|
|
|
'portToKey' => 'AJAMPortTLS', |
|
190
|
|
|
], |
|
191
|
|
|
], |
|
192
|
|
|
'action' => 'allow', |
|
193
|
|
|
'shortName' => 'AJAM', |
|
194
|
|
|
], |
|
195
|
|
|
'ICMP' => [ |
|
196
|
|
|
'rules' => [ |
|
197
|
|
|
['portfrom' => 0, 'portto' => 0, 'protocol' => 'icmp'], |
|
198
|
|
|
], |
|
199
|
|
|
'action' => 'allow', |
|
200
|
|
|
'shortName' => 'ICMP', |
|
201
|
|
|
], |
|
202
|
|
|
]; |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
//Add modules firewall rules |
|
206
|
|
|
$configClassObj = new ConfigClass(); |
|
207
|
|
|
$additionalRules = $configClassObj->hookModulesMethodWithArrayResult(ConfigClass::GET_DEFAULT_FIREWALL_RULES); |
|
208
|
|
|
foreach ($additionalRules as $additionalRuleFromModule) { |
|
209
|
|
|
if ($additionalRuleFromModule !== []) { |
|
210
|
|
|
$additionalRuleFromModule = array_change_key_case($additionalRuleFromModule, CASE_UPPER); |
|
211
|
|
|
foreach ($additionalRuleFromModule as $key => $rule) { |
|
212
|
|
|
$template[$key] = $rule; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $template; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Updates firewall rules after change PBXSettings records |
|
222
|
|
|
* |
|
223
|
|
|
* @param \MikoPBX\Common\Models\PbxSettings $entity |
|
224
|
|
|
*/ |
|
225
|
|
|
public static function updatePorts(PbxSettings $entity): void |
|
226
|
|
|
{ |
|
227
|
|
|
$conditions = [ |
|
228
|
|
|
'conditions'=>'portFromKey = :key: OR portToKey = :key:', |
|
229
|
|
|
'bind'=>[ |
|
230
|
|
|
'key'=>$entity->key |
|
231
|
|
|
] |
|
232
|
|
|
]; |
|
233
|
|
|
$rules = self::find($conditions); |
|
234
|
|
|
foreach ($rules as $rule){ |
|
235
|
|
|
if ($rule->portFromKey === $entity->key){ |
|
236
|
|
|
$rule->portfrom = $entity->value; |
|
237
|
|
|
} |
|
238
|
|
|
if ($rule->portToKey === $entity->key){ |
|
239
|
|
|
$rule->portto = $entity->value; |
|
240
|
|
|
} |
|
241
|
|
|
$rule->update(); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function initialize(): void |
|
246
|
|
|
{ |
|
247
|
|
|
$this->setSource('m_FirewallRules'); |
|
248
|
|
|
parent::initialize(); |
|
249
|
|
|
$this->belongsTo( |
|
250
|
|
|
'networkfilterid', |
|
251
|
|
|
NetworkFilters::class, |
|
252
|
|
|
'id', |
|
253
|
|
|
[ |
|
254
|
|
|
'alias' => 'NetworkFilters', |
|
255
|
|
|
'foreignKey' => [ |
|
256
|
|
|
'allowNulls' => false, |
|
257
|
|
|
'action' => Relation::NO_ACTION, |
|
258
|
|
|
], |
|
259
|
|
|
] |
|
260
|
|
|
); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|