Skip to content

Commit

Permalink
recover from a mistake delete
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyz committed Dec 29, 2012
1 parent 47c3f31 commit 6554694
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 238 deletions.
4 changes: 3 additions & 1 deletion config.json
Expand Up @@ -32,6 +32,8 @@
"ip": "127.0.0.1",
"port": 7070,
"pass": "cool"
},
"mask": {
}
},
"worker": {
Expand All @@ -46,4 +48,4 @@
"discovery": {},
"domain": {}
}
}
}
File renamed without changes.
57 changes: 35 additions & 22 deletions local/http.js → http.js
Expand Up @@ -2,7 +2,7 @@ var debug = require('debug')('LOCAL:HTTP')
, http = require('http')
, url = require('url')
, net = require('net')
, inherits = require('util').inherits
, util = require('util')
, d = require('domain').create()
, config = require('../util/config');

Expand All @@ -21,21 +21,23 @@ var upstream = require('../proto/'+protocol);

function Agent(options) {
http.Agent.call(this, options);
this.createConnection = upstream.connect;
this.createConnection = upstream.createConnection;
}
inherits(Agent, http.Agent);
util.inherits(Agent, http.Agent);
Agent.prototype.maxSockets = 32;

var agent = new Agent({maxSockets:64});
var agent = new Agent();

// ----
// ----

function tunnel(req, sock, head){
var self = this;
debug("connections:%s", self.connections);
var o = url.parse('http://'+req.url);
// var usock = net.connect(o.port, o.hostname);
var usock = upstream.connect(o.port, o.hostname);
// debug("usock", usock);
var usock = upstream.createConnection(o.port, o.hostname);
function close(){
debug('%s : tunnel %s %s END', req.ip, req.method, req.url);
debug("connections:%s", self.connections);
// debug('%s : tunnel %s %s END', req.ip, req.method, req.url);
try { sock.destroy(); } catch(x){ }
try { usock.destroy(); } catch(x){ }
}
Expand All @@ -47,25 +49,31 @@ function tunnel(req, sock, head){
debug('%s : tunnel %s %s ERROR %j', req.ip, req.method, req.url, e);
close();
}
// sock.on('timeout', timeout);
sock.on('error', error);
sock.on('end', close);
usock.setTimeout(connectTimeout, timeout);
usock.on('error', error);
usock.on('end', close);
usock.on('connect', function(){
debug('%s : tunnel %s %s BEGIN', req.ip, req.method, req.url);
sock.write('HTTP/1.0 200 Connect ok\r\n\r\n\r\n');
// debug('%s : tunnel %s %s BEGIN', req.ip, req.method, req.url);
usock.setTimeout(transferTimeout, timeout);
usock.setNoDelay(true);
usock.write(head);
sock.pipe(usock);
// sock.setTimeout(transferTimeout, timeout);
sock.setNoDelay(true);
sock.write('HTTP/1.0 200 Connect ok\r\n\r\n\r\n');
usock.pipe(sock);
});
usock.setTimeout(connectTimeout, timeout);
sock.setTimeout(transferTimeout, timeout);
// sock.setNoDelay(true);
sock.on('error', error);
sock.on('end', close);
}

// ----

function proxy(req, res){
var self = this;
debug("connections:%s", self.connections);
var o = url.parse(req.url);
// expose ip
var headers = req.headers;
Expand All @@ -81,12 +89,13 @@ function proxy(req, res){
path: o.path,
method: req.method,
headers: headers, // req.headers,
agent: agent // using the upstream connections
agent: agent // using the upstream.createConnections
// agent: false, // using the original http
};
var ureq = http.request(ropts);
function close(){
debug('%s : proxy %s %s END', req.ip, req.method, req.url);
debug("connections:%s", self.connections);
// debug('%s : proxy %s %s END', req.ip, req.method, req.url);
try { res.end(); } catch(x){ }
try { ureq.abort(); } catch(x){ }
}
Expand All @@ -101,16 +110,20 @@ function proxy(req, res){
close();
}
ureq.on('error', error);
ureq.on('end', close);
ureq.on('response', function(ures){
debug('%s : proxy %s %s BEGIN', req.ip, req.method, req.url);
// ureq.setTimeout(transferTimeout, timeout);
// debug('%s : proxy %s %s BEGIN', req.ip, req.method, req.url);
ures.on('error', error);
ures.on('end', close);
ureq.setTimeout(transferTimeout, timeout);
res.statusCode = ures.statusCode;
for(var k in ures.headers){ res.setHeader(k,ures.headers[k]); }
ures.pipe(res);
});
ureq.setTimeout(connectTimeout, timeout);
req.on('error', error);
// req.on('end', ureq.end);
req.pipe(ureq);
// req.on('end', function(){ ureq.end(); });
}

