Browse Source

support args

1.4
clowwindy 12 years ago
parent
commit
8a3adfc344
  1. 7
      README.md
  2. 31
      local.py
  3. 28
      server.py

7
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

31
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)

28
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)

Loading…
Cancel
Save