Skip to content

Commit

Permalink
add additional_ports in config
Browse files Browse the repository at this point in the history
using user's password as protocol param in multi-user mode
  • Loading branch information
breakwa11 committed Feb 19, 2017
1 parent e2957e6 commit e1b199f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 51 deletions.
5 changes: 3 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"local_port": 1080,

"password": "m",
"timeout": 120,
"udp_timeout": 60,
"method": "aes-128-ctr",
"protocol": "auth_aes128_md5",
"protocol_param": "",
Expand All @@ -16,6 +14,9 @@
"speed_limit_per_con": 0,
"speed_limit_per_user": 0,

"additional_ports" : {}, // only works under multi-user mode
"timeout": 120,
"udp_timeout": 60,
"dns_ipv6": false,
"connect_verbose_info": 0,
"redirect": "",
Expand Down
32 changes: 24 additions & 8 deletions db_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self):
self.port_uid_table = {} #端口到uid的映射(仅v3以上有用)
self.onlineuser_cache = lru_cache.LRUCache(timeout=60*30) #用户在线状态记录
self.pull_ok = False #记录是否已经拉出过数据
self.mu_ports = {}

def load_cfg(self):
pass
Expand All @@ -41,7 +42,7 @@ def push_db_all_user(self):
dt_transfer[id] = [self.last_get_transfer[id][0] - last_transfer[id][0], self.last_get_transfer[id][1] - last_transfer[id][1]]

for id in curr_transfer.keys():
if id in self.force_update_transfer:
if id in self.force_update_transfer or id in self.mu_ports:
continue
#算出与上次记录的流量差值,保存于dt_transfer表
if id in last_transfer:
Expand Down Expand Up @@ -95,11 +96,13 @@ def del_server_out_of_bound_safe(self, last_rows, rows):

port = row['port']
passwd = common.to_bytes(row['passwd'])
if hasattr(passwd, 'encode'):
passwd = passwd.encode('utf-8')
cfg = {'password': passwd}
if 'id' in row:
self.port_uid_table[row['port']] = row['id']

read_config_keys = ['method', 'obfs', 'obfs_param', 'protocol', 'protocol_param', 'forbidden_ip', 'forbidden_port']
read_config_keys = ['method', 'obfs', 'obfs_param', 'protocol', 'protocol_param', 'forbidden_ip', 'forbidden_port', 'speed_limit_per_con', 'speed_limit_per_user']
for name in read_config_keys:
if name in row and row[name]:
cfg[name] = row[name]
Expand All @@ -116,10 +119,11 @@ def del_server_out_of_bound_safe(self, last_rows, rows):
continue

if allow:
allow_users[port] = 1
allow_users[port] = passwd
if 'protocol' in cfg and 'protocol_param' in cfg and common.to_str(cfg['protocol']) in ['auth_aes128_md5', 'auth_aes128_sha1']:
if '#' in common.to_str(cfg['protocol_param']):
mu_servers[port] = 1
mu_servers[port] = passwd
del allow_users[port]

cfgchange = False
if port in ServerPool.get_instance().tcp_servers_pool:
Expand Down Expand Up @@ -177,10 +181,11 @@ def del_server_out_of_bound_safe(self, last_rows, rows):
passwd, cfg = new_servers[port]
self.new_server(port, passwd, cfg)

if isinstance(self, MuJsonTransfer): # works in MuJsonTransfer only
logging.debug('db allow users %s \nmu_servers %s' % (allow_users, mu_servers))
for port in mu_servers:
ServerPool.get_instance().update_mu_server(port, None, allow_users)
logging.debug('db allow users %s \nmu_servers %s' % (allow_users, mu_servers))
for port in mu_servers:
ServerPool.get_instance().update_mu_users(port, allow_users)

self.mu_ports = mu_servers

