Browse Source

lint code

1.4
clowwindy 11 years ago
parent
commit
c9ffb895c3
  1. 17
      shadowsocks/encrypt.py
  2. 15
      shadowsocks/local.py
  3. 20
      shadowsocks/server.py

17
shadowsocks/encrypt.py

@ -53,15 +53,17 @@ def init_table(key, method=None):
try: try:
__import__('M2Crypto') __import__('M2Crypto')
except ImportError: except ImportError:
logging.error('M2Crypto is required to use encryption other than default method') logging.error('M2Crypto is required to use encryption other than '
'default method')
sys.exit(1) sys.exit(1)
if not method: if not method:
global encrypt_table, decrypt_table global encrypt_table, decrypt_table
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('', ''))
else: else:
try: try:
Encryptor(key, method) # make an Encryptor to test if the settings if OK Encryptor(key, method) # test if the settings if OK
except Exception as e: except Exception as e:
logging.error(e) logging.error(e)
sys.exit(1) sys.exit(1)
@ -137,8 +139,10 @@ class Encryptor(object):
if iv is None: if iv is None:
iv = iv_[:m[1]] iv = iv_[:m[1]]
if op == 1: if op == 1:
self.cipher_iv = iv[:m[1]] # this iv is for cipher, not decipher self.cipher_iv = iv[:m[1]] # this iv is for cipher not decipher
return M2Crypto.EVP.Cipher(method.replace('-', '_'), key, iv, op, key_as_bytes=0, d='md5', salt=None, i=1, padding=1) return M2Crypto.EVP.Cipher(method.replace('-', '_'), key, iv, op,
key_as_bytes=0, d='md5', salt=None, i=1,
padding=1)
logging.error('method %s not supported' % method) logging.error('method %s not supported' % method)
sys.exit(1) sys.exit(1)
@ -164,7 +168,8 @@ class Encryptor(object):
if self.decipher is None: if self.decipher is None:
decipher_iv_len = self.get_cipher_len(self.method)[1] decipher_iv_len = self.get_cipher_len(self.method)[1]
decipher_iv = buf[:decipher_iv_len] decipher_iv = buf[:decipher_iv_len]
self.decipher = self.get_cipher(self.key, self.method, 0, iv=decipher_iv) self.decipher = self.get_cipher(self.key, self.method, 0,
iv=decipher_iv)
buf = buf[decipher_iv_len:] buf = buf[decipher_iv_len:]
if len(buf) == 0: if len(buf) == 0:
return buf return buf

15
shadowsocks/local.py

@ -78,7 +78,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
r = re.match(r'^(.*)\:(\d+)$', aServer) r = re.match(r'^(.*)\:(\d+)$', aServer)
if r: if r:
# support config like "server": "123.123.123.1:8381" # support config like "server": "123.123.123.1:8381"
# or "server": ["123.123.123.1:8381", "123.123.123.2:8381", "123.123.123.2:8382"] # or "server": ["123.123.123.1:8381", "123.123.123.2:8381"]
aServer = r.group(1) aServer = r.group(1)
aPort = int(r.group(2)) aPort = int(r.group(2))
return (aServer, aPort) return (aServer, aPort)
@ -203,7 +203,8 @@ def main():
try: try:
config = json.load(f) config = json.load(f)
except ValueError as e: except ValueError as e:
logging.error('found an error in config.json: %s', e.message) logging.error('found an error in config.json: %s',
e.message)
sys.exit(1) sys.exit(1)
else: else:
config = {} config = {}
@ -236,17 +237,19 @@ def main():
LOCAL = config.get('local', '127.0.0.1') LOCAL = config.get('local', '127.0.0.1')
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)
encrypt.init_table(KEY, METHOD) encrypt.init_table(KEY, METHOD)
if IPv6:
ThreadingTCPServer.address_family = socket.AF_INET6
try: try:
if IPv6:
ThreadingTCPServer.address_family = socket.AF_INET6
server = ThreadingTCPServer((LOCAL, PORT), Socks5Server) server = ThreadingTCPServer((LOCAL, PORT), Socks5Server)
logging.info("starting local at %s:%d" % tuple(server.server_address[:2])) logging.info("starting local at %s:%d" %
tuple(server.server_address[:2]))
server.serve_forever() server.serve_forever()
except socket.error, e: except socket.error, e:
logging.error(e) logging.error(e)

20
shadowsocks/server.py

@ -98,7 +98,8 @@ class Socks5Server(SocketServer.StreamRequestHandler):
def handle(self): def handle(self):
try: try:
self.encryptor = encrypt.Encryptor(self.server.key, self.server.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:
@ -159,7 +160,8 @@ def main():
try: try:
config = json.load(f) config = json.load(f)
except ValueError as e: except ValueError as e:
logging.error('found an error in config.json: %s', e.message) logging.error('found an error in config.json: %s',
e.message)
sys.exit(1) sys.exit(1)
logging.info('loading config from %s' % config_path) logging.info('loading config from %s' % config_path)
else: else:
@ -189,13 +191,16 @@ def main():
TIMEOUT = config.get('timeout', 600) 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 PORTPASSWORD:
if PORT or KEY: 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') logging.warn('warning: port_password should not be used with '
'server_port and password. server_port and password '
'will be ignored')
else: else:
PORTPASSWORD = {} PORTPASSWORD = {}
PORTPASSWORD[str(PORT)] = KEY PORTPASSWORD[str(PORT)] = KEY
@ -206,9 +211,12 @@ def main():
for port, key in PORTPASSWORD.items(): for port, key in PORTPASSWORD.items():
server = ThreadingTCPServer((SERVER, int(port)), Socks5Server) server = ThreadingTCPServer((SERVER, int(port)), Socks5Server)
server.key, server.method, server.timeout = key, METHOD, int(TIMEOUT) 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]))
threading.Thread(target=server.serve_forever).start() threading.Thread(target=server.serve_forever).start()
udprelay.UDPRelay(SERVER, int(port), None, None, key, METHOD,
int(TIMEOUT), False)
if __name__ == '__main__': if __name__ == '__main__':
try: try:

Loading…
Cancel
Save