import wisp from "wisp-server-node";
import { baremuxPath } from "@mercuryworkshop/bare-mux";
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { gamesPath } from "@amethystnetwork-dev/incognito-gfiles";
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
import { fileURLToPath } from "node:url";
import { createServer as createHttpsServer } from "node:https";
import { createServer as createHttpServer } from "node:http";
import { readFileSync, existsSync } from "node:fs";
import { hostname } from "node:os";
import serveStatic from "serve-static";
import serveIndex from "serve-index";
import connect from "connect";
import analytics from "./analytics.js";
console.log(`Incognito
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
`);
const app = connect();
const ssl = existsSync("../ssl/key.pem") && existsSync("../ssl/cert.pem");
const PORT = process.env.PORT || ssl ? 443 : 8080;
const server = ssl ? createHttpsServer({
key: readFileSync("../ssl/key.pem"),
cert: readFileSync("../ssl/cert.pem")
}) : createHttpServer();
app.use((req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
});
app.use(serveStatic(fileURLToPath(new URL("../static/", import.meta.url))));
app.use("/source", serveStatic(gamesPath));
app.use("/source", serveIndex(gamesPath, { icons: true }));
app.use("/uv", serveStatic(uvPath));
app.use("/lcl", serveStatic(libcurlPath));
app.use("/bm", serveStatic(baremuxPath));
analytics(app);
server.on("request", app);
server.on("upgrade", (req, socket, head) => {
if(req.url.startsWith("/wisp")) wisp.routeRequest(req, socket, head); else socket.end();
});
server.on("listening", () => {
const addr = server.address();
const formatURLWithPort = (hostname, addr) => `http${ssl ? "s" : ""}://${hostname}${(addr.port === 80 || ssl && addr.port === 443) ? "" : ":" + addr.port}`;
console.log(`Server running on port ${addr.port}`)
console.log("");
console.log("You can now view it in your browser.")
console.log(`Local: ${formatURLWithPort(addr.family === "IPv6" ? `[${addr.address}]` : addr.address, addr)}`);
console.log(`Local: ${formatURLWithPort("localhost", addr)}`);
try { console.log(`On Your Network: ${formatURLWithPort(hostname(), addr)}`); } catch (err) {};
});
server.listen({ port: PORT })