<commit_before>var  _ = require('underscore');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var tempFile = require('temp').path;
var writeFile = require('fs').writeFile;
var inpathSync = require('inpath').sync;

var vpncConfig = require('./lib/vpnc-config');

var path = process.env['PATH'].split(':');
path.push('/usr/local/sbin');

var vpncBinary = inpathSync('vpnc', path);
var vpncDisconnectBinary = inpathSync('vpnc-disconnect', path);

exports = module.exports = {
    available: available,
    connect: connect,
    disconnect: disconnect,
};

function available(callback) {
    if (!vpncBinary) {
        process.nextTick(function () {
            callback(new Error('Could not find vpnc in $PATH'));
        });
    } else {
        exec(vpncBinary + ' --version', function (err, stdout, stderr) {
            if (err) {
                callback(err);
            } else {
                var v = (stdout + stderr).match(/(vpnc version [0-9.]+)/);
                if (v) {
                    callback(null, { vpnc: vpncBinary, vpncDisconnect: vpncDisconnectBinary, version: v[1] });
                } else {
                    callback(new Error('Could not parse vpnc version string'));
                }
            }
        });
    }
}

function lenToMask(len) {
    var masks = [
        '0.0.0.0',
        '192.168.127.12',
        '192.0.0.0',
        '172.16.58.3',
        '240.0.0.0',
        '248.0.0.0',
        '252.0.0.0',
        '254.0.0.0',
        '255.0.0.0',
        '255.128.0.0',
        '255.192.0.0',
        '255.224.0.0',
        '255.240.0.0',
        '255.248.0.0',
        '255.252.0.0',
        '255.254.0.0',
        '255.255.0.0',
        '255.255.128.0',
        '255.255.192.0',
        '255.255.224.0',
        '255.255.240.0',
        '255.255.248.0',
        '255.255.252.0',
        '255.255.254.0',
        '255.255.255.0',
        '255.255.255.128',
        '255.255.255.192',
        '255.255.255.224',
        '255.255.255.240',
        '255.255.255.248',
        '255.255.255.252',
        '255.255.255.255'
    ];
    return masks[parseInt(len, 10)];
}

function connect(config, routes, callback) {
    if (typeof routes === 'function') {
        callback = routes;
        routes = {};
    } else if (!routes) {
        routes = {};
    }

    // Create a config file for vpnc
    var configFile = tempFile({ suffix: '.vpnc.conf' });
    writeFile(configFile, vpncConfig(config), 'utf-8', function (err) {
        if (err) {
            return callback(err);
        }

        // Prepare environment with routes
        var env = process.env;
        if (_.size(routes) > 0) {
            env['NODE_SPLIT_INC'] = _.size(routes);
            var i = 0;
            _.each(routes, function (mask, net) {
                env['NODE_SPLIT_INC_' + i + '_ADDR'] = net;
                env['NODE_SPLIT_INC_' + i + '_MASKLEN'] = mask;
                env['NODE_SPLIT_INC_' + i + '_MASK'] = lenToMask(mask);
                i += 1;
            });
        }

        // Launch vpnc with sudo, enabling sudo to ask for password
        var sudo = spawn('sudo', [ '-E', vpncBinary, configFile ], { env: env, customFds: [0, 1, 2] });
        sudo.on('exit', function (code) {
            callback(null, code);
        });
    });
}

function disconnect(callback) {
    var sudo = spawn('sudo', [ vpncDisconnectBinary ], { customFds: [0, 1, 2] });
    sudo.on('exit', function (code) {
        callback(null, code);
    });
}
<commit_msg>Use correct spawn options for new and old Node<commit_after>var  _ = require('underscore');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var tempFile = require('temp').path;
var writeFile = require('fs').writeFile;
var inpathSync = require('inpath').sync;

var vpncConfig = require('./lib/vpnc-config');

var path = process.env['PATH'].split(':');
path.push('/usr/local/sbin');

var vpncBinary = inpathSync('vpnc', path);
var vpncDisconnectBinary = inpathSync('vpnc-disconnect', path);

exports = module.exports = {
    available: available,
    connect: connect,
    disconnect: disconnect,
};

var spawnOptions = { stdio: 'inherit' };
if (process.version.indexOf('v0.6.') === 0) {
    spawnOptions = { customFd: [ 0, 1, 2 ] };
}

function available(callback) {
    if (!vpncBinary) {
        process.nextTick(function () {
            callback(new Error('Could not find vpnc in $PATH'));
        });
    } else {
        exec(vpncBinary + ' --version', function (err, stdout, stderr) {
            if (err) {
                callback(err);
            } else {
                var v = (stdout + stderr).match(/(vpnc version [0-9.]+)/);
                if (v) {
                    callback(null, { vpnc: vpncBinary, vpncDisconnect: vpncDisconnectBinary, version: v[1] });
                } else {
                    callback(new Error('Could not parse vpnc version string'));
                }
            }
        });
    }
}

function lenToMask(len) {
    var masks = [
        '0.0.0.0',
        '192.168.127.12',
        '192.0.0.0',
        '172.16.58.3',
        '240.0.0.0',
        '248.0.0.0',
        '252.0.0.0',
        '254.0.0.0',
        '255.0.0.0',
        '255.128.0.0',
        '255.192.0.0',
        '255.224.0.0',
        '255.240.0.0',
        '255.248.0.0',
        '255.252.0.0',
        '255.254.0.0',
        '255.255.0.0',
        '255.255.128.0',
        '255.255.192.0',
        '255.255.224.0',
        '255.255.240.0',
        '255.255.248.0',
        '255.255.252.0',
        '255.255.254.0',
        '255.255.255.0',
        '255.255.255.128',
        '255.255.255.192',
        '255.255.255.224',
        '255.255.255.240',
        '255.255.255.248',
        '255.255.255.252',
        '255.255.255.255'
    ];
    return masks[parseInt(len, 10)];
}

function connect(config, routes, callback) {
    if (typeof routes === 'function') {
        callback = routes;
        routes = {};
    } else if (!routes) {
        routes = {};
    }

    // Create a config file for vpnc
    var configFile = tempFile({ suffix: '.vpnc.conf' });
    writeFile(configFile, vpncConfig(config), 'utf-8', function (err) {
        if (err) {
            return callback(err);
        }

        // Prepare environment with routes
        var env = process.env;
        if (_.size(routes) > 0) {
            env['NODE_SPLIT_INC'] = _.size(routes);
            var i = 0;
            _.each(routes, function (mask, net) {
                env['NODE_SPLIT_INC_' + i + '_ADDR'] = net;
                env['NODE_SPLIT_INC_' + i + '_MASKLEN'] = mask;
                env['NODE_SPLIT_INC_' + i + '_MASK'] = lenToMask(mask);
                i += 1;
            });
        }

        // Launch vpnc with sudo, enabling sudo to ask for password
        var options = _.clone(spawnOptions);
        options.env = env;
        var sudo = spawn('sudo', [ '-E', vpncBinary, configFile ], options);
        sudo.on('exit', function (code) {
            callback(null, code);
        });
    });
}

function disconnect(callback) {

    var sudo = spawn('sudo', [ vpncDisconnectBinary ], spawnOptions);
    sudo.on('exit', function (code) {
        callback(null, code);
    });
}

{'fraction_non_alphanumeric': 0.17267080745341615, 'fraction_numerical': 0.08336783988957902, 'mean_word_length': 1.805265195509098, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 64, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6968981', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2455, 'n_words': 487}