Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,24 +1,50 @@
|
|
| 1 |
const express = require('express');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
const app = express();
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
// Route to check
|
| 7 |
app.get('/checkState', (req, res) => {
|
| 8 |
-
const {
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
const
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
};
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
} else {
|
| 17 |
-
res.status(401).json({ message: 'Invalid
|
| 18 |
}
|
| 19 |
});
|
| 20 |
|
| 21 |
-
// Root route
|
| 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 |
});
|