Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

socket5 server piece

  • Loading branch information...
commit d9f7133736241de7cc219e822865ffaec7a55cb6 1 parent b12979b
@substack substack authored
Showing with 77 additions and 5 deletions.
  1. +6 −5 lib/clients/socks5.js
  2. +71 −0 lib/servers/socks5.js
View
11 lib/clients/socks5.js
@@ -1,8 +1,9 @@
var Put = require('put');
var Binary = require('binary');
+var EventEmitter = require('events').EventEmitter;
module.exports = function (stream) {
- var self = {};
+ var self = new EventEmitter;
var auth = null;
self.auth = function (user, pass) {
@@ -34,14 +35,14 @@ module.exports = function (stream) {
console.log('No supported methods');
}
else {
- var dst = atype(
+ var dst = atype(addr);
Put()
.word8(5)
.word8(1) // connect
.pad(1) // reserved
- .word8(atype)
- .put(addrBuf)
+ .word8(dst.code)
+ .put(dst.buffer)
.word16be(port)
.write(stream)
;
@@ -95,4 +96,4 @@ function atype (addr) {
else {
throw new Error('IP version not supported');
}
-}
+};
View
71 lib/servers/socks5.js
@@ -0,0 +1,71 @@
+var net = require('net');
+var Binary = require('binary');
+var Put = require('put');
+
+module.exports = function () {
+ return net.createServer(session);
+};
+
+function session (stream) {
+ Binary.stream(stream)
+ .word8('ver')
+ .word8('nmethods')
+ .buffer('methods', 'nmethods')
+ .tap(function (vars) {
+ var methods = [].slice.call(vars.methods)
+ .reduce(function (acc,m) { acc[m] = true; return acc }, {});
+ // username + password only
+ var method = methods[2] ? 0x02 : 0xff;
+
+ Put().word8(5).word8(method).write(stream);
+ })
+ .loop(function (end) {
+ this
+ .word8('ver')
+ .word8('cmd')
+ .word8('rsv')
+ .word8('dst.atyp')
+ .tap(function (vars) {
+ var atyp = vars.dst.atyp;
+ if (atyp === 0x01) { // ipv4
+ this
+ .buffer('addr.buf', 4)
+ .tap(function (vars) {
+ vars.dst.addr = [].slice
+ .call(vars.addr.buf).join('.');
+ })
+ ;
+ }
+ else if (atyp === 0x03) { // domain name
+ this
+ .word8('addr.size')
+ .buffer('addr.buf', 'addr.size')
+ .tap(function (vars) {
+ vars.dst.addr = vars.addr.buf.toString();
+ })
+ ;
+ }
+ else if (atyp === 0x04) { // ipv6
+ this
+ .word32be('addr.a')
+ .word32be('addr.b')
+ .word32be('addr.c')
+ .word32be('addr.d')
+ .tap(function (vars) {
+ vars.dst.addr = 'abcd'.split('')
+ .map(function (x) {
+ return vars.addr[x].toString(16);
+ })
+ ;
+ })
+ ;
+ }
+ })
+ .word16bu('dst.port')
+ .tap(function (vars) {
+ var dst = vars.dst;
+ console.log(dst.addr + ':' + dst.port);
+ })
+ })
+ ;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.