-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.php
241 lines (199 loc) · 10.9 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
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
<?php
/* Copyright 2023 Plancksoft
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
// Import all custom classes here
require_once('Classes/Security.php');
require_once('Classes/SCrypt.php');
include_once("Classes/Token.php");
include_once("Classes/Logger.php");
require_once("Controllers/AuthenticationController.php");
require_once('vendor/autoload.php');
if(!empty($_SERVER['REMOTE_ADDR']))
{
$IP = $_SERVER['REMOTE_ADDR'];
if ($IP !== "localhost" || $IP !== "127.0.0.1") // Development IP Address for error logging capabilities. (re-work this part in a master configuration file.)
{
error_reporting(0);
} else {
error_reporting(E_ALL);
}
}
$RequestedAction = "";
$InputData = json_decode(json_encode(["InputData" => array()], true), true);
$_REQUEST = json_decode(file_get_contents('php://input'), true);
$PUBLIC_RSA_KEY = "";
$Signature = $_REQUEST["Signature"];
$InputData = $_REQUEST["InputData"];
if (isset($_REQUEST["PUBLIC_RSA_KEY"]))
{
$PUBLIC_RSA_KEY = base64_decode($_REQUEST["PUBLIC_RSA_KEY"]);
if ($PUBLIC_RSA_KEY == "")
{
echo json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a missing public RSA key."], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a missing public RSA key");
die();
}
} else {
echo json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a missing public RSA key."], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a missing public RSA key");
die();
}
if ((strpos($PUBLIC_RSA_KEY, "BEGIN RSA PUBLIC KEY") !== false))
{
$tempPUBLIC_RSA_KEY = str_replace('-----BEGIN RSA PUBLIC KEY-----', '', $PUBLIC_RSA_KEY);
$tempPUBLIC_RSA_KEY = trim(str_replace('-----END RSA PUBLIC KEY-----', '', $tempPUBLIC_RSA_KEY));
$PUBLIC_RSA_KEY = 'MIICIjANBgkqhkiG9w0BAQEFAAOCAg8A' . str_replace("\n", '', $tempPUBLIC_RSA_KEY);
$PUBLIC_RSA_KEY = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($PUBLIC_RSA_KEY, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
}
if (!Security::VerifyMessage($InputData, $Signature, $PUBLIC_RSA_KEY))
{
echo json_encode(["response" => Settings::$LogPrefix . " has refused the connection due to an invalid request signature."], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to an invalid request signature");
die();
} else {
$InputData = json_decode(Security::Decrypt($InputData), true);
}
if (isset($InputData))
{
$InputData = Security::Secure($InputData, $PUBLIC_RSA_KEY);
if (!$InputData)
{
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection as it was unable to secure input data."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " as it was unable to secure input data");
die();
} else {
Security::BlockInvalidTraffic($InputData, $PUBLIC_RSA_KEY);
}
}
if (isset($InputData["Action"]))
{
if ($InputData["Action"] !== null)
{
$RequestedAction = htmlspecialchars(strip_tags($InputData["Action"]));
} else {
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a missing action parameter."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a missing action parameter");
die();
}
} else {
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a missing action parameter."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a missing action parameter");
die();
}
$Actions = file_get_contents("Actions.json");
if ($Actions === false) {
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a misconfiguration in the Actions configuration file."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a misconfiguration in the Actions configuration file");
die();
}
$Actions = json_decode($Actions, true);
if ($Actions === null) {
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a misconfiguration in the Actions configuration file."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a misconfiguration in the Actions configuration file");
die();
}
$ActionController = "";
$ActionMethod = "";
foreach($Actions as $Action) {
$ActionInformation = $Action["0"];
if($RequestedAction === $ActionInformation["Action"]) {
$ActionController = $ActionInformation["Controller"];
$ActionMethod = $ActionInformation["Method"];
$ActionToken = $ActionInformation["TokenRequired"];
$ActionMaintenance = $ActionInformation["Maintenance"];
if ($ActionToken === 1)
{
if (!isset($InputData["Token"]) || $InputData["Token"] === "")
{
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a missing mandatory token."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a missing mandatory token");
die();
} else {
if (!$InputData["Token"] = Token::Validate($InputData["Token"], $PUBLIC_RSA_KEY)){
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a failure validating the token");
die();
}
}
} else if ($ActionToken === 2){
if (isset($InputData["Token"]))
{
if (!Token::Decode(Token::Validate($InputData["Token"], $PUBLIC_RSA_KEY))){
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a failure validating the token");
die();
}
}
}
if ($ActionMaintenance === 1)
{
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => -1, "message" => Settings::$LogPrefix . " has refused the connection due to requested API {$RequestedAction} being under maintenance."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to requested API {$RequestedAction} being under maintenance");
die();
}
}
}
if ($ActionController === "")
{
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to an invalid supplied Action."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to an invalid supplied Action");
die();
} else {
try{
$ActionControllerPath = "Controllers\\" . $ActionController . ".php";
require_once($ActionControllerPath);
if ($ActionMethod !== "")
{
$Action = new $ActionController($InputData, $ActionMethod, new MongoDB\Client, $IP, $PUBLIC_RSA_KEY);
$Response = $Action->$ActionMethod($PUBLIC_RSA_KEY);
if (isset($Response))
{
echo $Response;
die();
} else {
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " as it was unable to successfully perform requested action");
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " was unable to successfully perform requested action."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
die();
}
}
else {
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " due to a misconfiguration in the Actions configuration file");
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " has refused the connection due to a misconfiguration in the Actions configuration file."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
die();
}
} catch (Exception $Exception)
{
Logger::Log(Settings::$LogPrefix, "has refused a connection from IP: " . $IP . " as it was unable to successfully perform requested action");
$EncryptedResponse = Security::Encrypt(base64_encode(json_encode(["status" => 0, "message" => Settings::$LogPrefix . " was unable to successfully perform requested action."], true)), $PUBLIC_RSA_KEY);
$ResponseSignature = Security::SignMessage($EncryptedResponse);
echo json_encode(["response" => $EncryptedResponse, "signature" => $ResponseSignature], true);
die();
}
}
?>