Browse Source

implement send_all myself

1.4
clowwindy 12 years ago
parent
commit
004e9292f4
  1. 13
      local.py
  2. 14
      server.py

13
local.py

@ -50,6 +50,15 @@ def get_table(key):
table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i))) table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i)))
return table return table
def send_all(sock, data):
bytes_sent = 0
while True:
r = sock.send(data[bytes_sent:])
if r < 0:
return r
bytes_sent += r
if bytes_sent == len(data):
return bytes_sent
class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
allow_reuse_address = True allow_reuse_address = True
@ -65,13 +74,13 @@ class Socks5Server(SocketServer.StreamRequestHandler):
data = sock.recv(4096) data = sock.recv(4096)
if data <= 0: if data <= 0:
break break
remote.sendall(self.encrypt(data)) send_all(remote, self.encrypt(data))
if remote in r: if remote in r:
data = remote.recv(4096) data = remote.recv(4096)
if data <= 0: if data <= 0:
break break
sock.sendall(self.decrypt(data)) send_all(sock, self.decrypt(data))
finally: finally:
sock.close() sock.close()
remote.close() remote.close()

14
server.py

@ -50,6 +50,16 @@ def get_table(key):
table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i))) table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i)))
return table return table
def send_all(sock, data):
bytes_sent = 0
while True:
r = sock.send(data[bytes_sent:])
if r < 0:
return r
bytes_sent += r
if bytes_sent == len(data):
return bytes_sent
class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
allow_reuse_address = True allow_reuse_address = True
@ -65,12 +75,12 @@ class Socks5Server(SocketServer.StreamRequestHandler):
data = sock.recv(4096) data = sock.recv(4096)
if data <= 0: if data <= 0:
break break
remote.sendall(self.decrypt(data)) send_all(remote, self.decrypt(data))
if remote in r: if remote in r:
data = remote.recv(4096) data = remote.recv(4096)
if data <= 0: if data <= 0:
break break
sock.send(self.encrypt(data)) send_all(sock, self.encrypt(data))
finally: finally:
sock.close() sock.close()
remote.close() remote.close()

Loading…
Cancel
Save