-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
108 lines (93 loc) · 3.16 KB
/
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
import express from 'express';
import { createServer } from 'node:http';
import { publicPath } from 'ultraviolet-static';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
import { epoxyPath } from '@mercuryworkshop/epoxy-transport';
import { baremuxPath } from '@mercuryworkshop/bare-mux/node';
import { join } from 'node:path';
import { hostname } from 'node:os';
import wisp from 'wisp-server-node';
import Ultraviolet from '@titaniumnetwork-dev/ultraviolet';
import uvConfig from './uv.config.js';
const app = express();
// Log paths to ensure they are correct
console.log('publicPath:', publicPath);
console.log('uvPath:', uvPath);
console.log('epoxyPath:', epoxyPath);
console.log('baremuxPath:', baremuxPath);
// Load our publicPath first and prioritize it over UV.
app.use(express.static(publicPath));
// Load vendor files last.
// The vendor's uv.config.js won't conflict with our uv.config.js inside the publicPath directory.
app.use('/uv/', express.static(uvPath));
app.use('/epoxy/', express.static(epoxyPath));
app.use('/baremux/', express.static(baremuxPath));
// Configure Ultraviolet
const uv = new Ultraviolet(uvConfig);
// Initialize port variable
let port = parseInt(process.env.PORT || '');
if (isNaN(port)) port = 8080;
// Generate proxied URL
app.get('/generate-proxy-url', (req, res) => {
const serviceUrl = req.query.url;
if (serviceUrl) {
const encodedUrl = uv.encodeUrl(serviceUrl);
const proxyUrl = `http://${hostname()}:${port}/service/${encodedUrl}`;
res.send(proxyUrl);
} else {
res.status(400).send('No URL provided');
}
});
// Use Ultraviolet middleware
app.use('/service/', uv.middleware());
// Error for everything else
app.use((req, res) => {
res.status(404);
res.sendFile(join(publicPath, '404.html'));
});
const server = createServer();
server.on('request', (req, res) => {
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
app(req, res);
});
server.on('upgrade', (req, socket, head) => {
if (req.url.endsWith('/wisp/'))
wisp.routeRequest(req, socket, head);
else
socket.end();
});
server.on('listening', () => {
const address = server.address();
// by default we are listening on 0.0.0.0 (every interface)
// we just need to list a few
console.log('Listening on:');
console.log(`\thttp://localhost:${address.port}`);
console.log(`\thttp://${hostname()}:${address.port}`);
console.log(
`\thttp://${address.family === 'IPv6' ? `[${address.address}]` : address.address
}:${address.port}`
);
});
// https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
function shutdown() {
console.log('SIGTERM signal received: closing HTTP server');
server.close((err) => {
if (err) {
console.error('Error during server shutdown:', err);
process.exit(1);
}
console.log('HTTP server closed gracefully');
// Here you can close database connections or other resources
// db.close(() => {
// console.log('Database connection closed');
// process.exit(0);
// });
process.exit(0);
});
}
server.listen({
port,
});