forked from Ruby-Network/ruby-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
200 lines (191 loc) · 6.3 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
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();
const numCPUs = os.cpus().length;
let educationWebsite = fs.readFileSync(join(__dirname, 'education/index.html'));
const blacklisted: 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 (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);
app.use('/uv/', express.static(uvPath));
//env vars for the unlock feature
let key = process.env.KEY || '';
if (!key || key === undefined || key === null || key === '') {
key = 'unlock';
}
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 {
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;
}
} else if (req.headers.host === 'rubynetwork.tech') {
app(req, res);
} else if (
url.search === `?${key}` &&
!req.headers.cookie?.includes(key)
) {
res.writeHead(302, {
Location: '/',
'Set-Cookie': `key=${key}; Path=/`,
});
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.sendFile(join(__dirname, 'education/load.html'));
});
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,
});
}