-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
376 lines (304 loc) · 11.2 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
// Remoted API server main service.
// (C) 2018 CubicleSoft. All Rights Reserved.
if (!isset($_SERVER["argc"]) || !$_SERVER["argc"])
{
echo "This file is intended to be run from the command-line.";
exit();
}
// Temporary root.
$rootpath = str_replace("\\", "/", dirname(__FILE__));
require_once $rootpath . "/support/cli.php";
require_once $rootpath . "/support/ras_functions.php";
// Load configuration.
$config = RAS_LoadConfig();
if ($argc > 1)
{
// Service Manager PHP SDK.
require_once $rootpath . "/support/servicemanager.php";
$sm = new ServiceManager($rootpath . "/servicemanager");
echo "Service manager: " . $sm->GetServiceManagerRealpath() . "\n\n";
$servicename = preg_replace('/[^a-z0-9]/', "-", $config["servicename"]);
if ($argv[1] == "install")
{
// Install the service.
$args = array();
$options = array(
"nixuser" => $config["serviceuser"],
"nixgroup" => $config["serviceuser"]
);
$result = $sm->Install($servicename, __FILE__, $args, $options, true);
if (!$result["success"]) CLI::DisplayError("Unable to install the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "start")
{
// Start the service.
$result = $sm->Start($servicename, true);
if (!$result["success"]) CLI::DisplayError("Unable to start the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "stop")
{
// Stop the service.
$result = $sm->Stop($servicename, true);
if (!$result["success"]) CLI::DisplayError("Unable to stop the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "uninstall")
{
// Uninstall the service.
$result = $sm->Uninstall($servicename, true);
if (!$result["success"]) CLI::DisplayError("Unable to uninstall the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "dumpconfig")
{
$result = $sm->GetConfig($servicename);
if (!$result["success"]) CLI::DisplayError("Unable to retrieve the configuration for the '" . $servicename . "' service.", $result);
echo "Service configuration: " . $result["filename"] . "\n\n";
echo "Current service configuration:\n\n";
foreach ($result["options"] as $key => $val) echo " " . $key . " = " . $val . "\n";
}
else
{
echo "Command not recognized. Run the service manager directly for anything other than 'install', 'start', 'stop', 'uninstall', and 'dumpconfig'.\n";
}
exit();
}
// Start the main server.
require_once $rootpath . "/support/web_server.php";
require_once $rootpath . "/support/websocket_server.php";
require_once $rootpath . "/support/webroute_server.php";
require_once $rootpath . "/support/random.php";
require_once $rootpath . "/support/str_basics.php";
$webserver = new WebServer();
$wsserver = new WebSocketServer();
$wrserver = new WebRouteServer();
$pathmap = array();
echo "Starting server...\n";
$result = $webserver->Start($config["host"], $config["port"], false);
if (!$result["success"])
{
var_dump($result);
exit();
}
echo "Ready.\n";
$stopfilename = __FILE__ . ".notify.stop";
$reloadfilename = __FILE__ . ".notify.reload";
$lastservicecheck = time();
$running = true;
do
{
// Implement the stream_select() call directly since multiple server instances are involved.
$timeout = 3;
$readfps = array();
$writefps = array();
$exceptfps = NULL;
$webserver->UpdateStreamsAndTimeout("", $timeout, $readfps, $writefps);
$wsserver->UpdateStreamsAndTimeout("", $timeout, $readfps, $writefps);
$wrserver->UpdateStreamsAndTimeout("", $timeout, $readfps, $writefps);
$result = @stream_select($readfps, $writefps, $exceptfps, $timeout);
if ($result === false) break;
// Web server.
$result = $webserver->Wait(0);
// Handle active clients.
foreach ($result["clients"] as $id => $client)
{
if ($client->appdata === false)
{
echo "Webserver client ID " . $id . " connected.\n";
$client->appdata = array("mode" => false, "path" => false);
}
// Check for a valid API key.
if ($client->appdata["mode"] === false && isset($client->headers["X-Remoted-Apikey"]))
{
$apikey = $client->headers["X-Remoted-Apikey"];
if (Str::CTstrcmp($config["client_apikey"], $apikey) == 0) $client->appdata["mode"] = "client";
else if (Str::CTstrcmp($config["server_apikey"], $apikey) == 0) $client->appdata["mode"] = "server";
if ($client->appdata["mode"] !== false) echo "Valid " . $client->appdata["mode"] . " API key used.\n";
}
if ($client->appdata["mode"] !== false && $client->appdata["path"] === false)
{
$url = HTTP::ExtractURL($client->url);
$client->appdata["path"] = $url["path"];
}
// Wait until the request is complete before fully processing inputs.
if ($client->requestcomplete)
{
// Prevent proxies from doing bad things.
$client->SetResponseNoCache();
if ($client->appdata["mode"] === false)
{
echo "Missing API key.\n";
$client->SetResponseCode(403);
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode(array("success" => false, "error" => "Invalid or missing 'X-Remoted-APIKey' header.", "errorcode" => "invalid_missing_apikey")));
$client->FinalizeResponse();
}
else if ($client->appdata["path"] === false)
{
echo "Unknown or invalid path.\n";
$client->SetResponseCode(403);
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode(array("success" => false, "error" => "Unknown or invalid path. Bad request.", "errorcode" => "invalid_request_path")));
$client->FinalizeResponse();
}
else if ($client->appdata["mode"] === "server")
{
// Handle WebRoute upgrade requests.
$id2 = $wrserver->ProcessWebServerClientUpgrade($webserver, $client, true);
if ($id2 !== false)
{
$client2 = $wrserver->GetClient($id2);
// Clean up the waiting queue.
if ($client2->linkid !== false)
{
$id3 = $pathmap[$client2->appdata["path"]];
$client3 = $wsserver->GetClient($id3);
unset($client3->appdata["waiting"][$client2->linkid]);
}
echo "Webserver client ID " . $id . " upgraded to WebRoute. WebRoute client ID is " . $id2 . ".\n";
}
else
{
// Handle WebSocket upgrade requests.
if (!isset($pathmap[$client->appdata["path"]])) $id2 = $wsserver->ProcessWebServerClientUpgrade($webserver, $client);
if ($id2 !== false)
{
$client2 = $wsserver->GetClient($id2);
$client2->appdata["waiting"] = array();
$pathmap[$client2->appdata["path"]] = $id2;
echo "Webserver client ID " . $id . " upgraded to WebSocket. WebSocket client ID is " . $id2 . ". Listening on '" . $client2->appdata["path"] . "'.\n";
}
else
{
// Reject all other requests.
$result2 = array("success" => false, "error" => "Invalid request. Expected WebSocket or WebRoute upgrade.", "errorcode" => "invalid_request");
$client->SetResponseCode(400);
// Send the response.
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode($result2));
$client->FinalizeResponse();
$client->appdata["path"] = false;
}
}
}
else
{
// Handle WebRoute upgrade requests.
$ipaddr = $client->ipaddr;
$id2 = (isset($pathmap[$client->appdata["path"]]) ? $wrserver->ProcessWebServerClientUpgrade($webserver, $client) : false);
if ($id2 !== false)
{
$client2 = $wrserver->GetClient($id2);
echo "Webserver client ID " . $id . " upgraded to WebRoute. WebRoute client ID is " . $id2 . ".\n";
// Notify the appropriate WebSocket server.
$id3 = $pathmap[$client2->appdata["path"]];
$client3 = $wsserver->GetClient($id3);
$data = array(
"ipaddr" => $ipaddr,
"id" => $client2->webrouteid,
"timeout" => (isset($client2->headers["Webroute-Timeout"]) && is_numeric($client2->headers["Webroute-Timeout"]) && (int)$client2->headers["Webroute-Timeout"] > $client2->timeout ? (int)$client2->headers["Webroute-Timeout"] : $client2->timeout)
);
$client3->websocket->Write(json_encode($data, JSON_UNESCAPED_SLASHES), WebSocket::FRAMETYPE_TEXT);
$client3->appdata["waiting"][$id2] = true;
}
else
{
if (!isset($pathmap[$client->appdata["path"]]))
{
$result2 = array("success" => false, "error" => "Requested destination does not exist at this time.", "errorcode" => "missing_destination");
$client->SetResponseCode(502);
}
else
{
$result2 = array("success" => false, "error" => "Invalid request. Expected WebRoute upgrade.", "errorcode" => "invalid_request");
$client->SetResponseCode(400);
}
// Send the response.
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode($result2));
$client->FinalizeResponse();
$client->appdata["path"] = false;
}
}
}
}
// Handle removed clients.
foreach ($result["removed"] as $id => $result2)
{
if ($result2["client"]->appdata !== false)
{
echo "Web server client ID " . $id . " disconnected.\n";
// echo "Client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
}
}
// WebSocket server.
$result = $wsserver->Wait(0);
// Handle active clients.
foreach ($result["clients"] as $id => $client)
{
// Ignore all input packets.
$ws = $client->websocket;
$result2 = $ws->Read();
while ($result2["success"] && $result2["data"] !== false)
{
$result2 = $ws->Read();
}
}
foreach ($result["removed"] as $id => $result2)
{
$client = $result2["client"];
if ($client->appdata !== false)
{
echo "WebSocket client ID " . $id . " disconnected.\n";
// Remove the path.
unset($pathmap[$client->appdata["path"]]);
// Disconnect waiting WebRoute clients so they don't timeout.
foreach ($client->appdata["waiting"] as $id2 => $val)
{
$wrserver->RemoveClient($id2);
}
// echo "WebSocket client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
}
}
// WebRoute server.
$result = $wrserver->Wait(0);
foreach ($result["removed"] as $id => $result2)
{
$client = $result2["client"];
if ($client->appdata !== false)
{
echo "WebRoute client ID " . $id . " disconnected.\n";
// Remove from the associated WebSocket waiting queue.
if (isset($pathmap[$client->appdata["path"]]))
{
$id2 = $pathmap[$client->appdata["path"]];
$client2 = $wsserver->GetClient($id2);
unset($client2->appdata["waiting"][$id]);
}
// echo "WebRoute client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
}
}
// Check the status of the two service file options for correct Service Manager integration.
if ($lastservicecheck <= time() - 3)
{
if (file_exists($stopfilename))
{
// Initialize termination.
echo "Stop requested.\n";
$running = false;
}
else if (file_exists($reloadfilename))
{
// Reload configuration and then remove reload file.
echo "Reload config requested. Exiting.\n";
$running = false;
}
$lastservicecheck = time();
}
} while ($running);
?>