Skip to content

Commit

Permalink
Removed some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogs committed Jun 24, 2017
1 parent d1cf675 commit be07c1c
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 317 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ Differently from autovpn, this tool is able to run on Windows. Instead of execut
## Usage
To connect to any received vpn connection:
> easyvpn
> ```$ easyvpn```
To connect to a VPN from a specific country:
> easyvpn -c US
> ```$ easyvpn -c US```
Country name may be short or long:
> easyvpn -c Japan
> ```$ easyvpn -c Japan```
> easyvpn -c JP
> ```$ easyvpn -c JP```
You can even wait for easyvpn to resolve the countries and then choose between them:
> easyvpn -q
> ```$ easyvpn -q```
A proxy can be used to get data from vpngate.net:
> easyvpn -p http://myproxy:3128
> ```$ easyvpn -p http://myproxy:3128```
To pass special arguments to openvpn:
> easyvpn -o "--dev-type tun --dev tun0"
> ```$ easyvpn -o "--dev-type tun --dev tun0"```
## Contributing
1. Fork it!
Expand Down
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ListVPNs = require('./src/api');

const filePath = path.join(os.tmpdir(), 'openvpnconf');

function queryCountry(countries) {
const queryCountry = (countries) => {
const match = countries => new RegExp(`^(?:${countries.map(country => `(${country})$`).join('|')})`);
return new Promise((resolve, reject) => {
logger.info(`Choose between those countries: ${countries.join(', ')}`);
Expand All @@ -29,15 +29,15 @@ function queryCountry(countries) {
resolve(result.country);
});
});
}
};

function filter(vpns, country) {
const filter = country => (vpns) => {
if (!country) return vpns;
return vpns
.filter(vpn => vpn.countryNames.indexOf(country.toLowerCase()) !== -1);
}
};

function save(vpns) {
const save = (vpns) => {
return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(filePath, { overwrite: true });
writer
Expand All @@ -53,9 +53,9 @@ function save(vpns) {
.on('error', reject)
.once('close', resolve);
});
}
};

function startOpenvpn(options = []) {
const startOpenvpn = (options = []) => {
logger.info('Starting openvpn...');
const openvpn = `"${which.sync('openvpn')}"`;
const proc = spawn(openvpn, ['--config', `"${filePath}"`].concat(options), { shell: true });
Expand All @@ -65,9 +65,9 @@ function startOpenvpn(options = []) {

process.on('exit', () => proc.kill());
process.on('SIGINT', () => proc.kill());
}
};

function execute(options) {
const execute = (options) => {
logger.info('Querying data...');
ListVPNs(options.proxy)
.then((vpns) => {
Expand All @@ -84,10 +84,10 @@ function execute(options) {
.catch(reject);
});
})
.then(vpns => filter(vpns, options.country))
.then(filter(options.country))
.then(save)
.then(() => startOpenvpn(options.options))
.catch(logger.error);
}
};

module.exports = (country, query) => execute(country, query);
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easyvpn",
"version": "1.2.2",
"version": "1.2.3",
"description": "Easy way to get a VPN connection.",
"main": "index.js",
"repository": "https://github.com/rodrigogs/easyvpn.git",
Expand All @@ -19,21 +19,21 @@
"eslint": "eslint . --ext .js"
},
"dependencies": {
"bluebird": "^3.4.7",
"commander": "^2.9.0",
"csvtojson": "^1.1.3",
"bluebird": "^3.5.0",
"commander": "^2.10.0",
"csvtojson": "^1.1.7",
"prompt": "^1.0.0",
"request": "^2.79.0",
"request": "^2.81.0",
"socks5-http-client": "^1.0.2",
"split": "^1.0.0",
"through2": "^2.0.3",
"which": "^1.2.12",
"which": "^1.2.14",
"winston": "^2.3.1"
},
"devDependencies": {
"eslint": "^3.14.0",
"eslint-config-airbnb-base": "^11.0.1",
"eslint-plugin-import": "^2.2.0"
"eslint": "^4.1.0",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-plugin-import": "^2.6.0"
},
"bin": {
"easyvpn": "bin/easyvpn"
Expand Down
4 changes: 1 addition & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ function getData(proxy) {
.on('error', err => reject(networkError(err)))
.pipe(through2(filter))
.pipe(csv())
.on('error', (err) => {
reject(err);
})
.on('error', reject)
.on('end_parsed', resolve);
});
}
Expand Down
Loading

0 comments on commit be07c1c

Please sign in to comment.