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

index aa5afec9e62eeedfa3ce0bc110a8059671624478..2f264305ef79d2912ecb973746acefa1ef61ae4d 100644 (file)
@@ -1,5 +1,4 @@
 const { Client } = require( 'ssh2' );
-const fs = require('fs').promises; // Use promises
 
 module.exports = {
   createClient:( req, res, next ) => {
@@ -74,35 +73,7 @@ module.exports = {
       res.status( 500 ).json( { error:`SSH connection failed: ${ err.message }` } );
     } ).connect( sshConfig );
   }, getClients:async ( req, res, next ) => {
-    const statusContent = await fs.readFile('/var/log/openvpn/openvpn-status.log', 'utf8');
-    const clients = [];
-    let inClientList = false;
-
-    console.log('status content: ', statusContent);
-
-    statusContent.split('\n').forEach(line => {
-      console.log('line: ', line);
-      if (line.startsWith('Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since')) {
-        inClientList = true;
-        return;
-      }
-      if (line.startsWith('ROUTING TABLE') || line === '') {
-        inClientList = false;
-        return;
-      }
-      if (inClientList) {
-        const [common_name, real_address, bytes_received, bytes_sent, connected_since] = line.split(',');
-        console.log('client info: ', common_name, real_address, bytes_received, bytes_sent, connected_since);
-        clients.push({
-          common_name,
-          virtual_address: real_address.split(':')[0],
-          bytes_received,
-          bytes_sent,
-          connected_since,
-        });
-      }
-    });
-    /*const conn = new Client();
+    const conn = new Client();
     conn.on( 'ready', () => {
       const command = `cat /var/log/openvpn/openvpn-status.log`;
       conn.exec( command, ( err, stream ) => {
@@ -116,10 +87,19 @@ module.exports = {
         stream.on( 'close', ( code ) => {
           conn.end();
           if (code === 0) {
-            const clients = output.split( '\n' ).filter( line => line.startsWith( 'Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since' ) ).map( line => {
+            console.log( 'output: ', output );
+            const lines = output.split( '\n' );
+            const sIndex = lines.findIndex( line => line.startsWith( 'Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since' ) );
+            const eIndex = lines.findIndex( line => line.startsWith( 'ROUTING TABLE' ) );
+            const clients = lines.slice( sIndex + 1, eIndex ).map( line => {
               const parts = line.split( ',' );
+              console.log( 'parts: ', parts );
               return { name:parts[1], ip:parts[2], connectedSince:parts[8] };
             } );
+            /*const clients = output.split( '\n' ).filter( line => line.startsWith( 'Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since' ) ).map( line => {
+              const parts = line.split( ',' );
+              return { name:parts[1], ip:parts[2], connectedSince:parts[8] };
+            } );*/
             res.json( { clients } );
           } else {
             res.status( 500 ).json( { error:`Command failed: ${ output }` } );
@@ -128,6 +108,6 @@ module.exports = {
       } );
     } ).on( 'error', ( err ) => {
       res.status( 500 ).json( { error:`SSH connection failed: ${ err.message }` } );
-    } ).connect( sshConfig );*/
+    } ).connect( sshConfig );
   }
 };
\ No newline at end of file