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 = {
global.sshConfig = sshConfig;
global.docker = docker;
global.createError = createError;
+global.path = path;
// noinspection JSCheckFunctionSignatures
app.use( bodyParser.json( {
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', () => {
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