/
index.php
128 lines (110 loc) · 3.58 KB
/
index.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
<?php
declare(strict_types = 1);
require 'vendor/autoload.php';
use AmiDev\WrpDistributor\Actions\ActionInterface;
use AmiDev\WrpDistributor\Actions\DeadOrAlive;
use AmiDev\WrpDistributor\Actions\ExtendSession;
use AmiDev\WrpDistributor\Actions\StartSession;
use AmiDev\WrpDistributor\Actions\StopSession;
use AmiDev\WrpDistributor\Logger;
use AmiDev\WrpDistributor\ServiceContainer;
use AmiDev\WrpDistributor\Session;
$environmentVarsToLoad = [
'MAX_CONTAINERS_RUNNING',
'CONTAINER_HOSTS',
'CONTAINER_HOSTS_KEYS',
'CONTAINER_HOSTS_TLS_CERTS',
'CONTAINER_DISTRIBUTION_METHOD',
'SESSION_DATABASE_DSN',
'SESSION_DATABASE_USER',
'SESSION_DATABASE_PASS',
'START_PORT',
'AUTH_TOKEN',
];
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required($environmentVarsToLoad);
if (!isset($_SERVER['HTTP_BEARER']) || $_ENV['AUTH_TOKEN'] !== $_SERVER['HTTP_BEARER']) {
http_response_code(401);
return;
}
$pdo = new \PDO($_ENV['SESSION_DATABASE_DSN'], $_ENV['SESSION_DATABASE_USER'], $_ENV['SESSION_DATABASE_PASS']);
$logger = new Logger();
try {
$serviceContainer = new ServiceContainer(
$logger,
$pdo,
);
} catch (\Throwable $throwable) {
$logger->error('Startup error: invalid container host configuration!');
http_response_code(500);
echo sprintf(
'
<h1>%s</h1>
<p>%s</p>
<p>
<pre>%s</pre>
</p>',
'Startup error: invalid container host configuration!',
$throwable->getMessage(),
$throwable->getTraceAsString(),
);
exit(1);
}
$currentClientIp = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? trim($_SERVER['HTTP_X_FORWARDED_FOR']) : trim((string) $_SERVER['REMOTE_ADDR']);
$currentClientUserAgent = trim((string) $_SERVER['HTTP_USER_AGENT']);
if (empty($currentClientUserAgent) || empty($currentClientIp)) {
$serviceContainer->logger->warning(
'Invalid request, required HTTP stuff is missing!',
[
'server' => $_SERVER,
'request' => $_REQUEST,
],
);
http_response_code(405);
exit(1);
}
// Load session if it exists, else create a new one.
try {
$session = Session::loadFromDatabase($serviceContainer, $currentClientIp, $currentClientUserAgent);
} catch (\Throwable $ex) {
$session = Session::create($serviceContainer, $currentClientIp, $currentClientUserAgent);
$session->upsert();
}
/** @var ActionInterface[] $actionMap */
$actionMap = [
'DELETE' => new StopSession($serviceContainer),
'PUT' => new StartSession($serviceContainer),
'HEAD' => new ExtendSession($serviceContainer),
'GET' => new DeadOrAlive($serviceContainer),
];
$requestMethod = $_SERVER['REQUEST_METHOD'];
if ($requestMethod === null || $requestMethod === '' || !array_key_exists($requestMethod, $actionMap)) {
$serviceContainer->logger->warning(
'Invalid request, unsupported method used!',
[
'method' => $_SERVER['REQUEST_METHOD'],
'request' => $_REQUEST,
],
);
http_response_code(405);
exit(1);
}
try {
$actionMap[$requestMethod]($session);
exit(0);
} catch (\LogicException $logicException) {
$serviceContainer->logger->debug($logicException->getMessage(), $logicException->getTrace());
http_response_code(400);
echo sprintf(
'
<h1>%s</h1>
<p>%s</p>
<p>
<pre>%s</pre>
</p>',
'Your request either makes no sense, or is invalid',
$logicException->getMessage(),
$logicException->getTraceAsString(),
);
}