From c9be804ce5559714106cead14ec8369f0dac06bb Mon Sep 17 00:00:00 2001 From: Rodrigo Gomes da Silva Date: Thu, 2 Feb 2017 23:34:23 -0200 Subject: [PATCH] Fixed vpngate file parsing due to line separator type on linux | added openvpn options arg --- README.md | 3 +++ bin/easyvpn | 17 +++++++++-------- index.js | 7 ++++--- package.json | 3 +-- src/api.js | 5 ++--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bdd06c5..eeaa5bd 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ You can even wait for easyvpn to resolve the countries and then choose between t A proxy can be used to get data from vpngate.net: > easyvpn -p http://myproxy:3128 +To pass special arguments to openvpn: +> easyvpn -o "--dev-type tun --dev tun0" + ## Contributing 1. Fork it! 2. Create your feature branch: `git checkout -b my-new-feature` diff --git a/bin/easyvpn b/bin/easyvpn index 99b0c66..25be365 100644 --- a/bin/easyvpn +++ b/bin/easyvpn @@ -4,16 +4,17 @@ const program = require('commander'); +function list(val) { + return val + .match(/[^\s"]+|"([^"]*)"/gi); +} + program .version('1.1.3') - .option('-c, --country', 'Country name') + .option('-c, --country ', 'Country name') .option('-q, --query', 'Enable country query') - .option('-p, --proxy', 'Proxy url') + .option('-p, --proxy ', 'Proxy url') + .option('-o, --options ', 'Openvpn options', list) .parse(process.argv); -const options = {}; -if (program.country) options.country = program.country; -if (program.query) options.query = true; -if (program.proxy) options.proxy = program.proxy; - -require('../index.js')(options); +require('../index.js')(program); diff --git a/index.js b/index.js index ed34bd0..749a5b0 100644 --- a/index.js +++ b/index.js @@ -55,10 +55,11 @@ function save(vpns) { }); } -function startOpenvpn() { +function startOpenvpn(options = []) { logger.info('Starting openvpn...'); const openvpn = `"${which.sync('openvpn')}"`; - const proc = spawn(openvpn, ['--config', `"${filePath}"`], { shell: true }); + console.log(['--config', `"${filePath}"`].concat(options)); + const proc = spawn(openvpn, ['--config', `"${filePath}"`].concat(options), { shell: true }); proc.stdout.pipe(logger.stream); proc.stderr.on('data', data => logger.error(data.toString())); proc.on('close', code => logger.info(`child process exited with code ${code}`)); @@ -86,7 +87,7 @@ function execute(options) { }) .then(vpns => filter(vpns, options.country)) .then(save) - .then(startOpenvpn) + .then(() => startOpenvpn(options.options)) .catch(logger.error); } diff --git a/package.json b/package.json index 9875821..fac8a35 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easyvpn", - "version": "1.1.3", + "version": "1.2.0", "description": "Easy way to get a VPN connection.", "main": "index.js", "repository": "https://github.com/rodrigogs/easyvpn.git", @@ -16,7 +16,6 @@ "tunnel" ], "scripts": { - "start": "node index.js", "eslint": "eslint . --ext .js" }, "dependencies": { diff --git a/src/api.js b/src/api.js index b1f0ecd..4cc00a8 100644 --- a/src/api.js +++ b/src/api.js @@ -4,7 +4,6 @@ const request = require('request'); const csv = require('csvtojson'); const through2 = require('through2'); const Promise = require('bluebird'); -const os = require('os'); const VPN = require('./vpn'); const VPNGATE_API_URL = 'http://www.vpngate.net/api/iphone/'; @@ -18,7 +17,7 @@ Error message: ${err.message}`; function filter(chunk, enc, cb) { const createBuffer = data => new Buffer(data, DEFAULT_ENCODE); const lines = chunk.toString() - .split(os.EOL) + .split('\r\n') .filter(line => (line !== '*vpn_servers' && line !== '*')) .map((line) => { if (line.startsWith('#HostName')) { @@ -26,7 +25,7 @@ function filter(chunk, enc, cb) { } return line; }) - .join(os.EOL); + .join('\r\n'); this.push(createBuffer(lines)); cb();