How to use openvpn in nodejs?
Asked Answered
A

2

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 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){

        });
    });
Ambrosia answered 10/8, 2016 at 18:33 Comment(0)
O
9

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);
});
Ola answered 26/2, 2017 at 9:20 Comment(8)
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.Lorolla
I am fresh noder. Would you please teach how to build async function, that mean I could use then function when connected the vpn?Cornuted
If any possible to only dependence on child_process, ps-tree had no longer been maintained.Cornuted
I cannot make sense of those two lines: var ovpnProcess = null; if(ovpnProcess != null){Thorbert
@AlexChiang for async you can wrap methods in Promises and then use async/await to call themOla
@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 workOla
How can I bind openvpn to a local port and use it as proxy ?Overtime
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.Ola
E
-1

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

Eanes answered 10/8, 2016 at 18:48 Comment(1)
I try the both - on github I still waiting with 2 opened issues and that last variant is not good for me, because its only for single call inside App, but I need to switch between vpn connections.Allowedly

© 2022 - 2024 — McMap. All rights reserved.