-
Notifications
You must be signed in to change notification settings - Fork 3
/
webserver.js
289 lines (269 loc) · 8.72 KB
/
webserver.js
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
/**
* @description MeshCommander web server
* @author Ylian Saint-Hilaire
* @copyright Intel Corporation 2018-2020
* @license Apache-2.0
* @version v0.0.3
*/
module.exports.CreateWebServer = function (args) {
var obj = {};
obj.fs = require("fs");
obj.net = require("net");
obj.tls = require("tls");
obj.path = require("path");
obj.args = args;
obj.express = require("express");
obj.app = obj.express();
obj.expressWs = require("express-ws")(obj.app);
obj.interceptor = require("./interceptor");
obj.common = require("./common.js");
obj.constants = require("constants");
obj.computerlist = null;
obj.apiRouter = require("./api");
obj.debug = function (msg) {
if (args.debug) {
console.log(msg);
}
};
obj.getAmtPassword = function (host) {
if (!obj.computerlist || obj.computerlist == null) return null;
for (var i in obj.computerlist) {
if (obj.computerlist[i].host == host) {
return [obj.computerlist[i].user, obj.computerlist[i].pass];
}
}
return null;
};
// Indicates to ExpressJS that the public folder should be used to serve static files. Mesh Commander will be at "default.htm".
obj.app.use(obj.express.static(obj.path.join(__dirname, "public")));
// API router to push and pull computers
obj.app.use("/api", obj.apiRouter);
// Indicates that any request to "/" should be redirected to "/default.htm" which is the Mesh Commander web application.
obj.app.get("/", function (req, res) {
// Select the best supported language for this browser
var language = null,
availableLanguages = [
"en",
"de",
"es",
"fr",
"it",
"ja",
"ko",
"nl",
"pt",
"ru",
"zh",
];
if (req.headers["accept-language"] != null) {
var acceptLanguageSplit = req.headers["accept-language"].split(";");
for (var i in acceptLanguageSplit) {
var acceptLanguageSplitEx = acceptLanguageSplit[i].split(",");
for (var j in acceptLanguageSplitEx) {
if (acceptLanguageSplitEx[j].startsWith("q=") == false) {
var l = acceptLanguageSplitEx[j].toLowerCase().split("-")[0];
if (language == null && availableLanguages.indexOf(l) >= 0) {
language = l;
}
}
}
}
}
if (language == null || language == "en") {
language = "";
} else {
if (language == "zh") {
language = "zh-chz";
}
language = "-" + language;
}
res.set({
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
});
res.redirect("/default" + language + ".htm");
});
// For the server version of Mesh Commander, we send the computer list without credential and insertion credentials in the stream.
obj.app.get("/webrelay.ashx", function (req, res) {
res.set({
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
});
if (req.query.action == "getcomputerlist") {
obj.fs.readFile("computerlist.config", "utf8", function (err, data) {
if (err == null) {
var list = JSON.parse(data);
obj.computerlist = obj.common.Clone(list);
for (var i in list) {
delete list[i].pass;
} // Remove all passwords.
res.set({ "Content-Type": "application/json" });
res.send(JSON.stringify(list));
}
});
}
try {
res.close();
} catch (e) {}
});
// Indicates to ExpressJS what we want to handle websocket requests on "/webrelay.ashx". This is the same URL as IIS making things simple, we can use the same web application for both IIS and Node.
obj.app.ws("/webrelay.ashx", function (ws, req) {
ws.pause();
// When data is received from the web socket, forward the data into the associated TCP connection.
// If the TCP connection is pending, buffer up the data until it connects.
ws.on("message", function (msg) {
// Convert a buffer into a string, "msg = msg.toString('ascii');" does not work
var msg2 = "";
for (var i = 0; i < msg.length; i++) {
msg2 += String.fromCharCode(msg[i]);
}
msg = msg2;
if (ws.interceptor) {
msg = ws.interceptor.processBrowserData(msg);
} // Run data thru interceptor
ws.forwardclient.write(Buffer.from(msg, "ascii")); // Forward data to the associated TCP connection.
});
// If the web socket is closed, close the associated TCP connection.
ws.on("close", function (req) {
obj.debug(
"Closing web socket connection to " +
ws.upgradeReq.query.host +
":" +
ws.upgradeReq.query.port +
"."
);
if (ws.forwardclient) {
try {
ws.forwardclient.destroy();
} catch (e) {}
}
});
// We got a new web socket connection, initiate a TCP connection to the target Intel AMT host/port.
obj.debug(
"Opening web socket connection to " +
req.query.host +
":" +
req.query.port +
"."
);
if (req.query.tls == 0) {
// If this is TCP (without TLS) set a normal TCP socket
ws.forwardclient = new obj.net.Socket();
ws.forwardclient.setEncoding("binary");
ws.forwardclient.forwardwsocket = ws;
} else {
// If TLS is going to be used, setup a TLS socket
var tlsoptions = {
secureProtocol:
req.query.tls1only == 1 ? "TLSv1_method" : "SSLv23_method",
ciphers: "RSA+AES:!aNULL:!MD5:!DSS",
secureOptions:
obj.constants.SSL_OP_NO_SSLv2 |
obj.constants.SSL_OP_NO_SSLv3 |
obj.constants.SSL_OP_NO_COMPRESSION |
obj.constants.SSL_OP_CIPHER_SERVER_PREFERENCE,
rejectUnauthorized: false,
};
ws.forwardclient = obj.tls.connect(
req.query.port,
req.query.host,
tlsoptions,
function () {
// The TLS connection method is the same as TCP, but located a bit differently.
obj.debug(
"TLS connected to " + req.query.host + ":" + req.query.port + "."
);
ws.resume();
}
);
ws.forwardclient.setEncoding("binary");
ws.forwardclient.forwardwsocket = ws;
}
// When we receive data on the TCP connection, forward it back into the web socket connection.
ws.forwardclient.on("data", function (data) {
if (ws.interceptor) {
data = ws.interceptor.processAmtData(data);
} // Run data thru interceptor
try {
ws.send(Buffer.from(data, "ascii"));
} catch (ex) {}
});
// If the TCP connection closes, disconnect the associated web socket.
ws.forwardclient.on("close", function () {
obj.debug(
"TCP disconnected from " + req.query.host + ":" + req.query.port + "."
);
try {
ws.close();
} catch (ex) {}
});
// If the TCP connection causes an error, disconnect the associated web socket.
ws.forwardclient.on("error", function (err) {
obj.debug(
"TCP disconnected with error from " +
req.query.host +
":" +
req.query.port +
": " +
err.code +
", " +
req.url
);
try {
ws.close();
} catch (ex) {}
});
// Fetch Intel AMT credentials & Setup interceptor
var credentials = obj.getAmtPassword(req.query.host);
if (credentials != null) {
if (req.query.p == 1) {
ws.interceptor = obj.interceptor.CreateHttpInterceptor({
host: req.query.host,
port: req.query.port,
user: credentials[0],
pass: credentials[1],
});
} else if (req.query.p == 2) {
ws.interceptor = obj.interceptor.CreateRedirInterceptor({
user: credentials[0],
pass: credentials[1],
});
}
}
if (req.query.tls == 0) {
// A TCP connection to Intel AMT just connected, send any pending data and start forwarding.
ws.forwardclient.connect(req.query.port, req.query.host, function () {
obj.debug(
"TCP connected to " + req.query.host + ":" + req.query.port + "."
);
ws.resume();
});
}
});
// Start the ExpressJS web server
var port = 3000;
if (args.port != null) {
port = parseInt(args.port);
}
if (
isNaN(port) ||
port == null ||
typeof port != "number" ||
port < 0 ||
port > 65536
) {
port = 3000;
}
if (args.any != null) {
obj.app.listen(port, function () {
console.log("MeshCommander running on http://*:" + port + ".");
});
} else {
obj.app.listen(port, "0.0.0.0", function () {
console.log("MeshCommander running on http://localhost:" + port + ".");
});
}
return obj;
};