Exemplo n.º 1
0
 def get(self, request):
     node_id = request.GET.get("node_id", 0)
     user_id = request.user.pk
     now = pendulum.now()
     last_week = [now.subtract(days=i).date() for i in range(6, -1, -1)]
     configs = UserTrafficLog.gen_line_chart_configs(user_id, node_id, last_week)
     return JsonResponse(configs)
Exemplo n.º 2
0
 def get(self, request):
     node_id = request.GET.get("node_id", 0)
     node_type = request.GET.get("node_type", "ss")
     user_id = request.user.pk
     now = get_current_datetime()
     last_week = [now.subtract(days=i) for i in range(6, -1, -1)]
     configs = UserTrafficLog.gen_line_chart_configs(
         user_id, node_type, node_id, last_week)
     return JsonResponse(configs)
Exemplo n.º 3
0
    def post(self, request, node_id):
        node = VmessNode.get_or_none_by_node_id(node_id)
        if not node:
            return HttpResponseNotFound()

        log_time = get_current_datetime()
        node_total_traffic = 0
        need_clear_cache = False
        trafficlog_model_list = []
        user_model_list = []

        for log in request.json["user_traffics"]:
            user_id = log["user_id"]
            u = int(log["ut"] * node.enlarge_scale)
            d = int(log["dt"] * node.enlarge_scale)
            # 个人流量增量
            user = User.get_by_pk(user_id)
            user.download_traffic += d
            user.upload_traffic += u
            user.last_use_time = log_time
            user_model_list.append(user)
            if user.overflow:
                need_clear_cache = True
            # 个人流量记录
            trafficlog_model_list.append(
                UserTrafficLog(
                    node_type=UserTrafficLog.NODE_TYPE_VMESS,
                    node_id=node_id,
                    user_id=user_id,
                    download_traffic=u,
                    upload_traffic=d,
                )
            )
            # 节点流量增量
            node_total_traffic += u + d
        # 节点流量记录
        VmessNode.increase_used_traffic(node_id, node_total_traffic)
        # 流量记录
        UserTrafficLog.objects.bulk_create(trafficlog_model_list)
        # TODO 在线IP
        # 个人流量记录
        User.objects.bulk_update(
            user_model_list, ["download_traffic", "upload_traffic", "last_use_time"],
        )
        # 节点在线人数
        NodeOnlineLog.add_log(
            NodeOnlineLog.NODE_TYPE_VMESS, node_id, len(request.json["user_traffics"])
        )
        # check node && user traffic
        if node.overflow:
            node.enable = False
        if need_clear_cache or node.overflow:
            node.save()
        return JsonResponse(data={})
