From: charleswrayjr Date: Tue, 9 Sep 2025 18:41:08 +0000 (-0500) Subject: Adding disconnect, getStatus, and restart to the vpn.controller.js and associated... X-Git-Url: https://git.phasecustomsoft.com/?a=commitdiff_plain;h=8bffb2b00ebb30e2fec2b0efbd0f08de9c06686d;p=phs-api.git Adding disconnect, getStatus, and restart to the vpn.controller.js and associated routes in vpn.routes.js. --- diff --git a/src/controllers/vpn.controller.js b/src/controllers/vpn.controller.js index 2e64bce..b890e63 100644 --- a/src/controllers/vpn.controller.js +++ b/src/controllers/vpn.controller.js @@ -318,8 +318,29 @@ module.exports = { }, getStatus: async ( req, res, next ) => { try { - const { stdout } = await execPromise('systemctl is-active openvpn@server'); - res.json({ status: stdout.trim() }); + const conn = new Client(); + conn.on('ready', () => { + const command = `systemctl is-active openvpn@server`; + conn.exec( command, ( err, stream ) => { + if (err) { + conn.end(); + return next( new createError( 500, `SSH command failed: ${ err.message }` ) ); + } + let output = ''; + stream.on( 'data', ( data ) => (output += data) ); + stream.stderr.on( 'data', ( data ) => (output += data) ); + stream.on( 'close', ( code ) => { + conn.end(); + if (code === 0) { + res.json( { active:output.trim() === 'active' } ); + } else { + next( new createError( 500, `Command failed: ${ output }` ) ); + } + } ); + } ); + }).on('error', (err) => { + next(new createError(500, `SSH connection failed: ${err.message}`)) + }); } catch (error) { return next(new createError(500, `Failed to check VPN status: ${error.message}`)); }