Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use openvpn in nodejs?

I have NodeJS App and want to start use OpenVPN connection in it. To do that I found 2 modules on npm (openvpn-client and openvpn-bin) - but any of them has no good docs and examples, but I try as I can to use them and it was unsuccessful.

I have Ipvanish account (login/password) with 540 .opvn files, which I can use. I try this:

var openvpnmanager = require('node-openvpn');
 var openvpnBin = require('openvpn-bin');
 var path = require('path');

 var filePath = path.normalize('../geo/ipvanish/ipvanish-AU-Sydney-syd-a16.ovpn');

    var opts = {
        host: 'syd-a16.ipvanish.com', // normally '127.0.0.1', will default to if undefined
        port: 443, //port openvpn management console
        timeout: 60000, //timeout for connection - optional, will default to 1500ms if undefined
        config: filePath
    };
    var auth = {
        user: '[email protected]',
        pass: 'password'
    };

    var openvpn = openvpnmanager.connect(opts)

    openvpn.on('connected', function() { 
        // will be emited on successful interfacing with openvpn instance
        console.log('connected')
        openvpnmanager.authorize(auth).then(function(res){

        });
    });
like image 615
Александр Бережник Avatar asked Aug 10 '16 18:08

Александр Бережник


1 Answers

I use this, more effective way (with it I can handle OpenVPN connection as child process, close and reconnect on the fly).

var exec = require('child_process').exec;
var psTree = require('ps-tree');

var kill = function (pid, signal, callback) {
    signal   = signal || 'SIGKILL';
    callback = callback || function () {};
    var killTree = true;
    if(killTree) {
        psTree(pid, function (err, children) {
            [pid].concat(
                children.map(function (p) {
                    return p.PID;
                })
            ).forEach(function (tpid) {
                try { process.kill(tpid, signal) }
                catch (ex) { }
            });
            callback();
        });
    } else {
        try { process.kill(pid, signal) }
        catch (ex) { }
        callback();
    }
};

var ovpnProcess = null;

if(ovpnProcess != null){
    console.log('close connection');
    var isWin = /^win/.test(ovpnProcess.platform);
    if(!isWin) {
        kill(ovpnProcess.pid);
    } else {
    var cp = require('child_process');
            cp.exec('taskkill /PID ' + ovpnProcess.pid + ' /T /F', function (error, stdout, stderr) {
                // more debug if you need
                // console.log('stdout: ' + stdout);
                // console.log('stderr: ' + stderr);
                // if(error !== null) {
                //      console.log('exec error: ' + error);
                // }
            });
        }
    }

// to open connection I use this code:

ovpnProcess = exec('openvpn ipvanish/'+account.ip+'.ovpn');
ovpnProcess.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
});
ovpnProcess.stderr.on('data', function(data) {
    console.log('stdout: ' + data);
});
ovpnProcess.on('close', function(code) {
    console.log('closing code: ' + code);
});
like image 86
Alex Avatar answered Nov 16 '22 08:11

Alex