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

Commit

Permalink
Merge pull request #66 from Ruby-Network/dev
Browse files Browse the repository at this point in the history
v1.0.3 (Adding Rammerhead!)
  • Loading branch information
MotorTruck1221 authored Apr 14, 2023
2 parents 593634e + 51fe435 commit bcc425a
Show file tree
Hide file tree
Showing 18 changed files with 1,719 additions and 58 deletions.
103 changes: 83 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,49 @@ import createBareServer from '@tomphttp/bare-server-node';
import express, { Request, Response, NextFunction } from 'express';
import { createServer } from 'node:http';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
import { join } from 'node:path';
import path, { join } from 'node:path';
import { hostname } from 'node:os';
import cluster from 'cluster';
import os from 'os';
import chalk from 'chalk';
import compression from 'compression'
//@ts-ignore
import { handler as ssrHandler } from './dist/server/entry.mjs';
import path from 'node:path';
const __dirname = path.resolve();
import dotenv from 'dotenv';
import fs from 'fs';
import auth from 'http-auth';
//rammerhead stuff
//@ts-ignore
import createRammerhead from 'rammerhead/src/server/index.js';
const rh = createRammerhead();
const rammerheadScopes = [
'/rammerhead.js',
'/hammerhead.js',
'/transport-worker.js',
'/task.js',
'/iframe-task.js',
'/worker-hammerhead.js',
'/messaging',
'/sessionexists',
'/deletesession',
'/newsession',
'/editsession',
'/needpassword',
'/syncLocalStorage',
'/api/shuffleDict',
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
//END rammerhead specific stuff
//Chalk colors for codes
const error = chalk.bold.red;
const success = chalk.green;
const warning = chalk.yellow;
const info = chalk.blue;
const debug = chalk.magenta;
const boldInfo = chalk.bold.blue;
const debug2 = chalk.cyan;
//END CHALK
dotenv.config();
//getting environment vars
const numCPUs = process.env.CPUS || os.cpus().length;
Expand All @@ -30,7 +62,6 @@ let disableKEY = process.env.KEYDISABLE || 'false';
let educationWebsite = fs.readFileSync(join(__dirname, 'education/index.html'));
let loadingPage = fs.readFileSync(join(__dirname, 'education/load.html'));
const blacklisted: string[] = [];
console.log(uri)
const disableyt: string[] = [];
fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
if (err) {
Expand All @@ -41,22 +72,23 @@ fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
for (let i in lines) blacklisted.push(lines[i]);
});
if (numCPUs > 0 && cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
console.log(debug(`Primary ${process.pid} is running`));
for (let i = 0; i < numCPUs; i++) {
cluster.fork().on('online', () => {
console.log(`Worker ${i + 1} is online`);
console.log(debug2(`Worker ${i + 1} is online`));
});
}
cluster.on('exit', (worker, code, signal) => {
console.log(
console.log(error(
`Worker ${worker.process.pid} died with code: ${code} and signal: ${signal}`
);
console.log(`Starting new worker in it's place`);
));
console.log(warning(`Starting new worker in it's place`));
cluster.fork();
});
} else {
const bare = createBareServer('/bare/');
const app = express();
app.use(compression());
app.use(express.static(join(__dirname, 'dist/client')));
//Server side render middleware for astro
app.use(ssrHandler);
Expand All @@ -75,9 +107,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
try {
if (!req.headers.cookie?.includes('allowads')) {
for (let i in blacklisted)
if (
req.headers['x-bare-host']?.includes(blacklisted[i])
)
if (req.headers['x-bare-host']?.includes(blacklisted[i]))
return res.end('Denied');
}
bare.routeRequest(req, res);
Expand All @@ -89,6 +119,8 @@ if (numCPUs > 0 && cluster.isPrimary) {
res.end();
return;
}
} else if (shouldRouteRh(req)) {
routeRhRequest(req, res);
//@ts-ignore
} else if (req.headers.host === uri) {
app(req, res);
Expand All @@ -114,7 +146,11 @@ if (numCPUs > 0 && cluster.isPrimary) {
url.pathname.includes('/settings') ||
url.pathname.includes('/index') ||
url.pathname.includes('/ruby-assets') ||
url.pathname.includes('/games')
url.pathname.includes('/games') ||
url.pathname.includes('/uv') ||
url.pathname.includes('/aero') ||
url.pathname.includes('/osana') ||
url.pathname.includes('/dip')
) {
return res.end(educationWebsite);
} else {
Expand All @@ -125,7 +161,14 @@ if (numCPUs > 0 && cluster.isPrimary) {
server.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else {
}
else if (shouldRouteRh(req)) {
try {
routeRhUpgrade(req, socket, head);
}
catch (error) {}
}
else {
socket.end();
}
});
Expand Down Expand Up @@ -210,7 +253,6 @@ if (numCPUs > 0 && cluster.isPrimary) {
return;
}
});
// Define the /analytics endpoint
app.use((req, res) => {
res.writeHead(302, {
Location: '/404',
Expand All @@ -219,6 +261,27 @@ if (numCPUs > 0 && cluster.isPrimary) {
return;
});
//!CUSTOM ENDPOINTS END
//RAMMERHEAD FUNCTIONS
//@ts-ignore
function shouldRouteRh(req) {
const RHurl = new URL(req.url, 'http://0.0.0.0');
return (
rammerheadScopes.includes(RHurl.pathname) ||
rammerheadSession.test(RHurl.pathname)
);
}
//@ts-ignore
function routeRhRequest(req, res) {
rh.emit('request', req, res);
}
//@ts-ignore
function routeRhUpgrade(req, socket, head) {
try {
rh.emit('upgrade', req, socket, head);
}
catch (error) {}
}
//END RAMMERHEAD SPECIFIC FUNCTIONS
let port = parseInt(process.env.PORT || '');

if (isNaN(port)) port = 8080;
Expand All @@ -229,13 +292,13 @@ if (numCPUs > 0 && cluster.isPrimary) {
// by default we are listening on 0.0.0.0 (every interface)
// we just need to list a few
// LIST PID
console.log(`Process id: ${process.pid}`);
console.log('Listening on:');
console.log(success(`Process id: ${process.pid}`));
console.log(debug('Listening on:'));
//@ts-ignore
console.log(`\thttp://localhost:${address.port}`);
console.log(debug2(`\thttp://localhost:${address.port}`));
//@ts-ignore
console.log(`\thttp://${hostname()}:${address.port}`);
console.log(
console.log(debug2(`\thttp://${hostname()}:${address.port}`));
console.log(debug2(
`\thttp://${
//@ts-ignore
address.family === 'IPv6'
Expand All @@ -245,7 +308,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
address.address
//@ts-ignore
}:${address.port}`
);
));
});

server.listen({
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"@tomphttp/bare-server-node": "^1.2.5",
"astro": "^2.1.9",
"astro-robots-txt": "^0.4.1",
"chalk": "^5.2.0",
"compression": "^1.7.4",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"framer-motion": "^10.11.2",
Expand All @@ -41,12 +43,14 @@
"path-to-regexp": "^6.2.1",
"postcss": "^8.4.21",
"prettier": "^2.8.7",
"rammerhead": "https://github.com/Ruby-Network/rammerhead/releases/download/version/rammerhead.tgz",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"tailwindcss": "^3.0.24",
"ts-node": "^10.9.1"
},
"devDependencies": {
"@types/compression": "^1.7.2",
"@types/express": "^4.17.13",
"@types/http-auth": "^4.1.1",
"@types/node": "^18.15.10",
Expand Down
99 changes: 87 additions & 12 deletions public/chrome-tabs/tabbedLogic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let navToggle = document.getElementById('navToggle')
var el = document.querySelector('.chrome-tabs')
var chromeTabs = new ChromeTabs()
let id = 0;
Expand Down Expand Up @@ -27,8 +28,8 @@ var el = document.querySelector('.chrome-tabs')
}
}
}
function updateURL(id) {
let iframeURL = document.getElementById(id).contentWindow.document.getElementById('uv-iframe').contentWindow.location.href
async function updateURL(id) {
let iframeURL = document.getElementById(id).contentWindow.location.href
if (iframeURL.includes('/loading')) {
document.getElementById('url-bar').value = ''
}
Expand All @@ -46,6 +47,9 @@ var el = document.querySelector('.chrome-tabs')
case 'Aero':
iframeURL = iframeURL.split('/go/').slice(1).join('/go/')
break;
case 'Rammerhead':
iframeURL = ''
break;
default:
iframeURL = iframeURL
}
Expand All @@ -59,6 +63,7 @@ var el = document.querySelector('.chrome-tabs')
}
let tabContents = []
function init() {
localStorage.setItem('gamesBypass', 'false')
if (localStorage.getItem('savedTabs') === 'true') {
chromeTabs.removeTab(chromeTabs.activeTabEl);
if (localStorage.getItem('savedTabsLength') === '0') {
Expand Down Expand Up @@ -103,9 +108,7 @@ var el = document.querySelector('.chrome-tabs')
document.getElementById('tabContents').appendChild(iframe)
browserInit(detail.tabEl, iframeid);
iframe.addEventListener('load', function () {
document.getElementById(iframeid).contentWindow.document.getElementById('uv-iframe').addEventListener('load', function () {
window.parent.updateURL(iframeid)
})
updateURL(iframeid)
})
})
function saveTabs() {
Expand Down Expand Up @@ -142,7 +145,7 @@ var el = document.querySelector('.chrome-tabs')
let URLBAR = document.getElementById('url-bar')
let iframeSRC;
try {
iframeSRC = document.getElementById(id).contentWindow.document.getElementById('uv-iframe').contentWindow.location.href
iframeSRC = document.getElementById(id).contentWindow.location.href
}
catch (err) {
console.log('No content to load ignoring')
Expand All @@ -162,6 +165,9 @@ var el = document.querySelector('.chrome-tabs')
case 'Aero':
iframeSRC = iframeSRC.split('/go/').slice(1).join('/go/')
break;
case 'Rammerhead':
iframeSRC = ''
break;
default:
iframeSRC = iframeSRC
}
Expand All @@ -173,8 +179,11 @@ var el = document.querySelector('.chrome-tabs')
}
}
function browserSearch(value) {
document.getElementById(currentTab).contentWindow.document.getElementById('uv-address').value = value
document.getElementById(currentTab).contentWindow.document.getElementById('uv-form').dispatchEvent(new Event('submit'))
document.getElementById(currentTab).contentWindow.location.href = '/tabbedSearch'
document.getElementById(currentTab).addEventListener('load', function () {
document.getElementById(currentTab).contentWindow.document.getElementById('uv-address').value = value
document.getElementById(currentTab).contentWindow.document.getElementById('uv-form').dispatchEvent(new Event('submit'))
})
}
function decode(str) {
if (str.charAt(str.length - 1) == "/") str = str.slice(0, -1);
Expand Down Expand Up @@ -256,7 +265,7 @@ var el = document.querySelector('.chrome-tabs')

function popOut() {
try {
let SRC = document.getElementById(currentTab).contentWindow.document.getElementById('uv-iframe').contentWindow.location.href
let SRC = document.getElementById(currentTab).contentWindow.location.href
if (SRC.includes('/loading')) {
throw ('LOL')
}
Expand All @@ -272,14 +281,80 @@ var el = document.querySelector('.chrome-tabs')
console.log('To be implemented')
}
function Refresh() {
document.getElementById(currentTab).contentWindow.refreshIframe()
document.getElementById(currentTab).contentWindow.location.reload()
}
function Forward() {
document.getElementById(currentTab).contentWindow.forwardIframe()
document.getElementById(currentTab).contentWindow.history.forward()
}
function Backward() {
document.getElementById(currentTab).contentWindow.backIframe()
document.getElementById(currentTab).contentWindow.history.back()
}
navToggle.addEventListener('mouseover', function () {
let fullscreenBehavior = localStorage.getItem('fullscreenBehavior');
if (fullscreenBehavior === 'true') {
fullScreenIframe(false);
} else if (fullscreenBehavior === 'false') {
fullScreenIframe(false, 'content');
}
});
function fullScreenIframe(value, fullscreenBehavior) {
if (fullscreenBehavior === 'content') {
if (value === true) {
let iframe = document.getElementById('uv-iframe');
iframe.requestFullscreen();
navToggle.classList.remove('dnone');
} else if (value === false) {
document.exitFullscreen();
navToggle.classList.add('dnone');
}
} else {
if (value === true) {
let iframe = document.getElementById(currentTab);
document.getElementById('hamburger').classList.add('dnone')
navToggle.classList.remove('dnone');
//set to position absolute
iframe.style.position = 'absolute';
//set to top left corner
iframe.style.top = '0';
iframe.style.left = '0';
//set to full width and height
iframe.style.width = '100%';
iframe.style.height = '100%';
//set z-index to 9999
iframe.style.zIndex = '9998';
//add a transition
iframe.style.transition = 'all 0.5s ease-in-out';
} else if (value === false) {
let iframe = document.getElementById(currentTab);
document.getElementById('hamburger').classList.remove('dnone')
navToggle.classList.add('dnone');
//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);
iframe.style.height = 'calc(100% - 86px)';
iframe.style.width = '100%';
iframe.style.border = 'none';
iframe.style.position = 'fixed';
iframe.style.top = '86px';
iframe.style.right = '0';
iframe.style.left = '0';
iframe.style.bottom = '0';
iframe.style.border = 'none';
iframe.style.background = 'var(--bg-color)';
iframe.style.transition = 'all 0.5s ease-in-out';
iframe.style.zIndex = '9999';
}
}
}
function fullscreenIframe() {
let fullscreenBehavior = localStorage.getItem('fullscreenBehavior');
if (fullscreenBehavior === 'true') {
fullScreenIframe(true);
} else if (fullscreenBehavior === 'false') {
fullScreenIframe(true, 'content');
} else {
localStorage.setItem('fullscreenBehavior', 'true');
fullScreenIframe(true);
}
}

init();
initApps();
Loading

0 comments on commit bcc425a

Please sign in to comment.