Browse Source

use sendall()

1.4
clowwindy 12 years ago
parent
commit
65444c5b06
  1. 6
      README.md
  2. 10
      local.py
  3. 9
      server.py

6
README.md

@ -40,8 +40,12 @@ You can use args to override settings from `config.json`.
You may want to install gevent for better performance. You may want to install gevent for better performance.
$ apt-get install python-gevent
Or:
$ sudo easy_install gevent $ sudo easy_install gevent
troubleshooting troubleshooting
--------------- ---------------

10
local.py

@ -52,7 +52,7 @@ def get_table(key):
class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass allow_reuse_address = True
class Socks5Server(SocketServer.StreamRequestHandler): class Socks5Server(SocketServer.StreamRequestHandler):
@ -65,14 +65,13 @@ class Socks5Server(SocketServer.StreamRequestHandler):
data = sock.recv(4096) data = sock.recv(4096)
if data <= 0: if data <= 0:
break break
if remote.send(self.encrypt(data)) <= 0: remote.sendall(self.encrypt(data))
break
if remote in r: if remote in r:
data = remote.recv(4096) data = remote.recv(4096)
if data <= 0: if data <= 0:
break break
if sock.send(self.decrypt(data)) <= 0: sock.sendall(self.decrypt(data))
break
finally: finally:
sock.close() sock.close()
remote.close() remote.close()
@ -162,7 +161,6 @@ if __name__ == '__main__':
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
try: try:
server = ThreadingTCPServer(('', PORT), Socks5Server) server = ThreadingTCPServer(('', PORT), Socks5Server)
server.allow_reuse_address = True
logging.info("starting server at port %d ..." % PORT) logging.info("starting server at port %d ..." % PORT)
server.serve_forever() server.serve_forever()
except socket.error, e: except socket.error, e:

9
server.py

@ -52,7 +52,7 @@ def get_table(key):
class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass allow_reuse_address = True
class Socks5Server(SocketServer.StreamRequestHandler): class Socks5Server(SocketServer.StreamRequestHandler):
@ -65,14 +65,12 @@ class Socks5Server(SocketServer.StreamRequestHandler):
data = sock.recv(4096) data = sock.recv(4096)
if data <= 0: if data <= 0:
break break
if remote.send(self.decrypt(data)) <= 0: remote.sendall(self.decrypt(data))
break
if remote in r: if remote in r:
data = remote.recv(4096) data = remote.recv(4096)
if data <= 0: if data <= 0:
break break
if sock.send(self.encrypt(data)) <= 0: sock.send(self.encrypt(data))
break
finally: finally:
sock.close() sock.close()
remote.close() remote.close()
@ -137,7 +135,6 @@ if __name__ == '__main__':
ThreadingTCPServer.address_family = socket.AF_INET6 ThreadingTCPServer.address_family = socket.AF_INET6
try: try:
server = ThreadingTCPServer(('', PORT), Socks5Server) server = ThreadingTCPServer(('', PORT), Socks5Server)
server.allow_reuse_address = True
logging.info("starting server at port %d ..." % PORT) logging.info("starting server at port %d ..." % PORT)
server.serve_forever() server.serve_forever()
except socket.error, e: except socket.error, e:

Loading…
Cancel
Save