Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
amethystnetwork-dev
GitHub Repository: amethystnetwork-dev/Incognito
Path: blob/main/src/index.js
917 views
1
/**
2
* Incognito
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
import wisp from "wisp-server-node";
19
import { baremuxPath } from "@mercuryworkshop/bare-mux";
20
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
21
import { gamesPath } from "@amethystnetwork-dev/incognito-gfiles";
22
import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
23
24
import { fileURLToPath } from "node:url";
25
import { createServer as createHttpsServer } from "node:https";
26
import { createServer as createHttpServer } from "node:http";
27
import { readFileSync, existsSync } from "node:fs";
28
import { hostname } from "node:os";
29
30
import serveStatic from "serve-static";
31
import serveIndex from "serve-index";
32
import connect from "connect";
33
import analytics from "./analytics.js";
34
35
console.log(`Incognito
36
This program comes with ABSOLUTELY NO WARRANTY.
37
This is free software, and you are welcome to redistribute it
38
under the terms of the GNU General Public License as published by
39
the Free Software Foundation, either version 3 of the License, or
40
(at your option) any later version.
41
42
You should have received a copy of the GNU General Public License
43
along with this program. If not, see <https://www.gnu.org/licenses/>.
44
`);
45
46
const app = connect();
47
const ssl = existsSync("../ssl/key.pem") && existsSync("../ssl/cert.pem");
48
const PORT = process.env.PORT || ssl ? 443 : 8080;
49
const server = ssl ? createHttpsServer({
50
key: readFileSync("../ssl/key.pem"),
51
cert: readFileSync("../ssl/cert.pem")
52
}) : createHttpServer();
53
54
app.use((req, res, next) => {
55
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
56
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
57
next();
58
});
59
60
app.use(serveStatic(fileURLToPath(new URL("../static/", import.meta.url))));
61
app.use("/source", serveStatic(gamesPath));
62
app.use("/source", serveIndex(gamesPath, { icons: true }));
63
64
app.use("/uv", serveStatic(uvPath));
65
app.use("/lcl", serveStatic(libcurlPath));
66
app.use("/bm", serveStatic(baremuxPath));
67
analytics(app);
68
69
server.on("request", app);
70
server.on("upgrade", (req, socket, head) => {
71
if(req.url.startsWith("/wisp")) wisp.routeRequest(req, socket, head); else socket.end();
72
});
73
74
server.on("listening", () => {
75
const addr = server.address();
76
const formatURLWithPort = (hostname, addr) => `http${ssl ? "s" : ""}://${hostname}${(addr.port === 80 || ssl && addr.port === 443) ? "" : ":" + addr.port}`;
77
78
console.log(`Server running on port ${addr.port}`)
79
console.log("");
80
console.log("You can now view it in your browser.")
81
/* Code for listing IPS from website-aio */
82
console.log(`Local: ${formatURLWithPort(addr.family === "IPv6" ? `[${addr.address}]` : addr.address, addr)}`);
83
console.log(`Local: ${formatURLWithPort("localhost", addr)}`);
84
try { console.log(`On Your Network: ${formatURLWithPort(hostname(), addr)}`); } catch (err) {/* Can't find LAN interface */};
85
});
86
87
server.listen({ port: PORT })
88