]> PHS Git Server - phs-api.git/commitdiff
Adding available clients to VPN.
authorcharleswrayjr <charleswrayjr@gmail.com>
Tue, 9 Sep 2025 13:40:44 +0000 (08:40 -0500)
committercharleswrayjr <charleswrayjr@gmail.com>
Tue, 9 Sep 2025 13:40:44 +0000 (08:40 -0500)
app.js
src/controllers/vpn.controller.js
src/routes/vpn.routes.js

diff --git a/app.js b/app.js
index 2e0e7618e3069cd13903632d592802328ba42fa5..2a6dca8065fdee51f2b77f248df7da52e0f67553 100755 (executable)
--- a/app.js
+++ b/app.js
@@ -12,6 +12,7 @@ const runOnce = require ( './src/middleware/custom-startup' );
 const Routes = require( './src/routes' );
 const phsdb = require( './src/phsdb' );
 const fs = require('fs');
+const path = require('path');
 const Docker = require('dockerode');
 const docker = new Docker({ socketPath: '/var/run/docker.sock' });
 const sshConfig = {
@@ -36,6 +37,7 @@ global.fs = fs;
 global.sshConfig = sshConfig;
 global.docker = docker;
 global.createError = createError;
+global.path = path;
 
 // noinspection JSCheckFunctionSignatures
 app.use( bodyParser.json( {
index 95c507397f5b9cb407f63a714ba9448a5024b779..a4d755a91b5ce827c27deeee7a0b7c7d2e3be0e8 100644 (file)
@@ -143,6 +143,64 @@ module.exports = {
       next( error );
     } ).connect( sshConfig );
   },
+  getAvailableClients: async ( req, res, next ) => {
+    const conn = new Client();
+    conn.on( 'ready', () => {
+      const commands = [
+        `ls /etc/openvpn/ccd 2>/dev/null || true`,
+        `ls /etc/openvpn/easy-rsa/pki/issued/*.crt 2>/dev/null || true`,
+      ];
+      conn.exec( commands.join( ' && ' ), ( err, stream ) => {
+          if (err) {
+            conn.end();
+            const error = new createError( 500, `SSH command failed` );
+            next( error );
+          }
+        let output = '';
+        stream.on('data', (data) => (output += data));
+        stream.stderr.on('data', (data) => (output += data));
+        stream.on('close', async (code) => {
+          conn.end();
+          if (code !== 0) {
+            return next(new createError(500, `Command failed: ${output}`));
+          }
+          const clients = [];
+          const ccdFiles = output.split('\n').filter(line => line.endsWith('.ccd'));
+          const crtFiles = output.split('\n').filter(line => line.endsWith('.crt'));
+
+          // Process .ccd files for static IPs
+          for (const ccdFile of ccdFiles) {
+            const clientName = path.basename( ccdFile, '.ccd' );
+            try {
+              const ccdContent = fs.readFileSync( ccdFile, 'utf8' );
+              const staticIpMatch = ccdContent.match( /ifconfig-push (\S+)/ );
+              clients.push( {
+                clientName,
+                staticIp:staticIpMatch ? staticIpMatch[1] : null,
+                hasCertificate:crtFiles.some( crt => crt.includes( `${ clientName }.crt` ) ),
+              } );
+            } catch (error) {
+              logger.warn( `Failed to read ${ ccdFile }: ${ error.message }` );
+            }
+          }// Add clients with certificates but no .ccd
+          for (const crtFile of crtFiles) {
+            const clientName = path.basename( crtFile, '.crt' );
+            if (!clients.some( client => client.clientName === clientName )) {
+              clients.push( {
+                clientName,
+                staticIp:null,
+                hasCertificate:true,
+              } );
+            }
+          }
+          res.json( { clients } );
+          });
+      } );
+    } ).on( 'error', ( err ) => {
+      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', () => {
index 4626e15f57a9c32dc2504e07aa055efacc0f3c58..f4bf5db10b17d0e891daed5f771909a66b2cc4da 100644 (file)
@@ -4,8 +4,11 @@ const { validateAuth } = require( '../middleware/routeHelpers' );
 const vpnController = require( '../controllers/vpn.controller' );
 
 module.exports = ( passport ) => {
+
   router.post( '/create-client', vpnController.createClient );
   router.put( '/revoke-client/', vpnController.revokeClient );
   router.get( '/clients', vpnController.getClients );
+  router.get( '/available-clients', vpnController.getAvailableClients );
+
   return router;
 };
\ No newline at end of file