Ejemplo n.º 1
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={})
Ejemplo n.º 2
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={})