def clear_cache(self, port):
if port in self.force_update_transfer: del self.force_update_transfer[port]
Expand Down Expand Up @@ -237,6 +242,17 @@ def thread_db(obj):
rows = db_instance.pull_db_all_user()
if rows:
db_instance.pull_ok = True
config = shell.get_config(False)
for port in config['additional_ports']:
val = config['additional_ports'][port]
val['port'] = int(port)
val['enable'] = 1
val['transfer_enable'] = 1024 ** 7
val['u'] = 0
val['d'] = 0
if "password" in val:
val["passwd"] = val["password"]
rows.append(val)
db_instance.del_server_out_of_bound_safe(last_rows, rows)
last_rows = rows
except Exception as e:
Expand Down
38 changes: 21 additions & 17 deletions mujson_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,17 @@ def ssrlink(self, user, encode, muid):
obfs = user.get('obfs', '')
protocol = protocol.replace("_compatible", "")
obfs = obfs.replace("_compatible", "")
link = "%s:%s:%s:%s:%s:%s" % (self.server_addr, user['port'], protocol, user['method'], obfs, common.to_str(base64.urlsafe_b64encode(common.to_bytes(user['passwd']))).replace("=", ""))
protocol_param = ''
if muid is not None:
protocol_param = user.get('protocol_param', '')
param = protocol_param.split('#')
protocol_param_ = user.get('protocol_param', '')
param = protocol_param_.split('#')
if len(param) == 2:
user_dict = {}
user_list = param[1].split(',')
if user_list:
for userinfo in user_list:
items = userinfo.split(':')
if len(items) == 2:
user_int_id = int(items[0])
passwd = items[1]
user_dict[user_int_id] = passwd
if muid in user_dict:
param = str(muid) + ':' + user_dict[muid]
for row in self.data.json:
if int(row['port']) == muid:
param = str(muid) + ':' + row['passwd']
protocol_param = '/?protoparam=' + base64.urlsafe_b64encode(common.to_bytes(param)).replace("=", "")
link += protocol_param
break
link = ("%s:%s:%s:%s:%s:%s" % (self.server_addr, user['port'], protocol, user['method'], obfs, common.to_str(base64.urlsafe_b64encode(common.to_bytes(user['passwd']))).replace("=", ""))) + protocol_param
return "ssr://" + (encode and common.to_str(base64.urlsafe_b64encode(common.to_bytes(link))).replace("=", "") or link)

def userinfo(self, user, muid = None):
Expand All @@ -98,8 +91,19 @@ def userinfo(self, user, muid = None):
if key in ['enable'] or key not in user:
continue
ret += '\n'
if key in ['transfer_enable', 'u', 'd']:
val = user[key]
if (muid is not None) and (key in ['protocol_param']):
for row in self.data.json:
if int(row['port']) == muid:
ret += " %s : %s" % (key, str(muid) + ':' + row['passwd'])
break
elif key in ['transfer_enable', 'u', 'd']:
if muid is not None:
for row in self.data.json:
if int(row['port']) == muid:
val = row[key]
break
else:
val = user[key]
if val / 1024 < 4:
ret += " %s : %s" % (key, val)
elif val / 1024 ** 2 < 4:
Expand Down
10 changes: 5 additions & 5 deletions server_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,24 @@ def cb_del_server(self, port):

return True

def update_mu_server(self, port, protocol_param, acl):
def update_mu_users(self, port, users):
port = int(port)
if port in self.tcp_servers_pool:
try:
self.tcp_servers_pool[port].update_users(protocol_param, acl)
self.tcp_servers_pool[port].update_users(users)
except Exception as e:
logging.warn(e)
try:
self.udp_servers_pool[port].update_users(protocol_param, acl)
self.udp_servers_pool[port].update_users(users)
except Exception as e:
logging.warn(e)
if port in self.tcp_ipv6_servers_pool:
try:
self.tcp_ipv6_servers_pool[port].update_users(protocol_param, acl)
self.tcp_ipv6_servers_pool[port].update_users(users)
except Exception as e:
logging.warn(e)
try:
self.udp_ipv6_servers_pool[port].update_users(protocol_param, acl)
self.udp_ipv6_servers_pool[port].update_users(users)
except Exception as e:
logging.warn(e)

Expand Down
3 changes: 2 additions & 1 deletion shadowsocks/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_config(is_local):


if config_path:
logging.info('loading config from %s' % config_path)
logging.debug('loading config from %s' % config_path)
with open(config_path, 'rb') as f:
try:
config = parse_json_in_str(remove_comment(f.read().decode('utf8')))
Expand Down Expand Up @@ -244,6 +244,7 @@ def get_config(is_local):
config['obfs'] = to_str(config.get('obfs', 'plain'))
config['obfs_param'] = to_str(config.get('obfs_param', ''))
config['port_password'] = config.get('port_password', None)
config['additional_ports'] = config.get('additional_ports', {})
config['timeout'] = int(config.get('timeout', 300))
config['udp_timeout'] = int(config.get('udp_timeout', 120))
config['udp_cache'] = int(config.get('udp_cache', 64))
Expand Down
30 changes: 21 additions & 9 deletions shadowsocks/tcprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def __init__(self, max_speed = 0):
self._cache = deque()
self.sum_len = 0

