def count_flow(): # 取出配置文件,且获取出要操作的那些数据 ssrj = jfileutl.get_dict() port_info = ssrj.get("port_password") # 获取每个端口产生的流量 port_dic = Linux.count_all_port() # 记录是否有超出流量的用户,有的话需要重启一下ssr,重新加载一下配置文件 reboot_ssr = False # 遍历字典 for port, flow in port_dic.items(): # 获取出对应的端口数据 port_data = port_info.get(port) # 获取出该端口是否有效,有效才去更改数据 flowMark = port_data.get("flowMark") # 只更改有效的端口 if int(flowMark) == 0: # 获取最大数 total = int(port_data.get("total")) # 计算出剩余的流量 remain = total - flow # 设置剩余的流量 port_data['remain'] = 0 if remain <= 0 else remain # 设置用的流量 port_data['used'] = int(port_data.get("used")) + flow # 判断剩余的流量是否小于0,是的话需要将该端口的flowMark至为1 if remain <= 0: # 将标志至为无效 port_data['flowMark'] = 1 # 移除流量超标的端口 manager.remove_port({'server_port': port}) print( "have some customer flow out of the total,need restart the ssr software,The user info:%s" % (ssrj, )) jfileutl.write_file(ssrj)
def get_overdue_ports(): config = jfileutl.get_dict(1) # 已经存在的port overdue = [] for port, data in config: if int(data['dateMark']) == 1: overdue.append(port) return overdue
def get_valid_ports(): config = jfileutl.get_dict(1) # 有效的端口 exists = [] for port, data in config.items(): if data.get("dateMark") == 0: exists.append(port) return exists
def check_port_date(): overdue_port = [] # 获取所有的端口 load_dict = jfileutl.get_dict() port_password = ['port_password'] for port, info in port_password.items(): endTm = datetime.strptime(info['endTm'], "%Y-%m-%d") if endTm <= datetime.now(): # 添加到list中 overdue_port.append(port) # dateMark至为1 info['dateMark'] = 1 # 写入config文件中去 jfileutl.write_file(load_dict) return overdue_port
def query_valid_port_info(beginTm, endTm, begin, end): port_info = jfileutl.get_dict(1) beginTm = datetime.strptime(beginTm, "%Y-%m-%d") endTm = datetime.strptime(endTm, "%Y-%m-%d") sub = [] # 要返回的结果 result = [] # 找出符合条件的数据 for port, data in port_info.items(): portEndTm = datetime.strptime(data.get("endTm", ""), "%Y-%m-%d") # 判断在这个区间内的数据 if beginTm <= portEndTm and endTm >= portEndTm: sub.append(data) # 找出指定区间内的数据 for index, item in enumerate(sub): index = (index + 1) if index >= begin and index <= end: result.append(item) return result
def save_update_port_info(dic=None): if dic is not None: load_dict = jfileutl.get_dict() # 判断是修改还是添加 port_password = load_dict.get("port_password") if not port_password: return add_port_info(dic, load_dict) else: exists = False # 通过id 获取对应的端口数据 for port, data in port_password.items(): if data.get("id") == dic.get("id"): exists = True break if exists: return update_port_info(dic, load_dict) else: return add_port_info(dic, load_dict) else: print("The dic that can`t be null")
def destroy_port_info(dic=None): if dic is not None: load_dict = jfileutl.get_dict() # 去除所有的端口数据 port_password = load_dict.get("port_password") for port, data in port_password.items(): if data.get("id") == dic.get("id"): # 写入bak的delConfig中 jfileutl.write_file_to_bak(port_password[port]) # 把它有效至为1就行 del port_password[port] break # 将指定端口墙了 Linux.delete_port([dic.get("server_port")]) # 更新到文件中去 jfileutl.write_file(load_dict) # 重启ssr Linux.restart_ssr() else: print("The agr can`t be null")
def clear_port_flow(): # 获取有效的端口 valid_port = port_service.get_valid_ports() # 将他们的端口号至为flowMark都至为0 # 取出配置文件,且获取出要操作的那些数据 ssrj = jfileutl.get_dict() port_info = ssrj.get('port_password') for port in valid_port: port_data = port_info.get(port) port_data["flowMark"] = 0 # 将他们的流量都清空 port_data["used"] = 0 port_data["remain"] = port_data["total"] print("The 1st of every month clear the flows,clear after the data:" % (ssrj, )) # 写到文件里去 jfileutl.write_file(ssrj) # 将OUTPUT表中的数据流量全部清空 Linux.clear_output_port_flow() # 重启ssr Linux.restart_ssr()
def overdue_port_info(dic=None): if dic is not None: load_dict = jfileutl.get_dict() # 去除所有的端口数据 port_password = load_dict.get("port_password") for port, data in port_password.items(): if data.get("id") == dic.get("id"): # 把它有效至为1就行 port_password[port]['flowMark'] = 1 port_password[port]['dateMark'] = 1 port_password[port]['used'] = port_password[port]['total'] port_password[port]['remain'] = 0 port_password[port]['endTm'] = datetime.strftime( datetime.now(), "%Y-%m-%d") break # 将指定端口墙了 if Linux.delete_port([dic.get("server_port")]): pass # 更新到文件中去 jfileutl.write_file(load_dict) # 重启ssr Linux.restart_ssr() else: print("The agr can`t be null")
def get_all_port(): config = jfileutl.get_dict(1) return config