From 8031337d653dc7de8516b428eac9f8cf9591adf0 Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Tue, 21 Jun 2016 21:04:41 +0800 Subject: [PATCH] compatible python3 --- configloader.py | 22 +++++--------------- db_transfer.py | 32 +++++++++++++++++++----------- importloader.py | 22 ++++++++++++++++++++ mujson_mgr.py | 6 +++--- server_pool.py | 8 ++++---- shadowsocks/common.py | 7 +++---- shadowsocks/encrypt.py | 2 +- shadowsocks/obfsplugin/obfs_tls.py | 20 +++++++++---------- 8 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 importloader.py diff --git a/configloader.py b/configloader.py index 8d0ec0d..cf9d619 100644 --- a/configloader.py +++ b/configloader.py @@ -1,27 +1,15 @@ #!/usr/bin/python # -*- coding: UTF-8 -*- +import importloader -config = None +g_config = None def load_config(): - global config - try: - import userapiconfig - reload(userapiconfig) - config = userapiconfig - return - except: - pass - try: - import apiconfig - reload(apiconfig) - config = apiconfig - except: - pass + global g_config + g_config = importloader.loads(['userapiconfig', 'apiconfig']) def get_config(): - global config - return config + return g_config load_config() diff --git a/db_transfer.py b/db_transfer.py index 7115fe2..05bb31a 100644 --- a/db_transfer.py +++ b/db_transfer.py @@ -8,7 +8,9 @@ from server_pool import ServerPool import traceback from shadowsocks import common, shell from configloader import load_config, get_config +import importloader +switchrule = None db_instance = None class DbTransfer(object): @@ -80,8 +82,7 @@ class DbTransfer(object): import cymysql #数据库所有用户信息 try: - import switchrule - reload(switchrule) + switchrule = importloader.load('switchrule') keys = switchrule.getKeys() except Exception as e: keys = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'enable' ] @@ -99,13 +100,19 @@ class DbTransfer(object): conn.close() return rows + def cmp(self, val1, val2): + if type(val1) is bytes: + val1 = common.to_str(val1) + if type(val2) is bytes: + val2 = common.to_str(val2) + return val1 == val2 + def del_server_out_of_bound_safe(self, last_rows, rows): #停止超流量的服务 #启动没超流量的服务 #需要动态载入switchrule,以便实时修改规则 try: - import switchrule - reload(switchrule) + switchrule = importloader.load('switchrule') except Exception as e: logging.error('load switchrule.py fail') cur_servers = {} @@ -145,13 +152,13 @@ class DbTransfer(object): if port in ServerPool.get_instance().tcp_servers_pool: relay = ServerPool.get_instance().tcp_servers_pool[port] for name in merge_config_keys: - if name in cfg and cfg[name] != relay._config[name]: + if name in cfg and not self.cmp(cfg[name], relay._config[name]): cfgchange = True break; if not cfgchange and port in ServerPool.get_instance().tcp_ipv6_servers_pool: relay = ServerPool.get_instance().tcp_ipv6_servers_pool[port] for name in merge_config_keys: - if name in cfg and cfg[name] != relay._config[name]: + if name in cfg and not self.cmp(cfg[name], relay._config[name]): cfgchange = True break; #config changed @@ -182,10 +189,10 @@ class DbTransfer(object): @staticmethod def del_servers(): - for port in ServerPool.get_instance().tcp_servers_pool.keys(): + for port in [v for v in ServerPool.get_instance().tcp_servers_pool.keys()]: if ServerPool.get_instance().server_is_run(port) > 0: ServerPool.get_instance().cb_del_server(port) - for port in ServerPool.get_instance().tcp_ipv6_servers_pool.keys(): + for port in [v for v in ServerPool.get_instance().tcp_ipv6_servers_pool.keys()]: if ServerPool.get_instance().server_is_run(port) > 0: ServerPool.get_instance().cb_del_server(port) @@ -232,8 +239,8 @@ class MuJsonTransfer(DbTransfer): rows = None config_path = get_config().MUDB_FILE - with open(config_path, 'r+') as f: - rows = shell.parse_json_in_str(f.read().decode('utf8')) + with open(config_path, 'rb+') as f: + rows = json.loads(f.read().decode('utf8')) for row in rows: if "port" in row: port = row["port"] @@ -247,11 +254,12 @@ class MuJsonTransfer(DbTransfer): f.write(output) def pull_db_all_user(self): + import json rows = None config_path = get_config().MUDB_FILE - with open(config_path, 'r+') as f: - rows = shell.parse_json_in_str(f.read().decode('utf8')) + with open(config_path, 'rb+') as f: + rows = json.loads(f.read().decode('utf8')) for row in rows: try: if 'forbidden_ip' in row: diff --git a/importloader.py b/importloader.py new file mode 100644 index 0000000..fb7c26b --- /dev/null +++ b/importloader.py @@ -0,0 +1,22 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +def load(name): + try: + obj = __import__(name) + return obj + except: + pass + + try: + import importlib + obj = importlib.__import__(name) + return obj + except: + pass + +def loads(namelist): + for name in namelist: + obj = load(name) + if obj is not None: + return obj diff --git a/mujson_mgr.py b/mujson_mgr.py index c6403b9..cda7ea7 100644 --- a/mujson_mgr.py +++ b/mujson_mgr.py @@ -6,17 +6,17 @@ from shadowsocks import common, shell from configloader import load_config, get_config import getopt import sys +import json class MuJsonLoader(object): def __init__(self): self.json = None def load(self, path): - with open(path, 'r+') as f: - self.json = shell.parse_json_in_str(f.read().decode('utf8')) + with open(path, 'rb+') as f: + self.json = json.loads(f.read().decode('utf8')) def save(self, path): - import json if self.json: output = json.dumps(self.json, sort_keys=True, indent=4, separators=(',', ': ')) with open(path, 'w') as f: diff --git a/server_pool.py b/server_pool.py index 63a8f16..c25c832 100644 --- a/server_pool.py +++ b/server_pool.py @@ -24,7 +24,7 @@ import os import logging import time -from shadowsocks import shell, eventloop, tcprelay, udprelay, asyncdns +from shadowsocks import shell, eventloop, tcprelay, udprelay, asyncdns, common import threading import sys from socket import * @@ -124,7 +124,7 @@ class ServerPool(object): a_config['server_port'] = port a_config['max_connect'] = 128 try: - logging.info("starting server at [%s]:%d" % (a_config['server'], port)) + logging.info("starting server at [%s]:%d" % (common.to_str(a_config['server']), port)) tcp_server = tcprelay.TCPRelay(a_config, self.dns_resolver, False, stat_counter=self.stat_counter) tcp_server.add_to_loop(self.loop) @@ -134,14 +134,14 @@ class ServerPool(object): udp_server.add_to_loop(self.loop) self.udp_ipv6_servers_pool.update({port: udp_server}) - if a_config['server_ipv6'] == "::": + if common.to_str(a_config['server_ipv6']) == "::": ipv6_ok = True except Exception as e: logging.warn("IPV6 %s " % (e,)) if 'server' in self.config: if port in self.tcp_servers_pool: - logging.info("server already at %s:%d" % (self.config['server'], port)) + logging.info("server already at %s:%d" % (common.to_str(self.config['server']), port)) return 'this port server is already running' else: a_config = self.config.copy() diff --git a/shadowsocks/common.py b/shadowsocks/common.py index bfc099c..16386bd 100644 --- a/shadowsocks/common.py +++ b/shadowsocks/common.py @@ -85,7 +85,7 @@ def inet_pton(family, addr): if '.' in addr: # a v4 addr v4addr = addr[addr.rindex(':') + 1:] v4addr = socket.inet_aton(v4addr) - v4addr = map(lambda x: ('%02X' % ord(x)), v4addr) + v4addr = ['%02X' % ord(x) for x in v4addr] v4addr.insert(2, ':') newaddr = addr[:addr.rindex(':') + 1] + ''.join(v4addr) return inet_pton(family, newaddr) @@ -289,10 +289,9 @@ class IPNetwork(object): class PortRange(object): def __init__(self, range_str): - self.range_str = range_str + self.range_str = to_str(range_str) self.range = set() - if type(range_str) == str: - range_str = range_str.split(',') + range_str = to_str(range_str).split(',') for item in range_str: try: int_range = item.split('-') diff --git a/shadowsocks/encrypt.py b/shadowsocks/encrypt.py index e8f51b7..57540bc 100644 --- a/shadowsocks/encrypt.py +++ b/shadowsocks/encrypt.py @@ -75,7 +75,7 @@ def EVP_BytesToKey(password, key_len, iv_len): class Encryptor(object): def __init__(self, key, method): self.key = key - self.method = method + self.method = common.to_str(method) self.iv = None self.iv_sent = False self.cipher_iv = b'' diff --git a/shadowsocks/obfsplugin/obfs_tls.py b/shadowsocks/obfsplugin/obfs_tls.py index 00ca045..f483d46 100644 --- a/shadowsocks/obfsplugin/obfs_tls.py +++ b/shadowsocks/obfsplugin/obfs_tls.py @@ -169,8 +169,8 @@ class tls_auth(plain.plain): data = b"\x16" + self.tls_version + struct.pack('>H', len(data)) + data return data if self.has_recv_header: - data = b"\x14" + self.tls_version + "\x00\x01\x01" #ChangeCipherSpec - data += b"\x16" + self.tls_version + "\x00\x20" + os.urandom(22) #Finished + data = b"\x14" + self.tls_version + b"\x00\x01\x01" #ChangeCipherSpec + data += b"\x16" + self.tls_version + b"\x00\x20" + os.urandom(22) #Finished data += hmac.new(self.server_info.key + self.server_info.data.client_id, data, hashlib.sha1).digest()[:10] ret = data + self.send_buffer self.send_buffer = b'' @@ -196,8 +196,8 @@ class tls_auth(plain.plain): data = self.tls_version + self.pack_auth_data(self.client_id) + b"\x20" + self.client_id + binascii.unhexlify(b"0016c02bc02fc00ac009c013c01400330039002f0035000a0100006fff01000100000a00080006001700180019000b0002010000230000337400000010002900270568322d31360568322d31350568322d313402683208737064792f332e3108687474702f312e31000500050100000000000d001600140401050106010201040305030603020304020202") data = b"\x02\x00" + struct.pack('>H', len(data)) + data #server hello data = b"\x16" + self.tls_version + struct.pack('>H', len(data)) + data - data += b"\x14" + self.tls_version + "\x00\x01\x01" #ChangeCipherSpec - data += b"\x16" + self.tls_version + "\x00\x20" + os.urandom(22) #Finished + data += b"\x14" + self.tls_version + b"\x00\x01\x01" #ChangeCipherSpec + data += b"\x16" + self.tls_version + b"\x00\x20" + os.urandom(22) #Finished data += hmac.new(self.server_info.key + self.client_id, data, hashlib.sha1).digest()[:10] return data @@ -326,8 +326,8 @@ class tls_ticket_auth(plain.plain): data = b"\x16\x03\x01" + struct.pack('>H', len(data)) + data return data elif self.handshake_status == 1 and len(buf) == 0: - data = b"\x14" + self.tls_version + "\x00\x01\x01" #ChangeCipherSpec - data += b"\x16" + self.tls_version + "\x00\x20" + os.urandom(22) #Finished + data = b"\x14" + self.tls_version + b"\x00\x01\x01" #ChangeCipherSpec + data += b"\x16" + self.tls_version + b"\x00\x20" + os.urandom(22) #Finished data += hmac.new(self.server_info.key + self.server_info.data.client_id, data, hashlib.sha1).digest()[:10] ret = data + self.send_buffer self.send_buffer = b'' @@ -376,8 +376,8 @@ class tls_ticket_auth(plain.plain): data = self.tls_version + self.pack_auth_data(self.client_id) + b"\x20" + self.client_id + binascii.unhexlify(b"c02f000005ff01000100") data = b"\x02\x00" + struct.pack('>H', len(data)) + data #server hello data = b"\x16\x03\x03" + struct.pack('>H', len(data)) + data - data += b"\x14" + self.tls_version + "\x00\x01\x01" #ChangeCipherSpec - data += b"\x16" + self.tls_version + "\x00\x20" + os.urandom(22) #Finished + data += b"\x14" + self.tls_version + b"\x00\x01\x01" #ChangeCipherSpec + data += b"\x16" + self.tls_version + b"\x00\x20" + os.urandom(22) #Finished data += hmac.new(self.server_info.key + self.client_id, data, hashlib.sha1).digest()[:10] return data @@ -410,10 +410,10 @@ class tls_ticket_auth(plain.plain): verify_len = 43 - 10 if len(buf) < 43: raise Exception('server_decode data error') - if not match_begin(buf, b"\x14" + self.tls_version + "\x00\x01\x01"): #ChangeCipherSpec + if not match_begin(buf, b"\x14" + self.tls_version + b"\x00\x01\x01"): #ChangeCipherSpec raise Exception('server_decode data error') buf = buf[6:] - if not match_begin(buf, b"\x16" + self.tls_version + "\x00\x20"): #Finished + if not match_begin(buf, b"\x16" + self.tls_version + b"\x00\x20"): #Finished raise Exception('server_decode data error') if hmac.new(self.server_info.key + self.client_id, verify[:verify_len], hashlib.sha1).digest()[:10] != verify[verify_len:verify_len+10]: raise Exception('server_decode data error')