Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

getting further along in the tests now

  • Loading branch information...
commit 329191f386e6a260211e1964e529d17182657778 1 parent e93798e
@substack substack authored
View
84 lib/clients/socks5.js
@@ -1,31 +1,62 @@
var Put = require('put');
var Binary = require('binary');
-var EventEmitter = require('events').EventEmitter;
+var net = require('net');
-module.exports = function (stream) {
- var self = new EventEmitter;
+module.exports = function () {
+ var params = {};
+ [].slice.call(arguments).forEach(function (arg) {
+ if (typeof arg === 'string' && arg.match(/^\d+$/)) {
+ params.port = parseInt(arg, 10);
+ }
+ else if (typeof arg === 'string') {
+ params.host = arg;
+ }
+ else if (typeof arg === 'number') {
+ params.port = arg;
+ }
+ else { // event emitter
+ params.stream = arg;
+ }
+ });
+
+ if (params.stream) {
+ return fromStream(params.stream);
+ }
+ else {
+ var stream = net.createConnection(params.port, params.host);
+ return fromStream(stream);
+ }
+};
+
+function fromStream (stream) {
var auth = null;
- self.auth = function (user, pass) {
-
+ stream.auth = function (user, pass) {
+ return self;
};
- self.connect = function (addr, port) {
- var methods = new Buffer([
- 0x00, // no auth
- // 0x01, // gssapi
- // 0x02, // username / password
- ]);
+ var binary = Binary(stream);
+ stream.connect = function (addr, port) {
+ if (stream.readyState === 'open' || stream.readyState === 'readOnly') {
+ connect(addr, port);
+ }
+ else {
+ stream.on('connect', function () {
+ connect(addr, port);
+ });
+ }
- Binary(stream)
- .tap(function () {
- Put()
- .word8(5) // socks v5
- .word8(methods.length)
- .put(methods)
- .write(stream)
- ;
- })
+ return stream;
+ };
+
+ var methods = new Buffer([
+ 0x00, // no auth
+ // 0x01, // gssapi
+ // 0x02, // username / password
+ ]);
+
+ function connect (addr, port) {
+ binary = binary
.word8('ver')
.word8('method')
.tap(function (vars) {
@@ -51,8 +82,15 @@ module.exports = function (stream) {
;
};
- return self;
-};
+ Put()
+ .word8(5) // socks v5
+ .word8(methods.length)
+ .put(methods)
+ .write(stream)
+ ;
+
+ return stream;
+}
module.exports.atype = atype;
function atype (addr) {
@@ -96,4 +134,4 @@ function atype (addr) {
else {
throw new Error('IP version not supported');
}
-};
+}
View
4 lib/index.js
@@ -9,8 +9,8 @@ Object.keys(mod.clients)
})
.forEach(function (key) {
mod[key] = {
- server : mod.servers[key],
- client : mod.clients[key],
+ createServer : mod.servers[key],
+ createConnection : mod.clients[key],
};
})
;
View
12 lib/servers/socks5.js
@@ -2,11 +2,13 @@ var net = require('net');
var Binary = require('binary');
var Put = require('put');
-module.exports = function () {
- return net.createServer(session);
+module.exports = function (cb) {
+ return net.createServer(function (stream) {
+ session(stream, cb);
+ });
};
-function session (stream) {
+function session (stream, cb) {
Binary.stream(stream)
.word8('ver')
.word8('nmethods')
@@ -64,6 +66,10 @@ function session (stream) {
.word16bu('dst.port')
.tap(function (vars) {
var dst = vars.dst;
+ cb(null, {
+ host : dst.addr,
+ port : dst.port,
+ }, stream);
console.log(dst.addr + ':' + dst.port);
})
})
View
41 test/proxy.js
@@ -0,0 +1,41 @@
+var socks5 = require('prox').socks5;
+
+exports.socks5 = function (assert) {
+ var port = Math.floor(10000 + Math.random() * (Math.pow(2,16) - 10000));
+ var to = setTimeout(function () {
+ assert.fail('Never got request');
+ }, 500);
+
+ var tc = setTimeout(function () {
+ assert.fail('Never connected');
+ }, 500);
+
+ socks5.createServer(function (err, req, stream) {
+ assert.eql(req.host, 'moo');
+ assert.eql(req.port, 8080);
+ stream.write(new Buffer('oh hello'));
+ }).listen(port, ready);
+
+ function ready () {
+ var stream = socks5
+ .createConnection('localhost', port)
+ .connect('moo', 8080)
+ ;
+
+ stream.on('connect', function () {
+ clearTimeout(tc);
+ });
+
+ stream.on('error', function (msg) {
+ assert.fail(msg);
+ });
+
+ stream.on('data', function (buf) {
+ assert.eql(buf, new Buffer('oh hello'));
+ });
+
+ stream.on('end', function () {
+ clearTimeout(to);
+ });
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.