-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.php
352 lines (308 loc) · 7.49 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* @Package Kat
* @Author 陆之岇(kraity)
* @Studio 南博网络科技工作室
* @GitHub https://github.com/krait-team/kat-php
* @Version 1.0.0
* @Description Remote Procedure Call
*/
/**
* Kat_Server
*
* @package Kat
*/
class Kat_Server
{
/**
* Kat 版本号
*
* @var string
*/
const VERSION = '1.0.0';
/**
* HOOK函数
*
* @var array
*/
protected $hookers = [];
/**
* 回调函数
*
* @var array
*/
protected $methods = [];
/**
* 需验函数名
*
* @var array
*/
protected $metcips = [];
/**
* Kat_Server
*
* @access public
* @param $attach
*/
public function __construct($attach = null)
{
if (isset($attach)) {
$this->register($attach);
}
}
/**
* @param $attach
*/
public function register($attach)
{
// register
foreach (get_class_methods($attach) as $method) {
// cut
$mtd = substr(
$method, 7
);
// check
if ($mtd === false) {
continue;
}
switch (substr($method, 0, 7)) {
case 'metcip_':
$this->metcips[] = $mtd;
case 'method_':
$this->methods[$mtd] = $method;
break;
case 'hooker_':
$this->hookers[$mtd] = $method;
}
}
unset($mtd);
}
/**
* 接收
*
* @param $attach
* @param $request
* @return void
* @throws Exception
*/
public function receive($attach, $request)
{
// start
$this->hooker(
$attach, 'start'
);
try {
// read
$kat = kat_decode(
$request
);
// check data
if (!is_array($kat)) {
$kat = null;
throw new Exception(
'The data is empty or illegal'
);
}
// check method
$this->checkMethod(
$kat['method']
);
// ready
$this->hooker($attach,
'ready', [&$kat]
);
// security
if ($this->hasMetcip($kat['method'])) {
// accept
$this->hooker($attach,
'accept', [&$kat]
);
}
// check request
if (!is_string($kat['request'])) {
throw new Exception(
'The data is not a string'
);
}
// decode
$kat['request'] = kat_decode(
$kat['request']
);
// check request
if (!is_array($kat['request'])) {
throw new Exception(
'The data is illegal'
);
}
// hook before
$this->hooker($attach,
'before', [&$kat]
);
// callback
$kat['response'] = $this->callMethod([$attach,
$this->methods[$kat['method']]
], $kat['request']);
// isolate
unset($kat['request']);
// hook after
$this->hooker($attach,
'after', [&$kat]
);
// response
$this->onResponse($attach,
$kat, 'kat'
);
// isolate
unset($kat['response']);
} catch (Exception $e) {
// check data
if (empty($kat)) {
// thread
$kat = [
'accept' => 'kat-ins',
'method' => 'kat-test'
];
}
// error
$kat['response'] = $e;
// error
$this->onError(
$attach, $kat
);
}
// isolate
unset($kat, $request);
}
/**
* HOOK内部方法
*
* @param $attach
* @param string $method 方法名
* @param mixed $args 参数
* @return mixed
* @throws Exception
*/
protected function hooker($attach, $method, $args = [])
{
if ($this->hasHooker($method)) {
return $this->callMethod([
$attach, $this->hookers[$method]
], $args);
}
return null;
}
/**
* @param $attach
* @param $data
* @param string $name
* @throws Exception
*/
protected function onResponse($attach, $data, $name = 'err')
{
//callback
$callback = [
'accept' => $data['accept'],
'method' => $data['method'],
'response' => kat_encode(
$data['response']
)
];
// isolate
unset($data['response']);
// challenge
if ($this->hasMetcip($data['method'])) {
$this->hooker($attach, 'challenge', [
$data, &$callback
]);
}
// encode
$kat = kat_encode(
$callback, $name
);
// isolate
unset($callback);
// hook after
$this->hooker($attach, 'end', [&$kat, [
'Kat' => Kat_Server::VERSION,
'Content-Type' => 'text/kat'
]]);
// isolate
unset($data, $kat);
}
/**
* @param $attach
* @param $data
* @throws Exception
*/
protected function onError($attach, $data)
{
$this->onResponse($attach, $data,
$data['response']->getCode() > 99 ? 'kat' : 'err'
);
}
/**
* @param array $method
* @param mixed $args
* @return mixed
* @throws Exception
*/
public function callMethod($method, $args = [])
{
// check
if (is_callable($method)) {
// hook
return call_user_func_array(
$method, $args
);
}
throw new Exception(
'Server error, method does not exist'
);
}
/**
* 是否存在HOOK
*
* @param string $method 方法名
* @return bool
*/
protected function hasHooker($method)
{
return isset($this->hookers[$method]);
}
/**
* 是否存在方法
*
* @param string $method 方法名
* @return bool
*/
protected function hasMethod($method)
{
return isset($this->methods[$method]);
}
/**
* 是否签名方法
*
* @param string $method 方法名
* @return bool
*/
protected function hasMetcip($method)
{
return in_array($method, $this->metcips);
}
/**
* 是否合法方法
*
* @param string $method 方法名
* @throws Exception
*/
protected function checkMethod($method)
{
if (preg_match("/^[0-9a-zA-Z_]{4,}$/", $method)
&& $this->hasMethod($method)) {
return;
}
throw new Exception(
'Server error, method does not exist'
);
}
}