Browse Source

compatible python3

dev
BreakWa11 9 years ago
parent
commit
8031337d65
  1. 22
      configloader.py
  2. 32
      db_transfer.py
  3. 22
      importloader.py
  4. 6
      mujson_mgr.py
  5. 8
      server_pool.py
  6. 7
      shadowsocks/common.py
  7. 2
      shadowsocks/encrypt.py
  8. 20
      shadowsocks/obfsplugin/obfs_tls.py

22
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()

32
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:

22
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

6
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:

8
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()

7
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('-')

2
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''

20
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')

Loading…
Cancel
Save