-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
86 lines (67 loc) · 3.39 KB
/
index.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
import express from "express";
import http from "node:http";
import cors from "cors";
import path from "node:path";
import { hostname } from "node:os";
import chalk from "chalk";
import axios from "axios";
import { URL, parse } from 'url';
import contentType from 'content-type';
import { UmbraProxy } from "./src/umbra-proxy.js"
const server = http.createServer();
const app = express();
const PORT = process.env.PORT || 8080;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(process.cwd(), "/public")));
app.use(cors());
app.get("/", (req, res) => {
res.sendFile(path.join(process.cwd(), "/public/index.html"));
});
UmbraProxy(app, axios, URL, contentType);
server.on("request", (req, res) => {
app(req, res);
});
server.on("upgrade", (req, socket, head) => {
socket.end();
});
server.on("listening", () => {
const address = server.address();
const theme = chalk.hex("#800080");
const host = chalk.hex("0d52bd");
console.log(chalk.bold(theme(`
██╗ ██╗███╗ ███╗██████╗ ██████╗ █████╗
██║ ██║████╗ ████║██╔══██╗██╔══██╗██╔══██╗
██║ ██║██╔████╔██║██████╔╝██████╔╝███████║
██║ ██║██║╚██╔╝██║██╔══██╗██╔══██╗██╔══██║
╚██████╔╝██║ ╚═╝ ██║██████╔╝██║ ██║██║ ██║
╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝
`)));
console.log(` ${chalk.bold(host("Local System:"))} http://${address.family === "IPv6" ? `[${address.address}]` : address.address}${address.port === 80 ? "" : ":" + chalk.bold(address.port)}`);
console.log(` ${chalk.bold(host("Local System:"))} http://localhost${address.port === 8080 ? "" : ":" + chalk.bold(address.port)}`);
try {
console.log(` ${chalk.bold(host("On Your Network:"))} http://${hostname()}${address.port === 8080 ? "" : ":" + chalk.bold(address.port)}`);
} catch (err) {
// can't find LAN interface
}
if (process.env.REPL_SLUG && process.env.REPL_OWNER) {
console.log(` ${chalk.bold(host("Replit:"))} https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`);
}
if (process.env.HOSTNAME && process.env.GITPOD_WORKSPACE_CLUSTER_HOST) {
console.log(` ${chalk.bold(host("Gitpod:"))} https://${PORT}-${process.env.HOSTNAME}.${process.env.GITPOD_WORKSPACE_CLUSTER_HOST}`);
}
if (process.env.CODESPACE_NAME && process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN) {
console.log(` ${chalk.bold(host("Github Codespaces:"))} https://${process.env.CODESPACE_NAME}-${address.port === 80 ? "" : address.port}.${process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`);
}
});
server.listen({ port: PORT });
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
server.setMaxListeners(0);
function shutdown() {
console.log("SIGTERM signal received: closing HTTP server");
server.close(() => {
console.log("HTTP server closed");
process.exit(1);
});
}