// ----
Expand All @@ -122,11 +135,11 @@ function start(opt){
};
var onRequest = function(req, res){
req.ip = req.connection.remoteAddress;
proxy(req, res);
proxy.call(this, req, res);
};
var onConnect = function(req, sock, head){
req.ip = req.connection.remoteAddress;
tunnel(req, sock, head);
tunnel.call(this, req, sock, head);
};
var onClose = function(){
debug("closed %j", this.address());
Expand Down
14 changes: 0 additions & 14 deletions local/index.js

This file was deleted.

47 changes: 19 additions & 28 deletions worker/shadow.js → shadow.js
Expand Up @@ -39,62 +39,53 @@ function serve(sock){
debug('%s TIMEOUT', sock.remoteAddress);
close();
}
function await(en){
var d = shadow.decode(en);
buff.push(d);
}
function request(en){
sock.removeListener('data', request);
sock.on('data', await);
// delete sock.ondata;
// todo check v5
var d = shadow.decode(en);
// debug("req", d.toString('hex'));
var address = socks5.decodeAddress(d,0);
var host = address.host;
var port = address.port;
var leng = address.length;
// debug("req", d.slice(leng).toString('utf8'));
if(leng < d.length) buff.push(d.slice(leng));
// debug("REQUEST %s:%s", host, port);
usock = upstream.connect(port, host);
usock.on('end', close);
// debug('address:%j', address);
if(address.length < d.length) buff.push(d.slice(address.length));
// debug("connect %s:%s", address.host, address.port)
usock = upstream.createConnection(address.port, address.host);
usock.setTimeout(connectTimeout, timeout);
usock.on('error', error);
usock.on('end', close);
usock.on('connect', function(){
// debug('%s -> %s', sock.remoteAddress, host);
// debug('%s BEGIN', sock.remoteAddress);
usock.setTimeout(transferTimeout, timeout);
usock.setNoDelay(true);
// usock.setTimeout(0);
// usock.setTimeout(transferTimeout, timeout);
// usock.pipe(sock);
usock.on('data', function(d){
// debug('<-', d);
// debug('<-', d.toString('utf8'));
var en = shadow.encode(d);
if(!sock.write(en)) usock.pause();
});
usock.on('end', function(){ sock.end(); });
usock.on('drain', function(){ sock.resume(); });
while(buff.length) { usock.write(buff.shift()); }
// sock.setTimeout(transferTimeout, timeout);
sock.setNoDelay(true);
while(buff.length) usock.write(buff.shift());
sock.removeListener('data', await);
// sock.setTimeout(0);
// sock.setTimeout(transferTimeout, timeout);
// sock.pipe(usock);
sock.on('data', function(en){
var d = shadow.decode(en);
// debug('->', d);
// debug('->', d.toString('utf8'));
if(!usock.write(d)) sock.pause();
});
sock.on('end', function(){ usock.end(); });
sock.on('drain', function(){ usock.resume(); });
});
// usock.setTimeout(connectTimeout, timeout);
}
function await(en){
var d = shadow.decode(en);
buff.push(d);
}
sock.setTimeout(transferTimeout, timeout);
// sock.setNoDelay(true);
sock.on('error', error);
sock.on('data', request);
// sock.ondata = handshake;
sock.on('end', close);
sock.on('error', error);
// sock.setTimeout(transferTimeout, timeout);
}

// ----
Expand Down Expand Up @@ -130,7 +121,7 @@ function start(opt){
}
exports.start = start;

