Skip to content

Commit

Permalink
pause/resume socket properly
Browse files Browse the repository at this point in the history
closes issue #2
  • Loading branch information
mrluanma committed Jan 11, 2015
1 parent 5aa721b commit 2af12b2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ ws@0.6.4 node_modules/ws
Then run:

```
$ node local.js -s still-tor-8707.herokuapp.com -l 1080 -m rc4 -k foobar
shadowsocks-heroku v0.9.6
$ node local.js -s still-tor-8707.herokuapp.com -l 1080 -m rc4 -k foobar -r 80
server listening at { address: '0.0.0.0', family: 'IPv4', port: 1080 }
```

Change proxy settings of your browser into:
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"server": "127.0.0.1",
"local_address": "127.0.0.1",
"local_port": 1080,
"remote_port": 8080,
"password": "`try*(^^$some^$%^complex>:<>?~password/",
Expand Down
27 changes: 20 additions & 7 deletions local.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions src/local.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ server = net.createServer (connection) ->
if stage is 5
# pipe sockets
data = encryptor.encrypt data
ws.send data, { binary: true }
if ws.readyState is WebSocket.OPEN
ws.send data, { binary: true }
connection.pause() if ws.bufferedAmount > 0
return
if stage is 0
tempBuf = new Buffer(2)
Expand Down Expand Up @@ -130,11 +132,15 @@ server = net.createServer (connection) ->
ping = setInterval(->
ws.ping "", null, true
, 50 * 1000)

ws._socket.on "drain", ->
connection.resume()

return

ws.on "message", (data, flags) ->
data = encryptor.decrypt data
connection.write(data)
ws._socket.pause() unless connection.write(data)

ws.on "close", ->
clearInterval ping
Expand Down Expand Up @@ -165,22 +171,25 @@ server = net.createServer (connection) ->

connection.on "end", ->
console.log "local disconnected"
ws.close() if ws
ws.terminate() if ws
server.getConnections (err, count) ->
console.log "concurrent connections:", count
return

connection.on "error", (e)->
console.log "local error: #{e}"
ws.close() if ws
ws.terminate() if ws
server.getConnections (err, count) ->
console.log "concurrent connections:", count
return

connection.on "drain", ->
ws.resume()

connection.setTimeout timeout, ->
console.log "local timeout"
connection.destroy()
ws.close() if ws
ws.terminate() if ws

server.listen PORT, LOCAL_ADDRESS, ->
address = server.address()
Expand Down
17 changes: 13 additions & 4 deletions src/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ wss.on "connection", (ws) ->
ws.on "message", (data, flags) ->
data = encryptor.decrypt data
if stage is 5
remote.write(data)
ws.pause() unless remote.write(data)
return
if stage is 0
try
Expand Down Expand Up @@ -92,14 +92,20 @@ wss.on "connection", (ws) ->
)
remote.on "data", (data) ->
data = encryptor.encrypt data
ws.send data, { binary: true } if ws.readyState is WebSocket.OPEN
if ws.readyState is WebSocket.OPEN
ws.send data, { binary: true }
remote.pause() if ws.bufferedAmount > 0
return

remote.on "end", ->
ws.emit "close"
ws.close()
console.log "remote disconnected"

remote.on "drain", ->
ws.resume()

remote.on "error", (e)->
ws.emit "close"
ws.close()
console.log "remote: #{e}"

remote.setTimeout timeout, ->
Expand Down Expand Up @@ -127,6 +133,9 @@ wss.on "connection", (ws) ->
ws.on "ping", ->
ws.pong '', null, true

ws._socket.on "drain", ->
remote.resume() if remote

ws.on "close", ->
console.log "server disconnected"
console.log "concurrent connections:", wss.clients.length
Expand Down

0 comments on commit 2af12b2

Please sign in to comment.