forked from xrdebug/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
205 lines (196 loc) · 7.12 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
<?php
/**
* This file is part of Swow-Cloud/Job
* @license https://github.com/serendipity-swow/serendipity-job/blob/master/LICENSE
*/
declare(strict_types=1);
foreach (['/', '/../../../','/../'] as $path) {
$autoload = __DIR__ . $path . 'vendor/autoload.php';
if (stream_resolve_include_path($autoload)) {
require $autoload;
break;
}
}
use Chevere\ThrowableHandler\Documents\ThrowableHandlerConsoleDocument;
use Chevere\ThrowableHandler\ThrowableHandler;
use Chevere\Writer\StreamWriter;
use Chevere\Writer\Writers;
use Chevere\Writer\WritersInstance;
use Clue\React\Sse\BufferedChannel;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
use React\Http\Middleware\RequestBodyBufferMiddleware;
use React\Http\Middleware\RequestBodyParserMiddleware;
use React\Http\Middleware\StreamingRequestMiddleware;
use React\Stream\ThroughStream;
use samejack\PHP\ArgvParser;
use function Chevere\ThrowableHandler\throwableHandler;
use function Chevere\Writer\streamFor;
use function Chevere\Writer\writers;
new WritersInstance(
(new Writers())
->with(
new StreamWriter(
streamFor('php://output', 'w')
)
)
->withError(
new StreamWriter(
streamFor('php://stderr', 'w')
)
)
);
set_error_handler(ThrowableHandler::ERRORS_AS_EXCEPTIONS);
register_shutdown_function(ThrowableHandler::FATAL_ERROR_HANDLER);
set_exception_handler(function (Throwable $e) {
$handler = throwableHandler($e);
$docInternal = new ThrowableHandlerConsoleDocument($handler);
writers()->error()
->write($docInternal->__toString() . "\n");
exit(255);
});
$loop = Loop::get();
$channel = new BufferedChannel();
$handler = function (ServerRequestInterface $request) use ($channel, $loop) {
switch ($request->getUri()->getPath()) {
case '/':
return new Response(
'200',
['Content-Type' => 'text/html'],
file_get_contents(__DIR__ . '/asset/index.html')
);
case '/app.js':
return new Response(
'200',
['Content-Type' => 'text/javascript'],
file_get_contents(__DIR__ . '/asset/app.js')
);
case '/html2canvas.min.js':
return new Response(
'200',
['Content-Type' => 'text/javascript'],
file_get_contents(__DIR__ . '/asset/html2canvas.min.js')
);
case '/icon.svg':
return new Response(
'200',
['Content-Type' => 'image/svg+xml'],
file_get_contents(__DIR__ . '/asset/icon.svg')
);
case '/icon.png':
return new Response(
'200',
['Content-Type' => 'image/png'],
file_get_contents(__DIR__ . '/asset/icon.png')
);
case '/style.css':
return new Response(
'200',
['Content-Type' => 'text/css'],
file_get_contents(__DIR__ . '/asset/style.css')
);
case '/fonts/firacode/firacode-regular.woff':
return new Response(
'200',
['Content-Type' => 'font/woff'],
file_get_contents(__DIR__ . '/asset/fonts/firacode/firacode-regular.woff')
);
case '/message':
if ($request->getMethod() !== 'POST') {
return new Response(405);
}
$address = $request->getServerParams()['REMOTE_ADDR'];
$body = $request->getParsedBody() ?? [];
$message = $body['body'] ?? '';
$message = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $message);
$emote = $body['emote'] ?? '';
$topic = $body['topic'] ?? '';
if (($message . $emote . $topic) !== '') {
$file = $body['file_path'] ?? '';
$line = $body['file_line'] ?? '';
$fileDisplay = $file;
$fileDisplayShort = basename($file);
if ($line !== '') {
$fileDisplay .= ':' . $line;
$fileDisplayShort .= ':' . $line;
}
$channel->writeMessage(
json_encode([
'message' => $message,
'file_path' => $file,
'file_line' => $line,
'file_display' => $fileDisplay,
'file_display_short' => $fileDisplayShort,
'emote' => $emote,
'action' => $body['action'] ?? '',
'topic' => $topic,
])
);
echo "* [{$address}] {$fileDisplay}\n";
}
return new Response(
'201',
['Content-Type' => 'text/json']
);
case '/dump':
$stream = new ThroughStream();
$id = $request->getHeaderLine('Last-Event-ID');
$loop->futureTick(function () use ($channel, $stream, $id) {
$channel->connect($stream, $id);
});
$serverParams = $request->getServerParams();
$message = ['message' => 'New dump session started [' . $serverParams['REMOTE_ADDR'] . ']'];
$channel->writeMessage(json_encode($message));
$stream->on('close', function () use ($stream, $channel, $request, $serverParams) {
$channel->disconnect($stream);
$message = ['message' => 'Dump session end [' . $serverParams['REMOTE_ADDR'] . ']'];
$channel->writeMessage(json_encode($message));
});
return new Response(
200,
['Content-Type' => 'text/event-stream'],
$stream
);
default:
return new Response(404);
}
};
$http = new HttpServer(
$loop,
new StreamingRequestMiddleware(),
new LimitConcurrentRequestsMiddleware(100),
new RequestBodyBufferMiddleware(8 * 1024 * 1024),
new RequestBodyParserMiddleware(100 * 1024, 1),
$handler
);
$options = (new ArgvParser())->parseConfigs();
if (array_key_exists('h', $options) || array_key_exists('help', $options)) {
echo implode("\n", ['-p Port (default 27420)', '-c Cert .pem file', '']);
exit(0);
}
$host = '0.0.0.0';
$port = $options['p'] ?? '0';
$cert = $options['c'] ?? null;
$scheme = isset($cert) ? 'tls' : 'tcp';
$uri = "{$scheme}://{$host}:{$port}";
$context = $scheme === 'tcp'
? []
: [
'tls' => [
'local_cert' => $cert,
],
];
$socket = new \React\Socket\SocketServer(
uri: $uri,
context: $context,
loop: $loop
);
$http->listen($socket);
$socket->on('error', 'printf');
$scheme = parse_url($socket->getAddress(), PHP_URL_SCHEME);
$httpAddress = strtr($socket->getAddress(), ['tls:' => 'https:', 'tcp:' => 'http:']);
echo "Chevere XR debugger listening on ({$scheme}) {$httpAddress}" . PHP_EOL;
$loop->run();