diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..646ac51 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +node_modules/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..5aaa83d --- /dev/null +++ b/index.js @@ -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 +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..b89348c --- /dev/null +++ b/package.json @@ -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" + } +}