1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* php version 7.1.0 |
11
|
|
|
* |
12
|
|
|
* @category Web-security |
13
|
|
|
* @package Shieldon |
14
|
|
|
* @author Terry Lin <[email protected]> |
15
|
|
|
* @copyright 2019 terrylinooo |
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
18
|
|
|
* @see https://shieldon.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall\Driver; |
24
|
|
|
|
25
|
|
|
use Shieldon\Firewall\Driver\AbstractDriver; |
26
|
|
|
use RuntimeException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Driver Provider. |
30
|
|
|
*/ |
31
|
|
|
class DriverProvider extends AbstractDriver |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Data table for regular filter logs. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $tableFilterLogs = 'shieldon_filter_logs'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Data table name for whitelist. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $tableRuleList = 'shieldon_rule_list'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Data table for recording online session count. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $tableSessions = 'shieldon_sessions'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The prefix of the database tables, or the name of file directory. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $channel = ''; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if is initialized or not. |
63
|
|
|
* |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
protected $isInitialized = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The table types. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $tableTypes = [ |
74
|
|
|
'rule', |
75
|
|
|
'filter', |
76
|
|
|
'session', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set data channel. |
81
|
|
|
* |
82
|
|
|
* @param string $channel The prefix of the data tables. |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
63 |
|
public function setChannel(string $channel): void |
87
|
|
|
{ |
88
|
63 |
|
$this->channel = $channel; |
89
|
|
|
|
90
|
63 |
|
if (!empty($this->channel)) { |
91
|
63 |
|
$this->tableFilterLogs = $this->channel . '_shieldon_filter_logs'; |
92
|
63 |
|
$this->tableRuleList = $this->channel . '_shieldon_rule_list'; |
93
|
63 |
|
$this->tableSessions = $this->channel . '_shieldon_sessions'; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get channel name. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
46 |
|
public function getChannel(): string |
103
|
|
|
{ |
104
|
46 |
|
return $this->channel; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return parsed full data structure. |
109
|
|
|
* |
110
|
|
|
* @param array $data The data needed to be parsed. |
111
|
|
|
* @param string $type The type of data table. accepts: filter | session | rule |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
55 |
|
public function parseData(array $data, string $type = 'filter'): array |
116
|
|
|
{ |
117
|
55 |
|
$parsedData = []; |
118
|
|
|
|
119
|
|
|
switch ($type) { |
120
|
|
|
// Rule table data structure. |
121
|
55 |
|
case 'rule': |
122
|
1 |
|
break; |
123
|
|
|
|
124
|
|
|
// Session table data structure. |
125
|
55 |
|
case 'session': |
126
|
1 |
|
break; |
127
|
|
|
|
128
|
|
|
// Log table data structure. |
129
|
55 |
|
case 'filter': |
130
|
|
|
// no break |
131
|
|
|
default: |
132
|
|
|
$fields = [ |
133
|
55 |
|
|
134
|
|
|
// Basic IP data. |
135
|
|
|
'ip' => 'string', |
136
|
55 |
|
'session' => 'string', |
137
|
55 |
|
'hostname' => 'string', |
138
|
55 |
|
|
139
|
|
|
// timestamp while visting first time. |
140
|
|
|
'first_time_s' => 'int', |
141
|
55 |
|
'first_time_m' => 'int', |
142
|
55 |
|
'first_time_h' => 'int', |
143
|
55 |
|
'first_time_d' => 'int', |
144
|
55 |
|
'first_time_flag' => 'int', |
145
|
55 |
|
'last_time' => 'int', |
146
|
55 |
|
|
147
|
|
|
// Signals for flagged bad behavior. |
148
|
|
|
'flag_js_cookie' => 'int', |
149
|
55 |
|
'flag_multi_session' => 'int', |
150
|
55 |
|
'flag_empty_referer' => 'int', |
151
|
55 |
|
|
152
|
|
|
// Pageview count. |
153
|
|
|
'pageviews_cookie' => 'int', |
154
|
55 |
|
'pageviews_s' => 'int', |
155
|
55 |
|
'pageviews_m' => 'int', |
156
|
55 |
|
'pageviews_h' => 'int', |
157
|
55 |
|
'pageviews_d' => 'int', |
158
|
55 |
|
]; |
159
|
55 |
|
|
160
|
|
|
foreach ($fields as $k => $v) { |
161
|
55 |
|
$tmp = $data[$k] ?? ''; |
162
|
55 |
|
|
163
|
|
|
if ('string' === $v) { |
164
|
55 |
|
$parsedData[$k] = (string) $tmp; |
165
|
55 |
|
} |
166
|
|
|
|
167
|
|
|
if ('int' === $v) { |
168
|
55 |
|
$parsedData[$k] = (int) $tmp; |
169
|
55 |
|
} |
170
|
|
|
} |
171
|
|
|
break; |
172
|
55 |
|
// end switch |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $parsedData; |
176
|
55 |
|
} |
177
|
|
|
|
178
|
|
|
// @codeCoverageIgnoreStart |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Implement fetch. |
182
|
|
|
* |
183
|
|
|
* @param string $ip The data id of the entry to fetch. |
184
|
|
|
* @param string $type The type of data table. accepts: filter | session | rule |
185
|
|
|
* |
186
|
|
|
* @return array The data or an empty array. |
187
|
|
|
*/ |
188
|
|
|
protected function doFetch(string $ip, string $type = 'filter'): array |
189
|
|
|
{ |
190
|
|
|
return []; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Implement fetch all. |
195
|
|
|
* |
196
|
|
|
* @param string $type The type of data table. accepts: filter | session | rule |
197
|
|
|
* |
198
|
|
|
* @return array The data or an empty array. |
199
|
|
|
*/ |
200
|
|
|
protected function doFetchAll(string $type = 'filter'): array |
201
|
|
|
{ |
202
|
|
|
return []; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Implement has. |
207
|
|
|
* |
208
|
|
|
* @param string $ip The data id of the entry to check for. |
209
|
|
|
* @param string $type The type of data table. accepts: filter | session | rule |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
protected function checkExist(string $ip, string $type = 'filter'): bool |
214
|
|
|
{ |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Implement save. |
220
|
|
|
* |
221
|
|
|
* @param string $ip The IP address as the data id. |
222
|
|
|
* @param array $data The data. |
223
|
|
|
* @param string $type The type of the data table. |
224
|
|
|
* @param int $expire The data will be deleted after expiring. |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
protected function doSave(string $ip, array $data, string $type = 'filter', $expire = 0): bool |
229
|
|
|
{ |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Implement delete. |
235
|
|
|
* |
236
|
|
|
* @param string $ip The IP address. |
237
|
|
|
* @param string $type The type of data table. accepts: filter | session | rule |
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
protected function doDelete(string $ip, string $type = 'filter'): bool |
242
|
|
|
{ |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Rebuild data tables. |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
protected function doRebuild(): bool |
252
|
|
|
{ |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Initialize data tables. |
258
|
|
|
* |
259
|
|
|
* @param bool $dbCheck This is for creating data tables automatically |
260
|
|
|
* Turn it off, if you don't want to check data tables every pageview. |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
protected function doInitialize(bool $dbCheck = true): void |
265
|
|
|
{ |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Check data type. |
270
|
|
|
* |
271
|
|
|
* @param string $type The type of the data tables. |
272
|
|
|
* |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
protected function assertInvalidDataTable(string $type): void |
276
|
|
|
{ |
277
|
|
|
if (!in_array($type, $this->tableTypes)) { |
278
|
|
|
throw new RuntimeException( |
279
|
|
|
'Invalid data type of the data tables.' |
280
|
|
|
); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
// @codeCoverageIgnoreEnd |
284
|
|
|
} |
285
|
|
|
|