Browse Source

lint code

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

17
shadowsocks/encrypt.py

@ -53,15 +53,17 @@ def init_table(key, method=None):
try:
__import__('M2Crypto')
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)
if not method:
global encrypt_table, decrypt_table
encrypt_table = ''.join(get_table(key))
decrypt_table = string.maketrans(encrypt_table, string.maketrans('', ''))
decrypt_table = string.maketrans(encrypt_table,
string.maketrans('', ''))
else:
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:
logging.error(e)
sys.exit(1)
@ -137,8 +139,10 @@ class Encryptor(object):
if iv is None:
iv = iv_[:m[1]]
if op == 1:
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)
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)
logging.error('method %s not supported' % method)
sys.exit(1)
@ -164,7 +168,8 @@ class Encryptor(object):
if self.decipher is None:
decipher_iv_len = self.get_cipher_len(self.method)[1]
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:]
if len(buf) == 0:
return buf

13
shadowsocks/local.py

@ -78,7 +78,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
r = re.match(r'^(.*)\:(\d+)$', aServer)
if r:
# 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)
aPort = int(r.group(2))
return (aServer, aPort)
@ -203,7 +203,8 @@ def main():
try:
config = json.load(f)
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)
else:
config = {}
@ -236,17 +237,19 @@ def main():
LOCAL = config.get('local', '127.0.0.1')
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)
encrypt.init_table(KEY, METHOD)
try:
if IPv6:
ThreadingTCPServer.address_family = socket.AF_INET6
try:
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()
except socket.error, e:
logging.error(e)

18
shadowsocks/server.py

@ -98,7 +98,8 @@ class Socks5Server(SocketServer.StreamRequestHandler):
def handle(self):
try:
self.encryptor = encrypt.Encryptor(self.server.key, self.server.method)
self.encryptor = encrypt.Encryptor(self.server.key,
self.server.method)
sock = self.connection
iv_len = self.encryptor.iv_len()
if iv_len:
@ -159,7 +160,8 @@ def main():
try:
config = json.load(f)
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)
logging.info('loading config from %s' % config_path)
else:
@ -189,13 +191,16 @@ def main():
TIMEOUT = config.get('timeout', 600)
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)
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')
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
@ -206,8 +211,11 @@ def main():
for port, key in PORTPASSWORD.items():
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]))
threading.Thread(target=server.serve_forever).start()
udprelay.UDPRelay(SERVER, int(port), None, None, key, METHOD,
int(TIMEOUT), False)
if __name__ == '__main__':

Loading…
Cancel
Save