Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support the socks5 proxy.

  • Loading branch information...
commit 74ebbe9e3ba502291625e33db86a8c6286f7ea61 1 parent 21a6357
@sundq authored
View
1  README.md
@@ -194,6 +194,7 @@ The first argument can be either a url or an options object. The only required o
* `pool.maxSockets` - Integer containing the maximum amount of sockets in the pool.
* `timeout` - Integer containing the number of milliseconds to wait for a request to respond before aborting the request
* `proxy` - An HTTP proxy to be used. Support proxy Auth with Basic Auth the same way it's supported with the `url` parameter by embedding the auth info in the uri.
+* `socks5` - An socks5 proxy to be used.must be set to {socksHost:"xxx.xxx.xxx.xxx",socksPort:portNumber}
* `oauth` - Options for OAuth HMAC-SHA1 signing, see documentation above.
* `hawk` - Options for [Hawk signing](https://github.com/hueniverse/hawk). The `credentials` key must contain the necessary signing info, [see hawk docs for details](https://github.com/hueniverse/hawk#usage-example).
* `strictSSL` - Set to `true` to require that SSL certificates be valid. Note: to use your own certificate authority, you need to specify an agent that was created with that ca as an option.
View
28 lib/socks5-agent-http.js
@@ -0,0 +1,28 @@
+/**
+ * @overview
+ * @author Matthew Caruana Galizia <m@m.cg>
+ * @license MIT
+ * @copyright Copyright (c) 2013, Matthew Caruana Galizia
+ * @version 0.1.4
+ * @preserve
+ */
+
+'use strict';
+
+/*jshint node:true*/
+
+var http = require('http');
+var inherits = require('util').inherits;
+var socksClient = require('socks5-client');
+
+function Socks5ClientHttpAgent(options) {
+ http.Agent.call(this, options);
+
+ this.socksHost = options.socksHost || 'localhost';
+ this.socksPort = options.socksPort || 1080;
+ this.createConnection = socksClient.createConnection;
+}
+
+inherits(Socks5ClientHttpAgent, http.Agent);
+
+module.exports = Socks5ClientHttpAgent;
View
62 lib/socks5-agent-https.js
@@ -0,0 +1,62 @@
+/**
+ * @overview
+ * @author Matthew Caruana Galizia <m@m.cg>
+ * @license MIT
+ * @copyright Copyright (c) 2013, Matthew Caruana Galizia
+ * @version 0.1.2
+ * @preserve
+ */
+
+'use strict';
+
+/*jshint node:true*/
+
+var http = require('http');
+var inherits = require('util').inherits;
+
+var socksClient = require('socks5-client');
+var starttls = require('starttls');
+
+function createConnection(options) {
+ var socksSocket, handleSocksConnectToHost;
+
+ socksSocket = socksClient.createConnection(options);
+
+ handleSocksConnectToHost = socksSocket.handleSocksConnectToHost;
+ socksSocket.handleSocksConnectToHost = function() {
+ starttls({
+ socket: socksSocket.socket,
+ host: options.hostname
+ }, function(err) {
+ var clearText;
+
+ if (err) {
+ return socksSocket.emit('error', err);
+ }
+
+ clearText = this.cleartext;
+ socksSocket.socket = clearText;
+
+ handleSocksConnectToHost.call(socksSocket);
+
+ // The Socks5ClientSocket constructor (invoked by socksClient.createConnection) adds an 'error' event listener to the original socket. That behaviour needs to be mimicked by adding a similar listener to the cleartext stream, which replaces the original socket.
+ clearText.on('error', function(err) {
+ socksSocket.emit('error', err);
+ });
+ });
+ };
+
+ return socksSocket;
+}
+
+function Socks5ClientHttpsAgent(options) {
+ http.Agent.call(this, options);
+
+ this.socksHost = options.socksHost || 'localhost';
+ this.socksPort = options.socksPort || 1080;
+ this.createConnection = createConnection;
+}
+
+inherits(Socks5ClientHttpsAgent, http.Agent);
+
+module.exports = Socks5ClientHttpsAgent;
View
4 package.json
@@ -32,7 +32,9 @@
"cookie-jar": "~0.3.0",
"node-uuid": "~1.4.0",
"mime": "~1.2.9",
- "form-data": "~0.1.0"
+ "form-data": "~0.1.0",
+ "socks5-client":"~0.3.4",
+ "starttls": "0.x"
},
"scripts": {
"test": "node tests/run.js"
View
6 request.js
@@ -27,6 +27,8 @@ var http = require('http')
, copy = require('./lib/copy')
, debug = require('./lib/debug')
, getSafe = require('./lib/getSafe')
+ ,socksHttpAgent = require("./lib/socks5-agent-http")
+ ,socksHttpsAgent = require("./lib/socks5-agent-https")
;
function safeStringify (obj) {
@@ -170,6 +172,10 @@ Request.prototype.init = function (options) {
}
}
+ if(self.socks5){
+ self.agent = (self.uri.protocol === "https:")?new socksHttpsAgent(self.socks5):new socksHttpAgent(self.socks5);
+ }
+
if (!self.uri.pathname) {self.uri.pathname = '/'}
if (!self.uri.host) {
View
2  tests/test-https.js
@@ -70,7 +70,7 @@ s.listen(s.port, function () {
s.on('/'+i, test.resp)
test.uri = s.url + '/' + i
test.rejectUnauthorized = false
- request(test, function (err, resp, body) {
+ request(test, function (err, resp, body) {
if (err) throw err
if (test.expectBody) {
assert.deepEqual(test.expectBody, body)
Please sign in to comment.
Something went wrong with that request. Please try again.