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

index 7740d6a92791848a7d4bad5185370ddeada1e946..c60f03eca12bb2556298309c102b902e90de7eb1 100644 (file)
@@ -10,6 +10,7 @@ const parseVpnStatus = ( output ) => {
   // Split once and reuse
   const lines = output.split( '\n' ).map( line => line.trim() ).filter( Boolean );
 
+  logger.debug( [ 'lines:', lines ].join( ' ' ) );
   // Find indices using a single pass
   let sIndex = -1, mIndex = -1, eIndex = -1;
   lines.forEach( ( line, i ) => {
@@ -47,10 +48,12 @@ module.exports = {
   createClient:( req, res, next ) => {
     const { clientName, staticIp } = req.body;
     if (!clientName) {
-      return res.status( 400 ).json( { error:'Client name required' } );
+      const error = new createError( 400, 'Client name required' );
+      next( error );
     }
     if (staticIp && !staticIp.match( /^10\.8\.0\.\d{1,3}$/ )) {
-      return res.status( 400 ).json( { error:'Invalid static IP (must be 10.8.0.x)' } );
+      const error = new createError( 400, 'Invalid static IP (must be 10.8.0.x)' );
+      next( error );
     }
 
     const conn = new Client();
@@ -62,7 +65,8 @@ module.exports = {
       conn.exec( commands.join( ' && ' ), ( err, stream ) => {
         if (err) {
           conn.end();
-          return res.status( 500 ).json( { error:'SSH command failed' } );
+          const error = new createError( 500, `SSH command failed` );
+          next( error );
         }
         let output = '';
         stream.on( 'data', ( data ) => (output += data) );
@@ -75,18 +79,21 @@ module.exports = {
               ovpn:output
             } );
           } else {
-            res.status( 500 ).json( { error:`Command failed: ${ output }` } );
+            const error = new createError( 500, `Command failed: ${ output }` );
+            next( error );
           }
         } );
       } );
     } ).on( 'error', ( err ) => {
-      res.status( 500 ).json( { error:`SSH connection failed: ${ err.message }` } );
+      const error = new createError( 500, `SSH connection failed: ${ err.message }` );
+      next( error );
     } ).connect( sshConfig );
   },
   revokeClient:( req, res, next ) => {
     const { clientName } = req.body;
     if (!clientName) {
-      return res.status( 400 ).json( { error:'Client name required' } );
+      const error = new createError( 400, 'Client name required' );
+      next( error );
     }
 
     const conn = new Client();
@@ -99,7 +106,8 @@ module.exports = {
       conn.exec( commands.join( ' && ' ), ( err, stream ) => {
         if (err) {
           conn.end();
-          return res.status( 500 ).json( { error:'SSH command failed' } );
+          const error = new createError( 500, `SSH command failed` );
+          next( error );
         }
         let output = '';
         stream.on( 'data', ( data ) => (output += data) );
@@ -109,24 +117,25 @@ module.exports = {
           if (code === 0) {
             res.json( { message:`Client ${ clientName } revoked` } );
           } else {
-            res.status( 500 ).json( { error:`Command failed: ${ output }` } );
+            const error = new createError( 500, `Command failed: ${ output }` );
+            next( error );
           }
         } );
       } );
     } ).on( 'error', ( err ) => {
-      res.status( 500 ).json( { error:`SSH connection failed: ${ err.message }` } );
+      const error = new createError( 500, `SSH connection failed: ${ err.message }` );
+      next( error );
     } ).connect( sshConfig );
   },
   getClients:async ( req, res, next ) => {
     const conn = new Client();
     conn.on( 'ready', () => {
-      const error = new createError( 400, `SSH connection failed:` );
-      next( error );
-      /*const command = `cat /var/log/openvpn/openvpn-status.log`;
+      const command = `cat /var/log/openvpn/openvpn-status.log`;
       conn.exec( command, ( err, stream ) => {
         if (err) {
           conn.end();
-          return res.status( 500 ).json( { error:'SSH command failed' } );
+          const error = new createError( 500, `SSH command failed` );
+          next( error );
         }
         let output = '';
         stream.on( 'data', ( data ) => (output += data) );
@@ -141,11 +150,10 @@ module.exports = {
             next( error );
           }
         } );
-      } );*/
+      } );
     } ).on( 'error', ( err ) => {
       const error = new createError( 500, `SSH connection failed: ${ err.message }` );
       next( error );
-      // res.status( 500 ).json( { error:`SSH connection failed: ${ err.message }` } );
     } ).connect( sshConfig );
   }
 };
\ No newline at end of file