Exemplo n.º 4
0
    def post(self, request, node_id):
        """
        这个接口操作比较重,所以为了避免发信号
        所有写操作都需要用BULK的方式
        1 更新节点流量
        2 更新用户流量
        3 记录节点在线IP
        4 关闭超出流量的节点
        """
        ss_node = SSNode.get_or_none_by_node_id(node_id)
        if not ss_node:
            return HttpResponseNotFound()

        data = request.json["data"]
        node_total_traffic = 0
        log_time = get_current_datetime()
        active_tcp_connections = 0
        need_clear_cache = False
        user_model_list = []
        trafficlog_model_list = []
        online_ip_log_model_list = []

        for user_data in data:
            user_id = user_data["user_id"]
            u = int(user_data["upload_traffic"] * ss_node.enlarge_scale)
            d = int(user_data["download_traffic"] * ss_node.enlarge_scale)
            # 个人流量增量
            user = User.get_by_pk(user_id)
            user.download_traffic += d
            user.upload_traffic += u
            user.last_use_time = log_time
            user_model_list.append(user)
            if user.overflow:
                need_clear_cache = True
            # 个人流量记录
            trafficlog_model_list.append(
                UserTrafficLog(
                    node_type=UserTrafficLog.NODE_TYPE_SS,
                    node_id=node_id,
                    user_id=user_id,
                    download_traffic=u,
                    upload_traffic=d,
                )
            )
            # 节点流量增量
            node_total_traffic += u + d
            # active_tcp_connections
            active_tcp_connections += user_data["tcp_conn_num"]
            # online ip log
            for ip in user_data.get("ip_list", []):
                online_ip_log_model_list.append(
                    UserOnLineIpLog(user_id=user_id, node_id=node_id, ip=ip)
                )

        # 用户流量
        User.objects.bulk_update(
            user_model_list, ["download_traffic", "upload_traffic", "last_use_time"],
        )
        # 节点流量记录
        SSNode.increase_used_traffic(node_id, node_total_traffic)
        # 流量记录
        UserTrafficLog.objects.bulk_create(trafficlog_model_list)
        # 在线IP
        UserOnLineIpLog.objects.bulk_create(online_ip_log_model_list)
        # 节点在线人数
        NodeOnlineLog.add_log(
            NodeOnlineLog.NODE_TYPE_SS, node_id, len(data), active_tcp_connections
        )
        # check node && user traffic
        if ss_node.overflow:
            ss_node.enable = False
        if need_clear_cache or ss_node.overflow:
            ss_node.save()
        return JsonResponse(data={})
Exemplo n.º 5
0
    def post(self, request, node_id):
        """
        这个接口操作比较重,所以为了避免发信号
        所有写操作都需要用BULK的方式
        1 更新节点流量
        2 更新用户流量
        3 记录节点在线IP
        4 关闭超出流量的用户
        5 关闭超出流量的节点
        """
        data = request.json["data"]
        log_time = pendulum.now()
        node_total_traffic = 0
        trafficlog_model_list = []
        user_traffic_model_list = []
        online_ip_log_model_list = []
        user_ss_config_model_list = []

        for user_data in data:
            user_id = user_data["user_id"]
            u = user_data["upload_traffic"]
            d = user_data["download_traffic"]

            # 个人流量增量
            user_traffic = UserTraffic.get_by_user_id(user_id)
            user_traffic.download_traffic += d
            user_traffic.upload_traffic += u
            user_traffic.last_use_time = log_time
            user_traffic_model_list.append(user_traffic)
            if user_traffic.overflow:
                user_ss_config = UserSSConfig.get_by_user_id(user_id)
                user_ss_config.enable = False
                user_ss_config_model_list.append(user_ss_config)
            # 个人流量记录
            trafficlog_model_list.append(
                UserTrafficLog(
                    node_id=node_id,
                    user_id=user_id,
                    download_traffic=u,
                    upload_traffic=d,
                )
            )
            # 节点流量增量
            node_total_traffic += u + d
            # online ip log
            for ip in user_data.get("ip_list", []):
                online_ip_log_model_list.append(
                    UserOnLineIpLog(user_id=user_id, node_id=node_id, ip=ip)
                )

        # 节点流量记录
        SSNode.increase_used_traffic(node_id, node_total_traffic)
        # 流量记录
        UserTrafficLog.objects.bulk_create(trafficlog_model_list)
        # 在线IP
        UserOnLineIpLog.objects.bulk_create(online_ip_log_model_list)
        # 个人流量记录
        UserTraffic.objects.bulk_update(
            user_traffic_model_list,
            ["download_traffic", "upload_traffic", "last_use_time"],
        )
        # 用户开关
        UserSSConfig.objects.bulk_update(user_ss_config_model_list, ["enable"])
        # 节点在线人数
        SSNodeOnlineLog.add_log(node_id, len(data))
        # check node && user traffic
        ss_node = SSNode.get_or_none_by_node_id(node_id)
        if ss_node.overflow:
            ss_node.enable = False
        if user_ss_config_model_list or ss_node.overflow:
            # NOTE save for clear cache
            ss_node.save()
        return JsonResponse({"ret": 1, "data": []})