Exemplo n.º 1
0
def traffic_job():
    with __lock:
        if not v2_util.is_running():
            return
        try:
            traffics = v2_util.get_inbounds_traffic()
            if not traffics:
                return
            for traffic in traffics:
                upload = int(traffic.get('uplink', 0))
                download = int(traffic.get('downlink', 0))
                tag = traffic['tag']
                Inbound.query.filter_by(tag=tag).update({'up': Inbound.up + upload, 'down': Inbound.down + download})
            db.session.commit()
        except Exception as e:
            logging.warning(f'traffic job error: {e}')
Exemplo n.º 2
0
def traffic_job():
    # with __lock:
    if not v2_util.is_running():
        return
    try:
        traffics = v2_util.get_inbounds_traffic()
        if not traffics:
            return
        for traffic in traffics:
            upload = int(traffic.get("uplink", 0))
            download = int(traffic.get("downlink", 0))
            email = traffic["email"]
            Inbound.query.filter(Inbound.settings.contains(email)).update(
                {"up": Inbound.up + upload, "down": Inbound.down + download},
                synchronize_session=False,
            )
        db.session.commit()
    except Exception as e:
        logging.warning(f"traffic job error: {e}")
Exemplo n.º 3
0
def traffic_job():
    with __lock:
        print("进入流量统计")
        # if not v2_util.is_running():
        #    return
        traffics = v2_util.get_inbounds_traffic()
        if not traffics:
            print("没查到流量")
            return
        for traffic in traffics:
            upload = int(traffic.get('uplink', 0))
            download = int(traffic.get('downlink', 0))
            print("down:" + str(download) + ":up:" + str(upload))
            tag = traffic['tag']
            local_ip = get_ip()
            inbound = Inbound.query.filter_by(tag=tag).first()
            if inbound and download < inbound.down:
                Inbound.query.filter_by(tag=tag).update({
                    'up':
                    Inbound.up + upload,
                    'down':
                    Inbound.down + download
                })
            else:
                Inbound.query.filter_by(tag=tag).update({
                    'up': upload,
                    'down': download
                })
            # 更新mysql
            inbounding = mysqlsesson.query(InboundMysql).filter(
                InboundMysql.tag == tag).first()
            if inbounding and download < inbounding.down:
                mysqlsesson.query(InboundMysql).filter(
                    InboundMysql.tag == tag,
                    InboundMysql.server == local_ip).update(
                        {
                            InboundMysql.up: InboundMysql.up + upload,
                            InboundMysql.down: InboundMysql.down + download
                        },
                        synchronize_session=False)
                mysqlsesson.query(VpsNode).filter(
                    VpsNode.tag == tag, VpsNode.server == local_ip).update(
                        {
                            VpsNode.up: VpsNode.up + upload,
                            VpsNode.down: VpsNode.down + download
                        },
                        synchronize_session=False)

            else:
                mysqlsesson.query(InboundMysql).filter(
                    InboundMysql.tag == tag,
                    InboundMysql.server == local_ip).update(
                        {
                            InboundMysql.up: upload,
                            InboundMysql.down: download
                        },
                        synchronize_session=False)
                mysqlsesson.query(VpsNode).filter(
                    VpsNode.tag == tag, VpsNode.server == local_ip).update(
                        {
                            VpsNode.up: upload,
                            VpsNode.down: download
                        },
                        synchronize_session=False)

        db.session.commit()
        mysqlsesson.commit()