Supporting HTTPS communication via SOCKS proxy
npm install https-socks-proxyHTTPS SOCKS PROXY Agent
================
http.Agent implementation that connects to a
http
https and node-fetch modules.
ws module to establish a WebSocket
npm:
bash
$ npm install https-socks-proxy
`
Examples
--------
#### TypeScript example
`ts
import https from 'https';
import { SocksProxyAgent } from 'https-socks-proxy';
const info = {
host: 'br41.nordvpn.com',
userId: 'your-name@gmail.com',
password: 'abcdef12345124'
};
const agent = new SocksProxyAgent(info);
https.get('https://jsonip.org', { agent }, (res) => {
console.log(res.headers);
res.pipe(process.stdout);
});
`
#### http module example
`js
var url = require('url');
var http = require('http');
var SocksProxyAgent = require('https-socks-proxy');
// SOCKS proxy to connect to
var proxy = process.env.socks_proxy || 'socks://127.0.0.1:1080';
console.log('using proxy server %j', proxy);
// HTTP endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'http://nodejs.org/api/';
console.log('attempting to GET %j', endpoint);
var opts = url.parse(endpoint);
// create an instance of the SocksProxyAgent class with the proxy server information
var agent = new SocksProxyAgent(proxy);
opts.agent = agent;
http.get(opts, function (res) {
console.log('"response" event!', res.headers);
res.pipe(process.stdout);
});
`
#### https module example
`js
var url = require('url');
var https = require('https');
var SocksProxyAgent = require('https-socks-proxy');
// SOCKS proxy to connect to
var proxy = process.env.socks_proxy || 'socks://127.0.0.1:1080';
console.log('using proxy server %j', proxy);
// HTTP endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'https://encrypted.google.com/';
console.log('attempting to GET %j', endpoint);
var opts = url.parse(endpoint);
// create an instance of the SocksProxyAgent class with the proxy server information
var agent = new SocksProxyAgent(proxy);
opts.agent = agent;
//If you want HTTPS communication with CA enabled, then pass the following options
// rejectUnauthorized : true / false
// ca : [List of CA]
https.get(opts, function (res) {
console.log('"response" event!', res.headers);
res.pipe(process.stdout);
});
`
#### ws WebSocket connection example
` js
var WebSocket = require('ws');
var SocksProxyAgent = require('https-socks-proxy');
// SOCKS proxy to connect to
var proxy = process.env.socks_proxy || 'socks://127.0.0.1:1080';
console.log('using proxy server %j', proxy);
// WebSocket endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'ws://echo.websocket.org';
console.log('attempting to connect to WebSocket %j', endpoint);
// create an instance of the SocksProxyAgent class with the proxy server information
var agent = new SocksProxyAgent(proxy);
// initiate the WebSocket connection
var socket = new WebSocket(endpoint, { agent: agent });
socket.on('open', function () {
console.log('"open" event!');
socket.send('hello world');
});
socket.on('message', function (data, flags) {
console.log('"message" event! %j %j', data, flags);
socket.close();
});
``