• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1socks-proxy-agent
2================
3### A SOCKS proxy `http.Agent` implementation for HTTP and HTTPS
4[![Build Status](https://travis-ci.org/TooTallNate/node-socks-proxy-agent.svg?branch=master)](https://travis-ci.org/TooTallNate/node-socks-proxy-agent)
5
6This module provides an `http.Agent` implementation that connects to a
7specified SOCKS proxy server, and can be used with the built-in `http`
8or `https` modules.
9
10It can also be used in conjunction with the `ws` module to establish a WebSocket
11connection over a SOCKS proxy. See the "Examples" section below.
12
13Installation
14------------
15
16Install with `npm`:
17
18``` bash
19$ npm install socks-proxy-agent
20```
21
22
23Examples
24--------
25
26#### `http` module example
27
28``` js
29var url = require('url');
30var http = require('http');
31var SocksProxyAgent = require('socks-proxy-agent');
32
33// SOCKS proxy to connect to
34var proxy = process.env.socks_proxy || 'socks://127.0.0.1:9050';
35console.log('using proxy server %j', proxy);
36
37// HTTP endpoint for the proxy to connect to
38var endpoint = process.argv[2] || 'http://nodejs.org/api/';
39console.log('attempting to GET %j', endpoint);
40var opts = url.parse(endpoint);
41
42// create an instance of the `SocksProxyAgent` class with the proxy server information
43var agent = new SocksProxyAgent(proxy);
44opts.agent = agent;
45
46http.get(opts, function (res) {
47  console.log('"response" event!', res.headers);
48  res.pipe(process.stdout);
49});
50```
51
52#### `https` module example
53
54``` js
55var url = require('url');
56var https = require('https');
57var SocksProxyAgent = require('socks-proxy-agent');
58
59// SOCKS proxy to connect to
60var proxy = process.env.socks_proxy || 'socks://127.0.0.1:9050';
61console.log('using proxy server %j', proxy);
62
63// HTTP endpoint for the proxy to connect to
64var endpoint = process.argv[2] || 'https://encrypted.google.com/';
65console.log('attempting to GET %j', endpoint);
66var opts = url.parse(endpoint);
67
68// create an instance of the `SocksProxyAgent` class with the proxy server information
69var agent = new SocksProxyAgent(proxy);
70opts.agent = agent;
71
72https.get(opts, function (res) {
73  console.log('"response" event!', res.headers);
74  res.pipe(process.stdout);
75});
76```
77
78#### `ws` WebSocket connection example
79
80``` js
81var WebSocket = require('ws');
82var SocksProxyAgent = require('socks-proxy-agent');
83
84// SOCKS proxy to connect to
85var proxy = process.env.socks_proxy || 'socks://127.0.0.1:9050';
86console.log('using proxy server %j', proxy);
87
88// WebSocket endpoint for the proxy to connect to
89var endpoint = process.argv[2] || 'ws://echo.websocket.org';
90console.log('attempting to connect to WebSocket %j', endpoint);
91
92// create an instance of the `SocksProxyAgent` class with the proxy server information
93var agent = new SocksProxyAgent(proxy);
94
95// initiate the WebSocket connection
96var socket = new WebSocket(endpoint, { agent: agent });
97
98socket.on('open', function () {
99  console.log('"open" event!');
100  socket.send('hello world');
101});
102
103socket.on('message', function (data, flags) {
104  console.log('"message" event! %j %j', data, flags);
105  socket.close();
106});
107```
108
109License
110-------
111
112(The MIT License)
113
114Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
115
116Permission is hereby granted, free of charge, to any person obtaining
117a copy of this software and associated documentation files (the
118'Software'), to deal in the Software without restriction, including
119without limitation the rights to use, copy, modify, merge, publish,
120distribute, sublicense, and/or sell copies of the Software, and to
121permit persons to whom the Software is furnished to do so, subject to
122the following conditions:
123
124The above copyright notice and this permission notice shall be
125included in all copies or substantial portions of the Software.
126
127THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
128EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
129MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
130IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
131CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
132TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
133SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
134