diff --git a/client/src/hub/index.ts b/client/src/hub/index.ts index 176996b4..37712664 100644 --- a/client/src/hub/index.ts +++ b/client/src/hub/index.ts @@ -11,7 +11,7 @@ const data: { const ui: { projectsTreeView?: any } = {}; let socket: SocketIOClient.Socket; -const port = (window.location.port.length === 0) ? "80" : window.location.port; +const port = (window.location.port.length === 0) ? (window.location.protocol === "https:" ? "443" : "80") : window.location.port; const languageNamesById: { [id: string]: string; } = {}; if (localStorage.getItem("superpowers-dev-mode") != null) languageNamesById["none"] = "None"; diff --git a/client/src/login/index.ts b/client/src/login/index.ts index b11bd0cb..e10760a8 100644 --- a/client/src/login/index.ts +++ b/client/src/login/index.ts @@ -1,6 +1,6 @@ import "../window"; -const port = (window.location.port.length === 0) ? "80" : window.location.port; +const port = (window.location.port.length === 0) ? (window.location.protocol === "https" ? "443" : "80") : window.location.port; const connectingElt = document.querySelector(".connecting") as HTMLDivElement; const formElt = document.querySelector(".login") as HTMLDivElement; diff --git a/server/loadSystems.ts b/server/loadSystems.ts index df4a377e..4c8f4d62 100644 --- a/server/loadSystems.ts +++ b/server/loadSystems.ts @@ -22,7 +22,7 @@ export default function(mainApp: express.Express, buildApp: express.Express, cal // Expose public stuff try { fs.mkdirSync(`${systemPath}/public`); } catch (err) { /* Ignore */ } mainApp.use(`/systems/${systemId}`, express.static(`${systemPath}/public`)); - buildApp.use(`/systems/${systemId}`, express.static(`${systemPath}/public`)); + if (buildApp != null) buildApp.use(`/systems/${systemId}`, express.static(`${systemPath}/public`)); // Write templates list let templatesList: string[] = []; @@ -149,16 +149,18 @@ function loadPlugins (systemId: string, pluginsPath: string, mainApp: express.Ex }); }); - for (const app of [mainApp, buildApp]) { - app.get(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}/bundles/*.js`, (req, res) => { - const bundleFile = req.path.split("/bundles/")[1]; - const bundlePath = path.join(pluginPath, "public/bundles", bundleFile); - fs.exists(bundlePath, (exists) => { - if (exists) res.sendFile(bundlePath); - else res.send(""); + for (const app of [ mainApp, buildApp ]) { + if (app != null) { + app.get(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}/bundles/*.js`, (req, res) => { + const bundleFile = req.path.split("/bundles/")[1]; + const bundlePath = path.join(pluginPath, "public/bundles", bundleFile); + fs.exists(bundlePath, (exists) => { + if (exists) res.sendFile(bundlePath); + else res.send(""); + }); }); - }); - app.use(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}`, express.static(`${pluginPath}/public`)); + app.use(`/systems/${systemId}/plugins/${pluginAuthor}/${pluginName}`, express.static(`${pluginPath}/public`)); + } } }); }); diff --git a/server/start.ts b/server/start.ts index 5736f54e..3635214a 100644 --- a/server/start.ts +++ b/server/start.ts @@ -46,7 +46,13 @@ export default function start(serverDataPath: string) { const { version, superpowers: { appApiVersion: appApiVersion } } = JSON.parse(fs.readFileSync(`${__dirname}/../package.json`, { encoding: "utf8" })); SupCore.log(`Server v${version} starting...`); - fs.writeFileSync(`${__dirname}/../public/superpowers.json`, JSON.stringify({ version, appApiVersion, hasPassword: config.server.password.length !== 0 }, null, 2)); + + const serverPublicConfig = { + version, appApiVersion, + hasPassword: config.server.password.length !== 0, + useSSL: config.server.mainPort === 443 + }; + fs.writeFileSync(`${__dirname}/../public/superpowers.json`, JSON.stringify(serverPublicConfig, null, 2)); // SupCore (global as any).SupCore = SupCore; @@ -76,12 +82,13 @@ export default function start(serverDataPath: string) { io = socketio(mainHttpServer, { transports: [ "websocket" ] }); // Build HTTP server - buildApp = express(); + let hasSeparateBuildPort = config.server.mainPort !== config.server.buildPort; + buildApp = hasSeparateBuildPort ? express() : mainApp; - buildApp.get("/", redirectToHub); + if (hasSeparateBuildPort) buildApp.get("/", redirectToHub); buildApp.get("/systems/:systemId/SupCore.js", serveSystemSupCore); - buildApp.use("/", express.static(`${__dirname}/../public`)); + if (hasSeparateBuildPort) buildApp.use("/", express.static(`${__dirname}/../public`)); buildApp.get("/builds/:projectId/:buildId/*", (req, res) => { const projectServer = hub.serversById[req.params.projectId]; @@ -91,10 +98,12 @@ export default function start(serverDataPath: string) { res.sendFile(path.join(projectServer.buildsPath, buildId, req.params[0])); }); - buildHttpServer = http.createServer(buildApp); - buildHttpServer.on("error", onHttpServerError.bind(null, config.server.buildPort)); + if (hasSeparateBuildPort) { + buildHttpServer = http.createServer(buildApp); + buildHttpServer.on("error", onHttpServerError.bind(null, config.server.buildPort)); + } - loadSystems(mainApp, buildApp, onSystemsLoaded); + loadSystems(mainApp, hasSeparateBuildPort ? buildApp : null, onSystemsLoaded); // Save on exit and handle crashes process.on("SIGINT", onExit); @@ -109,6 +118,7 @@ function loadConfig() { for (const key in config.defaults) { if (config.server[key] == null) config.server[key] = config.defaults[key]; + else if (config.server[key] === "env.PORT") config.server[key] = process.env.PORT != null ? process.env.PORT : config.defaults[key]; } } else { fs.writeFileSync(serverConfigPath, JSON.stringify(config.defaults, null, 2) + "\n", { encoding: "utf8" }); @@ -195,15 +205,23 @@ function onSystemsLoaded() { const hostname = (config.server.password.length === 0) ? "localhost" : ""; mainHttpServer.listen(config.server.mainPort, hostname, () => { - buildHttpServer.listen(config.server.buildPort, hostname, () => { - SupCore.log(`Main server started on port ${config.server.mainPort}, build server started on port ${config.server.buildPort}.`); - if (hostname === "localhost") SupCore.log("NOTE: Setup a password to allow other people to connect to your server."); - if (process != null && process.send != null) process.send({ type: "started" }); - }); + if (buildHttpServer != null) { + buildHttpServer.listen(config.server.buildPort, hostname, () => { + onServerStarted(hostname); + }); + } else { + onServerStarted(hostname); + } }); }); } +function onServerStarted(hostname: string) { + SupCore.log(`Main server started on port ${config.server.mainPort}, build server started on port ${config.server.buildPort}.`); + if (hostname === "localhost") SupCore.log("NOTE: Setup a password to allow other people to connect to your server."); + if (process != null && process.send != null) process.send({ type: "started" }); +} + function handle404(err: any, req: express.Request, res: express.Response, next: Function) { if (err.status === 404) { res.status(404).end("File not found"); return; } next(); @@ -213,7 +231,7 @@ function onExit() { if (isQuitting) return; isQuitting = true; mainHttpServer.close(); - buildHttpServer.close(); + if (buildHttpServer != null) buildHttpServer.close(); if (hub == null) { process.exit(0);