fix a potential RST problem
This commit is contained in:
parent
289467923b
commit
6caeabd03d
2 changed files with 32 additions and 16 deletions
|
@ -100,6 +100,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
else:
|
else:
|
||||||
fdset = [sock, remote]
|
fdset = [sock, remote]
|
||||||
while True:
|
while True:
|
||||||
|
should_break = False
|
||||||
r, w, e = select.select(fdset, [], [])
|
r, w, e = select.select(fdset, [], [])
|
||||||
if sock in r:
|
if sock in r:
|
||||||
if not connected and config_fast_open:
|
if not connected and config_fast_open:
|
||||||
|
@ -123,7 +124,8 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
pending_data = None
|
pending_data = None
|
||||||
data = encryptor.encrypt(data)
|
data = encryptor.encrypt(data)
|
||||||
if len(data) <= 0:
|
if len(data) <= 0:
|
||||||
break
|
should_break = True
|
||||||
|
else:
|
||||||
result = send_all(remote, data)
|
result = send_all(remote, data)
|
||||||
if result < len(data):
|
if result < len(data):
|
||||||
raise Exception('failed to send all data')
|
raise Exception('failed to send all data')
|
||||||
|
@ -131,10 +133,16 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
if remote in r:
|
if remote in r:
|
||||||
data = encryptor.decrypt(remote.recv(4096))
|
data = encryptor.decrypt(remote.recv(4096))
|
||||||
if len(data) <= 0:
|
if len(data) <= 0:
|
||||||
break
|
should_break = True
|
||||||
|
else:
|
||||||
result = send_all(sock, data)
|
result = send_all(sock, data)
|
||||||
if result < len(data):
|
if result < len(data):
|
||||||
raise Exception('failed to send all data')
|
raise Exception('failed to send all data')
|
||||||
|
if should_break:
|
||||||
|
# make sure all data are read before we close the sockets
|
||||||
|
# TODO: we haven't read ALL the data, actually
|
||||||
|
# http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/TCPRST.pdf
|
||||||
|
break
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
remote.close()
|
remote.close()
|
||||||
|
|
|
@ -79,21 +79,29 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
try:
|
try:
|
||||||
fdset = [sock, remote]
|
fdset = [sock, remote]
|
||||||
while True:
|
while True:
|
||||||
|
should_break = False
|
||||||
r, w, e = select.select(fdset, [], [])
|
r, w, e = select.select(fdset, [], [])
|
||||||
if sock in r:
|
if sock in r:
|
||||||
data = self.decrypt(sock.recv(4096))
|
data = self.decrypt(sock.recv(4096))
|
||||||
if len(data) <= 0:
|
if len(data) <= 0:
|
||||||
break
|
should_break = True
|
||||||
|
else:
|
||||||
result = send_all(remote, data)
|
result = send_all(remote, data)
|
||||||
if result < len(data):
|
if result < len(data):
|
||||||
raise Exception('failed to send all data')
|
raise Exception('failed to send all data')
|
||||||
if remote in r:
|
if remote in r:
|
||||||
data = self.encrypt(remote.recv(4096))
|
data = self.encrypt(remote.recv(4096))
|
||||||
if len(data) <= 0:
|
if len(data) <= 0:
|
||||||
break
|
should_break = True
|
||||||
|
else:
|
||||||
result = send_all(sock, data)
|
result = send_all(sock, data)
|
||||||
if result < len(data):
|
if result < len(data):
|
||||||
raise Exception('failed to send all data')
|
raise Exception('failed to send all data')
|
||||||
|
if should_break:
|
||||||
|
# make sure all data are read before we close the sockets
|
||||||
|
# TODO: we haven't read ALL the data, actually
|
||||||
|
# http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/TCPRST.pdf
|
||||||
|
break
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
Loading…
Reference in a new issue