forked from Ruby-Network/ruby-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
247 lines (240 loc) · 7.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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 { hostname } from 'node:os';
import cluster from 'cluster';
import os from 'os';
//@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';
dotenv.config();
//getting environment vars
const numCPUs = process.env.CPUS || os.cpus().length;
let key = process.env.KEY || 'unlock';
let url = process.env.URL || 'rubynetwork.tech';
let user = process.env.USERNAME || 'ruby';
let pass = process.env.PASSWORD || 'ruby';
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[] = [];
const disableyt: string[] = [];
fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
if (err) {
console.error(err);
return;
}
const lines = data.toString().split('\n');
for (let i in lines) blacklisted.push(lines[i]);
});
if (numCPUs > 0 && cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork().on('online', () => {
console.log(`Worker ${i + 1} is online`);
});
}
cluster.on('exit', (worker, code, signal) => {
console.log(
`Worker ${worker.process.pid} died with code: ${code} and signal: ${signal}`
);
console.log(`Starting new worker in it's place`);
cluster.fork();
});
} else {
const bare = createBareServer('/bare/');
const app = express();
app.use(express.static(join(__dirname, 'dist/client')));
//Server side render middleware for astro
app.use(ssrHandler);
//express middleware for body
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//uv config
app.use('/uv/', express.static(uvPath));
const server = createServer();
server.on('request', (req, res) => {
//@ts-ignore
const url = new URL(req.url, `http://${req.headers.host}`);
//Get the url search parameters and check if it matches the key from the environment variable
//only block /,/404,/apps,/error,/search,/settings and /index if the key or cookie is not present
if (bare.shouldRoute(req)) {
try {
if (!req.headers.cookie?.includes('allowads')) {
for (let i in blacklisted)
if (
req.headers['x-bare-host']?.includes(blacklisted[i])
)
return res.end('Denied');
}
bare.routeRequest(req, res);
} catch (error) {
console.error(error);
res.writeHead(302, {
Location: '/error',
});
res.end();
return;
}
//@ts-ignore
} else if (req.headers.host === url) {
app(req, res);
} else if (
url.search === `?${key}` &&
!req.headers.cookie?.includes(key) &&
disableKEY === 'false'
) {
res.writeHead(302, {
Location: '/',
'Set-Cookie': `key=${key}; Path=/; expires=Thu, 31 Dec 2099 23:59:59 GMT;`,
});
res.end();
return;
} else if (req.headers.cookie?.includes(key)) {
app(req, res);
} else if (
(!req.headers.cookie?.includes(key) && url.pathname === '/') ||
url.pathname.includes('/404') ||
url.pathname.includes('/apps') ||
url.pathname.includes('/error') ||
url.pathname.includes('/search') ||
url.pathname.includes('/settings') ||
url.pathname.includes('/index') ||
url.pathname.includes('/ruby-assets') ||
url.pathname.includes('/games')
) {
return res.end(educationWebsite);
} else {
app(req, res);
}
});
server.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
//!AUTHENTICATION
const basic = auth.basic({
realm: 'Restricted Access',
file: __dirname + '/users.htpasswd',
});
//!END AUTHENTICATION
//!CUSTOM ENDPOINTS
app.get('/suggest', (req, res) => {
// Get the search query from the query string
const query = req.query.q;
// Make a request to the Brave API
fetch(
`https://search.brave.com/api/suggest?q=${encodeURIComponent(
//@ts-ignore
query
)}&format=json`
)
.then((response) => response.json())
.then((data) => {
// Send the response data back to the browser
res.json(data);
})
.catch((error) => {
// Handle the error
console.error(error);
res.sendStatus(500);
});
});
//@ts-ignore
app.get(
'/pid',
basic.check((req, res) => {
res.end(`Process id: ${process.pid}`);
})
);
app.get(
'/load',
basic.check((req, res) => {
res.end(`Load average: ${os.loadavg()}`);
})
);
app.get('/loading', (req, res) => {
return res.end(loadingPage);
});
app.post('/login-form', (req, res) => {
let body = req.body;
body = JSON.stringify(body);
body = JSON.parse(body);
if (body.username === user && body.password === pass) {
res.writeHead(302, {
location: '/',
'Set-Cookie': `key=${key}; Path=/; expires=Thu, 31 Dec 2099 23:59:59 GMT;`,
});
res.end();
return;
} else {
res.writeHead(401);
res.end(educationWebsite);
return;
}
});
app.get('/disable-ads', (req, res) => {
if (req.headers.cookie?.includes('allowads')) {
res.clearCookie('allowads');
res.writeHead(302, {
Location: '/settings',
});
res.end('Disabled ads');
return;
} else {
res.writeHead(302, {
Location: '/settings',
'Set-Cookie':
'allowads=allowads; Path=/; expires=Thu, 31 Dec 2099 23:59:59 GMT;',
});
res.end('Ads enabled');
return;
}
});
// Define the /analytics endpoint
app.use((req, res) => {
res.writeHead(302, {
Location: '/404',
});
res.end();
return;
});
//!CUSTOM ENDPOINTS END
let port = parseInt(process.env.PORT || '');
if (isNaN(port)) port = 8080;
server.on('listening', () => {
const address = server.address();
// 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:');
//@ts-ignore
console.log(`\thttp://localhost:${address.port}`);
//@ts-ignore
console.log(`\thttp://${hostname()}:${address.port}`);
console.log(
`\thttp://${
//@ts-ignore
address.family === 'IPv6'
? //@ts-ignore
`[${address.address}]`
: //@ts-ignore
address.address
//@ts-ignore
}:${address.port}`
);
});
server.listen({
port,
});
}