8

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@gmail.com',
        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){

        });
    });

2 Answers2

8

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);
});
Alex
  • 381
  • 4
  • 14
  • I have a bug when I run this code. This is my ERROR: `stderr: 'openvpn' is not recognized as an internal or external command, operable program or batch file. closing code: 1` I DON'T have this error if I run the same command in cmd or powershell. – milos Nov 02 '21 at 15:16
  • I am fresh noder. Would you please teach how to build async function, that mean I could use then function when connected the vpn? – Alex Chiang Dec 17 '21 at 06:25
  • If any possible to only dependence on ```child_process```, ```ps-tree``` had no longer been maintained. – Alex Chiang Dec 17 '21 at 06:28
  • I cannot make sense of those two lines: `var ovpnProcess = null; if(ovpnProcess != null){` – Roko C. Buljan Mar 31 '22 at 10:41
  • @AlexChiang for async you can wrap methods in Promises and then use async/await to call them – Alex Apr 06 '22 at 19:24
  • @RokoC.Buljan 1st sample code is demo how to close, first one is how to open, so I created openvpn as null to make that condition to work – Alex Apr 06 '22 at 19:25
  • How can I bind openvpn to a local port and use it as proxy ? – Ahmed Can Unbay Jul 06 '23 at 11:30
  • it's not port only, to setup it as proxy need to create network interface on local machine and assign port to it then I set proxies on Debian in past and it looks like we describe physical device which is available on port. Read more about network interfaces to dive into it. – Alex Jul 13 '23 at 19:52
-1

you can get help from https://www.npmjs.com/package/node-openvpn or OpenVPN with node, How it works?.

Community
  • 1
  • 1
Neeraj Kumar
  • 6,045
  • 2
  • 31
  • 23