-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c8abd4
commit 23a7a0d
Showing
3 changed files
with
171 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |