You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.1KB

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