@@ -2,17 +2,49 @@ import createBareServer from '@tomphttp/bare-server-node';
2
2
import express , { Request , Response , NextFunction } from 'express' ;
3
3
import { createServer } from 'node:http' ;
4
4
import { uvPath } from '@titaniumnetwork-dev/ultraviolet' ;
5
- import { join } from 'node:path' ;
5
+ import path , { join } from 'node:path' ;
6
6
import { hostname } from 'node:os' ;
7
7
import cluster from 'cluster' ;
8
8
import os from 'os' ;
9
+ import chalk from 'chalk' ;
10
+ import compression from 'compression'
9
11
//@ts -ignore
10
12
import { handler as ssrHandler } from './dist/server/entry.mjs' ;
11
- import path from 'node:path' ;
12
13
const __dirname = path . resolve ( ) ;
13
14
import dotenv from 'dotenv' ;
14
15
import fs from 'fs' ;
15
16
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 - 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
16
48
dotenv . config ( ) ;
17
49
//getting environment vars
18
50
const numCPUs = process . env . CPUS || os . cpus ( ) . length ;
@@ -30,7 +62,6 @@ let disableKEY = process.env.KEYDISABLE || 'false';
30
62
let educationWebsite = fs . readFileSync ( join ( __dirname , 'education/index.html' ) ) ;
31
63
let loadingPage = fs . readFileSync ( join ( __dirname , 'education/load.html' ) ) ;
32
64
const blacklisted : string [ ] = [ ] ;
33
- console . log ( uri )
34
65
const disableyt : string [ ] = [ ] ;
35
66
fs . readFile ( join ( __dirname , 'blocklists/ADS.txt' ) , ( err , data ) => {
36
67
if ( err ) {
@@ -41,22 +72,23 @@ fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
41
72
for ( let i in lines ) blacklisted . push ( lines [ i ] ) ;
42
73
} ) ;
43
74
if ( numCPUs > 0 && cluster . isPrimary ) {
44
- console . log ( `Primary ${ process . pid } is running` ) ;
75
+ console . log ( debug ( `Primary ${ process . pid } is running` ) ) ;
45
76
for ( let i = 0 ; i < numCPUs ; i ++ ) {
46
77
cluster . fork ( ) . on ( 'online' , ( ) => {
47
- console . log ( `Worker ${ i + 1 } is online` ) ;
78
+ console . log ( debug2 ( `Worker ${ i + 1 } is online` ) ) ;
48
79
} ) ;
49
80
}
50
81
cluster . on ( 'exit' , ( worker , code , signal ) => {
51
- console . log (
82
+ console . log ( error (
52
83
`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` ) ) ;
55
86
cluster . fork ( ) ;
56
87
} ) ;
57
88
} else {
58
89
const bare = createBareServer ( '/bare/' ) ;
59
90
const app = express ( ) ;
91
+ app . use ( compression ( ) ) ;
60
92
app . use ( express . static ( join ( __dirname , 'dist/client' ) ) ) ;
61
93
//Server side render middleware for astro
62
94
app . use ( ssrHandler ) ;
@@ -75,9 +107,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
75
107
try {
76
108
if ( ! req . headers . cookie ?. includes ( 'allowads' ) ) {
77
109
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 ] ) )
81
111
return res . end ( 'Denied' ) ;
82
112
}
83
113
bare . routeRequest ( req , res ) ;
@@ -89,6 +119,8 @@ if (numCPUs > 0 && cluster.isPrimary) {
89
119
res . end ( ) ;
90
120
return ;
91
121
}
122
+ } else if ( shouldRouteRh ( req ) ) {
123
+ routeRhRequest ( req , res ) ;
92
124
//@ts -ignore
93
125
} else if ( req . headers . host === uri ) {
94
126
app ( req , res ) ;
@@ -114,7 +146,11 @@ if (numCPUs > 0 && cluster.isPrimary) {
114
146
url . pathname . includes ( '/settings' ) ||
115
147
url . pathname . includes ( '/index' ) ||
116
148
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' )
118
154
) {
119
155
return res . end ( educationWebsite ) ;
120
156
} else {
@@ -125,7 +161,14 @@ if (numCPUs > 0 && cluster.isPrimary) {
125
161
server . on ( 'upgrade' , ( req , socket , head ) => {
126
162
if ( bare . shouldRoute ( req ) ) {
127
163
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 {
129
172
socket . end ( ) ;
130
173
}
131
174
} ) ;
@@ -210,7 +253,6 @@ if (numCPUs > 0 && cluster.isPrimary) {
210
253
return ;
211
254
}
212
255
} ) ;
213
- // Define the /analytics endpoint
214
256
app . use ( ( req , res ) => {
215
257
res . writeHead ( 302 , {
216
258
Location : '/404' ,
@@ -219,6 +261,27 @@ if (numCPUs > 0 && cluster.isPrimary) {
219
261
return ;
220
262
} ) ;
221
263
//!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
222
285
let port = parseInt ( process . env . PORT || '' ) ;
223
286
224
287
if ( isNaN ( port ) ) port = 8080 ;
@@ -229,13 +292,13 @@ if (numCPUs > 0 && cluster.isPrimary) {
229
292
// by default we are listening on 0.0.0.0 (every interface)
230
293
// we just need to list a few
231
294
// 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:' ) ) ;
234
297
//@ts -ignore
235
- console . log ( `\thttp://localhost:${ address . port } ` ) ;
298
+ console . log ( debug2 ( `\thttp://localhost:${ address . port } ` ) ) ;
236
299
//@ts -ignore
237
- console . log ( `\thttp://${ hostname ( ) } :${ address . port } ` ) ;
238
- console . log (
300
+ console . log ( debug2 ( `\thttp://${ hostname ( ) } :${ address . port } ` ) ) ;
301
+ console . log ( debug2 (
239
302
`\thttp://${
240
303
//@ts -ignore
241
304
address . family === 'IPv6'
@@ -245,7 +308,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
245
308
address . address
246
309
//@ts -ignore
247
310
} :${ address . port } `
248
- ) ;
311
+ ) ) ;
249
312
} ) ;
250
313
251
314
server . listen ( {
0 commit comments