]> PHS Git Server - phs-api.git/commitdiff
Testing vpn paths.
authorcharleswrayjr <charleswrayjr@gmail.com>
Mon, 8 Sep 2025 16:16:57 +0000 (11:16 -0500)
committercharleswrayjr <charleswrayjr@gmail.com>
Mon, 8 Sep 2025 16:16:57 +0000 (11:16 -0500)
src/controllers/vpn.controller.js

index 5d581b3b738599860f8734a6ee9f912364ce4c45..8dbad1a7d021c2ceb86cd613579ddd49892deaa6 100644 (file)
@@ -1,5 +1,42 @@
 const { Client } = require( 'ssh2' );
 
+const parseVpnStatus = (output) => {
+  // Split once and reuse
+  const lines = output.split('\n').map(line => line.trim()).filter(Boolean);
+
+  // Find indices using a single pass
+  let sIndex = -1, mIndex = -1, eIndex = -1;
+  lines.forEach((line, i) => {
+    if (sIndex === -1 && line.startsWith('Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since')) sIndex = i;
+    else if (mIndex === -1 && line.startsWith('ROUTING TABLE')) mIndex = i;
+    else if (eIndex === -1 && line.startsWith('GLOBAL STATS')) eIndex = i;
+  });
+
+  // Early return if indices not found
+  if (sIndex === -1 || mIndex === -1 || eIndex === -1) return [];
+
+  // Parse clients and routing table together in a single pass
+  const clientsMap = new Map();
+  for (let i = sIndex + 1; i < mIndex; i++) {
+    const parts = lines[i].split(',');
+    clientsMap.set(parts[0], {
+      name: parts[0],
+      ip: parts[1],
+      connectedSince: parts[4],
+      virtual_ip: ''
+    });
+  }
+
+  // Assign virtual IPs
+  for (let i = mIndex + 2; i < eIndex; i++) {
+    const parts = lines[i].split(',');
+    const client = clientsMap.get(parts[1]);
+    if (client) client.virtual_ip = parts[0];
+  }
+
+  return Array.from(clientsMap.values());
+};
+
 module.exports = {
   createClient:( req, res, next ) => {
     const { clientName, staticIp } = req.body;
@@ -87,49 +124,9 @@ module.exports = {
         stream.on( 'close', ( code ) => {
           conn.end();
           if (code === 0) {
-              // Split once and reuse
-              const lines = output.split('\n').map(line => line.trim());
-
-              console.log('lines: ', lines);
-
-              // Find indices using a single pass
-              let sIndex = -1, mIndex = -1, eIndex = -1;
-              lines.forEach((line, i) => {
-                if (sIndex === -1 && line.startsWith('Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since')) sIndex = i;
-                else if (mIndex === -1 && line.startsWith('ROUTING TABLE')) mIndex = i;
-                else if (eIndex === -1 && line.startsWith('GLOBAL STATS')) eIndex = i;
-              });
-
-              console.log('indexes: ', sIndex, mIndex, eIndex);
-
-              // Early return if indices not found
-              if (sIndex === -1 || mIndex === -1 || eIndex === -1) return [];
-
-              // Parse clients and routing table together in a single pass
-              const clientsMap = new Map();
-              for (let i = sIndex + 1; i < mIndex; i++) {
-                const parts = lines[i].split(',');
-                clientsMap.set(parts[0], {
-                  name: parts[0],
-                  ip: parts[1],
-                  connectedSince: parts[4],
-                  virtual_ip: ''
-                });
-              }
-
-              console.log('clientsMap: ', clientsMap);
-
-              // Assign virtual IPs
-              for (let i = mIndex + 2; i < eIndex; i++) {
-                const parts = lines[i].split(',');
-                const client = clientsMap.get(parts[1]);
-                if (client) client.virtual_ip = parts[0];
-              }
-
-              const clientsArray = Array.from(clientsMap.values()).sort((a, b) => Date.parse(b.connectedSince) - Date.parse(a.connectedSince));
-              console.log('clients: ', clientsArray);
 
-              res.json(clientsArray);
+            const clients = parseVpnStatus(output);
+            res.json({clients});
             /*const lines = output.split( '\n' );
             const sIndex = lines.findIndex( line => line.startsWith( 'Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since' ) );
             const mIndex = lines.findIndex( line => line.startsWith( 'ROUTING TABLE' ) );