Skip to content

Commit

Permalink
Implement extremely messy makeshift source randomization. Will come b…
Browse files Browse the repository at this point in the history
…ack to this after a potential indefinitely long hiatus.
  • Loading branch information
00Fjongl committed Aug 10, 2024
1 parent a7b7c52 commit 445ee61
Show file tree
Hide file tree
Showing 11 changed files with 2,077 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"title": "HU LTS",
"host": "0.0.0.0",
"minifyScripts": true,
"randomizeIdentifiers": true,
"production": false
}
9 changes: 7 additions & 2 deletions src/randomization.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pkg from './routes.mjs';
import { existsSync, readFileSync } from 'fs';
import { fileURLToPath } from 'node:url';
export { paintSource, preloaded404, tryReadFile };
const {
cookingInserts,
Expand Down Expand Up @@ -41,8 +42,12 @@ const randomListItem = (lis) => () => lis[(Math.random() * lis.length) | 0],
// Use this instead of text404 for a preloaded error page.
preloaded404 = paintSource(text404),
// Grab the text content of a file. Ensure the file is a string.
tryReadFile = (file) =>
existsSync(file + '') ? readFileSync(file + '', 'utf8') : preloaded404;
tryReadFile = (file, baseUrl) => {
file = fileURLToPath(new URL(file, baseUrl));
return existsSync(file + '')
? readFileSync(file + '', 'utf8')
: preloaded404;
};

/*
Expand Down
79 changes: 66 additions & 13 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ app.register(fastifyStatic, {

app.register(fastifyStatic, {
root: fileURLToPath(new URL('../views/archive', import.meta.url)),
prefix: '/arcade/',
prefix: '/archive/',
decorateReply: false,
});

Expand Down Expand Up @@ -233,12 +233,10 @@ app.get('/:path', (req, reply) => {
paintSource(
loadTemplates(
tryReadFile(
path.join(
__dirname,
'views',
'../views/' +
// Set the index the as the default page.
reqPath ? pages[reqPath] : pages.index
)
(reqPath ? pages[reqPath] : pages.index),
import.meta.url
)
)
)
Expand All @@ -251,15 +249,70 @@ app.get('/github/:redirect', (req, reply) => {
else reply.code(404).type('text/html').send(preloaded404);
});

/*
Testing for future restructuring of this config file.
const encodingTable = (() => {
let yummyOneBytes = '';
for (let i = 0; i < 128; i++)
if (
JSON.stringify(JSON.stringify(String.fromCodePoint(i)).slice(1, -1))
.length < 6
)
yummyOneBytes += String.fromCodePoint(i);
return yummyOneBytes;
})(),
randomValue = crypto
.randomUUID()
.split('-')
.map((gibberish) => {
let randomNumber = parseInt(gibberish, 16),
output = '';
while (randomNumber >= encodingTable.length) {
output +=
encodingTable[Math.floor(randomNumber) % encodingTable.length];
randomNumber = randomNumber / encodingTable.length;
}
return output + Math.floor(randomNumber);
})
.join(''),
randomizeGlobal = config.randomizeIdentifiers
? (file) =>
tryReadFile(file, import.meta.url).replace(
/(["'`])\{\{__uv\$config\}\}\1/g,
JSON.stringify(randomValue)
)
: (file) =>
tryReadFile(file, import.meta.url).replace(
/(["'`])\{\{__uv\$config\}\}\1/g,
JSON.stringify('__uv$config')
);

app.get('/assets/js/common-16451543478.js', (req, reply) => {
reply
.type('text/javascript')
.send(
randomizeGlobal(
'../views' + (config.minifyScripts ? '/dist' : '') + req.url
)
);
});

app.get("/assets/js/uv/uv.config.js", (req, reply) => {
console.log(req.url);
reply.type("text/javascript");
reply.send(tryReadFile(path.join(__dirname, "views/assets/js/uv/uv.config.js")));
app.get('/uv/:file.js', (req, reply) => {
const destination = existsSync(new URL('../views' + req.url, import.meta.url))
? '../views' + (config.minifyScripts ? '/dist' : '') + req.url
: uvPath + '/' + req.params.file + '.js';
reply
.type('text/javascript')
.send(
randomizeGlobal(destination).replace(
/(["'`])\{\{ultraviolet-error\}\}\1/g,
JSON.stringify(
tryReadFile(
'../views/pages/proxnav/ultraviolet-error.html',
import.meta.url
)
)
)
);
});
*/

// Set an error page for invalid paths outside the query string system.
app.setNotFoundHandler((req, reply) => {
Expand Down
15 changes: 7 additions & 8 deletions src/templates.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { tryReadFile } from './randomization.mjs';
import path from 'path';
export { loadTemplates as default };

const __dirname = path.resolve() + '/views/pages/misc/deobf';
const __dirname = '../views/pages/misc/deobf';

const header = tryReadFile(path.normalize(__dirname + '/header.html')),
footer = tryReadFile(path.normalize(__dirname + '/footer.html')),
documentation = tryReadFile(path.normalize(__dirname + '/docs.html')),
faq = tryReadFile(path.normalize(__dirname + '/faq.html')),
terms = tryReadFile(path.normalize(__dirname + '/tos.html')),
settings = tryReadFile(path.normalize(__dirname + '/settings.html')),
const header = tryReadFile(__dirname + '/header.html', import.meta.url),
footer = tryReadFile(__dirname + '/footer.html', import.meta.url),
documentation = tryReadFile(__dirname + '/docs.html', import.meta.url),
faq = tryReadFile(__dirname + '/faq.html', import.meta.url),
terms = tryReadFile(__dirname + '/tos.html', import.meta.url),
settings = tryReadFile(__dirname + '/settings.html', import.meta.url),
loadTemplates = (str) =>
str
.replace('<!--HEADER-->', header)
Expand Down
12 changes: 9 additions & 3 deletions views/assets/js/common-16451543478.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// Encase everything in a new scope so that variables are not accidentally
// attached to the global scope.
(() => {
// To be defined after the document has fully loaded.
let uvConfig = {};
// Get the preferred apex domain name. Not exactly apex, as any
// subdomain other than those listed will be ignored.
const getDomain = () =>
Expand Down Expand Up @@ -98,8 +100,8 @@ const sx = 'bing.com' + '/search?q=',
try {
url =
location.origin +
__uv$config.prefix +
__uv$config.encodeUrl(search(url));
uvConfig.prefix +
uvConfig.encodeUrl(search(url));
} catch (e) {
// This is for cases where the Ultraviolet scripts have not been loaded.
url = search(url);
Expand Down Expand Up @@ -352,6 +354,10 @@ const RammerheadEncode = async (baseUrl) => {
* goProx.searx();
*/
addEventListener('DOMContentLoaded', async () => {
// This won't break the service workers as they store the variable separately.
uvConfig = self['{{__uv$config}}'];
delete self['{{__uv$config}}'];

// Object.freeze prevents goProx from accidentally being edited.
const goProx = Object.freeze({
// `location.protocol + "//" + getDomain()` more like `location.origin`
Expand Down Expand Up @@ -495,7 +501,7 @@ addEventListener('DOMContentLoaded', async () => {
clickHandler = (parser, a) => (e) => {
if (e.target == a || e.target.tagName != 'A') {
e.preventDefault();
parser(item);
parser();
}
};

Expand Down
46 changes: 46 additions & 0 deletions views/pages/proxnav/ultraviolet-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Error</title>
<style>
* {
background-color: white;
}
</style>
</head>
<body>
<h1 id="errorTitle">Error processing your request</h1>
<hr />
<p>Failed to load <b id="fetchedURL"></b></p>
<p id="errorMessage">Internal Server Error</p>
<textarea id="errorTrace" cols="40" rows="10" readonly></textarea>
<p>Try:</p>
<ul>
<li>Checking your internet connection</li>
<li>Verifying you entered the correct address</li>
<li>Clearing the site data</li>
<li>Contacting <b id="uvHostname"></b>'s administrator</li>
<li>Verify the server isn't censored</li>
</ul>
<p>If you're the administrator of <b id="uvHostname"></b>, try:</p>
<ul>
<li>Restarting your server</li>
<li>Updating Ultraviolet</li>
<li>
Troubleshooting the error on the
<a
href="/github/ultraviolet"
target="_blank"
>GitHub repository</a
>
</li>
</ul>
<button id="reload">Reload</button>
<hr />
<p>
<i>Ultraviolet v<span id="uvVersion"></span></i>
</p>
<script src={{src}}></script>
</body>
</html>
2 changes: 1 addition & 1 deletion views/uv/sw-blacklist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
importScripts('/uv/uv.bundle.js');
importScripts('/uv/uv.config.js');
importScripts(__uv$config.sw || '/uv/uv.sw.js');
importScripts(self['{{__uv$config}}'].sw || '/uv/uv.sw.js');

/*
Expand Down
2 changes: 1 addition & 1 deletion views/uv/sw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
importScripts('/uv/uv.bundle.js');
importScripts('/uv/uv.config.js');
importScripts(__uv$config.sw || '/uv/uv.sw.js');
importScripts(self['{{__uv$config}}'].sw || '/uv/uv.sw.js');

const uv = new UVServiceWorker();

Expand Down
2 changes: 1 addition & 1 deletion views/uv/uv.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file overwrites the stock UV config.js

self.__uv$config = {
self['{{__uv$config}}'] = {
prefix: '/uv/service/',
encodeUrl: Ultraviolet.codec.xor.encode,
decodeUrl: Ultraviolet.codec.xor.decode,
Expand Down
Loading

0 comments on commit 445ee61

Please sign in to comment.