diff --git a/config.json b/config.json index 62a3a65..47e7219 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "server":"localhost", - "server_port":8388, - "local_port":1080, + "port":8388, + "localport":1080, "password":"barfoo!", "timeout":60 -} \ No newline at end of file +} diff --git a/local.py b/local.py index bd6e09d..1f71b28 100755 --- a/local.py +++ b/local.py @@ -31,7 +31,7 @@ import sys import os import json import logging -import getopt +import argparse def get_table(key): m = hashlib.md5() @@ -125,21 +125,27 @@ if __name__ == '__main__': with open('config.json', 'rb') as f: config = json.load(f) - SERVER = config['server'] - REMOTE_PORT = config['server_port'] - 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 + parser = argparse.ArgumentParser( + description='ShadowSocks Client' + ) + parser.add_argument('-p', '--port', action='store', help='remote port', type=int) + parser.add_argument('-k', '--key', action='store', help='password', type=unicode) + parser.add_argument('-l', '--localport', action='store', help='local port', type=int) + parser.add_argument('-s', '--server', action='store', help='server ip address', type=unicode) + if not len(sys.argv): + parser.print_help() + sys.exit(1) + + args = parser.parse_args() + d = vars(args) + _config = dict((k,d[k]) for k in d if d[k]) + config.update(_config) + + SERVER = config['server'] + REMOTE_PORT = config['port'] + PORT = config['localport'] + KEY = config['password'] logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') diff --git a/server.py b/server.py index e9e8944..6a8e45c 100755 --- a/server.py +++ b/server.py @@ -31,7 +31,7 @@ import sys import os import json import logging -import getopt +import argparse def get_table(key): m = hashlib.md5() @@ -102,16 +102,24 @@ if __name__ == '__main__': with open('config.json', 'rb') as f: config = json.load(f) - SERVER = config['server'] - PORT = config['server_port'] - KEY = config['password'] + parser = argparse.ArgumentParser( + description='ShadowSocks Server' + ) + parser.add_argument('-p', '--port', action='store', help='port', type=int) + parser.add_argument('-k', '--key', action='store', help='password', type=unicode) + if not len(sys.argv): + parser.print_help() + sys.exit(1) - 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 + args = parser.parse_args() + d = vars(args) + _config = dict((k,d[k]) for k in d if d[k]) + + config.update(_config) + + SERVER = config['server'] + PORT = config['port'] + KEY = config['password'] logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') @@ -127,4 +135,3 @@ if __name__ == '__main__': server.serve_forever() except socket.error, e: logging.error(e) -