arabdullah commited on
Commit
de7c795
·
verified ·
1 Parent(s): b0df1e1

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +36 -10
index.js CHANGED
@@ -1,24 +1,50 @@
1
  const express = require('express');
 
 
 
 
2
  const app = express();
3
 
4
- const validApiKeys = ['your_valid_api_key1', 'your_valid_api_key2'];
 
 
 
 
 
 
 
 
 
5
 
6
- // Route to check server state based on API key
7
  app.get('/checkState', (req, res) => {
8
- const { apikey } = req.query;
 
 
 
9
 
10
- if (validApiKeys.includes(apikey)) {
11
- const serverState = {
12
- message: 'Server is running successfully',
13
- server: 'running',
 
 
 
 
 
 
 
14
  };
15
- res.status(200).json(serverState);
 
 
 
16
  } else {
17
- res.status(401).json({ message: 'Invalid API key' });
18
  }
19
  });
20
 
21
- // Root route to display "Hello World"
22
  app.get('/', (req, res) => {
23
  res.send('Hello World');
24
  });
 
1
  const express = require('express');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const crypto = require('crypto');
5
+
6
  const app = express();
7
 
8
+ // Function to validate an IP address
9
+ const validateIp = (ip) => {
10
+ const ipRegex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/;
11
+ return ipRegex.test(ip);
12
+ };
13
+
14
+ // Function to generate a 16-character alphanumeric API key
15
+ const generateApiKey = () => {
16
+ return crypto.randomBytes(8).toString('hex');
17
+ };
18
 
19
+ // Route to check state and validate IP
20
  app.get('/checkState', (req, res) => {
21
+ const { ip } = req.query;
22
+
23
+ if (ip && validateIp(ip)) {
24
+ const apiKey = generateApiKey();
25
 
26
+ // Create session directory
27
+ const sessionDir = path.join(__dirname, 'session', ip);
28
+ if (!fs.existsSync(sessionDir)) {
29
+ fs.mkdirSync(sessionDir, { recursive: true });
30
+ }
31
+
32
+ // Create info.json file and store IP and API key
33
+ const infoPath = path.join(sessionDir, 'info.json');
34
+ const infoData = {
35
+ ip: ip,
36
+ apiKey: apiKey,
37
  };
38
+ fs.writeFileSync(infoPath, JSON.stringify(infoData, null, 2));
39
+
40
+ // Send API key to frontend
41
+ res.status(200).json({ message: 'Server is running successfully', apiKey: apiKey });
42
  } else {
43
+ res.status(401).json({ message: 'Invalid IP address' });
44
  }
45
  });
46
 
47
+ // Root route
48
  app.get('/', (req, res) => {
49
  res.send('Hello World');
50
  });