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

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +23 -1
index.js CHANGED
@@ -9,10 +9,32 @@ 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);
 
9
  return Math.random().toString(36).substr(2, 16);
10
  }
11
 
12
+ // Function to validate IP address format and ensure it's not a private or reserved IP
13
+ function isValidIp(ip) {
14
+ // Regular expression to validate IPv4 format
15
+ const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
16
+
17
+ if (!ipv4Regex.test(ip)) {
18
+ return false; // Invalid IP format
19
+ }
20
+
21
+ // Split the IP into octets
22
+ const octets = ip.split('.').map(Number);
23
+
24
+ // Check for private IP ranges
25
+ const isPrivateIp =
26
+ (octets[0] === 10) ||
27
+ (octets[0] === 172 && octets[1] >= 16 && octets[1] <= 31) ||
28
+ (octets[0] === 192 && octets[1] === 168) ||
29
+ (ip === '127.0.0.1'); // localhost
30
+
31
+ return !isPrivateIp; // Return true if IP is not private
32
+ }
33
+
34
  app.get('/checkState', (req, res) => {
35
  const { ip } = req.query;
36
 
37
+ if (ip && isValidIp(ip)) {
38
  const apiKey = generateApiKey();
39
 
40
  const sessionDir = path.join(__dirname, 'session', ip);