-
Notifications
You must be signed in to change notification settings - Fork 29
/
server.php
501 lines (420 loc) · 17.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
// Cloud Storage Server.
// (C) 2017 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/css_functions.php";
// Load configuration.
$config = CSS_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();
}
// Initialize server.
if (!isset($config["quota"])) CSS_DisplayError("Configuration is incomplete or missing. Run 'install.php' first.");
$config["quota"] = CSS_ConvertUserStrToBytes($config["quota"]);
require_once $rootpath . "/support/db.php";
require_once $rootpath . "/support/db_sqlite.php";
$db = new CSDB_sqlite();
try
{
$db->Connect("sqlite:" . $rootpath . "/data/main.db");
}
catch (Exception $e)
{
CSS_DisplayError("Unable to connect to SQLite database. " . $e->getMessage());
}
if (!$db->TableExists("users")) CSS_DisplayError("Database table is missing. Run 'install.php' first.");
require_once $rootpath . "/support/event_manager.php";
$em = new EventManager();
$userhelper = new CSS_UserHelper();
$userhelper->Init($config, $db, $em);
// Load all server extensions.
$serverexts = CSS_LoadServerExtensions();
// Let each server extension register event handler callbacks and initialize.
foreach ($serverexts as $serverext)
{
$serverext->RegisterHandlers($em);
$serverext->InitServer();
}
require_once $rootpath . "/support/web_server.php";
require_once $rootpath . "/support/remotedapi_web_server.php";
require_once $rootpath . "/support/websocket_server.php";
$wsserver = new WebSocketServer();
$webservers = array();
$webserver = (RemotedAPIWebServer::IsRemoted($config["host"]) ? new RemotedAPIWebServer() : new WebServer());
// Enable writing files to the system.
$cachedir = WebServer::MakeTempDir("cloudstorage");
$webserver->SetCacheDir($cachedir);
// Enable longer active client times.
$webserver->SetDefaultClientTimeout(300);
$webserver->SetMaxRequests(200);
echo "Starting server...\n";
$result = $webserver->Start($config["host"], $config["port"], ($config["host"] !== "[::1]" && $config["host"] !== "127.0.0.1" && isset($config["sslopts"]) ? $config["sslopts"] : false));
if (!$result["success"])
{
var_dump($result);
exit();
}
$webservers[] = $webserver;
if ($config["addlocalhost"])
{
$webserver = new WebServer();
// Enable writing files to the system.
$cachedir = sys_get_temp_dir();
$cachedir = str_replace("\\", "/", $cachedir);
if (substr($cachedir, -1) !== "/") $cachedir .= "/";
$cachedir .= "cloudstorage_local_" . microtime(true) . "/";
@mkdir($cachedir, 0770, true);
$webserver->SetCacheDir($cachedir);
// Enable longer active client times.
$webserver->SetDefaultClientTimeout(300);
$webserver->SetMaxRequests(200);
echo "Starting localhost server...\n";
$result = $webserver->Start(($config["host"][0] === "[" ? "[::1]" : "127.0.0.1"), $config["port"] + 1);
if (!$result["success"])
{
var_dump($result);
exit();
}
$webservers[] = $webserver;
}
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;
foreach ($webservers as $servernum => $webserver) $webserver->UpdateStreamsAndTimeout($servernum . "_", $timeout, $readfps, $writefps);
foreach ($serverexts as $ext) $ext->UpdateStreamsAndTimeout("", $timeout, $readfps, $writefps);
$wsserver->UpdateStreamsAndTimeout("", $timeout, $readfps, $writefps);
$result = @stream_select($readfps, $writefps, $exceptfps, $timeout);
if ($result === false)
{
if ($webservers[0] instanceof RemotedAPIWebServer)
{
sleep(5);
}
else
{
break;
}
}
// Web server.
foreach ($webservers as $servernum => $webserver)
{
$result = $webserver->Wait(0);
// Handle active clients.
foreach ($result["clients"] as $id => $client)
{
if ($client->appdata === false)
{
echo "Server " . $servernum . ", Client ID " . $id . " connected.\n";
$client->appdata = array("validapikey" => false, "userrow" => false, "guestrow" => false, "currext" => false, "pathparts" => false);
}
// Check for a valid API key.
if (!$client->appdata["validapikey"] && (isset($client->headers["X-Apikey"]) || isset($client->requestvars["apikey"])))
{
$apikey = explode("-", (isset($client->headers["X-Apikey"]) ? $client->headers["X-Apikey"] : $client->requestvars["apikey"]));
if (count($apikey) == 2)
{
$result2 = $userhelper->GetUserByID($apikey[1], $apikey[0]);
if ($result2["success"])
{
echo "Valid user API key used.\n";
$client->appdata["validapikey"] = true;
$client->appdata["userrow"] = $result2["info"];
}
}
else if (count($apikey) == 3)
{
$result2 = $userhelper->GetGuestByID($apikey[2], $apikey[1], $apikey[0]);
if ($result2["success"])
{
$result3 = $userhelper->GetUserByID($apikey[1]);
if ($result3["success"])
{
echo "Valid guest API key used.\n";
$client->appdata["validapikey"] = true;
$client->appdata["userrow"] = $result3["info"];
$client->appdata["guestrow"] = $result2["info"];
}
}
}
}
if ($client->appdata["validapikey"])
{
if ($client->appdata["currext"] === false)
{
$url = HTTP::ExtractURL($client->url);
$path = explode("/", $url["path"]);
$client->appdata["pathparts"] = $path;
if (count($path) > 1 && isset($serverexts[$path[1]]) && isset($client->appdata["userrow"]->serverexts[$path[1]]) && ($client->appdata["guestrow"] === false || isset($client->appdata["guestrow"]->serverexts[$path[1]]))) $client->appdata["currext"] = $path[1];
}
// Guaranteed to have at least the request line and headers if the request is incomplete.
if (!$client->requestcomplete && $client->appdata["currext"] !== false)
{
// Let server extensions raise the default limit of ~1MB of transfer per request (not per connection) if they want to.
// Extensions should only increase the limit for file uploads and should avoid decreasing the limit.
$serverexts[$client->appdata["currext"]]->HTTPPreProcessAPI($client->request["method"], $client->appdata["pathparts"], $client, $client->appdata["userrow"], $client->appdata["guestrow"]);
}
}
// Wait until the request is complete before fully processing inputs.
if ($client->requestcomplete)
{
if (!$client->appdata["validapikey"])
{
echo "Missing API key.\n";
$client->SetResponseCode(403);
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode(array("success" => false, "error" => "Invalid or missing 'apikey'.", "errorcode" => "invalid_missing_apikey")));
$client->FinalizeResponse();
}
else if ($client->appdata["currext"] === false)
{
echo "Unknown or invalid extension.\n";
$client->SetResponseCode(403);
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode(array("success" => false, "error" => "Unknown or invalid server extension requested.", "errorcode" => "bad_server_extension")));
$client->FinalizeResponse();
}
else if ($client->mode === "init_response")
{
// Handle WebSocket upgrade requests.
$id2 = $wsserver->ProcessWebServerClientUpgrade($webserver, $client);
if ($id2 !== false)
{
echo "Server " . $servernum . ", Client ID " . $id . " upgraded to WebSocket. WebSocket client ID is " . $id2 . ".\n";
}
else
{
echo "Sending API response for: " . $client->request["method"] . " " . implode("/", $client->appdata["pathparts"]) . "\n";
// Check transfer limits.
$userrow = $client->appdata["userrow"];
$received = $client->httpstate["result"]["rawrecvsize"];
$options = array();
if ($userrow->transferstart < time() - 86400)
{
$options["transferstart"] = time();
$userrow->transferbytes = 0;
}
$userrow->transferbytes += $received;
$options["transferbytes"] = $userrow->transferbytes;
$result2 = $userhelper->UpdateUser($userrow->id, $options);
if ($result2["success"])
{
// Check transfer limits.
if ($userrow->transferlimit > -1 && $userrow->transferlimit < $userrow->transferbytes) $result2 = array("success" => false, "error" => "Daily transfer limit exceeded. Try again tomorrow.", "errorcode" => "transfer_limit_exceeded");
else
{
// Attempt to normalize input.
if ($client->contenthandled) $data = $client->requestvars;
else if (!is_object($client->readdata)) $data = @json_decode($client->readdata, true);
else
{
$client->readdata->Open();
$data = @json_decode($client->readdata->Read(1000000), true);
}
// Process the request.
if (!is_array($data)) $result2 = array("success" => false, "error" => "Data sent is not an array/object or was not able to be decoded.", "errorcode" => "invalid_data");
else
{
$result2 = $serverexts[$client->appdata["currext"]]->ProcessAPI($client->request["method"], $client->appdata["pathparts"], $client, $userrow, $client->appdata["guestrow"], $data);
if ($result2 === false)
{
$webserver->RemoveClient($id);
echo "Server " . $servernum . ", Client ID " . $id . " removed.\n";
}
else if (!is_array($result2))
{
$client->appdata["data"] = $data;
}
}
}
}
if ($result2 !== false)
{
// Prevent proxies from doing bad things.
$client->SetResponseNoCache();
if (is_array($result2))
{
if (!$result2["success"]) $client->SetResponseCode(400);
// Send the response.
$client->SetResponseContentType("application/json");
$client->AddResponseContent(json_encode($result2));
$client->FinalizeResponse();
}
if ($client->responsefinalized) $client->appdata["currext"] = false;
}
}
}
else
{
// Continue where the API left off.
$result2 = $serverexts[$client->appdata["currext"]]->ProcessAPI($client->request["method"], $client->appdata["pathparts"], $client, $client->appdata["userrow"], $client->appdata["guestrow"], $client->appdata["data"]);
if ($result2 === false)
{
$webserver->RemoveClient($id);
echo "Server " . $servernum . ", Client ID " . $id . " removed.\n";
}
else
{
if ($client->responsefinalized) $client->appdata["currext"] = false;
}
}
}
}
// Do something with removed clients.
foreach ($result["removed"] as $id => $result2)
{
if ($result2["client"]->appdata !== false)
{
echo "Server " . $servernum . ", 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)
{
// Read the input as a normal API request.
$ws = $client->websocket;
$result2 = $ws->Read();
while ($result2["success"] && $result2["data"] !== false)
{
echo "Sending API response via WebSocket.\n";
// Attempt to normalize the input.
$data = @json_decode($result2["data"]["payload"], true);
// Process the request.
if (!is_array($data)) $result3 = array("success" => false, "error" => "Data sent is not an array/object or was not able to be decoded.", "errorcode" => "invalid_data");
else if (!isset($data["api_method"]) || !is_string($data["api_method"])) $result3 = array("success" => false, "error" => "The 'api_method' is missing or invalid.", "errorcode" => "missing_invalid_api_method");
else if (!isset($data["api_path"]) || !is_string($data["api_path"])) $result3 = array("success" => false, "error" => "The 'api_path' is missing or invalid.", "errorcode" => "missing_invalid_api_path");
else if (!isset($data["api_sequence"]) || !is_int($data["api_sequence"])) $result3 = array("success" => false, "error" => "The 'api_sequence' is missing or invalid.", "errorcode" => "missing_invalid_api_sequence");
else
{
$data["api_method"] = strtoupper($data["api_method"]);
$path = explode("/", str_replace("\\", "/", $data["api_path"]));
$client->appdata["pathparts"] = $path;
if (count($path) > 1 && isset($serverexts[$path[1]]) && isset($client->appdata["userrow"]->serverexts[$path[1]]) && ($client->appdata["guestrow"] === false || isset($client->appdata["guestrow"]->serverexts[$path[1]]))) $client->appdata["currext"] = $path[1];
echo "Sending API response for: " . $data["api_method"] . " " . implode("/", $client->appdata["pathparts"]) . "\n";
// Check transfer limits.
$userrow = $client->appdata["userrow"];
$received = $client->websocket->GetRawRecvSize();
$options = array();
if ($userrow->transferstart < time() - 86400)
{
$options["transferstart"] = time();
$userrow->transferbytes = 0;
}
$userrow->transferbytes += $received;
$options["transferbytes"] = $userrow->transferbytes;
$result3 = $userhelper->UpdateUser($userrow->id, $options);
if ($result3["success"])
{
// Check transfer limits.
if ($userrow->transferlimit > -1 && $userrow->transferlimit < $userrow->transferbytes) $result3 = array("success" => false, "error" => "Daily transfer limit exceeded. Try again tomorrow.", "errorcode" => "transfer_limit_exceeded");
else
{
// Process the request.
$result3 = $serverexts[$client->appdata["currext"]]->ProcessAPI($data["api_method"], $client->appdata["pathparts"], $client, $userrow, $client->appdata["guestrow"], $data);
if ($result3 === false || !is_array($result3)) $wsserver->RemoveClient($id);
else $result3["api_sequence"] = $data["api_sequence"];
}
}
}
// Send the response.
$result2 = $ws->Write(json_encode($result3), $result2["data"]["opcode"]);
$result2 = $ws->Read();
}
}
foreach ($result["removed"] as $id => $result2)
{
if ($result2["client"]->appdata !== false)
{
echo "WebSocket client ID " . $id . " disconnected.\n";
// echo "WebSocket 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);
?>