Skip to content

Commit 6caeabd

Browse files
author
clowwindy
committed
fix a potential RST problem
1 parent 2894679 commit 6caeabd

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

shadowsocks/local.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def handle_tcp(sock, remote, encryptor, pending_data=None,
100100
else:
101101
fdset = [sock, remote]
102102
while True:
103+
should_break = False
103104
r, w, e = select.select(fdset, [], [])
104105
if sock in r:
105106
if not connected and config_fast_open:
@@ -123,18 +124,25 @@ def handle_tcp(sock, remote, encryptor, pending_data=None,
123124
pending_data = None
124125
data = encryptor.encrypt(data)
125126
if len(data) <= 0:
126-
break
127-
result = send_all(remote, data)
128-
if result < len(data):
129-
raise Exception('failed to send all data')
127+
should_break = True
128+
else:
129+
result = send_all(remote, data)
130+
if result < len(data):
131+
raise Exception('failed to send all data')
130132

131133
if remote in r:
132134
data = encryptor.decrypt(remote.recv(4096))
133135
if len(data) <= 0:
134-
break
135-
result = send_all(sock, data)
136-
if result < len(data):
137-
raise Exception('failed to send all data')
136+
should_break = True
137+
else:
138+
result = send_all(sock, data)
139+
if result < len(data):
140+
raise Exception('failed to send all data')
141+
if should_break:
142+
# make sure all data are read before we close the sockets
143+
# TODO: we haven't read ALL the data, actually
144+
# http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/TCPRST.pdf
145+
break
138146
finally:
139147
sock.close()
140148
remote.close()

shadowsocks/server.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,29 @@ def handle_tcp(self, sock, remote):
7979
try:
8080
fdset = [sock, remote]
8181
while True:
82+
should_break = False
8283
r, w, e = select.select(fdset, [], [])
8384
if sock in r:
8485
data = self.decrypt(sock.recv(4096))
8586
if len(data) <= 0:
86-
break
87-
result = send_all(remote, data)
88-
if result < len(data):
89-
raise Exception('failed to send all data')
87+
should_break = True
88+
else:
89+
result = send_all(remote, data)
90+
if result < len(data):
91+
raise Exception('failed to send all data')
9092
if remote in r:
9193
data = self.encrypt(remote.recv(4096))
9294
if len(data) <= 0:
93-
break
94-
result = send_all(sock, data)
95-
if result < len(data):
96-
raise Exception('failed to send all data')
95+
should_break = True
96+
else:
97+
result = send_all(sock, data)
98+
if result < len(data):
99+
raise Exception('failed to send all data')
100+
if should_break:
101+
# make sure all data are read before we close the sockets
102+
# TODO: we haven't read ALL the data, actually
103+
# http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/TCPRST.pdf
104+
break
97105

98106
finally:
99107
sock.close()

0 commit comments

Comments
 (0)