From 7b9189177a69f5ea70febcc6ca0330684784df76 Mon Sep 17 00:00:00 2001 From: charleswrayjr Date: Mon, 8 Sep 2025 11:35:36 -0500 Subject: [PATCH] Testing vpn paths. --- src/controllers/vpn.controller.js | 38 +++++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/controllers/vpn.controller.js b/src/controllers/vpn.controller.js index 7740d6a..c60f03e 100644 --- a/src/controllers/vpn.controller.js +++ b/src/controllers/vpn.controller.js @@ -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 -- 2.43.0