Skip to content

Commit

Permalink
fix a few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
browser-vm committed Feb 7, 2025
1 parent 6c8abd4 commit 23a7a0d
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 30 deletions.
110 changes: 109 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
"author": "Alexander Patrick Scott",
"license": "MIT",
"dependencies": {
"@mercuryworkshop/bare-mux": "^2.1.7",
"@mercuryworkshop/epoxy-transport": "^2.1.27",
"@titaniumnetwork-dev/ultraviolet": "^1.0.11",
"entities": "5.0.0",
"express": "5.0.1"
"express": "^5.0.1",
"ultraviolet-static": "^1.0.2",
"wisp-server-node": "^1.1.7"
},
"devDependencies": {
"nodemon": "3.0.0"
Expand Down
85 changes: 57 additions & 28 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,70 @@
import express from 'express';
import { createServer } from 'node:http';
import Ultraviolet from '@titaniumnetwork-dev/ultraviolet';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet/dist';
import { join } from 'path';
import { hostname } from 'os';
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';

const app = express();
const httpServer = createServer(app);
const port = process.env.PORT || 8080;

// Serve static Ultraviolet files
// 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));

// Error for everything else
app.use((req, res) => {
res.status(404);
res.sendFile(join(publicPath, '404.html'));
});

// Serve your static frontend files
app.use(express.static('public'));
const server = createServer();

const uv = new Ultraviolet({
prefix: '/service/',
bare: '/bare/',
encodeUrl: Ultraviolet.codec.xor.encode,
decodeUrl: Ultraviolet.codec.xor.decode,
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();
});

// 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');
}
let port = parseInt(process.env.PORT || '');

if (isNaN(port)) port = 8080;

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}`
);
});

app.use('/service/', uv.middleware());
// 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();
process.exit(0);
}

httpServer.listen(port, () => {
console.log(`Ultraviolet Proxy is running on http://${hostname()}:${port}`);
server.listen({
port,
});

0 comments on commit 23a7a0d

Please sign in to comment.