From 12173a66d19cbba2bc15570001c046f9406b4533 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 30 Dec 2012 13:28:51 +0800 Subject: [PATCH] add gevent --- README.md | 6 +++++- local.py | 13 ++++++++++--- server.py | 13 ++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2ad1ee1..b93107f 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ First, make sure you have Python 2.6 or 2.7. $ python --version Python 2.6.8 + Then edit `config.json`, change the following values: server your server ip or hostname @@ -36,7 +37,10 @@ You can use args to override settings from `config.json`. python local.py -s server_name -p server_port -l local_port -k password python server.py -p server_port -k password - + +You may want to install gevent for better performance. + + $ sudo easy_install gevent troubleshooting --------------- diff --git a/local.py b/local.py index 079bdb7..8a5e507 100755 --- a/local.py +++ b/local.py @@ -20,6 +20,14 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import sys + +try: + import gevent, gevent.monkey + gevent.monkey.patch_all(dns=gevent.version_info[0]>=1) +except ImportError: + gevent = None + print >>sys.stderr, 'warning: gevent not found, using threading instead' import socket import select @@ -27,7 +35,6 @@ import SocketServer import struct import string import hashlib -import sys import os import json import logging @@ -58,13 +65,13 @@ class Socks5Server(SocketServer.StreamRequestHandler): data = sock.recv(4096) if data <= 0: break - if remote.sendall(self.encrypt(data)) is not None: + if remote.send(self.encrypt(data)) <= 0: break if remote in r: data = remote.recv(4096) if data <= 0: break - if sock.sendall(self.decrypt(data)) is not None: + if sock.send(self.decrypt(data)) <= 0: break finally: sock.close() diff --git a/server.py b/server.py index 6af8c7a..3558e22 100755 --- a/server.py +++ b/server.py @@ -20,6 +20,14 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import sys + +try: + import gevent, gevent.monkey + gevent.monkey.patch_all(dns=gevent.version_info[0]>=1) +except ImportError: + gevent = None + print >>sys.stderr, 'warning: gevent not found, using threading instead' import socket import select @@ -27,7 +35,6 @@ import SocketServer import struct import string import hashlib -import sys import os import json import logging @@ -58,13 +65,13 @@ class Socks5Server(SocketServer.StreamRequestHandler): data = sock.recv(4096) if data <= 0: break - if remote.sendall(self.decrypt(data)) is not None: + if remote.send(self.decrypt(data)) <= 0: break if remote in r: data = remote.recv(4096) if data <= 0: break - if sock.sendall(self.encrypt(data)) is not None: + if sock.send(self.encrypt(data)) <= 0: break finally: sock.close()