Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,24 +15,26 @@ 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 {
res.status(400).send('No URL provided');
}
});

// 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}`);
Expand Down
6 changes: 4 additions & 2 deletions uv.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
self.__uv$config = {
const uvConfig = {
prefix: '/service/',
bare: '/bare/',
encodeUrl: Ultraviolet.codec.xor.encode,
Expand All @@ -7,4 +7,6 @@ self.__uv$config = {
bundle: '/dist/uv.bundle.js',
config: '/dist/uv.config.js',
sw: '/dist/uv.sw.js',
};
};

export default uvConfig;