def update_limit(self, max_speed):
self.max_speed = max_speed * 1024

def add(self, data_len):
if self.max_speed > 0:
self._cache.append((time.time(), data_len))
Expand Down Expand Up @@ -1057,8 +1060,6 @@ def __init__(self, config, dns_resolver, is_local, stat_callback=None, stat_coun
self.mu = False
self._speed_tester_u = {}
self._speed_tester_d = {}
self.update_users_protocol_param = None
self.update_users_acl = None
self.server_connections = 0
self.protocol_data = obfs.obfs(config['protocol']).init_data()
self.obfs_data = obfs.obfs(config['obfs']).init_data()
Expand Down Expand Up @@ -1145,9 +1146,18 @@ def _update_users(self, protocol_param, acl):
passwd = items[1]
self.add_user(uid, passwd)

def update_users(self, protocol_param, acl):
self.update_users_protocol_param = protocol_param
self.update_users_acl = acl
def update_user(self, id, passwd):
uid = struct.pack('<I', id)
self.add_user(uid, passwd)

def update_users(self, users):
for uid in list(self.server_users.keys()):
id = struct.unpack('<I', uid)[0]
if id not in users:
self.del_user(uid)
for id in users:
uid = struct.pack('<I', id)
self.add_user(uid, users[id])

def add_user(self, user, passwd): # user: binstr[4], passwd: str
self.server_users[user] = common.to_bytes(passwd)
Expand Down Expand Up @@ -1190,6 +1200,12 @@ def speed_tester_d(self, uid):
self._speed_tester_d[uid] = SpeedTester(self._config.get("speed_limit_per_user", 0))
return self._speed_tester_d[uid]

def update_limit(self, uid, max_speed):
if uid in self._speed_tester_u:
self._speed_tester_u[uid].update_limit(max_speed)
if uid in self._speed_tester_d:
self._speed_tester_d[uid].update_limit(max_speed)

def update_stat(self, port, stat_dict, val):
newval = stat_dict.get(0, 0) + val
stat_dict[0] = newval
Expand Down Expand Up @@ -1286,10 +1302,6 @@ def handle_periodic(self):
logging.info('closed TCP port %d', self._listen_port)
for handler in list(self._fd_to_handlers.values()):
handler.destroy()
elif self.update_users_protocol_param is not None or self.update_users_acl is not None:
self._update_users(self.update_users_protocol_param, self.update_users_acl)
self.update_users_protocol_param = None
self.update_users_acl = None
self._sweep_timeout()

def close(self, next_tick=False):
Expand Down
21 changes: 12 additions & 9 deletions shadowsocks/udprelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,6 @@ def __init__(self, config, dns_resolver, is_local, stat_callback=None, stat_coun
self.server_users = {}
self.server_user_transfer_ul = {}
self.server_user_transfer_dl = {}
self.update_users_protocol_param = None
self.update_users_acl = None

if common.to_bytes(config['protocol']) in [b"auth_aes128_md5", b"auth_aes128_sha1"]:
self._update_users(None, None)
Expand Down Expand Up @@ -1000,9 +998,18 @@ def _update_users(self, protocol_param, acl):
passwd = items[1]
self.add_user(uid, passwd)

def update_users(self, protocol_param, acl):
self.update_users_protocol_param = protocol_param
self.update_users_acl = acl
def update_user(self, id, passwd):
uid = struct.pack('<I', id)
self.add_user(uid, passwd)

def update_users(self, users):
for uid in list(self.server_users.keys()):
id = struct.unpack('<I', uid)[0]
if id not in users:
self.del_user(uid)
for id in users:
uid = struct.pack('<I', id)
self.add_user(uid, users[id])

def add_user(self, user, passwd): # user: binstr[4], passwd: str
self.server_users[user] = common.to_bytes(passwd)
Expand Down Expand Up @@ -1476,10 +1483,6 @@ def handle_periodic(self):
self._dns_cache.sweep()
if before_sweep_size != len(self._sockets):
logging.debug('UDP port %5d sockets %d' % (self._listen_port, len(self._sockets)))
if self.update_users_protocol_param is not None or self.update_users_acl is not None:
self._update_users(self.update_users_protocol_param, self.update_users_acl)
self.update_users_protocol_param = None
self.update_users_acl = None
self._sweep_timeout()

def close(self, next_tick=False):
Expand Down

0 comments on commit e1b199f

Please sign in to comment.