Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit bcc425a

Browse files
Merge pull request #66 from Ruby-Network/dev
v1.0.3 (Adding Rammerhead!)
2 parents 593634e + 51fe435 commit bcc425a

File tree

18 files changed

+1719
-58
lines changed

18 files changed

+1719
-58
lines changed

index.ts

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,49 @@ import createBareServer from '@tomphttp/bare-server-node';
22
import express, { Request, Response, NextFunction } from 'express';
33
import { createServer } from 'node:http';
44
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
5-
import { join } from 'node:path';
5+
import path, { join } from 'node:path';
66
import { hostname } from 'node:os';
77
import cluster from 'cluster';
88
import os from 'os';
9+
import chalk from 'chalk';
10+
import compression from 'compression'
911
//@ts-ignore
1012
import { handler as ssrHandler } from './dist/server/entry.mjs';
11-
import path from 'node:path';
1213
const __dirname = path.resolve();
1314
import dotenv from 'dotenv';
1415
import fs from 'fs';
1516
import auth from 'http-auth';
17+
//rammerhead stuff
18+
//@ts-ignore
19+
import createRammerhead from 'rammerhead/src/server/index.js';
20+
const rh = createRammerhead();
21+
const rammerheadScopes = [
22+
'/rammerhead.js',
23+
'/hammerhead.js',
24+
'/transport-worker.js',
25+
'/task.js',
26+
'/iframe-task.js',
27+
'/worker-hammerhead.js',
28+
'/messaging',
29+
'/sessionexists',
30+
'/deletesession',
31+
'/newsession',
32+
'/editsession',
33+
'/needpassword',
34+
'/syncLocalStorage',
35+
'/api/shuffleDict',
36+
];
37+
const rammerheadSession = /^\/[a-z0-9]{32}/;
38+
//END rammerhead specific stuff
39+
//Chalk colors for codes
40+
const error = chalk.bold.red;
41+
const success = chalk.green;
42+
const warning = chalk.yellow;
43+
const info = chalk.blue;
44+
const debug = chalk.magenta;
45+
const boldInfo = chalk.bold.blue;
46+
const debug2 = chalk.cyan;
47+
//END CHALK
1648
dotenv.config();
1749
//getting environment vars
1850
const numCPUs = process.env.CPUS || os.cpus().length;
@@ -30,7 +62,6 @@ let disableKEY = process.env.KEYDISABLE || 'false';
3062
let educationWebsite = fs.readFileSync(join(__dirname, 'education/index.html'));
3163
let loadingPage = fs.readFileSync(join(__dirname, 'education/load.html'));
3264
const blacklisted: string[] = [];
33-
console.log(uri)
3465
const disableyt: string[] = [];
3566
fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
3667
if (err) {
@@ -41,22 +72,23 @@ fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
4172
for (let i in lines) blacklisted.push(lines[i]);
4273
});
4374
if (numCPUs > 0 && cluster.isPrimary) {
44-
console.log(`Primary ${process.pid} is running`);
75+
console.log(debug(`Primary ${process.pid} is running`));
4576
for (let i = 0; i < numCPUs; i++) {
4677
cluster.fork().on('online', () => {
47-
console.log(`Worker ${i + 1} is online`);
78+
console.log(debug2(`Worker ${i + 1} is online`));
4879
});
4980
}
5081
cluster.on('exit', (worker, code, signal) => {
51-
console.log(
82+
console.log(error(
5283
`Worker ${worker.process.pid} died with code: ${code} and signal: ${signal}`
53-
);
54-
console.log(`Starting new worker in it's place`);
84+
));
85+
console.log(warning(`Starting new worker in it's place`));
5586
cluster.fork();
5687
});
5788
} else {
5889
const bare = createBareServer('/bare/');
5990
const app = express();
91+
app.use(compression());
6092
app.use(express.static(join(__dirname, 'dist/client')));
6193
//Server side render middleware for astro
6294
app.use(ssrHandler);
@@ -75,9 +107,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
75107
try {
76108
if (!req.headers.cookie?.includes('allowads')) {
77109
for (let i in blacklisted)
78-
if (
79-
req.headers['x-bare-host']?.includes(blacklisted[i])
80-
)
110+
if (req.headers['x-bare-host']?.includes(blacklisted[i]))
81111
return res.end('Denied');
82112
}
83113
bare.routeRequest(req, res);
@@ -89,6 +119,8 @@ if (numCPUs > 0 && cluster.isPrimary) {
89119
res.end();
90120
return;
91121
}
122+
} else if (shouldRouteRh(req)) {
123+
routeRhRequest(req, res);
92124
//@ts-ignore
93125
} else if (req.headers.host === uri) {
94126
app(req, res);
@@ -114,7 +146,11 @@ if (numCPUs > 0 && cluster.isPrimary) {
114146
url.pathname.includes('/settings') ||
115147
url.pathname.includes('/index') ||
116148
url.pathname.includes('/ruby-assets') ||
117-
url.pathname.includes('/games')
149+
url.pathname.includes('/games') ||
150+
url.pathname.includes('/uv') ||
151+
url.pathname.includes('/aero') ||
152+
url.pathname.includes('/osana') ||
153+
url.pathname.includes('/dip')
118154
) {
119155
return res.end(educationWebsite);
120156
} else {
@@ -125,7 +161,14 @@ if (numCPUs > 0 && cluster.isPrimary) {
125161
server.on('upgrade', (req, socket, head) => {
126162
if (bare.shouldRoute(req)) {
127163
bare.routeUpgrade(req, socket, head);
128-
} else {
164+
}
165+
else if (shouldRouteRh(req)) {
166+
try {
167+
routeRhUpgrade(req, socket, head);
168+
}
169+
catch (error) {}
170+
}
171+
else {
129172
socket.end();
130173
}
131174
});
@@ -210,7 +253,6 @@ if (numCPUs > 0 && cluster.isPrimary) {
210253
return;
211254
}
212255
});
213-
// Define the /analytics endpoint
214256
app.use((req, res) => {
215257
res.writeHead(302, {
216258
Location: '/404',
@@ -219,6 +261,27 @@ if (numCPUs > 0 && cluster.isPrimary) {
219261
return;
220262
});
221263
//!CUSTOM ENDPOINTS END
264+
//RAMMERHEAD FUNCTIONS
265+
//@ts-ignore
266+
function shouldRouteRh(req) {
267+
const RHurl = new URL(req.url, 'http://0.0.0.0');
268+
return (
269+
rammerheadScopes.includes(RHurl.pathname) ||
270+
rammerheadSession.test(RHurl.pathname)
271+
);
272+
}
273+
//@ts-ignore
274+
function routeRhRequest(req, res) {
275+
rh.emit('request', req, res);
276+
}
277+
//@ts-ignore
278+
function routeRhUpgrade(req, socket, head) {
279+
try {
280+
rh.emit('upgrade', req, socket, head);
281+
}
282+
catch (error) {}
283+
}
284+
//END RAMMERHEAD SPECIFIC FUNCTIONS
222285
let port = parseInt(process.env.PORT || '');
223286

224287
if (isNaN(port)) port = 8080;
@@ -229,13 +292,13 @@ if (numCPUs > 0 && cluster.isPrimary) {
229292
// by default we are listening on 0.0.0.0 (every interface)
230293
// we just need to list a few
231294
// LIST PID
232-
console.log(`Process id: ${process.pid}`);
233-
console.log('Listening on:');
295+
console.log(success(`Process id: ${process.pid}`));
296+
console.log(debug('Listening on:'));
234297
//@ts-ignore
235-
console.log(`\thttp://localhost:${address.port}`);
298+
console.log(debug2(`\thttp://localhost:${address.port}`));
236299
//@ts-ignore
237-
console.log(`\thttp://${hostname()}:${address.port}`);
238-
console.log(
300+
console.log(debug2(`\thttp://${hostname()}:${address.port}`));
301+
console.log(debug2(
239302
`\thttp://${
240303
//@ts-ignore
241304
address.family === 'IPv6'
@@ -245,7 +308,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
245308
address.address
246309
//@ts-ignore
247310
}:${address.port}`
248-
);
311+
));
249312
});
250313

251314
server.listen({

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"@tomphttp/bare-server-node": "^1.2.5",
3333
"astro": "^2.1.9",
3434
"astro-robots-txt": "^0.4.1",
35+
"chalk": "^5.2.0",
36+
"compression": "^1.7.4",
3537
"dotenv": "^16.0.3",
3638
"express": "^4.18.2",
3739
"framer-motion": "^10.11.2",
@@ -41,12 +43,14 @@
4143
"path-to-regexp": "^6.2.1",
4244
"postcss": "^8.4.21",
4345
"prettier": "^2.8.7",
46+
"rammerhead": "https://github.com/Ruby-Network/rammerhead/releases/download/version/rammerhead.tgz",
4447
"react": "^18.0.0",
4548
"react-dom": "^18.0.0",
4649
"tailwindcss": "^3.0.24",
4750
"ts-node": "^10.9.1"
4851
},
4952
"devDependencies": {
53+
"@types/compression": "^1.7.2",
5054
"@types/express": "^4.17.13",
5155
"@types/http-auth": "^4.1.1",
5256
"@types/node": "^18.15.10",

public/chrome-tabs/tabbedLogic.js

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
let navToggle = document.getElementById('navToggle')
12
var el = document.querySelector('.chrome-tabs')
23
var chromeTabs = new ChromeTabs()
34
let id = 0;
@@ -27,8 +28,8 @@ var el = document.querySelector('.chrome-tabs')
2728
}
2829
}
2930
}
30-
function updateURL(id) {
31-
let iframeURL = document.getElementById(id).contentWindow.document.getElementById('uv-iframe').contentWindow.location.href
31+
async function updateURL(id) {
32+
let iframeURL = document.getElementById(id).contentWindow.location.href
3233
if (iframeURL.includes('/loading')) {
3334
document.getElementById('url-bar').value = ''
3435
}
@@ -46,6 +47,9 @@ var el = document.querySelector('.chrome-tabs')
4647
case 'Aero':
4748
iframeURL = iframeURL.split('/go/').slice(1).join('/go/')
4849
break;
50+
case 'Rammerhead':
51+
iframeURL = ''
52+
break;
4953
default:
5054
iframeURL = iframeURL
5155
}
@@ -59,6 +63,7 @@ var el = document.querySelector('.chrome-tabs')
5963
}
6064
let tabContents = []
6165
function init() {
66+
localStorage.setItem('gamesBypass', 'false')
6267
if (localStorage.getItem('savedTabs') === 'true') {
6368
chromeTabs.removeTab(chromeTabs.activeTabEl);
6469
if (localStorage.getItem('savedTabsLength') === '0') {
@@ -103,9 +108,7 @@ var el = document.querySelector('.chrome-tabs')
103108
document.getElementById('tabContents').appendChild(iframe)
104109
browserInit(detail.tabEl, iframeid);
105110
iframe.addEventListener('load', function () {
106-
document.getElementById(iframeid).contentWindow.document.getElementById('uv-iframe').addEventListener('load', function () {
107-
window.parent.updateURL(iframeid)
108-
})
111+
updateURL(iframeid)
109112
})
110113
})
111114
function saveTabs() {
@@ -142,7 +145,7 @@ var el = document.querySelector('.chrome-tabs')
142145
let URLBAR = document.getElementById('url-bar')
143146
let iframeSRC;
144147
try {
145-
iframeSRC = document.getElementById(id).contentWindow.document.getElementById('uv-iframe').contentWindow.location.href
148+
iframeSRC = document.getElementById(id).contentWindow.location.href
146149
}
147150
catch (err) {
148151
console.log('No content to load ignoring')
@@ -162,6 +165,9 @@ var el = document.querySelector('.chrome-tabs')
162165
case 'Aero':
163166
iframeSRC = iframeSRC.split('/go/').slice(1).join('/go/')
164167
break;
168+
case 'Rammerhead':
169+
iframeSRC = ''
170+
break;
165171
default:
166172
iframeSRC = iframeSRC
167173
}
@@ -173,8 +179,11 @@ var el = document.querySelector('.chrome-tabs')
173179
}
174180
}
175181
function browserSearch(value) {
176-
document.getElementById(currentTab).contentWindow.document.getElementById('uv-address').value = value
177-
document.getElementById(currentTab).contentWindow.document.getElementById('uv-form').dispatchEvent(new Event('submit'))
182+
document.getElementById(currentTab).contentWindow.location.href = '/tabbedSearch'
183+
document.getElementById(currentTab).addEventListener('load', function () {
184+
document.getElementById(currentTab).contentWindow.document.getElementById('uv-address').value = value
185+
document.getElementById(currentTab).contentWindow.document.getElementById('uv-form').dispatchEvent(new Event('submit'))
186+
})
178187
}
179188
function decode(str) {
180189
if (str.charAt(str.length - 1) == "/") str = str.slice(0, -1);
@@ -256,7 +265,7 @@ var el = document.querySelector('.chrome-tabs')
256265

257266
function popOut() {
258267
try {
259-
let SRC = document.getElementById(currentTab).contentWindow.document.getElementById('uv-iframe').contentWindow.location.href
268+
let SRC = document.getElementById(currentTab).contentWindow.location.href
260269
if (SRC.includes('/loading')) {
261270
throw ('LOL')
262271
}
@@ -272,14 +281,80 @@ var el = document.querySelector('.chrome-tabs')
272281
console.log('To be implemented')
273282
}
274283
function Refresh() {
275-
document.getElementById(currentTab).contentWindow.refreshIframe()
284+
document.getElementById(currentTab).contentWindow.location.reload()
276285
}
277286
function Forward() {
278-
document.getElementById(currentTab).contentWindow.forwardIframe()
287+
document.getElementById(currentTab).contentWindow.history.forward()
279288
}
280289
function Backward() {
281-
document.getElementById(currentTab).contentWindow.backIframe()
290+
document.getElementById(currentTab).contentWindow.history.back()
282291
}
292+
navToggle.addEventListener('mouseover', function () {
293+
let fullscreenBehavior = localStorage.getItem('fullscreenBehavior');
294+
if (fullscreenBehavior === 'true') {
295+
fullScreenIframe(false);
296+
} else if (fullscreenBehavior === 'false') {
297+
fullScreenIframe(false, 'content');
298+
}
299+
});
300+
function fullScreenIframe(value, fullscreenBehavior) {
301+
if (fullscreenBehavior === 'content') {
302+
if (value === true) {
303+
let iframe = document.getElementById('uv-iframe');
304+
iframe.requestFullscreen();
305+
navToggle.classList.remove('dnone');
306+
} else if (value === false) {
307+
document.exitFullscreen();
308+
navToggle.classList.add('dnone');
309+
}
310+
} else {
311+
if (value === true) {
312+
let iframe = document.getElementById(currentTab);
313+
document.getElementById('hamburger').classList.add('dnone')
314+
navToggle.classList.remove('dnone');
315+
//set to position absolute
316+
iframe.style.position = 'absolute';
317+
//set to top left corner
318+
iframe.style.top = '0';
319+
iframe.style.left = '0';
320+
//set to full width and height
321+
iframe.style.width = '100%';
322+
iframe.style.height = '100%';
323+
//set z-index to 9999
324+
iframe.style.zIndex = '9998';
325+
//add a transition
326+
iframe.style.transition = 'all 0.5s ease-in-out';
327+
} else if (value === false) {
328+
let iframe = document.getElementById(currentTab);
329+
document.getElementById('hamburger').classList.remove('dnone')
330+
navToggle.classList.add('dnone');
331+
//set styles to height: calc(100% - 4rem);width: 100%;border: none;position: fixed;top: 4rem;right: 0;left: 0;bottom: 0;border: none; background: var(--bg-color);
332+
iframe.style.height = 'calc(100% - 86px)';
333+
iframe.style.width = '100%';
334+
iframe.style.border = 'none';
335+
iframe.style.position = 'fixed';
336+
iframe.style.top = '86px';
337+
iframe.style.right = '0';
338+
iframe.style.left = '0';
339+
iframe.style.bottom = '0';
340+
iframe.style.border = 'none';
341+
iframe.style.background = 'var(--bg-color)';
342+
iframe.style.transition = 'all 0.5s ease-in-out';
343+
iframe.style.zIndex = '9999';
344+
}
345+
}
346+
}
347+
function fullscreenIframe() {
348+
let fullscreenBehavior = localStorage.getItem('fullscreenBehavior');
349+
if (fullscreenBehavior === 'true') {
350+
fullScreenIframe(true);
351+
} else if (fullscreenBehavior === 'false') {
352+
fullScreenIframe(true, 'content');
353+
} else {
354+
localStorage.setItem('fullscreenBehavior', 'true');
355+
fullScreenIframe(true);
356+
}
357+
}
283358

284359
init();
285360
initApps();

0 commit comments

Comments
 (0)