// 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 ) => {
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();
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) );
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();
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) );
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) );
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