Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey Britt committed Jun 25, 2017
0 parents commit 6ce2850
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules/
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const httpAgent = require('socks5-http-client/lib/Agent');
const httpsAgent = require('socks5-https-client/lib/Agent');

const getAgent = (proxy, type, url) => {
const options = {};
let user = null;
let pass = null;
let host = null;
let port = null;

if (proxy.split('@').length > 1) {
user = proxy.split('@')[0].split(':')[0];
pass = proxy.split('@')[0].split(':')[1];
host = proxy.split('@')[1].split(':')[0];
port = proxy.split('@')[1].split(':')[1];
} else {
host = proxy.split(':')[0];
port = proxy.split(':')[1];
}

if (type === 'socks') {
if (url.substr(0, 5) === 'https') {
options.agentClass = httpsAgent;
} else {
options.agentClass = httpAgent;
}

options.agentOptions = {
socksHost: host,
socksPort: port,
};

if (user) {
options.agentOptions.socksUsername = user;
options.agentOptions.socksPassword = pass;
}
}

if (type === 'http') {
options.proxy = `http://${proxy}`;
}

if (type === 'https') {
options.proxy = `https://${proxy}`;
}

return options;
};

module.exports = {
getAgent
};
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "auto-proxy-agent",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"socks5-http-client": "^1.0.2",
"socks5-https-client": "^1.2.1"
}
}

0 comments on commit 6ce2850

Please sign in to comment.