diff --git a/CHANGES b/CHANGES index d2519d0..b90a3ea 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +1.4.2 2013-10-29 +- Client now support multiple server ports and multiple server/port pairs. See test/config-client-*.json for example + 1.4.1 2013-07-30 - Change default cipher to AES-256-CFB in config.json - Fix a typo in local diff --git a/README.md b/README.md index e851103..7f0de91 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ shadowsocks-nodejs =========== -Current version: 1.4.1 +Current version: 1.4.2 [![Build Status](https://travis-ci.org/clowwindy/shadowsocks-nodejs.png)](https://travis-ci.org/clowwindy/shadowsocks-nodejs) shadowsocks-nodejs is a lightweight tunnel proxy which can help you get through diff --git a/lib/shadowsocks/local.js b/lib/shadowsocks/local.js index 8b51dfa..900721f 100644 --- a/lib/shadowsocks/local.js +++ b/lib/shadowsocks/local.js @@ -42,11 +42,21 @@ var getServer, server, udpServer; udpServer = udpRelay.createServer(null, port, serverAddr, serverPort, key, method, timeout, true); getServer = function() { + var aPort, aServer, r; + aPort = serverPort; + aServer = serverAddr; + if (serverPort instanceof Array) { + aPort = serverPort[Math.floor(Math.random() * serverPort.length)]; + } if (serverAddr instanceof Array) { - return serverAddr[Math.floor(Math.random() * serverAddr.length)]; - } else { - return serverAddr; + aServer = serverAddr[Math.floor(Math.random() * serverAddr.length)]; + } + r = /^(.*)\:(\d+)$/.exec(aServer); + if (r != null) { + aServer = r[1]; + aPort = +r[2]; } + return [aServer, aPort]; }; server = net.createServer(function(connection) { var addrLen, addrToSend, cachedPieces, clean, encryptor, headerLength, remote, remoteAddr, remotePort, stage; @@ -70,7 +80,7 @@ return utils.debug("connections: " + connections); }; connection.on("data", function(data) { - var aServer, addrtype, buf, cmd, e, reply, tempBuf; + var aPort, aServer, addrtype, buf, cmd, e, reply, tempBuf, _ref; utils.log(utils.EVERYTHING, "connection on data"); if (stage === 5) { data = encryptor.encrypt(data); @@ -141,10 +151,10 @@ buf.write("\u0000\u0000\u0000\u0000", 4, 4, "binary"); buf.writeInt16BE(2222, 8); connection.write(buf); - aServer = getServer(); - remote = net.connect(serverPort, aServer, function() { + _ref = getServer(), aServer = _ref[0], aPort = _ref[1]; + utils.info("connecting " + aServer + ":" + aPort); + remote = net.connect(aPort, aServer, function() { var addrToSendBuf, i, piece; - utils.info("connecting " + remoteAddr + ":" + remotePort); if (!encryptor) { if (remote) { remote.destroy(); diff --git a/lib/shadowsocks/utils.js b/lib/shadowsocks/utils.js index f2b8ccd..4524a0a 100644 --- a/lib/shadowsocks/utils.js +++ b/lib/shadowsocks/utils.js @@ -44,7 +44,7 @@ } }; - exports.version = "shadowsocks-nodejs v1.4.1"; + exports.version = "shadowsocks-nodejs v1.4.2"; exports.EVERYTHING = 0; diff --git a/package.json b/package.json index b659aeb..4297c15 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "a tunnel proxy that help you get through firewalls", "keywords": ["shadowsocks", "proxy", "socks5"], "author": "clowwindy", - "version": "1.4.1", + "version": "1.4.2", "licenses": [{ "type": "MIT", "url": "https://raw.github.com/clowwindy/shadowsocks-nodejs/master/LICENSE" diff --git a/src/local.coffee b/src/local.coffee index ab3fe1f..62feed8 100644 --- a/src/local.coffee +++ b/src/local.coffee @@ -49,10 +49,21 @@ createServer = (serverAddr, serverPort, port, key, method, timeout)-> udpServer = udpRelay.createServer(null, port, serverAddr, serverPort, key, method, timeout, true) getServer = -> + aPort = serverPort + aServer = serverAddr + if serverPort instanceof Array + # support config like "server_port": [8081, 8082] + aPort = serverPort[Math.floor(Math.random() * serverPort .length)] if serverAddr instanceof Array - serverAddr[Math.floor(Math.random() * serverAddr .length)] - else - serverAddr + # support config like "server": ["123.123.123.1", "123.123.123.2"] + aServer = serverAddr[Math.floor(Math.random() * serverAddr .length)] + r = /^(.*)\:(\d+)$/.exec(aServer) + # support config like "server": "123.123.123.1:8381" + # or "server": ["123.123.123.1:8381", "123.123.123.2:8381", "123.123.123.2:8382"] + if r? + aServer = r[1] + aPort = +r[2] + return [aServer, aPort] server = net.createServer((connection) -> connections += 1 @@ -149,9 +160,9 @@ createServer = (serverAddr, serverPort, port, key, method, timeout)-> buf.writeInt16BE 2222, 8 connection.write buf # connect remote server - aServer = getServer() - remote = net.connect(serverPort, aServer, -> - utils.info "connecting #{remoteAddr}:#{remotePort}" + [aServer, aPort] = getServer() + utils.info "connecting #{aServer}:#{aPort}" + remote = net.connect(aPort, aServer, -> if not encryptor remote.destroy() if remote return diff --git a/src/utils.coffee b/src/utils.coffee index a7179d5..4f46f07 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -50,7 +50,7 @@ exports.checkConfig = (config) -> if (config.method or '').toLowerCase() == 'rc4' exports.warn 'RC4 is not safe; please use a safer cipher, like AES-256-CFB' -exports.version = "shadowsocks-nodejs v1.4.1" +exports.version = "shadowsocks-nodejs v1.4.2" exports.EVERYTHING = 0 exports.DEBUG = 1 diff --git a/test/config-client-multi-port.json b/test/config-client-multi-port.json new file mode 100644 index 0000000..0d222af --- /dev/null +++ b/test/config-client-multi-port.json @@ -0,0 +1,8 @@ +{ + "server": "123.123.123.1", + "server_port":[8380, 8381, 8382, 8383], + "local_port":1081, + "password":"barfoo!", + "timeout":60, + "method":"aes-256-cfb" +} diff --git a/test/config-client-multi-server-port.json b/test/config-client-multi-server-port.json new file mode 100644 index 0000000..945118c --- /dev/null +++ b/test/config-client-multi-server-port.json @@ -0,0 +1,8 @@ +{ + "server": ["123.123.123.1:8381", "123.123.123.2:8381", "123.123.123.2:8382"], + "server_port":8380, + "local_port":1081, + "password":"barfoo!", + "timeout":60, + "method":"aes-256-cfb" +} diff --git a/test/config-client-multi-server.json b/test/config-client-multi-server.json new file mode 100644 index 0000000..81a5920 --- /dev/null +++ b/test/config-client-multi-server.json @@ -0,0 +1,8 @@ +{ + "server": ["123.123.123.1", "123.123.123.2", "123.123.123.3"], + "server_port":8380, + "local_port":1081, + "password":"barfoo!", + "timeout":60, + "method":"aes-256-cfb" +} diff --git a/test/config-multi-passwd.json b/test/config-multi-passwd.json deleted file mode 100644 index 4489ca6..0000000 --- a/test/config-multi-passwd.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "server":"127.0.0.1", - "server_port":8380, - "local_port":1081, - "password":"barfoo!", - "port_password": { - "8381": "foobar1", - "8382": "foobar2", - "8383": "foobar3", - "8384": "foobar4", - "8385": "foobar5", - "8386": "foobar6", - "8387": "foobar7", - "8388": "foobar8", - "8389": "foobar9", - "8390": "foobar10", - "8391": "foobar11", - "8392": "foobar12" - }, - "timeout":60 -} diff --git a/test/config-server-multi-passwd.json b/test/config-server-multi-passwd.json new file mode 100644 index 0000000..a0247f5 --- /dev/null +++ b/test/config-server-multi-passwd.json @@ -0,0 +1,22 @@ +{ + "server":"127.0.0.1", + "server_port":8380, + "local_port":1081, + "password":"barfoo!", + "port_password": { + "8381": "foobar1", + "8382": "foobar2", + "8383": "foobar3", + "8384": "foobar4", + "8385": "foobar5", + "8386": "foobar6", + "8387": "foobar7", + "8388": "foobar8", + "8389": "foobar9", + "8390": "foobar10", + "8391": "foobar11", + "8392": "foobar12" + }, + "timeout":60, + "method":"aes-256-cfb" +} diff --git a/test/config-server-multi-port.json b/test/config-server-multi-port.json new file mode 100644 index 0000000..9b8f9f5 --- /dev/null +++ b/test/config-server-multi-port.json @@ -0,0 +1,18 @@ +{ + "server":"127.0.0.1", + "server_port":8380, + "local_port":1081, + "password":"barfoo!", + "port_password": { + "8380": "barfoo!", + "8381": "barfoo!", + "8382": "barfoo!", + "8383": "barfoo!", + "8384": "barfoo!", + "8385": "barfoo!", + "8386": "barfoo!", + "8387": "barfoo!" + }, + "timeout":60, + "method":"aes-256-cfb" +} diff --git a/test/config.json b/test/config.json index 3a6f953..bdff395 100644 --- a/test/config.json +++ b/test/config.json @@ -1,8 +1,8 @@ { - "server":["127.0.0.1", "127.0.0.1"], + "server":"127.0.0.1", "server_port":8388, "local_port":1080, "password":"barfoo!", "timeout":60, - "method":"rc4" + "method":"aes-256-cfb" } \ No newline at end of file