-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile-server.js
155 lines (118 loc) · 4.37 KB
/
file-server.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
const express = require("express");
const app = express();
const port = 3110;
const router = express.Router();
const cors = require("cors");
const fs = require("fs");
const https = require("https");
const mkcert = require("mkcert");
const path = require("path");
const rogueConfig = require("./rogue-config.json");
const useHttps = rogueConfig.useHttps;
let refreshCount = 0;
(async () => {
if (useHttps) {
let certificateExists = fs.existsSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"));
if (certificateExists) {
const certificateTtl = 1000 * 60 * 60 * 24;
const certificateStat = fs.statSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"));
const now = new Date();
// if cert is old, remove it.
if ((now - certificateStat.ctime) / certificateTtl > 30) {
fs.unlinkSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"))
certificateExists = false;
}
}
if (!certificateExists) {
const cacheDirExists = fs.existsSync(path.join(__dirname, "/node_modules/.cache"));
if (!cacheDirExists) {
fs.mkdirSync(path.join(__dirname, "/node_modules/.cache"));
}
const reLocalDirExists = fs.existsSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local"));
if (!reLocalDirExists) {
fs.mkdirSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local"));
}
const ca = await mkcert.createCA({
organization: "Vortalix",
countryCode: "UK",
state: "Merseyside",
locality: "Liverpool",
validityDays: 35
});
const cert = await mkcert.createCert({
domains: ["127.0.0.1", "localhost", "192.168.0.*"],
validityDays: 35,
caKey: ca.key,
caCert: ca.cert
});
fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/ca.key"), ca.key);
fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/ca.crt"), ca.cert);
fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.key"), cert.key);
fs.writeFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"), `${cert.cert}\n${ca.cert}`);
}
}
let config = {
title: "",
scenes: [],
sceneJson: {},
assetPaths: {},
}
app.use(cors());
app.use(express.static(`${__dirname}/dist`));
app.use("/", router);
router.use(express.json({limit: "10000mb"}));
router.use(express.urlencoded({limit: "10000mb", extended: false }));
router.post("/setScenePlayerConfig", (req, res, next) => {
config = req.body;
res.send("Ok");
});
let setScenePlayerConfigOK = () => {};
router.get("/getScenePlayerConfig", (req, res, next) => {
setScenePlayerConfigOK = () => {
res.send(config);
};
process.send("getScenePlayerConfig");
});
router.post("/addRefreshCount", (req, res, next) => {
refreshCount += 1;
res.send("Ok");
});
router.get("/getRefreshCount", (req, res, next) => {
res.send(refreshCount.toString());
});
router.get("*", (req, res, next) => {
req.url = replaceEscapeCharacters(req.url);
req.path = replaceEscapeCharacters(req.path);
// use req.path, instead of req.url, to discard any query parameter (like webpack hashes)
res.sendFile(`${__dirname}` + req.url);
});
const replaceEscapeCharacters = ( text ) => {
text = text.replace( '%20', ' ' );
if( text.includes('%20') )
return replaceEscapeCharacters( text );
else
return text;
}
process.on("message", (message) => {
if (message === "setScenePlayerConfigOK") {
setScenePlayerConfigOK();
}
});
if (useHttps) {
let privateKey = fs.readFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.key"), "utf8");
let certificate = fs.readFileSync(path.join(__dirname, "/node_modules/.cache/rogue-engine-local/server.crt"), "utf8");
const customKeyPath = path.join(__dirname, "Certificates", "server.key");
const customCertPath = path.join(__dirname, "Certificates", "server.crt");
const customKeyExists = fs.existsSync(customKeyPath);
const customCertExists = fs.existsSync(customCertPath);
if (customKeyExists && customCertExists) {
privateKey = fs.readFileSync(customKeyPath, "utf8");
certificate = fs.readFileSync(customCertPath, "utf8");
}
const credentials = {key: privateKey, cert: certificate};
const server = https.createServer(credentials, app);
server.listen(port);
} else {
app.listen(port);
}
})();