Skip to content

Commit 68b91d4

Browse files
committed
pause/resume socket properly
closes issue #2
1 parent 5aa721b commit 68b91d4

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ [email protected] node_modules/ws
5454
Then run:
5555

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

6161
Change proxy settings of your browser into:

config.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"server": "127.0.0.1",
3+
"local_address": "127.0.0.1",
34
"local_port": 1080,
45
"remote_port": 8080,
56
"password": "`try*(^^$some^$%^complex>:<>?~password/",

local.js

+20-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.js

+17-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/local.coffee

+14-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ server = net.createServer (connection) ->
6363
if stage is 5
6464
# pipe sockets
6565
data = encryptor.encrypt data
66-
ws.send data, { binary: true }
66+
if ws.readyState is WebSocket.OPEN
67+
ws.send data, { binary: true }
68+
connection.pause() if ws.bufferedAmount > 0
6769
return
6870
if stage is 0
6971
tempBuf = new Buffer(2)
@@ -130,11 +132,15 @@ server = net.createServer (connection) ->
130132
ping = setInterval(->
131133
ws.ping "", null, true
132134
, 50 * 1000)
135+
136+
ws._socket.on "drain", ->
137+
connection.resume()
138+
133139
return
134140

135141
ws.on "message", (data, flags) ->
136142
data = encryptor.decrypt data
137-
connection.write(data)
143+
ws._socket.pause() unless connection.write(data)
138144

139145
ws.on "close", ->
140146
clearInterval ping
@@ -165,22 +171,25 @@ server = net.createServer (connection) ->
165171

166172
connection.on "end", ->
167173
console.log "local disconnected"
168-
ws.close() if ws
174+
ws.terminate() if ws
169175
server.getConnections (err, count) ->
170176
console.log "concurrent connections:", count
171177
return
172178

173179
connection.on "error", (e)->
174180
console.log "local error: #{e}"
175-
ws.close() if ws
181+
ws.terminate() if ws
176182
server.getConnections (err, count) ->
177183
console.log "concurrent connections:", count
178184
return
179185

186+
connection.on "drain", ->
187+
ws._socket.resume()
188+
180189
connection.setTimeout timeout, ->
181190
console.log "local timeout"
182191
connection.destroy()
183-
ws.close() if ws
192+
ws.terminate() if ws
184193

185194
server.listen PORT, LOCAL_ADDRESS, ->
186195
address = server.address()

src/server.coffee

+13-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ wss.on "connection", (ws) ->
5757
ws.on "message", (data, flags) ->
5858
data = encryptor.decrypt data
5959
if stage is 5
60-
remote.write(data)
60+
ws._socket.pause() unless remote.write(data)
6161
return
6262
if stage is 0
6363
try
@@ -92,14 +92,20 @@ wss.on "connection", (ws) ->
9292
)
9393
remote.on "data", (data) ->
9494
data = encryptor.encrypt data
95-
ws.send data, { binary: true } if ws.readyState is WebSocket.OPEN
95+
if ws.readyState is WebSocket.OPEN
96+
ws.send data, { binary: true }
97+
remote.pause() if ws.bufferedAmount > 0
98+
return
9699

97100
remote.on "end", ->
98-
ws.emit "close"
101+
ws.close()
99102
console.log "remote disconnected"
100103

104+
remote.on "drain", ->
105+
ws._socket.resume()
106+
101107
remote.on "error", (e)->
102-
ws.emit "close"
108+
ws.terminate()
103109
console.log "remote: #{e}"
104110

105111
remote.setTimeout timeout, ->
@@ -127,6 +133,9 @@ wss.on "connection", (ws) ->
127133
ws.on "ping", ->
128134
ws.pong '', null, true
129135

136+
ws._socket.on "drain", ->
137+
remote.resume() if stage is 5
138+
130139
ws.on "close", ->
131140
console.log "server disconnected"
132141
console.log "concurrent connections:", wss.clients.length

0 commit comments

Comments
 (0)