|
@ -37,6 +37,7 @@ except ImportError: |
|
|
|
|
|
|
|
|
import socket |
|
|
import socket |
|
|
import select |
|
|
import select |
|
|
|
|
|
import threading |
|
|
import SocketServer |
|
|
import SocketServer |
|
|
import struct |
|
|
import struct |
|
|
import os |
|
|
import os |
|
@ -94,7 +95,7 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
|
|
|
|
|
|
def handle(self): |
|
|
def handle(self): |
|
|
try: |
|
|
try: |
|
|
self.encryptor = encrypt.Encryptor(KEY, METHOD) |
|
|
self.encryptor = encrypt.Encryptor(self.server.key, self.server.method) |
|
|
sock = self.connection |
|
|
sock = self.connection |
|
|
iv_len = self.encryptor.iv_len() |
|
|
iv_len = self.encryptor.iv_len() |
|
|
if iv_len: |
|
|
if iv_len: |
|
@ -125,12 +126,11 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
logging.warn(e) |
|
|
logging.warn(e) |
|
|
|
|
|
|
|
|
def main(): |
|
|
def main(): |
|
|
global SERVER, PORT, KEY, METHOD, IPv6 |
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG, |
|
|
logging.basicConfig(level=logging.DEBUG, |
|
|
format='%(asctime)s %(levelname)-8s %(message)s', |
|
|
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+') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
version = '' |
|
|
version = '' |
|
|
try: |
|
|
try: |
|
|
import pkg_resources |
|
|
import pkg_resources |
|
@ -173,21 +173,32 @@ def main(): |
|
|
PORT = config['server_port'] |
|
|
PORT = config['server_port'] |
|
|
KEY = config['password'] |
|
|
KEY = config['password'] |
|
|
METHOD = config.get('method', None) |
|
|
METHOD = config.get('method', None) |
|
|
|
|
|
PORTPASSWORD = config.get('port_password', None) |
|
|
|
|
|
TIMEOUT = config.get('timeout', 600) |
|
|
|
|
|
|
|
|
if not KEY and not config_path: |
|
|
if not KEY and not config_path: |
|
|
sys.exit('config not specified, please read https://github.com/clowwindy/shadowsocks') |
|
|
sys.exit('config not specified, please read https://github.com/clowwindy/shadowsocks') |
|
|
|
|
|
|
|
|
utils.check_config(config) |
|
|
utils.check_config(config) |
|
|
|
|
|
|
|
|
|
|
|
if PORTPASSWORD: |
|
|
|
|
|
if PORT or KEY: |
|
|
|
|
|
logging.warn('warning: port_password should not be used with server_port and password. server_port and password will be ignored') |
|
|
|
|
|
else: |
|
|
|
|
|
PORTPASSWORD = {} |
|
|
|
|
|
PORTPASSWORD[str(PORT)] = KEY |
|
|
|
|
|
|
|
|
encrypt.init_table(KEY, METHOD) |
|
|
encrypt.init_table(KEY, METHOD) |
|
|
if IPv6: |
|
|
if IPv6: |
|
|
ThreadingTCPServer.address_family = socket.AF_INET6 |
|
|
ThreadingTCPServer.address_family = socket.AF_INET6 |
|
|
try: |
|
|
for port, key in PORTPASSWORD.items(): |
|
|
server = ThreadingTCPServer((SERVER, PORT), Socks5Server) |
|
|
server = ThreadingTCPServer((SERVER, int(port)), Socks5Server) |
|
|
|
|
|
server.key, server.method, server.timeout = key, METHOD, int(TIMEOUT) |
|
|
logging.info("starting server at %s:%d" % tuple(server.server_address[:2])) |
|
|
logging.info("starting server at %s:%d" % tuple(server.server_address[:2])) |
|
|
server.serve_forever() |
|
|
threading.Thread(target=server.serve_forever).start() |
|
|
except socket.error, e: |
|
|
|
|
|
logging.error(e) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
|
|
|
try: |
|
|
main() |
|
|
main() |
|
|
|
|
|
except socket.error, e: |
|
|
|
|
|
logging.error(e) |
|
|