diff --git a/lib/shadowsocks/local.js b/lib/shadowsocks/local.js index 2434353..abce347 100644 --- a/lib/shadowsocks/local.js +++ b/lib/shadowsocks/local.js @@ -85,13 +85,12 @@ return [aServer, aPort]; }; server = net.createServer(function(connection) { - var addrLen, addrToSend, cachedPieces, clean, encryptor, headerLength, remote, remoteAddr, remotePort, stage; + var addrLen, addrToSend, clean, encryptor, headerLength, remote, remoteAddr, remotePort, stage; connections += 1; encryptor = new Encryptor(key, method); stage = 0; headerLength = 0; remote = null; - cachedPieces = []; addrLen = 0; remoteAddr = null; remotePort = null; @@ -106,7 +105,7 @@ return utils.debug("connections: " + connections); }; connection.on("data", function(data) { - var aPort, aServer, addrtype, buf, cmd, e, reply, tempBuf, _ref; + var aPort, aServer, addrToSendBuf, addrtype, buf, cmd, e, piece, reply, tempBuf, _ref; utils.log(utils.EVERYTHING, "connection on data"); if (stage === 5) { data = encryptor.encrypt(data); @@ -180,24 +179,6 @@ _ref = getServer(), aServer = _ref[0], aPort = _ref[1]; utils.info("connecting " + aServer + ":" + aPort); remote = net.connect(aPort, aServer, function() { - var addrToSendBuf, i, piece; - if (!encryptor) { - if (remote) { - remote.destroy(); - } - return; - } - addrToSendBuf = new Buffer(addrToSend, "binary"); - addrToSendBuf = encryptor.encrypt(addrToSendBuf); - remote.write(addrToSendBuf); - i = 0; - while (i < cachedPieces.length) { - piece = cachedPieces[i]; - piece = encryptor.encrypt(piece); - remote.write(piece); - i++; - } - cachedPieces = null; stage = 5; return utils.debug("stage = 5"); }); @@ -261,11 +242,14 @@ return connection.destroy(); } }); + addrToSendBuf = new Buffer(addrToSend, "binary"); + addrToSendBuf = encryptor.encrypt(addrToSendBuf); + remote.write(addrToSendBuf); if (data.length > headerLength) { buf = new Buffer(data.length - headerLength); data.copy(buf, 0, headerLength); - cachedPieces.push(buf); - buf = null; + piece = encryptor.encrypt(buf); + remote.write(piece); } stage = 4; return utils.debug("stage = 4"); @@ -276,12 +260,20 @@ connection.destroy(); } if (remote) { - return remote.destroy(); + remote.destroy(); } + return clean(); } - } else { - if (stage === 4) { - return cachedPieces.push(data); + } else if (stage === 4) { + if (remote == null) { + if (connection) { + connection.destroy(); + } + return; + } + data = encryptor.encrypt(data); + if (!remote.write(data)) { + return connection.pause(); } } }); diff --git a/lib/shadowsocks/utils.js b/lib/shadowsocks/utils.js index 09de58a..f5f7276 100644 --- a/lib/shadowsocks/utils.js +++ b/lib/shadowsocks/utils.js @@ -106,7 +106,11 @@ exports.log = function(level, msg) { if (level >= _logging_level) { - return util.log(msg); + if (level >= exports.DEBUG) { + return util.log(new Date().getMilliseconds() + 'ms ' + msg); + } else { + return util.log(msg); + } } }; diff --git a/src/local.coffee b/src/local.coffee index ccc17db..e0d4ba4 100644 --- a/src/local.coffee +++ b/src/local.coffee @@ -73,7 +73,6 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres stage = 0 headerLength = 0 remote = null - cachedPieces = [] addrLen = 0 remoteAddr = null remotePort = null @@ -92,7 +91,7 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres if stage is 5 # pipe sockets data = encryptor.encrypt data - connection.pause() unless remote.write(data) + connection.pause() unless remote.write(data) return if stage is 0 tempBuf = new Buffer(2) @@ -165,20 +164,6 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres [aServer, aPort] = getServer() utils.info "connecting #{aServer}:#{aPort}" remote = net.connect(aPort, aServer, -> - if not encryptor - remote.destroy() if remote - return - addrToSendBuf = new Buffer(addrToSend, "binary") - addrToSendBuf = encryptor.encrypt addrToSendBuf - remote.write addrToSendBuf - i = 0 - - while i < cachedPieces.length - piece = cachedPieces[i] - piece = encryptor.encrypt piece - remote.write piece - i++ - cachedPieces = null # save memory stage = 5 utils.debug "stage = 5" ) @@ -218,12 +203,17 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres utils.debug "remote on timeout" remote.destroy() if remote connection.destroy() if connection - + + addrToSendBuf = new Buffer(addrToSend, "binary") + addrToSendBuf = encryptor.encrypt addrToSendBuf +# remote.setNoDelay false + remote.write addrToSendBuf + if data.length > headerLength buf = new Buffer(data.length - headerLength) data.copy buf, 0, headerLength - cachedPieces.push buf - buf = null + piece = encryptor.encrypt buf + remote.write piece stage = 4 utils.debug "stage = 4" catch e @@ -231,10 +221,14 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres utils.error e connection.destroy() if connection remote.destroy() if remote - else cachedPieces.push data if stage is 4 - # remote server not connected - # cache received buffers - # make sure no data is lost + clean() + else if stage is 4 + if not remote? + connection.destroy() if connection + return + data = encryptor.encrypt data +# remote.setNoDelay true + connection.pause() unless remote.write(data) connection.on "end", -> utils.debug "connection on end" diff --git a/src/utils.coffee b/src/utils.coffee index 20ae2f7..2722da1 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -103,7 +103,10 @@ exports.config = (level) -> exports.log = (level, msg)-> if level >= _logging_level - util.log msg + if level >= exports.DEBUG + util.log(new Date().getMilliseconds() + 'ms ' + msg) + else + util.log msg exports.debug = (msg)-> exports.log exports.DEBUG, msg diff --git a/test/benchmark.sh b/test/benchmark.sh new file mode 100644 index 0000000..39f8766 --- /dev/null +++ b/test/benchmark.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for i in {1..10} ; do + echo $i + curl --socks5-hostname 127.0.0.1:$1 http://www.google.com/ >/dev/null +done