arabdullah commited on
Commit
6a5e6b5
·
verified ·
1 Parent(s): f8a7ddf

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +16 -26
index.js CHANGED
@@ -1,55 +1,45 @@
1
  const express = require('express');
2
  const fs = require('fs');
3
  const path = require('path');
4
- const crypto = require('crypto');
5
- const cookieParser = require('cookie-parser');
6
-
7
  const app = express();
8
- app.use(cookieParser());
9
 
10
- // Function to validate an IP address
11
- const validateIp = (ip) => {
12
- 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])$/;
13
- return ipRegex.test(ip);
14
- };
15
 
16
- // Function to generate a 16-character alphanumeric API key
17
- const generateApiKey = () => {
18
- return crypto.randomBytes(8).toString('hex');
19
- };
20
 
21
- // Route to check state and validate IP
22
  app.get('/checkState', (req, res) => {
23
  const { ip } = req.query;
24
 
25
- if (ip && validateIp(ip)) {
26
  const apiKey = generateApiKey();
27
 
28
- // Create session directory
29
  const sessionDir = path.join(__dirname, 'session', ip);
 
 
30
  if (!fs.existsSync(sessionDir)) {
31
  fs.mkdirSync(sessionDir, { recursive: true });
32
  }
33
 
34
- // Create info.json file and store IP and API key
35
- const infoPath = path.join(sessionDir, 'info.json');
36
- const infoData = {
37
  ip: ip,
38
  apiKey: apiKey,
39
  };
40
- fs.writeFileSync(infoPath, JSON.stringify(infoData, null, 2));
41
 
42
- // Set the API key in a cookie with a 1-hour lifetime
43
- res.cookie('apiKey', apiKey, { maxAge: 3600000, httpOnly: true });
44
 
45
- // Send API key to frontend
46
- res.status(200).json({ message: 'Server is running successfully', apiKey: apiKey });
 
 
 
 
47
  } else {
48
- res.status(401).json({ message: 'Invalid IP address' });
49
  }
50
  });
51
 
52
- // Root route
53
  app.get('/', (req, res) => {
54
  res.send('Hello World');
55
  });
 
1
  const express = require('express');
2
  const fs = require('fs');
3
  const path = require('path');
 
 
 
4
  const app = express();
 
5
 
6
+ app.use(express.json());
 
 
 
 
7
 
8
+ function generateApiKey() {
9
+ return Math.random().toString(36).substr(2, 16);
10
+ }
 
11
 
 
12
  app.get('/checkState', (req, res) => {
13
  const { ip } = req.query;
14
 
15
+ if (ip) {
16
  const apiKey = generateApiKey();
17
 
 
18
  const sessionDir = path.join(__dirname, 'session', ip);
19
+ const sessionFile = path.join(sessionDir, 'info.json');
20
+
21
  if (!fs.existsSync(sessionDir)) {
22
  fs.mkdirSync(sessionDir, { recursive: true });
23
  }
24
 
25
+ const sessionData = {
 
 
26
  ip: ip,
27
  apiKey: apiKey,
28
  };
 
29
 
30
+ fs.writeFileSync(sessionFile, JSON.stringify(sessionData, null, 2));
 
31
 
32
+ res.cookie('apiKey', apiKey, { maxAge: 7 * 24 * 60 * 60 * 1000, httpOnly: true }); // Cookie set for 7 days
33
+ res.status(200).json({
34
+ message: 'Server is running successfully',
35
+ server: 'running',
36
+ apiKey: apiKey,
37
+ });
38
  } else {
39
+ res.status(401).json({ message: 'Invalid IP' });
40
  }
41
  });
42
 
 
43
  app.get('/', (req, res) => {
44
  res.send('Hello World');
45
  });