Beauty the logging format and and logger handle for debug and
This commit is contained in:
parent
8b69df61e7
commit
4e598810fd
4 changed files with 31 additions and 26 deletions
|
@ -26,6 +26,7 @@ import string
|
||||||
import struct
|
import struct
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('encrypt')
|
||||||
|
|
||||||
def random_string(length):
|
def random_string(length):
|
||||||
import M2Crypto.Rand
|
import M2Crypto.Rand
|
||||||
|
@ -53,7 +54,7 @@ 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')
|
logger.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
|
||||||
|
@ -63,7 +64,7 @@ def init_table(key, method=None):
|
||||||
try:
|
try:
|
||||||
Encryptor(key, method) # make an Encryptor to test if the settings if OK
|
Encryptor(key, method) # make an Encryptor to test if the settings if OK
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
logger.error(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,7 +141,7 @@ class Encryptor(object):
|
||||||
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)
|
logger.error('method %s not supported' % method)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def encrypt(self, buf):
|
def encrypt(self, buf):
|
||||||
|
|
|
@ -48,6 +48,8 @@ import encrypt
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger('local')
|
||||||
|
|
||||||
def send_all(sock, data):
|
def send_all(sock, data):
|
||||||
bytes_sent = 0
|
bytes_sent = 0
|
||||||
while True:
|
while True:
|
||||||
|
@ -126,7 +128,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
data = self.rfile.read(4) or '\x00' * 4
|
data = self.rfile.read(4) or '\x00' * 4
|
||||||
mode = ord(data[1])
|
mode = ord(data[1])
|
||||||
if mode != 1:
|
if mode != 1:
|
||||||
logging.warn('mode != 1')
|
logger.warn('mode != 1')
|
||||||
return
|
return
|
||||||
addrtype = ord(data[3])
|
addrtype = ord(data[3])
|
||||||
addr_to_send = data[3]
|
addr_to_send = data[3]
|
||||||
|
@ -143,7 +145,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
addr = socket.inet_ntop(socket.AF_INET6, addr_ip)
|
addr = socket.inet_ntop(socket.AF_INET6, addr_ip)
|
||||||
addr_to_send += addr_ip
|
addr_to_send += addr_ip
|
||||||
else:
|
else:
|
||||||
logging.warn('addr_type not supported')
|
logger.warn('addr_type not supported')
|
||||||
# not supported
|
# not supported
|
||||||
return
|
return
|
||||||
addr_port = self.rfile.read(2)
|
addr_port = self.rfile.read(2)
|
||||||
|
@ -157,13 +159,13 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
aServer, aPort = self.getServer()
|
aServer, aPort = self.getServer()
|
||||||
remote = socket.create_connection((aServer, aPort))
|
remote = socket.create_connection((aServer, aPort))
|
||||||
self.send_encrypt(remote, addr_to_send)
|
self.send_encrypt(remote, addr_to_send)
|
||||||
logging.info('connecting %s:%d' % (addr, port[0]))
|
logger.info('connecting %s:%d' % (addr, port[0]))
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
logging.warn(e)
|
logger.warn(e)
|
||||||
return
|
return
|
||||||
self.handle_tcp(sock, remote)
|
self.handle_tcp(sock, remote)
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
logging.warn(e)
|
logger.warn(e)
|
||||||
|
|
||||||
|
|
||||||
class ShadowSocksServer(object):
|
class ShadowSocksServer(object):
|
||||||
|
@ -202,11 +204,11 @@ class ShadowSocksServer(object):
|
||||||
if self.options['IPv6']:
|
if self.options['IPv6']:
|
||||||
ThreadingTCPServer.address_family = socket.AF_INET6
|
ThreadingTCPServer.address_family = socket.AF_INET6
|
||||||
server = ThreadingTCPServer((LOCAL, PORT), Socks5Server)
|
server = ThreadingTCPServer((LOCAL, PORT), Socks5Server)
|
||||||
logging.info("starting local at %s:%d" %
|
logger.info("starting local at %s:%d" %
|
||||||
tuple(server.server_address[:2]))
|
tuple(server.server_address[:2]))
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
logging.error(e)
|
logger.error(e)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
server.shutdown()
|
server.shutdown()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
@ -235,12 +237,12 @@ class ShadowSocksServer(object):
|
||||||
|
|
||||||
def _parse_file_options(self, config_path):
|
def _parse_file_options(self, config_path):
|
||||||
if config_path:
|
if config_path:
|
||||||
logging.info('loading config from %s' % config_path)
|
logger.info('loading config from %s' % config_path)
|
||||||
with open(config_path, 'rb') as f:
|
with open(config_path, 'rb') as f:
|
||||||
try:
|
try:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
logging.error(
|
logger.error(
|
||||||
'found an error in config.json: %s', e.message)
|
'found an error in config.json: %s', e.message)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
|
@ -288,7 +290,7 @@ class ShadowSocksServer(object):
|
||||||
version = pkg_resources.get_distribution('shadowsocks').version
|
version = pkg_resources.get_distribution('shadowsocks').version
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
logging.info('shadowsocks %s' % version)
|
logger.info('shadowsocks %s' % version)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -46,6 +46,7 @@ import getopt
|
||||||
import encrypt
|
import encrypt
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
logger = logging.getLogger('server')
|
||||||
|
|
||||||
def send_all(sock, data):
|
def send_all(sock, data):
|
||||||
bytes_sent = 0
|
bytes_sent = 0
|
||||||
|
@ -113,19 +114,19 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
||||||
self.decrypt(self.rfile.read(16)))
|
self.decrypt(self.rfile.read(16)))
|
||||||
else:
|
else:
|
||||||
# not supported
|
# not supported
|
||||||
logging.warn('addr_type not supported, maybe wrong password')
|
logger.warn('addr_type not supported, maybe wrong password')
|
||||||
return
|
return
|
||||||
port = struct.unpack('>H', self.decrypt(self.rfile.read(2)))
|
port = struct.unpack('>H', self.decrypt(self.rfile.read(2)))
|
||||||
try:
|
try:
|
||||||
logging.info('connecting %s:%d' % (addr, port[0]))
|
logger.info('connecting %s:%d' % (addr, port[0]))
|
||||||
remote = socket.create_connection((addr, port[0]))
|
remote = socket.create_connection((addr, port[0]))
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
# Connection refused
|
# Connection refused
|
||||||
logging.warn(e)
|
logger.warn(e)
|
||||||
return
|
return
|
||||||
self.handle_tcp(sock, remote)
|
self.handle_tcp(sock, remote)
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
logging.warn(e)
|
logger.warn(e)
|
||||||
|
|
||||||
|
|
||||||
class ShadowSocksServer(object):
|
class ShadowSocksServer(object):
|
||||||
|
@ -161,7 +162,7 @@ class ShadowSocksServer(object):
|
||||||
|
|
||||||
if PORTPASSWORD:
|
if PORTPASSWORD:
|
||||||
if PORT or KEY:
|
if PORT or KEY:
|
||||||
logging.warn(
|
logger.warn(
|
||||||
'warning: port_password should not be used with server_port and password. server_port and password will be ignored')
|
'warning: port_password should not be used with server_port and password. server_port and password will be ignored')
|
||||||
else:
|
else:
|
||||||
PORTPASSWORD = {}
|
PORTPASSWORD = {}
|
||||||
|
@ -174,7 +175,7 @@ class ShadowSocksServer(object):
|
||||||
server = ThreadingTCPServer((SERVER, int(port)), Socks5Server)
|
server = ThreadingTCPServer((SERVER, int(port)), Socks5Server)
|
||||||
server.key, server.method, server.timeout = key, METHOD, int(
|
server.key, server.method, server.timeout = key, METHOD, int(
|
||||||
TIMEOUT)
|
TIMEOUT)
|
||||||
logging.info("starting server at %s:%d" %
|
logger.info("starting server at %s:%d" %
|
||||||
tuple(server.server_address[:2]))
|
tuple(server.server_address[:2]))
|
||||||
threading.Thread(target=server.serve_forever).start()
|
threading.Thread(target=server.serve_forever).start()
|
||||||
|
|
||||||
|
@ -201,12 +202,12 @@ class ShadowSocksServer(object):
|
||||||
|
|
||||||
def _parse_file_options(self, config_path):
|
def _parse_file_options(self, config_path):
|
||||||
if config_path:
|
if config_path:
|
||||||
logging.info('loading config from %s' % config_path)
|
logger.info('loading config from %s' % config_path)
|
||||||
with open(config_path, 'rb') as f:
|
with open(config_path, 'rb') as f:
|
||||||
try:
|
try:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
logging.error(
|
logger.error(
|
||||||
'found an error in config.json: %s', e.message)
|
'found an error in config.json: %s', e.message)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
|
@ -254,11 +255,11 @@ class ShadowSocksServer(object):
|
||||||
version = pkg_resources.get_distribution('shadowsocks').version
|
version = pkg_resources.get_distribution('shadowsocks').version
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
logging.info('shadowsocks %s' % version)
|
logger.info('shadowsocks %s' % version)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
ShadowSocksServer().serve_forever()
|
ShadowSocksServer().serve_forever()
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
logging.error(e)
|
logger.error(e)
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger('utils')
|
||||||
|
|
||||||
def find_config():
|
def find_config():
|
||||||
config_path = 'config.json'
|
config_path = 'config.json'
|
||||||
|
@ -16,7 +17,7 @@ def find_config():
|
||||||
|
|
||||||
def check_config(config):
|
def check_config(config):
|
||||||
if config.get('server', '') in ['127.0.0.1', 'localhost']:
|
if config.get('server', '') in ['127.0.0.1', 'localhost']:
|
||||||
logging.warn('Server is set to "%s", maybe it\'s not correct' % config['server'])
|
logger.warn('Server is set to "%s", maybe it\'s not correct' % config['server'])
|
||||||
logging.warn('Notice server will listen at %s:%s' % (config['server'], config['server_port']))
|
logger.warn('Notice server will listen at %s:%s' % (config['server'], config['server_port']))
|
||||||
if (config.get('method', '') or '').lower() == 'rc4':
|
if (config.get('method', '') or '').lower() == 'rc4':
|
||||||
logging.warn('RC4 is not safe; please use a safer cipher, like AES-256-CFB')
|
logger.warn('RC4 is not safe; please use a safer cipher, like AES-256-CFB')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue