diff --git a/server.js b/server.js index 50e8268..911fb39 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ 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'; @@ -14,11 +15,18 @@ app.use('/uv/', express.static(uvPath)); // Serve your static frontend files app.use(express.static('public')); +const uv = new Ultraviolet({ + prefix: '/service/', + bare: '/bare/', + encodeUrl: Ultraviolet.codec.xor.encode, + decodeUrl: Ultraviolet.codec.xor.decode, +}); + // Generate proxied URL app.get('/generate-proxy-url', (req, res) => { const serviceUrl = req.query.url; if (serviceUrl) { - const encodedUrl = Buffer.from(serviceUrl).toString('base64'); + const encodedUrl = uv.encodeUrl(serviceUrl); const proxyUrl = `http://${hostname()}:${port}/service/${encodedUrl}`; res.send(proxyUrl); } else { @@ -26,12 +34,7 @@ app.get('/generate-proxy-url', (req, res) => { } }); -// Ultraviolet handler -app.use('/service/*', (req, res) => { - const urlToProxy = Buffer.from(req.url.slice(1), 'base64').toString('utf-8'); - req.url = urlToProxy; - uvPath.requireHandle()(req, res); -}); +app.use('/service/', uv.middleware()); httpServer.listen(port, () => { console.log(`Ultraviolet Proxy is running on http://${hostname()}:${port}`); diff --git a/uv.config.js b/uv.config.js index d3da480..98dbd9d 100644 --- a/uv.config.js +++ b/uv.config.js @@ -1,4 +1,4 @@ -self.__uv$config = { +const uvConfig = { prefix: '/service/', bare: '/bare/', encodeUrl: Ultraviolet.codec.xor.encode, @@ -7,4 +7,6 @@ self.__uv$config = { bundle: '/dist/uv.bundle.js', config: '/dist/uv.config.js', sw: '/dist/uv.sw.js', -}; \ No newline at end of file +}; + +export default uvConfig; \ No newline at end of file