// ----
// ----

if(!module.parent) {
start({port:7070});
Expand Down
83 changes: 43 additions & 40 deletions local/socks5.js → socks5.js
Expand Up @@ -23,6 +23,7 @@ function serve(sock){
var self = this;
debug("connections:%s", self.connections);
var usock = null;
var buff = [];
function close(){
// debug('%s END', sock.remoteAddress);
debug("connections:%s", self.connections);
Expand All @@ -39,58 +40,59 @@ function serve(sock){
}
function handshake(d){
sock.removeListener('data', handshake);
sock.on('data', request);
// sock.ondata = request;
sock.on('data', command);
// todo check v5
// todo auth
sock.write(new Buffer([0x05, 0x00])); // socks5 noauth
sock.write(new Buffer([0x05, 0x00])); // socks5 noauth
}
function request(d){
sock.removeListener('data', request);
// delete sock.ondata;
function command(d){
sock.removeListener('data', command);
sock.on('data', await);
// todo check v5
var cmd = d[1];
var address = socks5.decodeAddress(d,3);
var host = address.host;
var port = address.port;
// debug("REQUEST %d %s:%s", cmd, host, port);
if (cmd == 0x01) { // connect
usock = upstream.connect(port, host);
usock.on('end', close);
usock.on('error', error);
usock.on('connect', function(){
// debug('%s -> %s', sock.remoteAddress, host);
// debug('%s BEGIN', sock.remoteAddress);
usock.setNoDelay(true);
usock.pipe(sock);
// usock.setTimeout(0);
// usock.setTimeout(transferTimeout, timeout);
sock.pipe(usock);
// sock.setTimeout(0);
// sock.setTimeout(transferTimeout, timeout);
var resp = new Buffer(d.length);
d.copy(resp);
resp[0] = 0x05;
resp[1] = 0x00;
resp[2] = 0x00;
sock.write(resp);
});
// usock.setTimeout(connectTimeout, timeout);
/*
} else if (cmd == 0x02) { // bind
} else if (cmd == 0x03) { // udp associate
*/
connect(d);
//} else if (cmd == 0x02) { // bind
//} else if (cmd == 0x03) { // udp associate
} else { // unsupport
sock.end(new Buffer([0x05,0x07,0x00,0x01]));
error('UNSUPPORT_CMD');
}
}
function await(d){
buff.push(d);
}
function connect(d){
var address = socks5.decodeAddress(d,3);
if(address.length < d.length) buff.push(d.slice(address.length));
// debug("%s con %s:%s", sock.remoteAddress, address.host, address.port)
usock = upstream.createConnection(address.port, address.host);
usock.setTimeout(connectTimeout, timeout);
usock.on('error', error);
usock.on('end', close);
usock.on('connect', function(){
// debug("%s est %s:%s", sock.remoteAddress, address.host, address.port)
usock.setTimeout(transferTimeout, timeout);
usock.setNoDelay(true);
while(buff.length) usock.write(buff.shift());
sock.removeListener('data', await);
sock.pipe(usock);
// sock.setTimeout(transferTimeout, timeout);
sock.setNoDelay(true);
var resp = new Buffer(d.length);
d.copy(resp);
resp[0] = 0x05;
resp[1] = 0x00;
resp[2] = 0x00;
sock.write(resp);
usock.pipe(sock);
});
}
sock.setTimeout(transferTimeout, timeout);
// sock.setNoDelay(true);
sock.on('error', error);
sock.on('data', handshake);
// sock.ondata = handshake;
sock.on('end', close);
sock.on('error', error);
sock.setNoDelay(true);
// sock.setTimeout(transferTimeout, timeout);
}

// ----
Expand All @@ -117,6 +119,7 @@ function start(opt){
});
d.run(function(){
var server = net.createServer();
// var server = socks5.createServer();
server.on('listening', onListening);
server.on('connection', onConnection);
server.on('close', onClose);
Expand All @@ -126,7 +129,7 @@ function start(opt){
}
exports.start = start;

// ----
// ----

if(!module.parent) {
start({port:1080});
Expand Down

0 comments on commit 6554694

Please sign in to comment.