Jonell01 commited on
Commit
f83a9bb
·
verified ·
1 Parent(s): 4cdad8e

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +208 -0
index.js ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { exec, spawn } = require('child_process');
5
+ const axios = require('axios');
6
+ const compression = require('compression');
7
+ const socketIo = require('socket.io');
8
+ const rateLimit = require('express-rate-limit');
9
+ const cors = require('cors');
10
+ const app = express();
11
+ const port = process.env.PORT || 7860;
12
+ const routesPath = path.join(__dirname, 'routes');
13
+ const apiRoutes = [];
14
+ const requiredModules = new Set();
15
+ const server = require('http').createServer(app);
16
+ const io = socketIo(server);
17
+ app.use(compression());
18
+ app.use(cors());
19
+ const MASTER_SECRET = "jonell10";
20
+
21
+ app.use(express.json());
22
+ app.use(express.static(path.join(__dirname, 'public')));
23
+
24
+ let limiter;
25
+ const resetLimiter = () => {
26
+ limiter = rateLimit({
27
+ windowMs: 60 * 1000,
28
+ max: 10,
29
+ handler: (req, res) => {
30
+ res.status(429).sendFile(path.join(__dirname, 'public', 'detected.html'));
31
+ }
32
+ });
33
+ };
34
+
35
+ resetLimiter();
36
+ app.use((req, res, next) => {
37
+ const clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
38
+ console.log(`Request from IP: ${clientIp}, URL: ${req.originalUrl}`);
39
+ next();
40
+ }, limiter);
41
+
42
+ let requestCount = 0;
43
+ const requestsFilePath = path.join(__dirname, 'requests.txt');
44
+ if (fs.existsSync(requestsFilePath)) {
45
+ const data = fs.readFileSync(requestsFilePath, 'utf8');
46
+ const requestObj = JSON.parse(data);
47
+ requestCount = requestObj.count || 0;
48
+ }
49
+ app.use((req, res, next) => {
50
+ requestCount++;
51
+ fs.writeFile(requestsFilePath, JSON.stringify({ count: requestCount }), err => {
52
+ if (err) console.error('Error writing to requests.txt:', err);
53
+ });
54
+ io.emit('updateRequestCount', requestCount);
55
+ next();
56
+ });
57
+
58
+ const installModule = (moduleName) => {
59
+ return new Promise((resolve, reject) => {
60
+ exec(`npm install ${moduleName}`, (error, stdout, stderr) => {
61
+ if (error) reject(`Error installing module ${moduleName}: ${stderr}`);
62
+ else resolve(`Module ${moduleName} installed successfully.`);
63
+ });
64
+ });
65
+ };
66
+
67
+ const ensureModules = async (moduleNames) => {
68
+ for (const moduleName of moduleNames) {
69
+ try {
70
+ require.resolve(moduleName);
71
+ } catch (e) {
72
+ await installModule(moduleName);
73
+ }
74
+ }
75
+ };
76
+
77
+ const loadRoutes = async () => {
78
+ apiRoutes.length = 0;
79
+ const files = fs.readdirSync(routesPath);
80
+ for (const file of files) {
81
+ if (file.endsWith('.js')) {
82
+ const apiPath = path.join(routesPath, file);
83
+ delete require.cache[require.resolve(apiPath)];
84
+ const api = require(apiPath);
85
+
86
+ const moduleMatches = api.toString().match(/require\(['"](.+?)['"]\)/g) || [];
87
+ moduleMatches.forEach(match => {
88
+ const moduleName = match.match(/require\(['"](.+?)['"]\)/)[1];
89
+ requiredModules.add(moduleName);
90
+ });
91
+
92
+ const method = api.routes.method.toLowerCase();
93
+ apiRoutes.push({
94
+ name: api.routes.name,
95
+ desc: api.routes.desc,
96
+ usages: api.routes.usages,
97
+ method: api.routes.method,
98
+ category: api.routes.category,
99
+ query: api.routes.query
100
+ });
101
+
102
+ app[method](api.routes.usages, api.onAPI);
103
+ }
104
+ }
105
+
106
+ await ensureModules(Array.from(requiredModules));
107
+ };
108
+
109
+ app.get('/requests', (req, res) => {
110
+ fs.readFile(requestsFilePath, 'utf8', (err, data) => {
111
+ if (err) {
112
+ console.error('Error reading requests.txt:', err);
113
+ res.status(500).json({ error: 'Error reading request count' });
114
+ } else {
115
+ const requestObj = JSON.parse(data);
116
+ res.json({ request: requestObj.count });
117
+ }
118
+ });
119
+ });
120
+
121
+ const saveModule = async (url, name, secret) => {
122
+ const response = await axios.get(url);
123
+ const modulePath = path.join(routesPath, `${name}.js`);
124
+ fs.writeFileSync(modulePath, response.data.replace('const secret = "";', `const secret = "${secret}";`));
125
+ };
126
+
127
+ const removeModule = (name) => {
128
+ const modulePath = path.join(routesPath, `${name}.js`);
129
+ if (fs.existsSync(modulePath)) fs.unlinkSync(modulePath);
130
+ else throw new Error(`Module ${name} does not exist`);
131
+ };
132
+
133
+ app.get('/add/modules', async (req, res) => {
134
+ const { url, name, secret } = req.query;
135
+ if (!url || !name || !secret) return res.status(400).send('Missing required parameters');
136
+ try {
137
+ await saveModule(url, name, secret);
138
+ await loadRoutes();
139
+ res.send(`Module ${name} added and loaded successfully`);
140
+ } catch (error) {
141
+ res.status(500).send(`Error adding module ${name}: ${error.message}`);
142
+ }
143
+ });
144
+
145
+ app.get('/remove/modules', async (req, res) => {
146
+ const { name, secret } = req.query;
147
+ if (!name || !secret) return res.status(400).send('Missing required parameters');
148
+ if (secret !== MASTER_SECRET) return res.status(403).send('Forbidden: Incorrect secret key');
149
+ try {
150
+ removeModule(name);
151
+ await loadRoutes();
152
+ res.send(`Module ${name} removed successfully`);
153
+ } catch (error) {
154
+ res.status(500).send(`Error removing module ${name}: ${error.message}`);
155
+ }
156
+ });
157
+
158
+ app.get('/shell', (req, res) => {
159
+ const { command, secret } = req.query;
160
+ if (!command || secret !== MASTER_SECRET) return res.status(403).send('Forbidden: Incorrect secret key or missing command');
161
+ exec(command, (error, stdout, stderr) => {
162
+ if (error) return res.status(500).send(`Error: ${error.message}`);
163
+ if (stderr) return res.status(500).send(`Stderr: ${stderr}`);
164
+ res.send(`Output: ${stdout}`);
165
+ });
166
+ });
167
+
168
+ app.get('/routes', (req, res) => {
169
+ const { name, secret } = req.query;
170
+ if (secret !== MASTER_SECRET) return res.status(403).json({ error: 'Forbidden: Incorrect secret key' });
171
+ const routeFilePath = path.join(routesPath, `${name}.js`);
172
+ if (!fs.existsSync(routeFilePath)) return res.status(404).json({ error: 'Route not found' });
173
+ const code = fs.readFileSync(routeFilePath, 'utf8');
174
+ res.json({ code });
175
+ });
176
+
177
+ const startServer = async () => {
178
+ await loadRoutes();
179
+ app.get('/jonellmagallanes', (req, res) => res.json(apiRoutes));
180
+ app.use((req, res) => res.status(404).sendFile(path.join(__dirname, 'public', '404.html')));
181
+ function startBot() {
182
+ const child = spawn("node", ["--trace-warnings", "--async-stack-traces", ""], {
183
+ cwd: __dirname,
184
+ stdio: "inherit",
185
+ shell: true
186
+ });
187
+ child.on("close", (codeExit) => {
188
+ console.log(`Bot process exited with code: ${codeExit}`);
189
+ if (codeExit !== 0) setTimeout(startBot, 3000);
190
+ });
191
+ child.on("error", (error) => console.error(`An error occurred starting the bot: ${error}`));
192
+ }
193
+ startBot();
194
+ app.listen(port, () => console.log(`Server is running on http://localhost:${port}`));
195
+ };
196
+
197
+ startServer().catch(error => console.error('Error during server setup:', error));
198
+
199
+ // Add this at the end of the file
200
+ process.on('unhandledRejection', (reason, promise) => {
201
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
202
+ // Keep the server running
203
+ });
204
+
205
+ process.on('uncaughtException', (error) => {
206
+ console.error('Uncaught Exception thrown:', error);
207
+ // Keep the server running
208
+ });