@@ -2,17 +2,49 @@ import createBareServer from '@tomphttp/bare-server-node';
22import express , { Request , Response , NextFunction } from 'express' ;
33import { createServer } from 'node:http' ;
44import { uvPath } from '@titaniumnetwork-dev/ultraviolet' ;
5- import { join } from 'node:path' ;
5+ import path , { join } from 'node:path' ;
66import { hostname } from 'node:os' ;
77import cluster from 'cluster' ;
88import os from 'os' ;
9+ import chalk from 'chalk' ;
10+ import compression from 'compression'
911//@ts -ignore
1012import { handler as ssrHandler } from './dist/server/entry.mjs' ;
11- import path from 'node:path' ;
1213const __dirname = path . resolve ( ) ;
1314import dotenv from 'dotenv' ;
1415import fs from 'fs' ;
1516import 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 - z 0 - 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
1648dotenv . config ( ) ;
1749//getting environment vars
1850const numCPUs = process . env . CPUS || os . cpus ( ) . length ;
@@ -30,7 +62,6 @@ let disableKEY = process.env.KEYDISABLE || 'false';
3062let educationWebsite = fs . readFileSync ( join ( __dirname , 'education/index.html' ) ) ;
3163let loadingPage = fs . readFileSync ( join ( __dirname , 'education/load.html' ) ) ;
3264const blacklisted : string [ ] = [ ] ;
33- console . log ( uri )
3465const disableyt : string [ ] = [ ] ;
3566fs . 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} ) ;
4374if ( 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 ( {
0 commit comments