]> PHS Git Server - phs-api.git/commitdiff
Adding disconnect, getStatus, and restart to the vpn.controller.js and associated...
authorcharleswrayjr <charleswrayjr@gmail.com>
Tue, 9 Sep 2025 18:41:08 +0000 (13:41 -0500)
committercharleswrayjr <charleswrayjr@gmail.com>
Tue, 9 Sep 2025 18:41:08 +0000 (13:41 -0500)
src/controllers/vpn.controller.js

index 2e64bce33135c44a0389b6bb44691b2df2296b2d..b890e635cf87f10092db5b814f5954c2cd292e54 100644 (file)
@@ -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}`));
     }