webrelayserver.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @description Meshcentral web relay server
  3. * @author Ylian Saint-Hilaire
  4. * @copyright Intel Corporation 2018-2022
  5. * @license Apache-2.0
  6. * @version v0.0.1
  7. */
  8. /*jslint node: true */
  9. /*jshint node: true */
  10. /*jshint strict:false */
  11. /*jshint -W097 */
  12. /*jshint esversion: 6 */
  13. "use strict";
  14. // Construct a HTTP redirection web server object
  15. module.exports.CreateWebRelayServer = function (parent, db, args, certificates, func) {
  16. var obj = {};
  17. obj.parent = parent;
  18. obj.db = db;
  19. obj.express = require('express');
  20. obj.expressWs = null;
  21. obj.tlsServer = null;
  22. obj.net = require('net');
  23. obj.app = obj.express();
  24. if (args.compression !== false) { obj.app.use(require('compression')()); }
  25. obj.app.disable('x-powered-by');
  26. obj.webRelayServer = null;
  27. obj.port = 0;
  28. obj.cleanupTimer = null;
  29. var relaySessions = {} // RelayID --> Web Mutli-Tunnel
  30. const constants = (require('crypto').constants ? require('crypto').constants : require('constants')); // require('constants') is deprecated in Node 11.10, use require('crypto').constants instead.
  31. var tlsSessionStore = {}; // Store TLS session information for quick resume.
  32. var tlsSessionStoreCount = 0; // Number of cached TLS session information in store.
  33. function serverStart() {
  34. // Setup CrowdSec bouncer middleware if needed
  35. if (parent.crowdsecMiddleware != null) { obj.app.use(parent.crowdsecMiddleware); }
  36. if (args.trustedproxy) {
  37. // Reverse proxy should add the "X-Forwarded-*" headers
  38. try {
  39. obj.app.set('trust proxy', args.trustedproxy);
  40. } catch (ex) {
  41. // If there is an error, try to resolve the string
  42. if ((args.trustedproxy.length == 1) && (typeof args.trustedproxy[0] == 'string')) {
  43. require('dns').lookup(args.trustedproxy[0], function (err, address, family) { if (err == null) { obj.app.set('trust proxy', address); args.trustedproxy = [address]; } });
  44. }
  45. }
  46. }
  47. else if (typeof args.tlsoffload == 'object') {
  48. // Reverse proxy should add the "X-Forwarded-*" headers
  49. try {
  50. obj.app.set('trust proxy', args.tlsoffload);
  51. } catch (ex) {
  52. // If there is an error, try to resolve the string
  53. if ((Array.isArray(args.tlsoffload)) && (args.tlsoffload.length == 1) && (typeof args.tlsoffload[0] == 'string')) {
  54. require('dns').lookup(args.tlsoffload[0], function (err, address, family) { if (err == null) { obj.app.set('trust proxy', address); args.tlsoffload = [address]; } });
  55. }
  56. }
  57. }
  58. // Setup a keygrip instance with higher default security, default hash is SHA1, we want to bump that up with SHA384
  59. // If multiple instances of this server are behind a load-balancer, this secret must be the same for all instances
  60. // If args.sessionkey is a string, use it as a single key, but args.sessionkey can also be used as an array of keys.
  61. const keygrip = require('keygrip')((typeof args.sessionkey == 'string') ? [args.sessionkey] : args.sessionkey, 'sha384', 'base64');
  62. // Watch for device share removal
  63. parent.AddEventDispatch(['server-shareremove'], obj);
  64. obj.HandleEvent = function (source, event, ids, id) {
  65. if (event.action == 'removedDeviceShare') {
  66. for (var relaySessionId in relaySessions) {
  67. // A share was removed that matches an active session, close the session.
  68. if (relaySessions[relaySessionId].xpublicid === event.publicid) { relaySessions[relaySessionId].close(); }
  69. }
  70. }
  71. }
  72. // Setup cookie session
  73. const sessionOptions = {
  74. name: 'xid', // Recommended security practice to not use the default cookie name
  75. httpOnly: true,
  76. keys: keygrip,
  77. secure: (args.tlsoffload == null), // Use this cookie only over TLS (Check this: https://expressjs.com/en/guide/behind-proxies.html)
  78. sameSite: (args.sessionsamesite ? args.sessionsamesite : 'lax')
  79. }
  80. if (args.sessiontime != null) { sessionOptions.maxAge = (args.sessiontime * 60000); } // sessiontime is minutes
  81. obj.app.use(require('cookie-session')(sessionOptions));
  82. obj.app.use(function(request, response, next) { // Patch for passport 0.6.0 - https://github.com/jaredhanson/passport/issues/904
  83. if (request.session && !request.session.regenerate) {
  84. request.session.regenerate = function (cb) {
  85. cb()
  86. }
  87. }
  88. if (request.session && !request.session.save) {
  89. request.session.save = function (cb) {
  90. cb()
  91. }
  92. }
  93. next()
  94. });
  95. // Add HTTP security headers to all responses
  96. obj.app.use(function (req, res, next) {
  97. parent.debug('webrelay', req.url);
  98. res.removeHeader('X-Powered-By');
  99. res.set({
  100. 'strict-transport-security': 'max-age=60000; includeSubDomains',
  101. 'Referrer-Policy': 'no-referrer',
  102. 'x-frame-options': 'SAMEORIGIN',
  103. 'X-XSS-Protection': '1; mode=block',
  104. 'X-Content-Type-Options': 'nosniff',
  105. 'Content-Security-Policy': "default-src 'self'; style-src 'self' 'unsafe-inline';"
  106. });
  107. // Set the real IP address of the request
  108. // If a trusted reverse-proxy is sending us the remote IP address, use it.
  109. var ipex = '0.0.0.0', xforwardedhost = req.headers.host;
  110. if (typeof req.connection.remoteAddress == 'string') { ipex = (req.connection.remoteAddress.startsWith('::ffff:')) ? req.connection.remoteAddress.substring(7) : req.connection.remoteAddress; }
  111. if (
  112. (args.trustedproxy === true) || (args.tlsoffload === true) ||
  113. ((typeof args.trustedproxy == 'object') && (isIPMatch(ipex, args.trustedproxy))) ||
  114. ((typeof args.tlsoffload == 'object') && (isIPMatch(ipex, args.tlsoffload)))
  115. ) {
  116. // Get client IP
  117. if (req.headers['cf-connecting-ip']) { // Use CloudFlare IP address if present
  118. req.clientIp = req.headers['cf-connecting-ip'].split(',')[0].trim();
  119. } else if (req.headers['x-forwarded-for']) {
  120. req.clientIp = req.headers['x-forwarded-for'].split(',')[0].trim();
  121. } else if (req.headers['x-real-ip']) {
  122. req.clientIp = req.headers['x-real-ip'].split(',')[0].trim();
  123. } else {
  124. req.clientIp = ipex;
  125. }
  126. // If there is a port number, remove it. This will only work for IPv4, but nice for people that have a bad reverse proxy config.
  127. const clientIpSplit = req.clientIp.split(':');
  128. if (clientIpSplit.length == 2) { req.clientIp = clientIpSplit[0]; }
  129. // Get server host
  130. if (req.headers['x-forwarded-host']) { xforwardedhost = req.headers['x-forwarded-host'].split(',')[0]; } // If multiple hosts are specified with a comma, take the first one.
  131. } else {
  132. req.clientIp = ipex;
  133. }
  134. // If this is a session start or a websocket, have the application handle this
  135. if ((req.headers.upgrade == 'websocket') || (req.url.startsWith('/control-redirect.ashx?n='))) {
  136. return next();
  137. } else {
  138. // If this is a normal request (GET, POST, etc) handle it here
  139. var webSessionId = null;
  140. if ((req.session.userid != null) && (req.session.x != null)) { webSessionId = req.session.userid + '/' + req.session.x; }
  141. else if (req.session.z != null) { webSessionId = req.session.z; }
  142. if ((webSessionId != null) && (parent.webserver.destroyedSessions[webSessionId] == null)) {
  143. var relaySession = relaySessions[webSessionId];
  144. if (relaySession != null) {
  145. // The web relay session is valid, use it
  146. relaySession.handleRequest(req, res);
  147. } else {
  148. // No web relay ession with this relay identifier, close the HTTP request.
  149. res.end();
  150. }
  151. } else {
  152. // The user is not logged in or does not have a relay identifier, close the HTTP request.
  153. res.end();
  154. }
  155. }
  156. });
  157. // Start the server, only after users and meshes are loaded from the database.
  158. if (args.tlsoffload) {
  159. // Setup the HTTP server without TLS
  160. obj.expressWs = require('express-ws')(obj.app, null, { wsOptions: { perMessageDeflate: (args.wscompression === true) } });
  161. } else {
  162. // Setup the HTTP server with TLS, use only TLS 1.2 and higher with perfect forward secrecy (PFS).
  163. const tlsOptions = { cert: certificates.web.cert, key: certificates.web.key, ca: certificates.web.ca, rejectUnauthorized: true, ciphers: "HIGH:TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_8_SHA256:TLS_AES_128_CCM_SHA256:TLS_CHACHA20_POLY1305_SHA256", secureOptions: constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_COMPRESSION | constants.SSL_OP_CIPHER_SERVER_PREFERENCE | constants.SSL_OP_NO_TLSv1 | constants.SSL_OP_NO_TLSv1_1 };
  164. obj.tlsServer = require('https').createServer(tlsOptions, obj.app);
  165. obj.tlsServer.on('secureConnection', function () { /*console.log('tlsServer secureConnection');*/ });
  166. obj.tlsServer.on('error', function (err) { console.log('tlsServer error', err); });
  167. obj.tlsServer.on('newSession', function (id, data, cb) { if (tlsSessionStoreCount > 1000) { tlsSessionStoreCount = 0; tlsSessionStore = {}; } tlsSessionStore[id.toString('hex')] = data; tlsSessionStoreCount++; cb(); });
  168. obj.tlsServer.on('resumeSession', function (id, cb) { cb(null, tlsSessionStore[id.toString('hex')] || null); });
  169. obj.expressWs = require('express-ws')(obj.app, obj.tlsServer, { wsOptions: { perMessageDeflate: (args.wscompression === true) } });
  170. }
  171. // Handle incoming web socket calls
  172. obj.app.ws('/*', function (ws, req) {
  173. var webSessionId = null;
  174. if ((req.session.userid != null) && (req.session.x != null)) { webSessionId = req.session.userid + '/' + req.session.x; }
  175. else if (req.session.z != null) { webSessionId = req.session.z; }
  176. if ((webSessionId != null) && (parent.webserver.destroyedSessions[webSessionId] == null)) {
  177. var relaySession = relaySessions[webSessionId];
  178. if (relaySession != null) {
  179. // The multi-tunnel session is valid, use it
  180. relaySession.handleWebSocket(ws, req);
  181. } else {
  182. // No multi-tunnel session with this relay identifier, close the websocket.
  183. ws.close();
  184. }
  185. } else {
  186. // The user is not logged in or does not have a relay identifier, close the websocket.
  187. ws.close();
  188. }
  189. });
  190. // This is the magic URL that will setup the relay session
  191. obj.app.get('/control-redirect.ashx', function (req, res) {
  192. res.set({ 'Cache-Control': 'no-store' });
  193. parent.debug('webrelay', 'webRelaySetup');
  194. // Decode the relay cookie
  195. if (req.query.c == null) { res.sendStatus(404); return; }
  196. // Decode and check if this relay cookie is valid
  197. var userid, domainid, domain, nodeid, addr, port, appid, webSessionId, expire, publicid;
  198. const urlCookie = obj.parent.decodeCookie(req.query.c, parent.loginCookieEncryptionKey, 32); // Allow cookies up to 32 minutes old. The web page will renew this cookie every 30 minutes.
  199. if (urlCookie == null) { res.sendStatus(404); return; }
  200. // Decode the incoming cookie
  201. if ((urlCookie.ruserid != null) && (urlCookie.x != null)) {
  202. if (parent.webserver.destroyedSessions[urlCookie.ruserid + '/' + urlCookie.x] != null) { res.sendStatus(404); return; }
  203. // This is a standard user, figure out what our web relay will be.
  204. if (req.session.x != urlCookie.x) { req.session.x = urlCookie.x; } // Set the sessionid if missing
  205. if (req.session.userid != urlCookie.ruserid) { req.session.userid = urlCookie.ruserid; } // Set the session userid if missing
  206. if (req.session.z) { delete req.session.z; } // Clear the web relay guest session
  207. userid = req.session.userid;
  208. domainid = userid.split('/')[1];
  209. domain = parent.config.domains[domainid];
  210. nodeid = ((req.query.relayid != null) ? req.query.relayid : req.query.n);
  211. addr = (req.query.addr != null) ? req.query.addr : '127.0.0.1';
  212. port = parseInt(req.query.p);
  213. appid = parseInt(req.query.appid);
  214. webSessionId = req.session.userid + '/' + req.session.x;
  215. // Check that all the required arguments are present
  216. if ((req.session.userid == null) || (req.session.x == null) || (req.query.n == null) || (req.query.p == null) || (parent.webserver.destroyedSessions[webSessionId] != null) || ((req.query.appid != 1) && (req.query.appid != 2))) { res.redirect('/'); return; }
  217. } else if (urlCookie.r == 8) {
  218. // This is a guest user, figure out what our web relay will be.
  219. userid = urlCookie.userid;
  220. domainid = userid.split('/')[1];
  221. domain = parent.config.domains[domainid];
  222. nodeid = urlCookie.nid;
  223. addr = (urlCookie.addr != null) ? urlCookie.addr : '127.0.0.1';
  224. port = urlCookie.port;
  225. appid = (urlCookie.p == 16) ? 2 : 1; // appid: 1 = HTTP, 2 = HTTPS
  226. webSessionId = userid + '/' + urlCookie.pid;
  227. publicid = urlCookie.pid;
  228. if (req.session.x) { delete req.session.x; } // Clear the web relay sessionid
  229. if (req.session.userid) { delete req.session.userid; } // Clear the web relay userid
  230. if (req.session.z != webSessionId) { req.session.z = webSessionId; } // Set the web relay guest session
  231. expire = urlCookie.expire;
  232. if ((expire != null) && (expire <= Date.now())) { parent.debug('webrelay', 'expired link'); res.sendStatus(404); return; }
  233. }
  234. // No session identifier was setup, exit now
  235. if (webSessionId == null) { res.sendStatus(404); return; }
  236. // Check to see if we already have a multi-relay session that matches exactly this device and port for this user
  237. const xrelaySession = relaySessions[webSessionId];
  238. if ((xrelaySession != null) && (xrelaySession.domain.id == domain.id) && (xrelaySession.userid == userid) && (xrelaySession.nodeid == nodeid) && (xrelaySession.addr == addr) && (xrelaySession.port == port) && (xrelaySession.appid == appid)) {
  239. // We found an exact match, we are all setup already, redirect to root
  240. res.redirect('/');
  241. return;
  242. }
  243. // Check that the user has rights to access this device
  244. parent.webserver.GetNodeWithRights(domain, userid, nodeid, function (node, rights, visible) {
  245. // If there is no remote control or relay rights, reject this web relay
  246. if ((rights & 0x00200008) == 0) { res.sendStatus(404); return; } // MESHRIGHT_REMOTECONTROL or MESHRIGHT_RELAY
  247. // There is a relay session, but it's not correct, close it.
  248. if (xrelaySession != null) { xrelaySession.close(); delete relaySessions[webSessionId]; }
  249. // Create a web relay session
  250. const relaySession = require('./apprelays.js').CreateWebRelaySession(obj, db, req, args, domain, userid, nodeid, addr, port, appid, webSessionId, expire, node.mtype);
  251. relaySession.xpublicid = publicid;
  252. relaySession.onclose = function (sessionId) {
  253. // Remove the relay session
  254. delete relaySessions[sessionId];
  255. // If there are not more relay sessions, clear the cleanup timer
  256. if ((Object.keys(relaySessions).length == 0) && (obj.cleanupTimer != null)) { clearInterval(obj.cleanupTimer); obj.cleanupTimer = null; }
  257. }
  258. // Set the multi-tunnel session
  259. relaySessions[webSessionId] = relaySession;
  260. // Setup the cleanup timer if needed
  261. if (obj.cleanupTimer == null) { obj.cleanupTimer = setInterval(checkTimeout, 10000); }
  262. // Redirect to root
  263. res.redirect('/');
  264. });
  265. });
  266. }
  267. // Check that everything is cleaned up
  268. function checkTimeout() {
  269. for (var i in relaySessions) { relaySessions[i].checkTimeout(); }
  270. }
  271. // Find a free port starting with the specified one and going up.
  272. function CheckListenPort(port, addr, func) {
  273. var s = obj.net.createServer(function (socket) { });
  274. obj.webRelayServer = s.listen(port, addr, function () { s.close(function () { if (func) { func(port, addr); } }); }).on("error", function (err) {
  275. if (args.exactports) { console.error("ERROR: MeshCentral HTTP relay server port " + port + " not available."); process.exit(); }
  276. else { if (port < 65535) { CheckListenPort(port + 1, addr, func); } else { if (func) { func(0); } } }
  277. });
  278. }
  279. // Start the ExpressJS web server, if the port is busy try the next one.
  280. function StartWebRelayServer(port, addr) {
  281. if (port == 0 || port == 65535) { return; }
  282. if (obj.tlsServer != null) {
  283. if (args.lanonly == true) {
  284. obj.tcpServer = obj.tlsServer.listen(port, addr, function () { console.log('MeshCentral HTTPS relay server running on port ' + port + ((typeof args.relayaliasport == 'number') ? (', alias port ' + args.relayaliasport) : '') + '.'); });
  285. } else {
  286. obj.tcpServer = obj.tlsServer.listen(port, addr, function () { console.log('MeshCentral HTTPS relay server running on ' + certificates.CommonName + ':' + port + ((typeof args.relayaliasport == 'number') ? (', alias port ' + args.relayaliasport) : '') + '.'); });
  287. obj.parent.updateServerState('servername', certificates.CommonName);
  288. }
  289. if (obj.parent.authlog) { obj.parent.authLog('https', 'Web relay server listening on ' + ((addr != null) ? addr : '0.0.0.0') + ' port ' + port + '.'); }
  290. obj.parent.updateServerState('https-relay-port', port);
  291. if (typeof args.relayaliasport == 'number') { obj.parent.updateServerState('https-relay-aliasport', args.relayaliasport); }
  292. } else {
  293. obj.tcpServer = obj.app.listen(port, addr, function () { console.log('MeshCentral HTTP relay server running on port ' + port + ((typeof args.relayaliasport == 'number') ? (', alias port ' + args.relayaliasport) : '') + '.'); });
  294. obj.parent.updateServerState('http-relay-port', port);
  295. if (typeof args.relayaliasport == 'number') { obj.parent.updateServerState('http-relay-aliasport', args.relayaliasport); }
  296. }
  297. obj.port = port;
  298. }
  299. function getRandomPassword() { return Buffer.from(require('crypto').randomBytes(9), 'binary').toString('base64').split('/').join('@'); }
  300. // Perform a IP match against a list
  301. function isIPMatch(ip, matchList) {
  302. const ipcheck = require('ipcheck');
  303. for (var i in matchList) { if (ipcheck.match(ip, matchList[i]) == true) return true; }
  304. return false;
  305. }
  306. // Start up the web relay server
  307. serverStart();
  308. CheckListenPort(args.relayport, args.relayportbind, StartWebRelayServer);
  309. return obj;
  310. };