file-server.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const express = require("express");
  2. const app = express();
  3. const port = 3110;
  4. const router = express.Router();
  5. const cors = require("cors");
  6. const fs = require("fs");
  7. const https = require("https");
  8. const mkcert = require("mkcert");
  9. const path = require("path");
  10. const rogueConfig = require("./rogue-config.json");
  11. const useHttps = rogueConfig.useHttps;
  12. let refreshCount = 0;
  13. (async () => {
  14. if (useHttps) {
  15. let certificateExists = fs.existsSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"));
  16. if (certificateExists) {
  17. const certificateTtl = 1000 * 60 * 60 * 24;
  18. const certificateStat = fs.statSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"));
  19. const now = new Date();
  20. // if cert is old, remove it.
  21. if ((now - certificateStat.ctime) / certificateTtl > 30) {
  22. fs.unlinkSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"))
  23. certificateExists = false;
  24. }
  25. }
  26. if (!certificateExists) {
  27. const cacheDirExists = fs.existsSync(path.join(__dirname, "/node_modules/.cache"));
  28. if (!cacheDirExists) {
  29. fs.mkdirSync(path.join(__dirname, "/node_modules/.cache"));
  30. }
  31. const reLocalDirExists = fs.existsSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local"));
  32. if (!reLocalDirExists) {
  33. fs.mkdirSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local"));
  34. }
  35. const ca = await mkcert.createCA({
  36. organization: "Vortalix",
  37. countryCode: "UK",
  38. state: "Merseyside",
  39. locality: "Liverpool",
  40. validityDays: 35
  41. });
  42. const cert = await mkcert.createCert({
  43. domains: ["127.0.0.1", "localhost", "192.168.0.*"],
  44. validityDays: 35,
  45. caKey: ca.key,
  46. caCert: ca.cert
  47. });
  48. fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/ca.key"), ca.key);
  49. fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/ca.crt"), ca.cert);
  50. fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.key"), cert.key);
  51. fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"), `${cert.cert}\n${ca.cert}`);
  52. }
  53. }
  54. let config = {
  55. title: "",
  56. scenes: [],
  57. sceneJson: {},
  58. assetPaths: {},
  59. }
  60. app.use(cors());
  61. app.use(express.static(`${__dirname}/dist`));
  62. app.use("/", router);
  63. router.use(express.json({limit: "10000mb"}));
  64. router.use(express.urlencoded({limit: "10000mb", extended: false }));
  65. router.post("/setScenePlayerConfig", (req, res, next) => {
  66. config = req.body;
  67. res.send("Ok");
  68. });
  69. let setScenePlayerConfigOK = () => {};
  70. router.get("/getScenePlayerConfig", (req, res, next) => {
  71. setScenePlayerConfigOK = () => {
  72. res.send(config);
  73. };
  74. process.send("getScenePlayerConfig");
  75. });
  76. router.post("/addRefreshCount", (req, res, next) => {
  77. refreshCount += 1;
  78. res.send("Ok");
  79. });
  80. router.get("/getRefreshCount", (req, res, next) => {
  81. res.send(refreshCount.toString());
  82. });
  83. router.get("*", (req, res, next) => {
  84. req.url = replaceEscapeCharacters(req.url);
  85. req.path = replaceEscapeCharacters(req.path);
  86. // use req.path, instead of req.url, to discard any query parameter (like webpack hashes)
  87. res.sendFile(`${__dirname}` + req.url);
  88. });
  89. const replaceEscapeCharacters = ( text ) => {
  90. text = text.replace( '%20', ' ' );
  91. if( text.includes('%20') )
  92. return replaceEscapeCharacters( text );
  93. else
  94. return text;
  95. }
  96. process.on("message", (message) => {
  97. if (message === "setScenePlayerConfigOK") {
  98. setScenePlayerConfigOK();
  99. }
  100. });
  101. if (useHttps) {
  102. let privateKey = fs.readFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.key"), "utf8");
  103. let certificate = fs.readFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"), "utf8");
  104. const customKeyPath = path.join(__dirname, "Certificates", "server.key");
  105. const customCertPath = path.join(__dirname, "Certificates", "server.crt");
  106. const customKeyExists = fs.existsSync(customKeyPath);
  107. const customCertExists = fs.existsSync(customCertPath);
  108. if (customKeyExists && customCertExists) {
  109. privateKey = fs.readFileSync(customKeyPath, "utf8");
  110. certificate = fs.readFileSync(customCertPath, "utf8");
  111. }
  112. const credentials = {key: privateKey, cert: certificate};
  113. const server = https.createServer(credentials, app);
  114. server.listen(port);
  115. } else {
  116. app.listen(port);
  117. }
  118. })();