support args
This commit is contained in:
parent
9bbb89581a
commit
8a3adfc344
3 changed files with 50 additions and 16 deletions
|
@ -21,3 +21,10 @@ Change proxy settings of your browser into
|
||||||
|
|
||||||
SOCKS5 127.0.0.1:local_port
|
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
|
||||||
|
|
23
local.py
23
local.py
|
@ -31,6 +31,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import getopt
|
||||||
|
|
||||||
def get_table(key):
|
def get_table(key):
|
||||||
m = hashlib.md5()
|
m = hashlib.md5()
|
||||||
|
@ -111,12 +112,12 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
remote.connect((SERVER, REMOTE_PORT))
|
remote.connect((SERVER, REMOTE_PORT))
|
||||||
self.send_encrypt(remote, addr_to_send)
|
self.send_encrypt(remote, addr_to_send)
|
||||||
logging.info('connecting %s:%d' % (addr, port[0]))
|
logging.info('connecting %s:%d' % (addr, port[0]))
|
||||||
except socket.error as e:
|
except socket.error, e:
|
||||||
logging.warn('socket error ' + str(e))
|
logging.warn(e)
|
||||||
return
|
return
|
||||||
self.handle_tcp(sock, remote)
|
self.handle_tcp(sock, remote)
|
||||||
except socket.error as e:
|
except socket.error, e:
|
||||||
logging.warn('socket error ' + str(e))
|
logging.warn(e)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -129,13 +130,27 @@ if __name__ == '__main__':
|
||||||
PORT = config['local_port']
|
PORT = config['local_port']
|
||||||
KEY = config['password']
|
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',
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
|
||||||
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
||||||
|
|
||||||
encrypt_table = ''.join(get_table(KEY))
|
encrypt_table = ''.join(get_table(KEY))
|
||||||
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
|
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
|
||||||
|
try:
|
||||||
server = ThreadingTCPServer(('', PORT), Socks5Server)
|
server = ThreadingTCPServer(('', PORT), Socks5Server)
|
||||||
server.allow_reuse_address = True
|
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:
|
||||||
|
logging.error(e)
|
||||||
|
|
||||||
|
|
20
server.py
20
server.py
|
@ -31,6 +31,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import getopt
|
||||||
|
|
||||||
def get_table(key):
|
def get_table(key):
|
||||||
m = hashlib.md5()
|
m = hashlib.md5()
|
||||||
|
@ -87,23 +88,31 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
logging.info('connecting %s:%d' % (addr, port[0]))
|
logging.info('connecting %s:%d' % (addr, port[0]))
|
||||||
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
remote.connect((addr, port[0]))
|
remote.connect((addr, port[0]))
|
||||||
except socket.error as e:
|
except socket.error, e:
|
||||||
# Connection refused
|
# Connection refused
|
||||||
logging.warn('socket error ' + str(e))
|
logging.warn(e)
|
||||||
return
|
return
|
||||||
self.handle_tcp(sock, remote)
|
self.handle_tcp(sock, remote)
|
||||||
except socket.error as e:
|
except socket.error, e:
|
||||||
logging.warn('socket error ' + str(e))
|
logging.warn(e)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
os.chdir(os.path.dirname(__file__) or '.')
|
os.chdir(os.path.dirname(__file__) or '.')
|
||||||
|
|
||||||
with open('config.json', 'rb') as f:
|
with open('config.json', 'rb') as f:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
|
|
||||||
SERVER = config['server']
|
SERVER = config['server']
|
||||||
PORT = config['server_port']
|
PORT = config['server_port']
|
||||||
KEY = config['password']
|
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',
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
|
||||||
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
||||||
|
|
||||||
|
@ -111,8 +120,11 @@ if __name__ == '__main__':
|
||||||
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
|
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
|
||||||
if '-6' in sys.argv[1:]:
|
if '-6' in sys.argv[1:]:
|
||||||
ThreadingTCPServer.address_family = socket.AF_INET6
|
ThreadingTCPServer.address_family = socket.AF_INET6
|
||||||
|
try:
|
||||||
server = ThreadingTCPServer(('', PORT), Socks5Server)
|
server = ThreadingTCPServer(('', PORT), Socks5Server)
|
||||||
server.allow_reuse_address = True
|
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:
|
||||||
|
logging.error(e)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue