예제 #1
0
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)
예제 #2
0
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
예제 #3
0
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")
예제 #4
0
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()
예제 #5
0
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")
예제 #6
0
def update_port_info(dic=None, load_dict=None):
    # 去除所有的端口数据
    port_password = load_dict.get("port_password")
    # 要操作数据
    option_data = {}
    old_port = 0
    # 通过id 获取对应的端口数据
    for port, data in port_password.items():
        if data.get("id") == dic.get("id"):
            option_data = data
            old_port = port
            # 删除这个port的数据
            del port_password[port]
            break
    # 修改对应数据
    userName = dic.get("userName")
    password = dic.get("password")
    client = dic.get("client")
    port = dic.get("server_port")
    method = dic.get("method")
    protocol = dic.get("protocol")
    obfs = dic.get("obfs")
    limit = dic.get("limit")
    remain = dic.get("remain")
    total = dic.get("total")
    used = dic.get("used")
    # 判断是否需要更改有效标识
    beginTm = dic.get("beginTm")
    endTm = dic.get("endTm")
    # 判断是否需要更改有效标识
    dateMark = 1
    flowMark = 1
    today = datetime.strptime(datetime.strftime(datetime.now(), "%Y-%m-%d"),
                              "%Y-%m-%d")
    # 如果结束日期大于今天就将有效标签改为0
    if datetime.strptime(endTm, "%Y-%m-%d") >= today:
        dateMark = 0
    if int(remain) != 0:
        flowMark = 0

    # 设置是否有效标识
    option_data["flowMark"] = flowMark
    option_data["dateMark"] = dateMark
    # 更新下面的数据
    if password is not None:
        option_data["password"] = password
    if port is not None:
        option_data["server_port"] = port
    if method is not None:
        option_data["method"] = method
    if client is not None:
        option_data["client"] = client
    if protocol is not None:
        option_data["protocol"] = protocol
    if obfs is not None:
        option_data["obfs"] = obfs
    if limit is not None:
        option_data["limit"] = limit

    if total is not None:
        option_data["total"] = int(total)
        if remain is not None:
            option_data["remain"] = int(total) - int(
                used) if int(total) - int(used) > 0 else 0
        if used is not None:
            option_data["used"] = int(total)-int(option_data["remain"]) if int(total)-int(option_data["remain"]) \
                                                                           > 0 else 0

    if beginTm is not None:
        option_data["beginTm"] = beginTm
    if total is not None:
        option_data["endTm"] = endTm
    if userName is not None:
        option_data["userName"] = userName

    # 重新设置到字典中去
    port_password[port] = option_data
    # 判断端口是否改变
    if int(port) != int(old_port):
        # 更新端口墙规则
        if Linux.update_port(old_port, port):
            print("Update a rule of port,The origin:%s,The new:%s" %
                  (old_port, port))
    # 更新到文件中去
    jfileutl.write_file(load_dict)
    # 重启ssr
    Linux.restart_ssr()
예제 #7
0
def add_port_info(dic=None, load_dict=None):
    port_password = load_dict.get("port_password")
    if not port_password:
        load_dict['port_password'] = {}
        port_password = load_dict.get("port_password")
    # 要添加的数据
    option_data = {}
    # 修改对应数据
    id = dic.get("id")
    userId = dic.get("userId")
    userName = dic.get("userName")
    password = dic.get("password")
    client = dic.get("client")
    port = dic.get("server_port")
    method = dic.get("method")
    protocol = dic.get("protocol")
    obfs = dic.get("obfs")
    limit = dic.get("limit")
    used = dic.get("used", "0")
    remain = dic.get("remain", "0")
    total = dic.get("total")
    beginTm = dic.get("beginTm")
    endTm = dic.get("endTm")

    # 判断是否需要更改有效标识
    dateMark = 1
    flowMark = 0
    today = datetime.strptime(datetime.strftime(datetime.now(), "%Y-%m-%d"),
                              "%Y-%m-%d")
    # 如果结束日期大于今天就将有效标签改为0
    if datetime.strptime(endTm, "%Y-%m-%d") >= today:
        dateMark = 0
    # 设置是否有效标识
    option_data["flowMark"] = flowMark
    option_data["dateMark"] = dateMark
    # 更新下面的数据
    if id is not None:
        option_data["id"] = id
    if userId is not None:
        option_data["userId"] = userId
    if client is not None:
        option_data["client"] = client
    if userName is not None:
        option_data["userName"] = userName
    if password is not None:
        option_data["password"] = password
    if port is not None:
        option_data["server_port"] = port
    if method is not None:
        option_data["method"] = method
    if protocol is not None:
        option_data["protocol"] = protocol
    if limit is not None:
        option_data["limit"] = limit
    if obfs is not None:
        option_data["obfs"] = obfs
    if remain is not None:
        option_data["remain"] = int(total)
    if used is not None:
        option_data["used"] = int(used) if used != '' else 0
    if total is not None:
        option_data["total"] = int(total)
    if beginTm is not None:
        option_data["beginTm"] = beginTm
    if endTm is not None:
        option_data["endTm"] = endTm

    # 重新设置到字典中去
    port_password[port] = option_data
    # 判断端口是否改变
    if Linux.add_port(option_data):
        print("Insert a rule of port:%s" % (port))

    # 更新到文件中去
    jfileutl.write_file(load_dict)
    # 重启ssr
    Linux.restart_ssr()