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
|
/*
* MIT License
*
* Copyright (c) 2022- Minteck
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
global.fs = require('fs')
global.os = require('os')
global.mime = require('node-mime');
global.dirconf = null;
module.exports.start = function () {
String.prototype.replacei = function (search, replace) {
var regex = new RegExp(search, "ig");
return this.replace(regex, replace);
}
global.http = require('http');
http.createServer(function (req, res) {
global.res = res;
if (req.url.length > 1 && req.url.endsWith("/")) {
req.url = req.url.substr(0, req.url.length - 1);
}
req.url_orig = req.url;
req.url_arg = req.url.split("?")[1];
req.url = req.url.split("?")[0];
if (req.headers["x-forwarded-for"]) {
global.address = req.headers["x-forwarded-for"];
} else {
global.address = req.connection.remoteAddress
}
log.verbose("request: " + address + "; " + req.url)
// TODO: Detect user language
let dlang = "en";
frhtml = false;
ejs = false;
if (req.url.includes(".")) {
} else {
if (fs.existsSync(wwwdata + req.url + "/index.html")) {
req.url = req.url + "/index.html"
} else {
req.url = req.url + "/index.ejs"
ejs = true;
}
}
if ((fs.existsSync(wwwdata + req.url) || fs.existsSync(wwwdata + req.url_orig + "/$command.json")) && !req.url.startsWith("/assets/")) {
if (req.url_orig.replace(/\/+/g, '/').trim() !== "/") {
res.writeHead(301, {"Location": "/" + dlang + req.url_orig.replace(/\/+/g, '/')});
} else {
res.writeHead(301, {"Location": "/" + dlang});
}
res.end();
} else if (!req.url.startsWith("/assets/")) {
slang = req.url.split("/")[1];
if (!slang.includes(".") && !slang.includes("/") && fs.existsSync(serverRoot + "/htdocs/data/lang/" + slang + ".json")) {
global.lang = JSON.parse(fs.readFileSync(serverRoot + "/htdocs/data/lang/" + slang + ".json").toString());
parts = req.url.split("/");
parts.shift();
parts.shift();
req.url = "/" + parts.join("/");
} else {
parts = req.url_orig.split("/");
parts.shift();
parts.shift();
newUrl = "/" + parts.join("/");
if (newUrl.replace(/\/+/g, '/').trim() !== "/") {
res.writeHead(301, {"Location": "/en" + newUrl.replace(/\/+/g, '/')});
} else {
res.writeHead(301, {"Location": "/en"});
}
res.end();
}
}
if (req.url.includes("..")) {
if (config.errors_show_trace) {
trace = "<div><code>Satellite Server - System Backtrace<br><br>Server Backtrace:<br>401F0000 PERMISSION_DENIED<br>00000001 SERVER_RUNTIME<br>0000001A HTTP_WEBSERVER<br><br>Kernel Backtrace:<br></code></div>"
} else {
trace = "";
}
log.verbose("return 401")
res.writeHead(401, {'Content-Type': 'text/html'});
res.write("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Satellite Fatal Error</title></head><body><h1>Internal Server Error</h1>Minteck Satellite Server returned an error while trying to request your file.<br><br>0x401: Forbidden<br>" + req.url + "<br><br><b>Minteck Satellite Server " + mpws.version + ", kernel " + process.version + "</b>" + trace + "</body></html>");
res.end();
return;
}
global.dirconf = null;
if (fs.existsSync(wwwdata + req.url.substring(0, req.url.indexOf('/')) + "/" + config.access)) {
log.verbose('reading ' + config.access + ' file...')
try {
if (require.cache[require.resolve(wwwdata + req.url.substring(0, req.url.indexOf('/')) + "/" + config.access)]) {
delete require.cache[require.resolve(wwwdata + req.url.substring(0, req.url.indexOf('/')) + "/" + config.access)]
}
global.dirconf = require(wwwdata + req.url.substring(0, req.url.indexOf('/')) + "/" + config.access)
} catch (err) {
global.dirconf = null;
if (config.errors_show_trace) {
trace = "<div><code>Satellite Server - System Backtrace<br><br>Server Backtrace:<br>50A00000 INVALID_DIR_CONF<br>00000001 SERVER_RUNTIME<br>0000001A HTTP_WEBSERVER<br><br>Kernel Backtrace:<br>" + err.toString().replace('\n', '<br>') + "</code></div>"
} else {
trace = "";
}
log.verbose("return 500")
res.writeHead(500, {'Content-Type': 'text/html'});
res.write("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Satellite Fatal Error</title></head><body><h1>Internal Server Error</h1>Minteck Satellite Server returned an error while trying to request your file.<br><br>0x50A: Invalid Directory Configuration<br>" + req.url + "<br><br><b>Minteck Satellite Server " + mpws.version + ", kernel " + process.version + "</b>" + trace + "</body></html>");
res.end();
return;
}
if (dirconf == null) {
} else {
if (typeof (dirconf.access) != "undefined") {
if (typeof (dirconf.access) == "boolean") {
if (dirconf.access == false) {
if (config.errors_show_trace) {
trace = "<div><code>Satellite Server - System Backtrace<br><br>Server Backtrace:<br>401F0000 PERMISSION_DENIED<br>00000001 SERVER_RUNTIME<br>0000001A HTTP_WEBSERVER<br><br>Kernel Backtrace:<br></code></div>"
} else {
trace = "";
}
log.verbose("return 401")
res.writeHead(500, {'Content-Type': 'text/html'});
res.write("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Satellite Fatal Error</title></head><body><h1>Internal Server Error</h1>Minteck Satellite Server returned an error while trying to request your file.<br><br>0x401: Forbidden<br>" + req.url + "<br><br><b>Minteck Satellite Server " + mpws.version + ", kernel " + process.version + "</b>" + trace + "</body></html>");
res.end();
return;
}
}
}
}
}
if (fs.existsSync(wwwdata + req.url)) {
if (ejs) {
log.verbose("return " + mime.lookUpType((wwwdata + req.url).split(".").pop()))
log.verbose("parser: ejs");
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
res.writeHead(200, {
'Content-Type': "text/html",
'Set-Cookie': '__mpws_request=' + year + month + day + hour + min + sec
});
try {
if (typeof req.url_arg === "string") {
get = require('querystring').parse(require('url').parse(req.url_orig).query);
} else {
get = {};
}
require('ejs').renderFile(wwwdata + req.url, {wwwdata, private, req, res, slang, lang, get, fs: require('fs'), child_process: require('child_process')}, (err, str) => {
if (err) throw err;
res.write(str);
res.end();
})
} catch (e) {
res.writeHead(200, {
'Content-Type': "text/plain",
'Set-Cookie': '__mpws_request=' + year + month + day + hour + min + sec
});
res.write(e.stack);
res.end();
}
return;
} else {
var type = mime.lookUpType((wwwdata + req.url).split(".").pop());
log.verbose("return " + type)
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
res.writeHead(200, {
'Content-Type': type,
'Set-Cookie': '__mpws_request=' + year + month + day + hour + min + sec
});
if (req.url.endsWith("/$command.json")) {
prejson = JSON.parse(fs.readFileSync(wwwdata + req.url));
prejson.token = "<-- TOKEN TRUNCATED - Access file on real server to get the authentication token -->"
res.write(JSON.stringify(prejson));
} else {
res.write(fs.readFileSync(wwwdata + req.url));
}
res.end();
return;
}
} else {
if (config.errors_show_trace) {
trace = "<div><code>Satellite Server - System Backtrace<br><br>Server Backtrace:<br>404F0000 FILE_NOT_FOUND<br>00000001 SERVER_RUNTIME<br>0000001A HTTP_WEBSERVER<br><br>Kernel Backtrace:<br></code></div>"
} else {
trace = "";
}
log.verbose("return 404")
res.writeHead(404, {'Content-Type': 'text/html'});
res.write("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Satellite Server Fatal Error</title></head><body><h1>Internal Server Error</h1>Minteck Satellite Server returned an error while trying to request your file.<br><br>0x404: Not Found<br>" + req.url + "<br><br><b>Minteck Satellite Server " + mpws.version + ", kernel " + process.version + "</b>" + trace + "</body></html>");
res.end();
}
}).listen(config.port);
log.info('Started Satellite at port ' + config.port)
}
|