Skip to content

Commit

Permalink
added socks5 proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogs committed Feb 12, 2017
1 parent c5700c6 commit d1cf675
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 36 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easyvpn",
"version": "1.2.1",
"version": "1.2.2",
"description": "Easy way to get a VPN connection.",
"main": "index.js",
"repository": "https://github.com/rodrigogs/easyvpn.git",
Expand All @@ -24,6 +24,7 @@
"csvtojson": "^1.1.3",
"prompt": "^1.0.0",
"request": "^2.79.0",
"socks5-http-client": "^1.0.2",
"split": "^1.0.0",
"through2": "^2.0.3",
"which": "^1.2.12",
Expand Down
33 changes: 32 additions & 1 deletion src/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const url = require('url');
const request = require('request');
const csv = require('csvtojson');
const through2 = require('through2');
const Promise = require('bluebird');
const Agent = require('socks5-http-client/lib/Agent');
const VPN = require('./vpn');

const VPNGATE_API_URL = 'http://www.vpngate.net/api/iphone/';
Expand Down Expand Up @@ -31,12 +33,41 @@ function filter(chunk, enc, cb) {
cb();
}

function resolveProxy(proxy, options) {
try {
proxy = url.parse(proxy);
} catch (ex) {
throw new Error('Invalid proxy url');
}
if (proxy.protocol.startsWith('socks')) {
const authParts = (proxy.auth || '')
.split(':')
.filter(part => part.length);
const auth = {
username: authParts[0],
password: authParts[1],
};
options.agentClass = Agent;
options.agentOptions = {
socksHost: proxy.hostname,
socksPort: proxy.port,
socksUsername: auth.username,
socksPassword: auth.password,
};
return;
}
options.proxy = proxy.href;
}

function getData(proxy) {
return new Promise((resolve, reject) => {
const options = { url: VPNGATE_API_URL };
if (proxy) options.proxy = proxy;
if (proxy) resolveProxy(proxy, options);

request(options)
.on('response', (response) => {
if (response.statusCode !== 200) reject(new Error(`Request failed with code ${response.statusCode}`));
})
.on('error', err => reject(networkError(err)))
.pipe(through2(filter))
.pipe(csv())
Expand Down
Loading

0 comments on commit d1cf675

Please sign in to comment.