From 8a3adfc344433fddb9fb565a698d3f0fe0165102 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Thu, 6 Dec 2012 14:39:06 +0800 Subject: [PATCH] support args --- README.md | 7 +++++++ local.py | 31 +++++++++++++++++++++++-------- server.py | 28 ++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7dc4855..e329e80 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,10 @@ Change proxy settings of your browser into SOCKS5 127.0.0.1:local_port +advanced +------------ + +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 diff --git a/local.py b/local.py index e45fffe..bd6e09d 100755 --- a/local.py +++ b/local.py @@ -31,6 +31,7 @@ import sys import os import json import logging +import getopt def get_table(key): m = hashlib.md5() @@ -111,12 +112,12 @@ class Socks5Server(SocketServer.StreamRequestHandler): remote.connect((SERVER, REMOTE_PORT)) self.send_encrypt(remote, addr_to_send) logging.info('connecting %s:%d' % (addr, port[0])) - except socket.error as e: - logging.warn('socket error ' + str(e)) + except socket.error, e: + logging.warn(e) return self.handle_tcp(sock, remote) - except socket.error as e: - logging.warn('socket error ' + str(e)) + except socket.error, e: + logging.warn(e) if __name__ == '__main__': @@ -129,13 +130,27 @@ if __name__ == '__main__': PORT = config['local_port'] KEY = config['password'] + optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:l:') + for key, value in optlist: + if key == '-p': + REMOTE_PORT = int(value) + elif key == '-k': + KEY = value + elif key == '-l': + PORT = int(value) + elif key == '-s': + SERVER = value + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') encrypt_table = ''.join(get_table(KEY)) decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) - server = ThreadingTCPServer(('', PORT), Socks5Server) - server.allow_reuse_address = True - logging.info("starting server at port %d ..." % PORT) - server.serve_forever() + try: + server = ThreadingTCPServer(('', PORT), Socks5Server) + server.allow_reuse_address = True + logging.info("starting server at port %d ..." % PORT) + server.serve_forever() + except socket.error, e: + logging.error(e) diff --git a/server.py b/server.py index 670c6d8..e9e8944 100755 --- a/server.py +++ b/server.py @@ -31,6 +31,7 @@ import sys import os import json import logging +import getopt def get_table(key): m = hashlib.md5() @@ -87,23 +88,31 @@ class Socks5Server(SocketServer.StreamRequestHandler): logging.info('connecting %s:%d' % (addr, port[0])) remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) remote.connect((addr, port[0])) - except socket.error as e: + except socket.error, e: # Connection refused - logging.warn('socket error ' + str(e)) + logging.warn(e) return self.handle_tcp(sock, remote) - except socket.error as e: - logging.warn('socket error ' + str(e)) + except socket.error, e: + logging.warn(e) if __name__ == '__main__': os.chdir(os.path.dirname(__file__) or '.') with open('config.json', 'rb') as f: config = json.load(f) + SERVER = config['server'] PORT = config['server_port'] KEY = config['password'] + optlist, args = getopt.getopt(sys.argv[1:], 'p:k:') + for key, value in optlist: + if key == '-p': + PORT = int(value) + elif key == '-k': + KEY = value + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') @@ -111,8 +120,11 @@ if __name__ == '__main__': decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) if '-6' in sys.argv[1:]: ThreadingTCPServer.address_family = socket.AF_INET6 - server = ThreadingTCPServer(('', PORT), Socks5Server) - server.allow_reuse_address = True - logging.info("starting server at port %d ..." % PORT) - server.serve_forever() + try: + server = ThreadingTCPServer(('', PORT), Socks5Server) + server.allow_reuse_address = True + logging.info("starting server at port %d ..." % PORT) + server.serve_forever() + except socket.error, e: + logging.error(e)